Sunday, November 16, 2014

Introducing Xposed Media Scanner Optimizer for Android

Let me introduce my new Xposed Module. It allows you to customize the way the media scanner works on Android.
It solves several problems that I have with the media scanner. As Xposed module developer one needs to reboot the device quite often. The media scanner runs on each reboot and eating much battery as the device stays awake while the scanner is running and doing lots of I/O.
For non developer the Xposed Media Scanner Optimizer also provides useful functionality.

First of all let me explain how the media scanner works on Android. Each time the device has bootet it runs and scans all volumes/storage. Depending on the size of the storage available on the device and the SD Card and the number of files this usually takes between 5 and 20 minutes. For each file and directory it finds it creates an entry in the so-called Android Media Store. For special file types like images, videos and music files it also scans for meta data like EXIF information for images and mp3 tags for music files. Applications may query the Media Store to find specific files bases on the file type or meta data. Well known applications that do so are the Gallery and the Music Player. Other applications may also do so.
The media scanner runs as a background service and is not visible to the user. 

The Xposed Media Scanner Optimizer provides the following tweaks to the media scanner:
  • The service can be run as a foreground service allowing the user to see when and how long it runs. Foregound services are visible to the user via a notification.
  • When the scanner has finished details of how long each scan took are shown using a notification.
  • The scanner can be run in a mode where only specific directories will be scanned. This reduces scan times and thus saves battery. It also allows you to control what shows up in the Gallery or Music Player apps.
  • It is possible to tell the scanner that only specific file types (e.g. music files) should be scanned on a directory basis.
  • It allows you to completely delete the contents of the media store.
  • It allows you to trigger a scan whenever you want to
  • On some Android phones the scanner is scheduled to run several times after a boot which is completely unnecessary and in my opinion is a bug. These repetitive scans can be prevented. 
All files that will not be scanned will not show up in the Gallery or Music Player. I for myself use this feature to restrict what is shown in the Music Player for example. I only want to see music files from specific directories on my SD card to show up in the music player.
The directories that contain my music files also contain images for the album and the artist. These files would normally show up in the Gallery. There, I only want to see the contents of the directories where I keep my pictures. I don't want to have the gallery cluttered with album artwork or images of artists from the music directories.

The Xposed Media Scanner Optimizer settings application can be started from the launcher and allows you to configure the following settings:

  • Repetitive Scans: If this preference is checked repetitive scans at boot time will be prevented. Repetitive scans are not necessary and only cause greater battery drain. Depending on the Android version and vendor repetitive scans may occur and can be prevented by checking this preference.
  • Service Importance: When checked the media scanner will run as foreground service and its operation will be visible to the user via a notification.
  • Thread Priority: If checked the media scanner will be forced to run with background thread priority even if it is running as a foreground service. This is the suggested setting. If unchecked the thread priority depends on whether it runs as a foreground service or not.
  • Directories: If checked the media scanner will only scan directories that have a .scanMedia file (i.e. an empty file with a dot as first part of the name similar to the .noMedia file). Use a file explorer app to create this file. This file can be placed in a directory that may contain subdirectories. The subdirectories will be scanned as well. If unchecked all directories will be scanned (except for the ones containing a .noMedia file - this default behavior of the media scanner is not altered by the Xposed Media Scanner Optimizer).
  • Media Types: If checked only certain file types will be scanned. To define which file types the scanner will scan in a directory create the following files: .scanMusic, .scanVideo and .scanPictures. So for a directory containing a collection of music albums you would need to create a .scanMusic file at the directory that contains all albums if you only want the music files scanned. Images contained in the album directories would then be skipped.
  • Result Notifications: If checked a notification will be shown when the scanner has finished including information about the scan time.
  • Trigger Media Scanner: When touched a media scanner is triggered.
  • Delete Media Store Contents: When touched the contents of the media store are deleted. This will not delete the actual files on the storage. Trigger a scan or reboot to let the scanner populate the media store after deleting its contents. This is useful if the media stores already contains entries that you do not want like images from all directories in the Gallery. First check the preferences to restrict what will be scanned, empty the media store and trigger a rescan. After the rescan only what has been scanned depending on your preferences will appear in the Gallery.
  • Android Logging: If this preference is checked details of the media scanner and the Xposed module are written to the Android log. This is useful for development and to troubleshoot. For end users this should stay unchecked.
  • Xposed Log: If this preference is checked details will be written to the Xposed log. Since the Xposed log is persisted this is also useful for troubleshooting. For end users this should stay unchecked.
Depending on the number of files on your storage media scan times can be reduced significantly.

The Xposed module is available from the Xposed repository. Please use this XDA Developer forum thread for feedback and discussions.

It should work on Android 4.1.2 (API 16) and up.

If you like the module feel free to donate :)

Saturday, July 5, 2014

Fixing random reboots on Samsung Kitkat Firmwares like I9505XXUGNF1

After updating my SGS 4 to I9505XXUGNF1 I like many others experienced random crashes which at first glance seemed to be reboots of the device. Looking at the logcat I figured out that actually the system server process crashes due to an ArrayIndexOutOfBoundsException. After reading the source code I figured what the problem is and wrote this small module to workaround the crash. My S4 is running without a single crash since.

Here are some technical details:

The logcat shows:

06-27 16:29:22.471 E/AndroidRuntime(28244): !@*** FATAL EXCEPTION IN SYSTEM PROCESS: ActivityManager
06-27 16:29:22.471 E/AndroidRuntime(28244): java.lang.ArrayIndexOutOfBoundsException: length=14; index=-1
06-27 16:29:22.471 E/AndroidRuntime(28244): at
06-27 16:29:22.471 E/AndroidRuntime(28244): at
06-27 16:29:22.471 E/AndroidRuntime(28244): at
06-27 16:29:22.471 E/AndroidRuntime(28244): at
06-27 16:29:22.471 E/AndroidRuntime(28244): at
06-27 16:29:22.471 E/AndroidRuntime(28244): at$ActivityStackSupervisorHandler.activityIdleInternal(
06-27 16:29:22.471 E/AndroidRuntime(28244): at$ActivityStackSupervisorHandler.handleMessage(
06-27 16:29:22.471 E/AndroidRuntime(28244): at android.os.Handler.dispatchMessage(
06-27 16:29:22.471 E/AndroidRuntime(28244): at android.os.Looper.loop(
06-27 16:29:22.471 E/AndroidRuntime(28244): at$

The problem is in the method computeNextPssTime.

The last statement in the method accesses either the array named sFirstAwakePssTimes or sSameAwakePssTimes. An ArrayIndexOutOfBoundsException happens when the parameter procState is wrong. My first guess was that -1 is used as index into the array.
Looking at the call stack one can see that the parameter is a value taken from an instance of the class ProcessRecord. The value is initialized with -1 when that instance is created.

Somehow this invalid value for the procState is being used and finally ends up being used as the index into the array.

I don't know what causes this (could be timing issues or some changes that Samsung made). Nevertheless, it is possible to prevent the system server to crash by changing what the computeNextPssTime method does. 

Using the amazing Xposed Framework I created an Xposed module that will check the procStat parameter value and in case of an invalid valued (like -1) will return a default value for the result of the method. This works pretty well to workaround the problem. Of course, it does not fix the actual problem but my phone is running well since then.

Whenever -1 or any other invalid value is passed to computeNextPssTime a message is logged to Android's log with the tag SSCF. Using logcat you can see how often this happens and how often the system server would crash if not using the module.

Since the problematic code is in AOSP I wonder why this seems to happen on newer Samsung Kitkat ROMs only. To me the problem seems to occur with an increasing chance the more processes are running. Maybe this is why users are starting to see random reboots only after a few days (when more apps might have been installed). At least this is what happened to me.

Discussion can be found on this XDA forum thread.