Created
February 1, 2018 17:06
-
-
Save kevalpatel2106/573c4426999ae92941cb5b59c589c0fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add the view to the phone's window. | |
*/ | |
private void addToWindow() { | |
//Add the view to the window. | |
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); | |
//Specify the view position | |
params.gravity = Gravity.TOP | Gravity.START; //Initially view will be added to top-left corner | |
params.x = 0; | |
params.y = 100; | |
//Add the view to the window | |
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); | |
mWindowManager.addView(mFloatingView, params); | |
//Drag and move floating view using user's touch action. | |
mFloatingView.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() { | |
private int initialX; | |
private int initialY; | |
private float initialTouchX; | |
private float initialTouchY; | |
private long mStartTime; | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
mStartTime = System.currentTimeMillis(); | |
//remember the initial position. | |
initialX = params.x; | |
initialY = params.y; | |
//get the touch location | |
initialTouchX = event.getRawX(); | |
initialTouchY = event.getRawY(); | |
return true; | |
case MotionEvent.ACTION_UP: | |
//Check for the click event | |
int Xdiff = (int) (event.getRawX() - initialTouchX); | |
int Ydiff = (int) (event.getRawY() - initialTouchY); | |
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking. | |
//So that is click event. | |
if ((initialX < 10 || initialX > mDeviceWidth - 30) && Xdiff < 8 && Ydiff < 8) { | |
if (PermissionUtils.isDeviceAdminEnable(FloatingLockWidgetService.this)) { | |
Utils.lockDevice(FloatingLockWidgetService.this); | |
} else { | |
startActivity(LockWidgetErrorActivity.getLaunchIntenet(FloatingLockWidgetService.this)); | |
} | |
} | |
//Stick to the verticle edges of the screen | |
float velocity = Math.abs(Xdiff / (System.currentTimeMillis() - mStartTime)); | |
if (velocity > 0.2 || Math.abs(Xdiff) > mDeviceWidth / 2) { | |
if (initialX >= mDeviceWidth / 2) stickToLeftEdge(); | |
else stickToRightEdge(); | |
} else { | |
if (initialX >= mDeviceWidth / 2) stickToRightEdge(); | |
else stickToLeftEdge(); | |
} | |
return true; | |
case MotionEvent.ACTION_MOVE: | |
//Calculate the X and Y coordinates of the view. | |
params.x = initialX + (int) (event.getRawX() - initialTouchX); | |
params.y = initialY + (int) (event.getRawY() - initialTouchY); | |
//Update the layout with new X & Y coordinate | |
mWindowManager.updateViewLayout(mFloatingView, params); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Stick the floating widget to the right edge of the screen. | |
*/ | |
private void stickToRightEdge() { | |
for (int i = params.x; i <= mDeviceWidth; i++) { | |
params.x = i; | |
mWindowManager.updateViewLayout(mFloatingView, params); | |
} | |
} | |
/** | |
* Stick the floating widget to the left edge of the screen. | |
*/ | |
private void stickToLeftEdge() { | |
for (int i = params.x; i >= 0; i--) { | |
params.x = i; | |
mWindowManager.updateViewLayout(mFloatingView, params); | |
} | |
} | |
}); | |
isViewAdded = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment