移除不需要的代码和功能
parent
913747e6c3
commit
2baf96fe67
@ -1,270 +0,0 @@
|
|||||||
package com.xypower.mpapp;
|
|
||||||
|
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.IBinder;
|
|
||||||
import android.text.Editable;
|
|
||||||
import android.text.TextWatcher;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.WindowManager;
|
|
||||||
import android.view.inputmethod.InputMethodManager;
|
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
|
||||||
|
|
||||||
public class FloatingWindow extends Service {
|
|
||||||
|
|
||||||
private static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 8466506;
|
|
||||||
private static final String FOREGROUND_CHANNEL_ID = "floatingwin_mpapp";
|
|
||||||
|
|
||||||
private Context mContext;
|
|
||||||
private WindowManager mWindowManager;
|
|
||||||
private View mView;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate() {
|
|
||||||
super.onCreate();
|
|
||||||
mContext = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
|
||||||
|
|
||||||
allAboutLayout(intent);
|
|
||||||
moveView();
|
|
||||||
|
|
||||||
final Context context = getApplicationContext();
|
|
||||||
Handler handler = new Handler();
|
|
||||||
handler.postDelayed(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
createNotificationChannel();
|
|
||||||
|
|
||||||
Notification notification = new NotificationCompat.Builder(context, FOREGROUND_CHANNEL_ID)
|
|
||||||
.setContentTitle("FloatingWindow")
|
|
||||||
.setContentText("App Running")
|
|
||||||
// .setSmallIcon(R.drawable.ic_notification)
|
|
||||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// 启动前台服务
|
|
||||||
startForeground(NOTIFICATION_ID_FOREGROUND_SERVICE, notification);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 4000);
|
|
||||||
|
|
||||||
return super.onStartCommand(intent, flags, startId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (mView != null) {
|
|
||||||
mWindowManager.removeView(mView);
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
// ex.printStackTrace();
|
|
||||||
Log.e("FW", "Exception " + ex.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createNotificationChannel() {
|
|
||||||
// >= Android 8.0
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
NotificationChannel channel = new NotificationChannel(
|
|
||||||
FOREGROUND_CHANNEL_ID,
|
|
||||||
"FloatingWindow",
|
|
||||||
NotificationManager.IMPORTANCE_LOW);
|
|
||||||
|
|
||||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
|
||||||
if (manager != null) {
|
|
||||||
manager.createNotificationChannel(channel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowManager.LayoutParams mWindowsParams;
|
|
||||||
private void moveView() {
|
|
||||||
/*
|
|
||||||
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
|
|
||||||
int width = (int) (metrics.widthPixels * 1f);
|
|
||||||
int height = (int) (metrics.heightPixels * 1f);
|
|
||||||
|
|
||||||
mWindowsParams = new WindowManager.LayoutParams(
|
|
||||||
width,//WindowManager.LayoutParams.WRAP_CONTENT,
|
|
||||||
height,//WindowManager.LayoutParams.WRAP_CONTENT,
|
|
||||||
//WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
|
|
||||||
|
|
||||||
(Build.VERSION.SDK_INT <= 25) ? WindowManager.LayoutParams.TYPE_PHONE : WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
|
|
||||||
,
|
|
||||||
|
|
||||||
//WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
|
|
||||||
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
|
|
||||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN // Not displaying keyboard on bg activity's EditText
|
|
||||||
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|
|
||||||
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|
|
||||||
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
|
||||||
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
|
|
||||||
//WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, //Not work with EditText on keyboard
|
|
||||||
PixelFormat.TRANSLUCENT);
|
|
||||||
|
|
||||||
|
|
||||||
mWindowsParams.gravity = Gravity.TOP | Gravity.LEFT;
|
|
||||||
//params.x = 0;
|
|
||||||
mWindowsParams.y = 100;
|
|
||||||
mWindowManager.addView(mView, mWindowsParams);
|
|
||||||
|
|
||||||
mView.setOnTouchListener(new View.OnTouchListener() {
|
|
||||||
private int initialX;
|
|
||||||
private int initialY;
|
|
||||||
private float initialTouchX;
|
|
||||||
private float initialTouchY;
|
|
||||||
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
@Override
|
|
||||||
public boolean onTouch(View v, MotionEvent event) {
|
|
||||||
if (System.currentTimeMillis() - startTime <= 300) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (isViewInBounds(mView, (int) (event.getRawX()), (int) (event.getRawY()))) {
|
|
||||||
editTextReceiveFocus();
|
|
||||||
} else {
|
|
||||||
editTextDontReceiveFocus();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (event.getAction()) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
|
||||||
initialX = mWindowsParams.x;
|
|
||||||
initialY = mWindowsParams.y;
|
|
||||||
initialTouchX = event.getRawX();
|
|
||||||
initialTouchY = event.getRawY();
|
|
||||||
break;
|
|
||||||
case MotionEvent.ACTION_UP:
|
|
||||||
break;
|
|
||||||
case MotionEvent.ACTION_MOVE:
|
|
||||||
mWindowsParams.x = initialX + (int) (event.getRawX() - initialTouchX);
|
|
||||||
mWindowsParams.y = initialY + (int) (event.getRawY() - initialTouchY);
|
|
||||||
mWindowManager.updateViewLayout(mView, mWindowsParams);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isViewInBounds(View view, int x, int y) {
|
|
||||||
Rect outRect = new Rect();
|
|
||||||
int[] location = new int[2];
|
|
||||||
view.getDrawingRect(outRect);
|
|
||||||
view.getLocationOnScreen(location);
|
|
||||||
outRect.offset(location[0], location[1]);
|
|
||||||
return outRect.contains(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void editTextReceiveFocus() {
|
|
||||||
if (!wasInFocus) {
|
|
||||||
mWindowsParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
|
|
||||||
mWindowManager.updateViewLayout(mView, mWindowsParams);
|
|
||||||
wasInFocus = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void editTextDontReceiveFocus() {
|
|
||||||
if (wasInFocus) {
|
|
||||||
mWindowsParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
|
|
||||||
mWindowManager.updateViewLayout(mView, mWindowsParams);
|
|
||||||
wasInFocus = false;
|
|
||||||
hideKeyboard(mContext, edt1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean wasInFocus = true;
|
|
||||||
private EditText edt1;
|
|
||||||
private void allAboutLayout(Intent intent) {
|
|
||||||
|
|
||||||
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
||||||
mView = layoutInflater.inflate(R.layout.ovelay_window, null);
|
|
||||||
|
|
||||||
edt1 = (EditText) mView.findViewById(R.id.edt1);
|
|
||||||
final TextView tvValue = (TextView) mView.findViewById(R.id.tvValue);
|
|
||||||
Button btnClose = (Button) mView.findViewById(R.id.btnClose);
|
|
||||||
|
|
||||||
edt1.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
mWindowsParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
|
|
||||||
// mWindowsParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
|
|
||||||
mWindowManager.updateViewLayout(mView, mWindowsParams);
|
|
||||||
wasInFocus = true;
|
|
||||||
showSoftKeyboard(v);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
edt1.addTextChangedListener(new TextWatcher() {
|
|
||||||
@Override
|
|
||||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
||||||
tvValue.setText(edt1.getText());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterTextChanged(Editable editable) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
btnClose.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
stopSelf();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void hideKeyboard(Context context, View view) {
|
|
||||||
if (view != null) {
|
|
||||||
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
||||||
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showSoftKeyboard(View view) {
|
|
||||||
if (view.requestFocus()) {
|
|
||||||
InputMethodManager imm = (InputMethodManager)
|
|
||||||
getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
||||||
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
package com.xypower.mpapp;
|
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
public class ScreenActionReceiver extends BroadcastReceiver {
|
|
||||||
|
|
||||||
private String TAG = "ScreenActionReceiver";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
|
|
||||||
//LOG
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("Action: " + intent.getAction() + "\n");
|
|
||||||
// sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
|
|
||||||
String log = sb.toString();
|
|
||||||
Log.d(TAG, log);
|
|
||||||
Toast.makeText(context, log, Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
String action = intent.getAction();
|
|
||||||
try {
|
|
||||||
|
|
||||||
if (Intent.ACTION_SCREEN_ON.equals(action)) {
|
|
||||||
Log.d(TAG, "screen is on...");
|
|
||||||
Toast.makeText(context, "screen ON", Toast.LENGTH_SHORT);
|
|
||||||
|
|
||||||
//Run the locker
|
|
||||||
|
|
||||||
context.startService(new Intent(context, FloatingWindow.class));
|
|
||||||
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
|
|
||||||
Log.d(TAG, "screen is off...");
|
|
||||||
Toast.makeText(context, "screen OFF", Toast.LENGTH_SHORT);
|
|
||||||
|
|
||||||
} else if (Intent.ACTION_USER_PRESENT.equals(action)) {
|
|
||||||
Log.d(TAG, "screen is unlock...");
|
|
||||||
Toast.makeText(context, "screen UNLOCK", Toast.LENGTH_SHORT);
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
context.startForegroundService(new Intent(context, FloatingWindow.class));
|
|
||||||
} else {
|
|
||||||
context.startService(new Intent(context, FloatingWindow.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
|
||||||
Log.d(TAG, "boot completed...");
|
|
||||||
Toast.makeText(context, "BOOTED..", Toast.LENGTH_SHORT);
|
|
||||||
//Run the locker
|
|
||||||
/* Intent i = new Intent(context, FloatingWindow.class);
|
|
||||||
context.startService(i);
|
|
||||||
|
|
||||||
*/
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
// context.startForegroundService(new Intent(context, FloatingWindow.class));
|
|
||||||
} else {
|
|
||||||
// context.startService(new Intent(context, FloatingWindow.class));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public IntentFilter getFilter(){
|
|
||||||
final IntentFilter filter = new IntentFilter();
|
|
||||||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
|
||||||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue