Documente Academic
Documente Profesional
Documente Cultură
info
27/10/2014
Piwa.info
Honni soit qui mal y code
Chathead Basics
April 14, 2013
http://www.piwai.info/chatheads-basics/
1/6
27/10/2014
I was surprised that chatheads could be drawn on top of any app. Here is a quick
explanation of how it works.
No Activity?
At first you may think it's a trick with a transparent activity. Let's see:
$ adb shell dumpsys activity
Running activities (most recent first):
TaskRecord{42b03c38 #2 A com.android.launcher U 0}
Run #0: ActivityRecord{42adf3f8 u0 com.android.launcher/com.android.launcher2.Launcher}
Principle
It's simple: just add a view to a Window.
As you probably know, an Activity has a Window instance. Dialogs also have their own
dedicated Window. Even Services can have Window: InputMethodService uses a Window to
receive touch events and draw a keyboard on top of another Window, and DreamService is
http://www.piwai.info/chatheads-basics/
2/6
27/10/2014
receive touch events and draw a keyboard on top of another Window, and DreamService is
used to create screensavers.
Permission
To open a new window in which you will draw the chathead, you need the
SYSTEM_ALERT_WINDOW permission.
Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other
applications. Very few applications should use this permission; these windows are intended for systemlevel interaction with the user.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
This is what your users will see when installing the app:
Android Head
Now that you have the right permission, you just need to call WindowManager#addView()
with the view and the corresponding layout params:
public class ChatHeadService extends Service {
private WindowManager windowManager;
private ImageView chatHead;
@Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.android_head);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
http://www.piwai.info/chatheads-basics/
3/6
27/10/2014
http://www.piwai.info/chatheads-basics/
4/6
27/10/2014
Conclusion
http://www.piwai.info/chatheads-basics/
5/6
27/10/2014
Prior to Facebook Chatheads, this trick was already used by some apps. A few examples:
Display a side app launcher
Take screenshots
Draw notes over apps
Reduce the screen brightness
This feature is nice, but remember that with great power comes great responsibility.
Please take care of your user pixels.
Comments
hidden-markov
Does this imply that Facebook Chatheads (or any application with
SYSTEM_ALERT_WINDOW permission) is able to conduct keylogging and take screenshots
at arbitrary time?
Comment
Android String Placeholders
http://www.piwai.info/chatheads-basics/
6/6