|
|
@@ -0,0 +1,241 @@
|
|
|
+package com.sheep.jiuyan.samllsheep.service;
|
|
|
+
|
|
|
+import android.app.AlertDialog;
|
|
|
+import android.app.Service;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
+import android.content.Intent;
|
|
|
+import android.graphics.PixelFormat;
|
|
|
+import android.graphics.Point;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.IBinder;
|
|
|
+import android.provider.Settings;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.Gravity;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+
|
|
|
+import com.sheep.gamegroup.util.Jump2View;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.DeviceInfo;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.OpenService;
|
|
|
+
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import rx.Observable;
|
|
|
+import rx.Subscription;
|
|
|
+import rx.android.schedulers.AndroidSchedulers;
|
|
|
+import rx.functions.Action1;
|
|
|
+import rx.schedulers.Schedulers;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2018/5/8.
|
|
|
+ * realicing@sina.com
|
|
|
+ */
|
|
|
+public class FloatService extends Service implements View.OnClickListener {
|
|
|
+
|
|
|
+ private static final String TAG = "MainService";
|
|
|
+
|
|
|
+ LinearLayout mFloatwindow;
|
|
|
+ WindowManager.LayoutParams params;
|
|
|
+ WindowManager mWm;
|
|
|
+
|
|
|
+ ImageView floaticon;
|
|
|
+
|
|
|
+ //状态栏高度.
|
|
|
+ int statusBarHeight = -1;
|
|
|
+ private Point mSize;
|
|
|
+ private View mLeft;
|
|
|
+ private View mRight;
|
|
|
+
|
|
|
+ //不与Activity进行绑定.
|
|
|
+ @Override
|
|
|
+ public IBinder onBind(Intent intent) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
+ Log.i(TAG, "MainService Created");
|
|
|
+ mSize = DeviceInfo.getDeviceSize(this);
|
|
|
+ createToucher();
|
|
|
+ startTimer();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stopTimer() {
|
|
|
+ if(subscription != null){
|
|
|
+ subscription.unsubscribe();
|
|
|
+ }
|
|
|
+ subscription = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Subscription subscription;
|
|
|
+ private void startTimer() {
|
|
|
+ if(subscription == null)
|
|
|
+ subscription = Observable.interval(1000, TimeUnit.MILLISECONDS)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new Action1<Long>() {
|
|
|
+ @Override
|
|
|
+ public void call(Long aLong) {
|
|
|
+ Jump2View.getInstance().startAccessibility(getApplication());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void createToucher() {
|
|
|
+ //赋值WindowManager&LayoutParam.
|
|
|
+ params = new WindowManager.LayoutParams();
|
|
|
+ if(mWm==null) {
|
|
|
+ mWm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
|
|
|
+ }
|
|
|
+ //设置type.系统提示型窗口,一般都在应用程序窗口之上.
|
|
|
+ //设置效果为背景透明.
|
|
|
+ params.format = PixelFormat.RGBA_8888;
|
|
|
+ //设置flags.不可聚焦及不可使用按钮对悬浮窗进行操控.
|
|
|
+ params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
|
|
|
+ | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
|
|
|
+//
|
|
|
+// //设置窗口初始停靠位置.
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ // 大于等于 24 即为 7.0 及以上执行内容
|
|
|
+ Log.e("qx", "7.0");
|
|
|
+ params.type = WindowManager.LayoutParams.TYPE_PHONE; // 7.1
|
|
|
+ } else {
|
|
|
+ // 低于 24 即为 7.0 以下执行内容
|
|
|
+ params.type = WindowManager.LayoutParams.TYPE_TOAST; // 4.4
|
|
|
+ Log.e("qx", "4.4");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ params.gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
+ params.x = 0;
|
|
|
+ params.y = DeviceInfo.dip2px(getApplicationContext(), 25);
|
|
|
+
|
|
|
+ //设置悬浮窗口长宽数据.
|
|
|
+ params.width = -2;
|
|
|
+ params.height = DeviceInfo.dip2px(getApplicationContext(), 40f);
|
|
|
+
|
|
|
+ LayoutInflater inflater = LayoutInflater.from(getApplication());
|
|
|
+ //获取浮动窗口视图所在布局.
|
|
|
+ mFloatwindow = (LinearLayout) inflater.inflate(R.layout.float_window, null);
|
|
|
+ //添加toucherlayout
|
|
|
+ Log.i(TAG, "toucherlayout-->left:" + mFloatwindow.getLeft());
|
|
|
+ Log.i(TAG, "toucherlayout-->right:" + mFloatwindow.getRight());
|
|
|
+ Log.i(TAG, "toucherlayout-->top:" + mFloatwindow.getTop());
|
|
|
+ Log.i(TAG, "toucherlayout-->bottom:" + mFloatwindow.getBottom());
|
|
|
+
|
|
|
+ //主动计算出当前View的宽高信息.
|
|
|
+ mFloatwindow.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
|
|
|
+
|
|
|
+ //用于检测状态栏高度.
|
|
|
+ int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
|
|
|
+ if (resourceId > 0) {
|
|
|
+ statusBarHeight = getResources().getDimensionPixelSize(resourceId);
|
|
|
+ }
|
|
|
+ Log.i(TAG, "状态栏高度为:" + statusBarHeight);
|
|
|
+
|
|
|
+ //浮动窗口按钮.
|
|
|
+ floaticon = (ImageView) mFloatwindow.findViewById(R.id.floate_icon);
|
|
|
+ mLeft = mFloatwindow.findViewById(R.id.ll_info_left);
|
|
|
+ mRight = mFloatwindow.findViewById(R.id.ll_info_right);
|
|
|
+ ImageView imgCommitRight = (ImageView) mFloatwindow.findViewById(R.id.img_commit_right);
|
|
|
+ ImageView imgCommitLeft = (ImageView) mFloatwindow.findViewById(R.id.img_commit_left);
|
|
|
+ ImageView imgOpenRight = (ImageView) mFloatwindow.findViewById(R.id.img_open_right);
|
|
|
+ ImageView imgOpenLeft = (ImageView) mFloatwindow.findViewById(R.id.img_open_left);
|
|
|
+
|
|
|
+ imgCommitRight.setOnClickListener(this);
|
|
|
+ imgCommitLeft.setOnClickListener(this);
|
|
|
+ imgOpenRight.setOnClickListener(this);
|
|
|
+ imgOpenLeft.setOnClickListener(this);
|
|
|
+ floaticon.setOnClickListener(this);
|
|
|
+
|
|
|
+ floaticon.setOnTouchListener(new View.OnTouchListener() {
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View v, MotionEvent event) {
|
|
|
+ params.x = (int) event.getRawX() - 20;
|
|
|
+ params.y = (int) event.getRawY() - 20 - statusBarHeight;
|
|
|
+ mWm.updateViewLayout(mFloatwindow, params);
|
|
|
+ switch (event.getAction()) {
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ case MotionEvent.ACTION_CANCEL:
|
|
|
+ int x = (int) event.getRawX();
|
|
|
+ if (x > mSize.x / 2) {
|
|
|
+ params.x = mSize.x;
|
|
|
+ } else {
|
|
|
+ params.x = 0;
|
|
|
+ }
|
|
|
+ mWm.updateViewLayout(mFloatwindow, params);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ try {
|
|
|
+ if (intent != null && intent.getBooleanExtra("isShow", true)) {
|
|
|
+ if (mFloatwindow != null) {
|
|
|
+ mWm.addView(mFloatwindow, params);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (mFloatwindow != null)
|
|
|
+ mWm.removeViewImmediate(mFloatwindow);
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ }
|
|
|
+ return super.onStartCommand(intent, flags, START_STICKY);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ if (floaticon != null) {
|
|
|
+ mWm.removeView(mFloatwindow);
|
|
|
+ }
|
|
|
+ stopTimer();
|
|
|
+ super.onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 前往开启辅助服务界面
|
|
|
+ */
|
|
|
+ private void goAccess() {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ startActivity(intent);
|
|
|
+
|
|
|
+ Intent intent2 = new Intent(this, MonitorAppService.class);
|
|
|
+ startService(intent2);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ if (!new OpenService().isAccessibilitySettingsOn(this)) {
|
|
|
+ AlertDialog.Builder builder= new AlertDialog.Builder(this)
|
|
|
+ .setTitle("温馨提示!")
|
|
|
+ .setMessage("亲!要赚钱需要手动开启小绵羊辅助功能哟!")
|
|
|
+ .setPositiveButton("去开启", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+ goAccess();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ builder.setCancelable(false);
|
|
|
+ builder.show();
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|