0

I'm trying to create a custom chatHead which would show only when a particular app is open. (e.g. Messenger or WhatsApp). And would be destroyed when that app has been closed.

Here's my code-

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.view_icon);

    final 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);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead, params);

    getForeground();


    chatHead.setOnClickListener(new View.OnClickListener() {
       /*.....
         .....
         ClickListener implemented
         .....
         .....
       */
    });

}

Here, in the onCreate(), I'm calling the getForeground() method, which gets the current activity in the foreground

public String getForeground() {
    final ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    String foreground = am.getRunningTasks(1).get(0).topActivity.getPackageName();
    if (foreground.equals("com.whatsapp")) {
        if (chatHead==null)
            onResume();
    }
    else
        onDestroy();

    new Thread() {
        @Override
        public void run() {
            getForeground();

        }
    }.start();
    return foreground;
}

Where, onResume() is-

public void onResume() {
    final 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);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead,params);
}

And onDestroy() is-

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null)
        windowManager.removeView(chatHead);
    else
        chatHead=null;
}

However, it shows an IllegalStateException as follows-

java.lang.IllegalArgumentException: View=android.widget.ImageView{41f07a28 V.ED..C. ......ID 0,0-93,99} not attached to window manager
            at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
            at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
            at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
            at automated.whatsapp.com.automatedtest.ChatHeadService.onDestroy(ChatHeadService.java:172)
            at automated.whatsapp.com.automatedtest.ChatHeadService.getForeground(ChatHeadService.java:188)
            at automated.whatsapp.com.automatedtest.ChatHeadService$3.run(ChatHeadService.java:194)

So, basically, it says that the chatHead is not attached to the WindowManager. How do I correct this error?

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
deathstroke
  • 526
  • 12
  • 33

2 Answers2

3

You can use try-catch around your window manager like this

try{
    WindowManager?.removeView(view);
}catch(e:Exception){
    Log.e(debug_tag, "view not found");
}

or can check windowtoken of view exist

if (view?.windowToken!=null) {
   WindowManager?.removeView(view);
}

because when your view is not added or remove view later your windowmanager make a new exception

Mohsen mokhtari
  • 2,841
  • 1
  • 30
  • 39
sajadab
  • 383
  • 2
  • 11
0

Not sure if this can help you but this is what I did:

@Override
protected void onUserLeaveHint() {
    if (this.chatHead != null) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ((WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE)).removeView(chatHead);
            }
        });
    }
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
DucTran
  • 68
  • 10