Posts

Showing posts from March, 2015

Android - Drupe like Translucent overlay

Image
    Drupe (Contacts. Your Way) for Android has a smart way to interact with contacts and communicate quickly. At a glance, this seems to be using some kind of overlay to display the contacts and a shortcut to launch an app. The shortcut to launch the main window appears on any screen and this is similar to facebook's chat heads and implemented via SYSTEM_ALERT_WINDOW.    The real trick is how the application launches the main window to show the contents. Expanding or clicking on the drupe shortcut launches an Activity which basically doesn't display any content or an action bar. This can be achieved via a Theme,     <style name="Theme.AppCompat.Empty">         <item name="android:windowNoTitle">true</item>         <item name="android:windowBackground">@android:color/transparent</item>         <item name="android:colorBackgroundCacheHint">@null...

Android - Priority scheduling with AsyncTask's THREAD_POOL_EXECUTOR

    AsyncTask's THREAD_POOL_EXECUTOR gives application an ability to execute tasks simultaneously limited by the number of threads in the pool. Any new request that can't be serviced immediately is queued and considered on a FIFO basis. This might seem fine for most applications but few might want to prioritize certain tasks based on user interaction.    One might be tempted to use a custom ThreadPoolExecutor managing a PriorityBlockingQueue ,    public final AsyncTask<Params, Progress, Result> executeOnExecutor (Executor exec, Params... params)    Sadly, this approach fails half way as the actual Runnable object type submitted to the executor is internal within AsyncTask and isn't exposed to facilitate comparison via  Comparator .         mWorker = new WorkerRunnable <Params, Result>() {             public Result call() throws Exception {       ...

Android - AsyncTask's RejectedExecutionException

   Android's AsyncTask helps in abstracting the need to deal with thread pools, thread factory, minimum number of threads in the pool etc. However, any internal failures isn't really handled well or translated into an AsyncTask terminology. One such failure is the RejectedExecutionException which isn't even mentioned in the official documentation .    So when and why is this exception thrown and how are application developers supposed to handle? AsyncTask's execute API basically executes a Runnable object on an Executor . Developers could either use a default executor or AsyncTask.THREAD_POOL_EXECUTOR or specify custom implementations.    AsyncTask.THREAD_POOL_EXECUTOR is meant to be used as a performance optimization where multiple asynchronous tasks could be executed in parallel. This is however limited by the minimum, maximum number of threads in the pool and a queue size.     public static final Executor THREAD_POOL_EXECUTOR  ...

Android - Valgrind for applications

    This is just about running an android application with Valgrind. From my experience, it didn't help at all as the application is painfully slow and in some cases causes ANR. This is even more complicated as non-nexus devices based on Snapdragon has a patch to kill the process upon ANR instead of showing the ANR dialog. This is documented here .    Anyways, here are the steps to enable Valgrind 1) Prerequisites : Ubuntu, Android ND, SVN, Rooted android device based on ARM 2) Download Valgrind source code         svn co svn://svn.valgrind.org/valgrind/trunk valgrind 3) Set NDKROOT to Android NDK's path         export NDKROOT=/home/user/android-ndk-r10d 4) Change directory into the downloaded valgrind source code and run a script,         ./autogen.sh 5) Execute the following commands in the same directory to build and generate valgrind binary, export HWKIND=generic export AR=$NDKROOT...

Android - EXTERNAL_CONTENT_URI vs INTERNAL_CONTENT_URI

Image
    Android's external vs internal storage is something that developers working on media applications have to deal with. This is even more confusing as some devices without an external SD card slot would return valid data set for  MediaStore.Images.Media.EXTERNAL_CONTENT_URI . Official documentation too doesn't help.     So how are developers supposed to frame the query, given that the real world external/internal terminology (understood by app users) is interpreted differently by Android APIs? So lets take a look at the source path used by the content provider serving media dataset (audio, images, video etc).     Android's media scanner service kicks off upon boot completion and scans the physical storage and populates the media provider. In this process, the meta data along with the file path is stored as database entries. From here on, the applications can use a content resolver to query and fetch data set.        if...

android:process - application private vs global

   Android's process attribute documentation claims that the newly spawned process can either be private to the application or global. It is determined based on the first character of string value specified to the attribute.    A value starting with : indicates a application private process, which can't be shared with other applications.              android:process=" : local" And a value starting with . and a lower case represents a global process, shareable across applications.              android:process=" . global"     The idea of having a single global process across applications is motivated by effective resource usage. This makes sense for 3rd party libraries as its components could be shared across applications. In android world, processes could share components only when they are signed by the same certificate and share a user id. I had blogged about this a while bac...

Android - Foreground service notification and recent app removal

    Android framework supports restarting application services if they are started as sticky. This helps application resume background tasks after a low memory kill etc. However, its not clear as to what happens when users remove the application (hosting the service) from the recent apps list. Ideally, the service shouldn't even be restarted as the user has requested for the app to be killed, hoping to recover some memory and keep the least recently used app list clear. Developers often tend to miss this use case especially when the application has a foreground service.    An application hosting a foreground service is considered to be important even when the application is in background. The process schedule group is set to default ( Process.THREAD_GROUP_DEFAULT ) and not as background ( Process.THREAD_GROUP_BG_NONINTERACTIVE ). Android framework tries to clean up activities and services as and when the application is removed from the recent app list and eventual...