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</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
<item name="windowActionBar">false</item>
</style>
So how exactly is the content shown by Drupe? The dummy activity launched by expanding the drupe shortcut basically starts a service. This service uses application context and window manager to create and show the content. I had blogged about this a while back as to how a service can create a dialog. Drupe basically uses this approach to add a main content view with a different type, TYPE_PHONE. This requires a permission, android.permission.SYSTEM_ALERT_WINDOW. Apart from that, adding the view ensures that Window Manager creates a new window shown on top of other applications but underneath the status bar. The dummy activity launch basically pauses the top resumed activity of whatever application was in focus but the activity stack is not disturbed and drupe's translucent window seems to appear fine on top of previously used application.
private void launchTranslucentWindow() {
LayoutInflater inflate = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView textView = new TextView( getApplicationContext() );
textView.setText("TextView in a translucent window created by a Service");
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.format = PixelFormat.TRANSLUCENT;
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.addView(textView, params);
}
The dummy activity doesn't finish instead stays as resumed activity without any content. This gives it maximum life time as a foreground app and wouldn't be killed by Android's low memory killer.
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</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
<item name="windowActionBar">false</item>
</style>
So how exactly is the content shown by Drupe? The dummy activity launched by expanding the drupe shortcut basically starts a service. This service uses application context and window manager to create and show the content. I had blogged about this a while back as to how a service can create a dialog. Drupe basically uses this approach to add a main content view with a different type, TYPE_PHONE. This requires a permission, android.permission.SYSTEM_ALERT_WINDOW. Apart from that, adding the view ensures that Window Manager creates a new window shown on top of other applications but underneath the status bar. The dummy activity launch basically pauses the top resumed activity of whatever application was in focus but the activity stack is not disturbed and drupe's translucent window seems to appear fine on top of previously used application.
private void launchTranslucentWindow() {
LayoutInflater inflate = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView textView = new TextView( getApplicationContext() );
textView.setText("TextView in a translucent window created by a Service");
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.format = PixelFormat.TRANSLUCENT;
final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.addView(textView, params);
}
The dummy activity doesn't finish instead stays as resumed activity without any content. This gives it maximum life time as a foreground app and wouldn't be killed by Android's low memory killer.
No comments:
Post a Comment