Not able to remove added view to status bar in android - android

I am trying to block status bar and I am successful in doing that by adding a view to status bar. I am trying to remove the added view when onClick is detected, but the view still remains and I am not able to remove the view. Can any one please let me know what am I missing. The current code is as shown below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
m_activateButton = (Button)findViewById(R.id.button);
m_activateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(manager != null){
manager.removeView(view);
}
}});
}
CustomView Class:
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}

Related

Android : disable swipe down and view wifi/airplane mode

How can I disable users can swipe down the title/notification bar as seen in the picture below. I want my app run in kiosk mode and my users must not be able to swipe down or anything else.
I tried using setting some flags but all I get is some kind of kiosk mode but users can still view the wifi/airplane etc screen on swipe down.
Please help, I can not find a good answer on Stackoverflow
I don not want my users can see the screen below.
Refer to this answer here
or
private void disablePullNotificationTouch() {
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (25 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.RGBX_8888;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
}
//Add this class in your project
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}

How comes that this code hangs after 1 hour?

I am running this code after every 30 second and after running app coninute 1 hour my phone and app hung up and i need to restart it again.
private void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager)
context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
Activity activity = (Activity) context;
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
}
public class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
You're effectively calling the following calls every 30 seconds.
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
This creates and adds a view every 30 seconds, so it will surely scrape out the memory when being run for long durations. You should clearly remove the view once it is not required. Otherwise you're repeatedly stacking up the memory with the new views.

pass onTouch event window manager

I need to get the onclick event only on the button, but when the user clicks on the view (windowManager), I want to pass the touch to the fragment behind. My problem is that I get the onclick of the button but I don't know how to pass the event to the view behind. Any suggestions?
public class ReconnectionLinkedInOverlayView extends View {
private final Context mContext;
private final ViewGroup mPopupLayout;
private Button refresh;
private WindowManager mWinManager;
public ReconnectionLinkedInOverlayView(Activity activity) {
super(activity.getApplicationContext());
mContext = activity.getApplicationContext();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
|WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
mWinManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPopupLayout = (RelativeLayout) inflater.inflate(R.layout.notification_linkedin_reconnection_card, null);
mPopupLayout.setVisibility(GONE);
refresh = (Button) mPopupLayout.findViewById(R.id.notification_linked_in_reconnection_button);
Typeface face = Typeface.createFromAsset(activity.getAssets(),
"fonts/OpenSans-Regular.ttf");
refresh.setTypeface(face);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hide();
}
});
mPopupLayout.setTouchInterceptor(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "PopupWindow :: onTouch()");
return false;
}
});
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
mWinManager.addView(mPopupLayout, params);
mPopupLayout.setVisibility(GONE);
}
/**
* Shows view
*/
public void show() {
final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_in);
in.setDuration(1000);
mPopupLayout.setVisibility(VISIBLE);
mPopupLayout.startAnimation(in);
}
/**
* Hides view
*/
public void hide() {
final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_out);
in.setDuration(1000);
if(mPopupLayout != null)
mWinManager.removeView(mPopupLayout);
mPopupLayout.setVisibility(GONE);
}
}

Developing Android application with same result as Settings ->Developer options ->Show touch data

I want to develop an independent application to find user touch events(Similar to Settings>Developer Options>Show touch Data).
I have used WindowManager with type set to TYPE_SYSTEM_ALERT and flag set with FLAG_WATCH_OUTSIDE_TOUCH.
Here i am getting the user touch coordinates but, the touch is not passed on to the below window to open the corresponding touched application.
Please suggests me on how I can get the required result.
Below is the service which does the work.
public class HUD extends Service {
HUDView mView;
// #Override
// public IBinder onBind(Intent intent) {
// return null;
// }
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show();
mView = new HUDView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
// WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
// | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_SHORT).show();
if(mView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
mView = null;
}
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
class HUDView extends ViewGroup {
private Paint mLoadPaint;
public HUDView(Context context) {
super(context);
Toast.makeText(getContext(),"HUDView", Toast.LENGTH_SHORT).show();
mLoadPaint = new Paint();
mLoadPaint.setAntiAlias(true);
mLoadPaint.setTextSize(10);
mLoadPaint.setARGB(255, 255, 0, 0);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 15, mLoadPaint);
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
Toast.makeText(getContext(),"X= "+ event.getX()+"Y= "+event.getY(), Toast.LENGTH_SHORT).show();
return true;
}
}
Here i am getting the user touch coordinates but, the touch is not passed on to the below window to open the corresponding touched application.
Fortunately, what you want is not possible on Android 4.0+, for privacy and security reasons. What you are proposing is the malware technique referred to as "tapjacking".

How to disable the notification bar pull-down in Android?

I am building a new lock screen for Android, but I am unable to lock the notification bar from pulling it down.
I want to disable the notification bar pull-down.
private void disablePullNotificationTouch() {
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (25 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.RGBX_8888;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
}
//Add this class in your project
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
Unless you modify system SystemUI.apk file, it's not possible, even then your app will require root permissions.
The best you could do is create an app in fullscreen mode which will hide notification bar.
Firstly, you need the EXPAND_STATUS_BAR permission:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
and hope this link will help you for sure..

Categories

Resources