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 :)