Detect gestures without blocking other apps - android

I want to create system overlay view, which detect gestures without blocking other apps. How can i do it (apps like OmniSwipe, Easy Swipe detect gestures without blocking anything)? Below code, which blocking other apps cause of system overlay view.
package com.carxsing.anglelockscreen.GestureDetector;
public class GestureService extends Service implements
GestureOverlayView.OnGesturePerformedListener{
private WindowManager windowManager;
private GestureOverlayView GestureOverlay, GestureOverlay2;
private GestureLibrary mGestureLib;
public GestureService() {
}
#Override public IBinder onBind(Intent intent) {
return null;
}
#Override public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
GestureOverlay = new GestureOverlayView(this);
GestureOverlay2 = new GestureOverlayView(this);
GestureOverlay.setGestureColor(00000000);
GestureOverlay.setUncertainGestureColor(00000000);
GestureOverlay2.setGestureColor(00000000);
GestureOverlay2.setUncertainGestureColor(00000000);
GestureOverlay.addOnGesturePerformedListener(this);
GestureOverlay2.addOnGesturePerformedListener(this);
mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mGestureLib.load()) {
mGestureLib.save();
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.height = 450;
params.width = 75;
params.gravity = Gravity.BOTTOM | Gravity.LEFT;
windowManager.addView(GestureOverlay, params);
params.height = 450;
params.width = 75;
params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
windowManager.addView(GestureOverlay2, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if (GestureOverlay != null) windowManager.removeView(GestureOverlay);
if (GestureOverlay2 != null) windowManager.removeView(GestureOverlay2);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
if (predictions.size() > 0) {
Prediction prediction = predictions.get(0);
if (prediction.score > 1.0) {
if (prediction.name.equals(Gestures.bottomleft) || prediction.name.equals(Gestures.bottomright)) {
Intent i = new Intent(GestureService.this, LockScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("start", 1);
startActivity(i);
}
}
}
}
}

Related

Show a popup when user click on Overlay button

I have an Overlay Button in my Android app. I want to show a layout and interact with the views of my Layout when the user click on the button.
For the moment, I show a Toast. How can I do that ?
This is my OverlayShowingService.class :
public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {
private View topLeftView;
private Button overlayedButton;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setText("Overlay button");
overlayedButton.setOnTouchListener(this);
overlayedButton.setBackgroundColor(Color.BLACK);
overlayedButton.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Here I want to show a layout with some options in a box", Toast.LENGTH_SHORT).show();
}
}
Send a broadcast when then the button is clicked and start activity from Broadcast receivers onReceive method.
Create a broadcast receiver,Register a Broadcast reciever onCreate() of your service then. Broadcast intent when the button is clicked. Finally the receiver will open a Activity(layout) when the overlay button is clicked.
If you only wanted to open a dialog this is a decent answer. https://stackoverflow.com/a/31221355/4179914
Turns out you can also theme a activity like a dialog as mentioned here
https://stackoverflow.com/a/7918720/4179914
public class OverlayClickReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent toDialog = new Intent(context, Main2Activity.class);
toDialog.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(toDialog);
}
}
Send Broadcast on overlay button clicked.
#Override
public void onClick(View v) {
broadcastClick();
}
private void broadcastClick() {
final Intent intent = new Intent("user.clicked.overlay.button");
final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.sendBroadcast(intent);
}
Register Broadcast receiver onCreate()
#Override
public void onCreate() {
super.onCreate();
overlayShowing = new OverlayClickReceiver();
.......
}
private void registerClickReceiver() {
LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing,
new IntentFilter("user.clicked.overlay.button"));
}
Unregister Broadcast receiver onDestroy()
#Override
public void onDestroy() {
super.onDestroy();
..........
unregisterClickReceriver();
}
private void unregisterClickReceriver() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(overlayShowing);
}
From what i understood from the question, you want to get a view form xml files and add it to a window.
You can use View view = LayoutInflater.from(context).inflate(r.layout.overlay,null,false); and add that view with WindowManager.addView(view) to the window.

How to create a fab like DU Speed Booster that keep running?

I want to create a floating action button that keep running like DU Speed Booster.
Something like iOS assistive touch.
Here is a picture.
For doing this you might need to have this permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Create a Service and put this code inside OnCreate()
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);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
}
#Override
public void onDestroy() {
super.onDestroy();
if (chatHead != null) windowManager.removeView(chatHead);
}
}
Start serviceusing below code
startService(new Intent(context, ChatHeadService.class));
Please refer this blog for complete example http://androidsrc.net/facebook-chat-like-floating-chat-heads/

Window manager not showing window on the top

In an android application I have to show an internal notification that comes over the top of every screen in my app . I have used WindowManager. But it doesn't seem to work as expected. Sometimes I see the first notification but the second is not shown though when debugged ,step by step execution of the following method is visible. Can't figure out why is this happening. Help!!
public void addNotification(String message, Bundle bundle) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
lp.gravity = Gravity.END | Gravity.BOTTOM;
lp.x = getResources().getInteger(R.integer.notification_side_margin);
lp.y = getResources().getInteger(R.integer.notification_side_margin);
// Initialise container view first in which every notification is to be
// added.
if (notificationConatinerView == null) {
notificationConatinerView = getLayoutInflater().inflate(
R.layout.custom_notification_main_layout, null);
notificationContainer = (LinearLayout) notificationConatinerView
.findViewById(R.id.notification_container);
notificationContainer.setPadding(
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin));
windowManager.addView(notificationConatinerView, lp);
// Add that container to the window manager
windowManager.updateViewLayout(notificationConatinerView, lp);
}
final View view = getLayoutInflater().inflate(
R.layout.notification_custom, null);
view.setTag(bundle);
view.setId(notificationContainer.getChildCount());
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = (Bundle) v.getTag();
setClickForNotifications(bundle);
}
});
final ImageView crossImage = (ImageView) view
.findViewById(R.id.notification_cross);
crossImage.setTag(view);
TextView messageTextView = (TextView) view
.findViewById(R.id.message_text);
messageTextView.setText(message);
crossImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (notificationContainer.getChildCount() == 1) {
showNotificationExitingView(v);
} else {
if (!isFinishing())
notificationContainer.removeView((View) crossImage
.getTag());
}
}
});
LinearLayout.LayoutParams childLP = new LinearLayout.LayoutParams(
(int) getResources().getDimension(
R.dimen.notification_view_width),
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (notificationContainer.getChildCount() == getResources().getInteger(
R.integer.no_of_notifications)) {
notificationContainer.removeViewAt(notificationContainer
.getChildCount() - 1);
}
notificationContainer.addView(view, 0, childLP);
notificationContainer.updateViewLayout(view, childLP);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (notificationContainer.getChildCount() == 1) {
showNotificationExitingView(view);
} else {
if (!isFinishing())
notificationContainer.removeView(view);
}
}
}, 10000);
try {
windowManager.updateViewLayout(notificationConatinerView, lp);
} catch (Exception e) {
windowManager.addView(notificationConatinerView, lp);
windowManager.updateViewLayout(notificationConatinerView, lp);
}
if (notificationContainer.getChildCount() == 1) {
showNotificationEnteringAnimation();
}
}
Thanks!!
use
windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
instead of
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

WindowManager.removeView() gives IllegalStateException

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?
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
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);
}
});
}
}

Android : How to show dialog or activity over lock screen[ not unlock the screen]

I hava a app, it will receive msg from server and make a dialog to user.So when phone is on lock screen i want dialog will show on the top of lock screen but not unlock it.could anyone give me suggestion ?
I solved something similar in the following way.
Create services, to broadcast the action "ACTION_SCREEN_ON & ACTION_USER_PRESENT & ACTION_SCREEN_OFF", create function to show the windows with WINDOW_SERVICE. I use a service for my requirements, but it can adapt.
public class OverlayService extends Service {
private static final String TAG = OverlayService.class.getSimpleName();
WindowManager mWindowManager;
View mView;
Animation mAnimation;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerOverlayReceiver();
return super.onStartCommand(intent, flags, startId);
}
private void showDialog(String aTitle){
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mView = View.inflate(getApplicationContext(), R.layout.fragment_overlay, null);
mView.setTag(TAG);
int top = getApplicationContext().getResources().getDisplayMetrics().heightPixels / 2;
RelativeLayout dialog = (RelativeLayout) mView.findViewById(R.id.dialog);
LayoutParams lp = (LayoutParams) dialog.getLayoutParams();
lp.topMargin = top;
lp.bottomMargin = top;
mView.setLayoutParams(lp);
ImageButton imageButton = (ImageButton) mView.findViewById(R.id.close);
lp = (LayoutParams) imageButton.getLayoutParams();
lp.topMargin = top - 58;
imageButton.setLayoutParams(lp);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mView.setVisibility(View.INVISIBLE);
}
});
TextView title = (TextView) mView.findViewById(R.id.Title);
title.setText(aTitle);
final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
PixelFormat.RGBA_8888);
mView.setVisibility(View.VISIBLE);
mAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.in);
mView.startAnimation(mAnimation);
mWindowManager.addView(mView, mLayoutParams);
}
private void hideDialog(){
if(mView != null && mWindowManager != null){
mWindowManager.removeView(mView);
mView = null;
}
}
#Override
public void onDestroy() {
unregisterOverlayReceiver();
super.onDestroy();
}
private void registerOverlayReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(overlayReceiver, filter);
}
private void unregisterOverlayReceiver() {
hideDialog();
unregisterReceiver(overlayReceiver);
}
private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "[onReceive]" + action);
if (action.equals(Intent.ACTION_SCREEN_ON)) {
showDialog("Esto es una prueba y se levanto desde");
}
else if (action.equals(Intent.ACTION_USER_PRESENT)) {
hideDialog();
}
else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
hideDialog();
}
}
};
}
I hope it useful!
public void onAttachedToWindow() {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
Finally I achieved the same. Don't go for activity, because android will not show lock screen behind your activity for security reason, so use service instead of Activity.
Below is my code in onStartCommand of my service
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
View mView = mInflater.inflate(R.layout.score, null);
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);
mWindowManager.addView(mView, mLayoutParams);
And add <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> to manifest

Categories

Resources