kp_time 桌面悬浮按钮

 

// 桌面悬浮按钮

public class WindowButtonReceiver extends BroadcastReceiver implements MySubscriber.OnSubscriberOnNextListener<ResultBean<TaskItemBean>> {

    public static final String ACTION_HIDE_SUSPEND_BTN = "com.mpyf.keeptime.ACTION_HIDE_SUSPEND_BTN";
    public static final String ACTION_SHOW_SUSPEND_BTN = "com.mpyf.keeptime.ACTION_SHOW_SUSPEND_BTN";
    public static final String ACTION_REFRESH_ITEM_COUNT = "com.mpyf.keeptime.ACTION_REGRESH_ITEM_COUNT_SUSPEND_BTN";
    private Context context;
    private static View ivSuspendButtonLayoutView;
    private static WindowManager windowManager;
    private View ivSuspendBtn;
    private static TextView tvListCount;

    @Override
    public void onReceive(Context context, Intent intent) {
        EventBus.getDefault().register(this);
        this.context = context;
        String action = intent.getAction();

//        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
        if (Intent.ACTION_USER_PRESENT.equals(action) && SpUtil.getBoolean(SpUtil.SET_SUSPEND_BTN)) {
            showSuspendButton();
//            解锁后进入app,隐藏掉悬浮按钮
            context.sendBroadcast(new Intent("android.intent.action.USER_PRESENT.after.close"));
        } else if (ACTION_SHOW_SUSPEND_BTN.equals(action) && SpUtil.getBoolean(SpUtil.SET_SUSPEND_BTN)) {
//            EventBus.getDefault().post(new CloseBaseActivityBus("colseBaseActiivty"));
            showSuspendButton();
        } else if (ACTION_HIDE_SUSPEND_BTN.equals(action)) {
            hideSuspendButton();
        } else if (ACTION_REFRESH_ITEM_COUNT.equals(action)) {
            getTaskItems();
        }
//        } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
//            hideSuspendButton();
//        }
        Log.e("action", "onReceive" + action);
    }

    private void getTaskItems() {
        HashMap<String, Object> param = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(new Date());
        param.put("date", formatDate);
        DataAccess.getTaskItems(this, ParamUtil.getParamJson(param));
    }

    public void showSuspendButton() {
        windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
//        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
//        ImageView imageView = new ImageView(context);
//        imageView.setImageResource(R.drawable.bg_suspend_btn);

        final WindowManager.LayoutParams mWindowLayoutParams = new WindowManager.LayoutParams();
        mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; //图片之外的其他地方透明
        mWindowLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
//        mWindowLayoutParams.x = downX - mPoint2ItemLeft + mOffset2Left;
//        mWindowLayoutParams.y = downY - mPoint2ItemTop + mOffset2Top - mStatusHeight;
        mWindowLayoutParams.alpha = 1f; //不透明度
//        mWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mWindowLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
        int btnX = SpUtil.getInt(SpUtil.SUSPEND_BTN_POSITION_X);
        int btnY = SpUtil.getInt(SpUtil.SUSPEND_BTN_POSITION_Y);
        int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        int heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        if (btnX == -1) {
            btnX = (int) (widthPixels * 0.8);
        }
        if (btnX == -1) {
            btnY = (int) (heightPixels * 0.8);
        }

        mWindowLayoutParams.x = btnX;
        mWindowLayoutParams.y = btnY;

        if (ivSuspendButtonLayoutView != null && ivSuspendButtonLayoutView.isAttachedToWindow()) {
//            windowManager.removeView(ivSuspendButtonLayoutView);
            return;
//            Log.e("event", "remove");
        } else {
            ivSuspendButtonLayoutView = View.inflate(context, R.layout.layout_suspend_btn, null);
            ivSuspendBtn = ivSuspendButtonLayoutView.findViewById(R.id.iv_suspend_btn);
            tvListCount = ivSuspendButtonLayoutView.findViewById(R.id.tv_today_list_count);
//            初次创建悬浮按钮,记载一次数据,否则通过刷新数据广播刷新
            getTaskItems();
        }

        windowManager.addView(ivSuspendButtonLayoutView, mWindowLayoutParams);

        final int radius = ivSuspendButtonLayoutView.getWidth() / 2;
        final long[] startTime = new long[1];
        ivSuspendButtonLayoutView.setOnTouchListener(new View.OnTouchListener() {

            private int limitDistance;
            private double sqrt;
            private float downRawY;
            private float downRawX;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                boolean isConsume = true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downRawX = event.getRawX();
                        downRawY = event.getRawY();
                        startTime[0] = SystemClock.currentThreadTimeMillis();
                        limitDistance = UIUtils.dip2px(12);
                        break;
                    case MotionEvent.ACTION_MOVE:
//                        Log.e("event", "action_move");
                        float rawX = event.getRawX();
                        float rawY = event.getRawY();
                        int radius = UIUtils.dip2px(30);
                        mWindowLayoutParams.x = (int) rawX - radius;
                        mWindowLayoutParams.y = (int) rawY - radius;

//
                        long stopMoreTime = SystemClock.currentThreadTimeMillis();
                        float dx = downRawX - event.getRawX();
                        float dy = downRawY - event.getRawY();
                        sqrt = Math.sqrt(dx * dx + dy * dy);
                        if (stopMoreTime - startTime[0] > 20 || sqrt > limitDistance) {
                            windowManager.updateViewLayout(ivSuspendButtonLayoutView, mWindowLayoutParams);
//                            if slidding occurs ,save the coordinate of the button of home
                            SpUtil.putInt(SpUtil.SUSPEND_BTN_POSITION_X, mWindowLayoutParams.x);
                            SpUtil.putInt(SpUtil.SUSPEND_BTN_POSITION_Y, mWindowLayoutParams.y);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        float dxUp = downRawX - event.getRawX();
                        float dyUp = downRawY - event.getRawY();
                        double sqrtUp = Math.sqrt(dxUp * dxUp + dyUp * dyUp);
                        long stopTime = SystemClock.currentThreadTimeMillis();
                        Log.e("event", "action_up" + (stopTime - startTime[0]));
                        if (stopTime - startTime[0] < 20 && (sqrt < limitDistance || sqrtUp < limitDistance)) {
                            startActiivty();
                        }
                        break;
                }

                return isConsume;
            }
        });

    }

    public void startActiivty() {
        int todayTaskItenSize = SpUtil.getInt(SpUtil.TODAY_TASK_ITEM_SIZE);
        Intent intent = new Intent();
        if (todayTaskItenSize == 0) {
            intent.setClass(context, ScheduleMainActivity.class);
        } else {
            intent.setClass(context, TaskItemActivity.class);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setListCount(ListCountCountBus listCount) {
        tvListCount.setText(listCount.listCount);
    }

    private void hideSuspendButton() {
        if (windowManager != null && ivSuspendButtonLayoutView != null && ivSuspendButtonLayoutView.isAttachedToWindow()) {
//            ivSuspendButtonLayoutView.setVisibility(View.GONE);
            windowManager.removeView(ivSuspendButtonLayoutView);
            Log.e("action", "hideSuspendButton()");
        }
    }

    private void showTaskItemDialog() {
    }

    private void HideTaskItemDialog() {
    }

    @Override
    public void onNext(ResultBean<TaskItemBean> taskItemBeanResultBean) {
        if (taskItemBeanResultBean == null) {
            return;
        }
        SpUtil.putInt(SpUtil.TODAY_TASK_ITEM_SIZE, taskItemBeanResultBean.getList().size());
        if (tvListCount != null) {
            if (taskItemBeanResultBean.getList().size() != 0) {
                tvListCount.setVisibility(View.VISIBLE);
                tvListCount.setText(String.valueOf(taskItemBeanResultBean.getList().size()));
            } else {
                tvListCount.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onError() {
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值