How to clear memory when remove view from WindowManager - android

My app uses:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
I add the view to windowManager via code:
MyContextWrapper myContextWrapper= new MyContextWrapper(this);
// I created customview in myContextWrapper
View view = myContextWrapper.getView();
int FLAG = WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
| WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
int type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, type,
FLAG, PixelFormat.UNKNOWN);
mWindowManager.addView(view, params);
How do I clear memory of the view when I remove it? I want to clear it because memory increased after new MyContextWrapper(this) getview and add to mWindowManager.
mWindowManager.removeView(view);
myContextWrapper.getView() include (ImageView, ViewPager...)

Related

How to change Screen overlay after system ui mode changed(Fullscreen, hide navigation)?

I create overlay:
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*The parent view (actually the root view) the will be added in front of all the windows on the screen*/
mOverlayView = (RelativeLayout) inflater.inflate(R.layout.overlay_control, null);
mWindowManager.getDefaultDisplay().getSize(size);
mWindowSize = new Size(size.x, size.y);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
mWindowManager.addView(mOverlayView, params);
I need to move overlay if system UI Visibility changed. There is possibility to detect it(documentation) for view but it not works for overlay. So I only need callback to detect if mode of device changed.
I found the way to do it:
In Android there is OnSystemUiVisibilityChangeListener but it not works if there is not view which will has match_parent value of width or hight.
So I created such overlay and made it 1 px height and match_parent width and add OnSystemUiVisibilityChangeListener to it.

Status bar and overlay

I'm showing a custom overlay with this parameter inflating the view from a service:
params = new WindowManager.LayoutParams(WindowManager
.LayoutParams
.MATCH_PARENT,
statusBarHeight, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager
.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams
.FLAG_NOT_FOCUSABLE, 0);
params.gravity = Gravity.CENTER | Gravity.TOP;
The problem is that if the status bar is expanded, the overlay is over the quick settings menu. How can I avoid that? I'm using android 6.
Instead of TYPE_SYSTEM_OVERLAY use TYPE_SYSTEM_ERROR.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
100,
// Allows the view to be on top of the StatusBar
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
// Keeps the button presses from going to the background window
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// Enables the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP|Gravity.CENTER;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mView, mLP);
This should solve your issue.

how to make system overlay full height

I am creating a system-wide fullscreen overlay layout in my activity code(not xml). The problem is that the layout does not occupy the full height of the screen. There is a gap at the top and bottom. Any idea how to make the layout fit the entire screen height?
mFrameLayout = new FrameLayout(this);
mFrameLayout.setBackgroundColor(getResources().getColor(R.color.black));
mFrameLayout.getBackground().setAlpha(50);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT
);
WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
windowManager.addView(mFrameLayout, params);
Simply Change 'FLAG_FULLSCREEN' to 'MATCH_PARENT'.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
Or to remove the system ui(Status & Nav Bar) from view too:
int KitKatFlags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
getWindow().getDecorView().setSystemUiVisibility(KitKatFlags);

The back button is not working when a layout is overlayed using WindowManager. How to make it work?

LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
myview = li.inflate(R.layout.locked_layout, null);
wm.addView(myview);
This is the code I have used for overlaying a layout over a screen. Only the HOME button is working. But the BACK button is not working. I want both to work. Am I missing anything? Please help me!! Thanks in advance
Add this flag as well my friend:
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
This worked for me!!
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindwoManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);
params.gravity = Gravity.RIGHT | Gravity.TOP;

Screenorientation in addview()

I add view by addView() and I want to block screen orientation on that view to portrait.
I tried:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.TOP;
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
LinearLayout glass = new LinearLayout(this);
glass.setBackgroundResource(R.drawable.glass6);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(glass, params);
It doesn't work.
How can I do it?
Also how can I do that my new view do not dissapear after app close?
Try this
glass.setOrintation(Configuration.ORIENTATION_LANDSCAPE)
Use android:screenOrientation="landscape" or android:screenOrientation="portrait" in your manifest file.To restrict your activity to be viewed in portrait or landscape mode.

Categories

Resources