Translate

Tuesday, October 31, 2017

Take screenshots from Android Wear watch

ADB is useful for other things in addition to debugging. Taking screenshots on wearables is not as trivial as it seems. The “Take wearable screenshot” option in an Android Wear companion app allows only sharing screenshots through mail or social networks. You can use ADB as another way to save images of wearable screens on your PC.
adb shell screencap -p /sdcard/screenshot.pngadb pull /sdcard/screenshot.png
Notice that even on round dials the screenshots are actually square. You should keep this point in mind to improve your apps’ usability.

 Android taking screenshots on wearables

Monday, October 30, 2017

Android Wear Watch adb Debugging over Bluetooth


Debugging over Bluetooth


Bluetooth debugging only works for Android-paired watches. Before you begin, be sure the watch and phone are paired and you've enabled developer options on the watch. You should also be sure that developer options is also enabled on the phone. To check, open the phone's Settings menu, select About phone and click the build number seven times.

Enable USB debugging on the phone

  1. Open the phone's Settings menu.
  2. Select Developer Options and enable USB debugging.

Enable ADB/Bluetooth debugging on the watch

  1. Open the watch's Settings menu.
  2. Scroll to Developer Options.
  3. Confirm that ADB debugging is enabled.
  4. Enable Debug over Bluetooth.

Enable Bluetooth debugging on the phone


    1. On the phone, open the Android Wear companion app.
    2. Tap the Settings → Debugging over Bluetooth. Next click on the “Device to Debug” and select your devices.
    3. Then a status message appears under the option. It looks like this:
      Host: disconnected
      Target: connected
      At this point the development machine (the host) is not communicating with with the watch (the target). You need to complete the link.

      Connect the debugger to the watch

      In this final step, you'll use everything: the debugger, the phone, and the watch.
      1. Connect the phone to your development machine with a USB cable.
      2. Run these two commands in the debugger:
        adb forward tcp:4444 localabstract:/adb-hub
        adb connect 127.0.0.1:4444
         
        Note: You must use the IP address 127.0.0.1. You can use any available port on your development machine. Be sure you use the same port in both commands. (In this example the port is 4444.)
      3. After you type the connect command, look at the watch. It will ask you to confirm that you are allowing ADB Debugging.
      4. Go back to the phone and check the status display in the Android Wear companion app. It should look like this:
        Host: connected
        Target: connected
         
         Android Wear Status - Debugging over bluetooth
         
         
      5. The watch is now connected to the debugger and you're ready to start debugging.
      When you debug a watch using Bluetooth, adb always uses the IP address 127.0.0.1 plus the port that you assigned. Therefore, all adb commands use this format (continuing the example, the port is 4444):
      adb -s 127.0.0.1:4444 <command> 
       
      If you are not using the emulator and have only one device connected for debugging, you don't need to specify the address at all:
      adb <command>
       
       

      Once after above setup is done then it should show two(phone and watch) devices as below : 

      ~$ adb devices
      List of devices attached 
      9887b7394b30585930 device
      127.0.0.1:4444 device
       
      To connect exactly android wear device :
      ~$ adb connect 127.0.0.1:4444
      connected to 127.0.0.1:4444
       
       
      In Linux terminal use following commands to get wearable logs:
      $ adb -s 127.0.0.1:4444 logcat *:i|grep -i "your project package name"
      ~$ adb -s 127.0.0.1:4444 logcat 
       
      Global Options Description
      -a Listen on all network interfaces instead of only on localhost.
      -d Direct an adb command to the only attached USB device. Returns an error when more than one USB device is attached.
      -e Direct an adb command to the only running emulator. Returns an error when more than one emulator is running.
      -s serial_number Direct an adb command to a specific device, referred to by its adb-assigned serial number (such as emulator-5556). Overrides the serial number value stored in the $ANDROID_SERIAL environment variable. See Send Commands to a Specific Device.
      -H server The name of the adb server host. The default value is localhost.
      -P port The adb server port number. The default value is 5037.
      -L socket Listen on the provided adb server socket. The default value is tcp:localhost:5037.

      Take a screenshot in samsung device

      Take a screenshot traditional way

      This method has pretty much become muscle memory for screen shooting, as it is available pretty much with every Android smartphone out there. Samsung is no exception.
      1. Navigate to what you want the screenshot to display.
      2. Press and hold the “Volume Down” and “Power” buttons simultaneously.
      3. Done!

      Take a screenshot using palm gesture

      Using this gesture to take a screenshot has become one of Samsung’s staple features. It does the same as the button method, but it is much faster and simpler once you get used to it.
      1. Navigate to what you want the screenshot to display.
      2. Extend your palm and swipe the whole screen with the side of your hand.
      3. Done!
      Keep in mind this is an advanced feature, so it can be turned off. Toggle it on/off by going to Settings>Advanced features>Palm swipe to capture.

      Take a screenshot of long pages using scroll capture


      Have you ever used Samsung’s scroll capture feature? It allows users to grab an image of more than what your physical screen can display. It’s great for capturing entire scrollable pages or apps in a single image.
      1. Navigate to what you want the screenshot to display.
      2. Use the button or palm swipe methods to capture a screenshot.
      3. A preview and multiple options will show up at the bottom of the screen.
      4. Tap “Scroll capture”. You can also draw, crop or share from this menu.
      5. Keep pressing the “Scroll capture” button to keep going down the page.
      6. Done!
      This feature is found in a section called Smart Capture, which can be turned off. Toggle it on/off by going to Settings>Advanced Features>Smart capture.

      Smart Select: ovals, squares, GIFs and pins


      Take things to the next level with TouchWiz Smart Select. This feature makes it possible to create screenshots in the shape of ovals and squares. You can also put GIFs together and pin screen sections (keeps them floating above the UI).
      1. Navigate to what you want the screenshot to display.
      2. Open the Edge panel.
      3. Slide left/right until you find the “Smart select” section.
      4. Select which mode you prefer.
      5. Select the area you want to screenshot and follow instructions.
      6. Done!
      There are special functions within each feature here. For example, one can extract text from screenshots. It is also easy to draw over them, share and more.
      Smart Select is a feature within the Edge panel, which can be deactivated within the settings. Toggle it on/off by going to Settings>Display>Edge screen>Edge panels.

       

      Thursday, October 26, 2017

      Describe MediaMetadataRetriever and how to find Media file duration android



      MediaMetadataRetriever: This class is used to extract metadata from media file. It comes from “android.media.MediaMetadataRetriever” package. It has many of available metadata types. A developer can use this class as per need. Here is the below some metadata types that we can extract from a media file in android.
      • METADATA_KEY_ALBUM : This key of metadata is used to extract the information about the Album associated with media file.
      • METADATA_KEY_ALBUMARTIST : This key of metadata is used to extract the information about the Artist or Performer associated with media file.
      • METADATA_KEY_ARTIST : This key of metadata is used to extract the information about the Artist associated with media file.
      • METADATA_KEY_AUTHOR : This key of metadata is used to extract the information about the author associated with media file.
      • METADATA_KEY_COMPOSER : This key of metadata is used to extract the information about the Composer associated with media file.
      • METADATA_KEY_BITRATE : This key of metadata is used to extract the information about the Bitrate associated with media file.
      • METADATA_KEY_GENRE: This key of metadata is used to extract the information about the Genre associated with media file.
      Methods available in MediaMetadataRetriever class:
      1. void setDataSource(String path) : This method is used to set the media file source path in string format.
      2. string extractMetadata(int keyCode) : This method is used to extract metadata as per keys selected that are mentioned above.
      3. byte[] getEmbeddedPicture() : This method is used to extract album art associated with media file.
      
      
       
      Code to get Media Duration : 
       
      private long getMediaDuration(){
          android.media.MediaMetadataRetriever metaRetriever = new android.media.MediaMetadataRetriever();
          metaRetriever.setDataSource(mRecordedFileName);
          return Long.parseLong(metaRetriever.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION));
      }

      Monday, October 16, 2017

      Audio Focus and AudioManager Api changes in Android O – 26.0


      Audio Focus Android O – 26.0 :

      https://developer.android.com/guide/topics/media-apps/audio-focus.html#audio-focus-change
      https://developer.android.com/reference/android/media/AudioManager.html#AUDIOFOCUS_REQUEST_GRANTED
      To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.
      When your app needs to output audio, it should request audio focus. When it has focus, it can play sound. However, after you acquire audio focus you may not be able to keep it until you’re done playing. Another app can request focus, which preempts your hold on audio focus. If that happens your app should pause playing or lower its volume to let users hear the new audio source more easily.

      → Apps running Android 8.0 (API level 26) or greater should use the requestAudioFocus() method, which takes an AudioFocusRequest parameter. The AudioFocusRequest contains information about the audio context and capabilities of your app. The system uses this information to manage the gain and loss of audio focus automatically.
      int requestAudioFocus (AudioFocusRequest focusRequest).
      People generally using following list of Api’s in code , but found that are deprecated in Android O(Api level 26) . Even though no issues following functionalities should work as expected in devices:
      1) Blue-tooth pairing was fine and able to listen audio.
      2) focus gain and loss notification between the apps.
      3) pause/resume audio behavior between the apps.
      4) Pause audio while lock the screen.
      5) Pause audio while app goes background.
      6) headphones audio fine, but need to test in Bluetooth speakers , Bluetooth audio on Android Auto .

      AudioManager Api changes:



      a) Android 26 deprecated Api’s using in our App:


      This method was deprecated in API level 26. use abandonAudioFocusRequest(AudioFocusRequest)
      Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
      requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint) : Even-thought it is deprecated no effect in our App.
      This method was deprecated in API level 26. use requestAudioFocus(AudioFocusRequest)


      This method was deprecated in API level 26. Use getDevices(int) instead to list available audio devices.


      b) Newly Added audio Api’s in Android 26:

      AUDIOFOCUS_NONE

      int AUDIOFOCUS_NONE  :   Used to indicate no audio focus has been gained or lost, or requested. Constant Value: 0 (0x00000000) 
      AUDIOFOCUS_REQUEST_DELAYED
      int AUDIOFOCUS_REQUEST_DELAYED  :   A focus change request whose granting is delayed: the request was successful, but the requester will only be granted audio focus once the condition that prevented immediate granting has ended. See requestAudioFocus(AudioFocusRequest) and setAcceptsDelayedFocusGain(boolean) 
      Constant Value: 2 (0x00000002)
      STREAM_ACCESSIBILITY :
      int STREAM_ACCESSIBILITY
      Used to identify the volume of audio streams for accessibility prompts : Constant Value: 10 (0x0000000a)
      getActivePlaybackConfigurations
      List<AudioPlaybackConfiguration> getActivePlaybackConfigurations ()
      Returns the current active audio playback configurations of the device.
      registerAudioPlaybackCallback
      void registerAudioPlaybackCallback (AudioManager.AudioPlaybackCallback cb, 
                      Handler handler)
      Register a callback to be notified of audio playback changes through AudioManager.AudioPlaybackCallback
      unregisterAudioPlaybackCallback
      void unregisterAudioPlaybackCallback (AudioManager.AudioPlaybackCallback cb)
      Unregister an audio playback callback previously registered with registerAudioPlaybackCallback(AudioPlaybackCallback, Handler).





      Remove all unused resources from an android project

      You can easily search for unused resources from Android Studio. Just press Ctrl+Alt+Shift+i and type "Unused resources" (without quotes). That will execute lint. Super easy way how run lint commands (and other stuff from IDE).
      OR
      In Android Studio -> Menu -> Refactor -> Remove Unused Resources...
      Select the resources you want to remove. You can exclude resources you want to keep by right clicking on the resource item.
      Use Do Refactor to remove all Resources at once.
      Update: use cmd+ Alt+ Shift for mac

      shortcut in Android Studio to locate the current opened file in Project explorer.

      You can use the Scroll from Source button in the Project Toolbar in the left of Android Studio.



      Another solution is Alt+F1+1. Select target like ProjectView -> Project/Android etc in which target you want to open this file and Enter. You can also read Locate current file in IntelliJ for more .

      Monday, October 2, 2017

      Notification issue in Android O

      I have faced Notification Issue:

      Notification working as expected in all Android versions but not in latest Android O.

      I figured out the issue will be because they have introduces ChannelIds and that are mandatory to use in Android O.

      Currently insted of Notification iam getting following Toast message in Android O device.

      https://i.stack.imgur.com/A4jbH.png

      I have resolved this issue with following code changes :

      Append  NotificationChannel to the NotificationManager :
      mNotificationManager = (android.app.NotificationManager) mContext        .getSystemService(Context.NOTIFICATION_SERVICE);
      
      NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
              mContext.getString(R.string.app_name), android.app.NotificationManager.IMPORTANCE_DEFAULT);
      mNotificationManager.createNotificationChannel(chan1);
       
       
      Add above channel to Notification Builder:
      NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext , PRIMARY_CHANNEL); 
       
      Channel value is :
      public static final String PRIMARY_CHANNEL = "default";


      Thats it everything is woking after this change.


      For more info please refer following links:
      https://stackoverflow.com/questions/44489657/android-o-reporting-notification-not-posted-to-channel-but-it-is
      https://stackoverflow.com/questions/45668079/notificationchannel-issue-in-android-o

      Could not identify launch activity: Default activity not found : Error while Launching activity

      Problem : I got this Error , When I tried to create an application without any Activity . Basically like to develop an Android Headless appl...