Paste: GlobalMenuService

Author: 2
Mode: factor
Date: Thu, 21 Sep 2023 02:10:11
Plain Text |
package com.akubela.panel.service.menu;

import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.akubela.panel.R;
import com.akubela.panel.activity.notification.NotificationActivity;
import com.akubela.panel.adapter.MenuRecyclerAdapter;
import com.akubela.panel.common.defined.ComponentInfo;
import com.akubela.panel.common.defined.ExtraKey;
import com.akubela.panel.common.defined.MenuDefined;
import com.akubela.panel.common.util.Log;
import com.akubela.panel.common.util.PanelJumpTool;
import com.akubela.panel.data.idle.MenuItem;
import com.akubela.panel.defined.CommonDefined;
import com.akubela.panel.defined.setting.SettingsCommonDefined;
import com.akubela.panel.defined.settings.SettingsGestureDefined;
import com.akubela.panel.dialog.OneButtonDialog;
import com.akubela.panel.listener.BrightnessChangedListener;
import com.akubela.panel.service.base.BaseGlobalService;
import com.akubela.panel.ui.utils.SwipeBackHelper;
import com.akubela.panel.ui.view.ISlideAble;
import com.akubela.panel.ui.view.MultiDirectionSlidingDrawer;
import com.akubela.panel.ui.view.SettingsSoundProcessView;
import com.akubela.panel.ui.view.SwipeLinearLayout;
import com.akubela.panel.ui.view.VerticalSlideLayout;
import com.akubela.panel.util.SystemTools;
import com.akubela.panel.view.BrightnessWindow;
import com.akubela.panel.view.VolumeWindow;
import com.akubela.panel.view.dialog.SettingsCircleProcessView;
import com.akubela.panel.viewmodel.ControlCenterViewModel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class GlobalMenuService extends BaseGlobalService {

    private static final int MSG_HANDLER_HIDE_MENU = 1;
    private static final int MSG_HANDLER_MENU_UNLOCK_SLIDE = 2;
    private static final int MSG_HANDLER_CONTROL_UNLOCK_SLIDE = 3;
    private WindowManager mWindowManager = null;

    private MenuHandler mHandler = null;

    private WindowManager.LayoutParams mMenuWindowLayoutParams;
    private ViewGroup.LayoutParams mMenuLayoutParams;
    private View mMenuView = null;
    private VerticalSlideLayout mMenuVerticalSlideLayout = null;
    private RelativeLayout mMenuContent = null;
    private RelativeLayout mMenuHandle = null;

    private WindowManager.LayoutParams mControlLayoutParams;
    private View mControlCenterView = null;
    private MultiDirectionSlidingDrawer mControlCenterDrawer;
    private View mControlHandle;
    private View mControlContent;
    private ImageButton mBtnSetting;
    private ImageButton mBtnDnd;
    private ImageButton mBtnNotification;
    private ImageButton mBtnScreenOff;
    private ImageButton mBtnHome;
    private ImageButton mBtnDisplay;
    private ImageButton mBtnVolume;

    private OneButtonDialog mErrorCodeDialog = null;
    //back swipe view
    private WindowManager.LayoutParams mBackLayoutParamsLeft;
    private SwipeLinearLayout mBackSwipeViewLeft = null;
    private WindowManager.LayoutParams mBackLayoutParamsRight;
    private SwipeLinearLayout mBackSwipeViewRight = null;
    private static final int sBackSwipeViewShapeColor = Color.WHITE;
    private static final int sBackSwipeViewArrowColor = Color.WHITE;
    private static final int sBackSwipeViewShapeAlpha = 76;

    private boolean mMenuSlideLock = false;
    private boolean mControlSlideLock = false;

    private View mDndAlertView;
    private WindowManager.LayoutParams mDndAlertLayoutParams;

    private ControlCenterViewModel mControlCenterViewModel = null;

    private VolumeWindow mVolumeWindow;

    private BrightnessWindow mBrightnessWindow;

    private MenuItem mTempToMenuItem = null;

    private MenuRecyclerAdapter mAdapter = null;

    private static int INDEX_MUSIC = 3;

    private String upgradeActivityName = "com.akuvox.upgradeui";

    private static MenuItem createMenuItem(int nameResId, int backgroundResId, int iconResId, int type) {
        MenuItem menuItem = new MenuItem();
        menuItem.setNameResId(nameResId);
        menuItem.setBackgroundResId(backgroundResId);
        menuItem.setIconResId(iconResId);
        menuItem.setComponentType(type);
        return menuItem;
    }


    private int mDisplayWidth = -1;
    private int mDisplayHeight = -1;

    private boolean isShowing = false;

    @Override
    public void onCreate() {
        super.onCreate();

        mControlCenterViewModel = new ViewModelProvider(this).get(ControlCenterViewModel.class);
        mHandler = new MenuHandler();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MenuBinder();
    }

    public class MenuBinder extends Binder {
        public GlobalMenuService getService() {
            return GlobalMenuService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //if (!mControlCenterViewModel.isInited() || mControlCenterViewModel.isFirstBoot()) {
        if (!mControlCenterViewModel.isInited()
                || mControlCenterViewModel.isFirstBoot()
                || SystemTools.isPhoneKilled()) {
            return super.onStartCommand(intent, flags, startId);
        }
        if (null != intent) {
            boolean isShow = intent.getBooleanExtra(ExtraKey.EXTRA_KEY_GLOBAL_MENU_SHOW, true);
            if (!isShow) {
                hideMenuAndControlCenter();
                return super.onStartCommand(intent, flags, startId);
            } else {
                int type = intent.getIntExtra(ExtraKey.EXTRA_KEY_GLOBAL_MENU_SHOW_TYPE, 0);
                if (type == MenuDefined.MenuType.MENU_TYPE_MENU) {
                    if (mMenuVerticalSlideLayout != null) {
                        mMenuVerticalSlideLayout.open();
                    }
                } else if (type == MenuDefined.MenuType.MENU_TYPE_CONTROL_CENTER) {
                    if (mControlCenterDrawer != null) {
                        mControlCenterDrawer.open();
                    }
                }
            }
        }
        initShow();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d("*******onConfigurationChanged***** ");
        stopSelf();
        //release();
        //initShow();
    }

    private void addGlobalBackSwipeView() {
        if (null == mWindowManager) {
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        }
        removeGlobalBackSwipeView();
        Display defaultDisplay = mWindowManager.getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        mDisplayWidth = point.x;
        mDisplayHeight = point.y;

        if (mBackLayoutParamsLeft == null) {
            mBackLayoutParamsLeft = initGlobalBackSwipeViewParams(true);
        }
        if (mBackLayoutParamsRight == null) {
            mBackLayoutParamsRight = initGlobalBackSwipeViewParams(false);
        }

        if (mBackSwipeViewLeft == null) {
            mBackSwipeViewLeft = initGlobalBackSwipeView();
        }
        mBackSwipeViewLeft.setCallback(mSwipeBackHelperCallback);
        if (mBackSwipeViewRight == null) {
            mBackSwipeViewRight = initGlobalBackSwipeView();
        }
        mBackSwipeViewRight.setCallback(mSwipeBackHelperCallback);
        mWindowManager.addView(mBackSwipeViewLeft, mBackLayoutParamsLeft);
        mWindowManager.addView(mBackSwipeViewRight, mBackLayoutParamsRight);
    }

    private void removeGlobalBackSwipeView() {
        try {
            if (mWindowManager != null) {
                if (mBackSwipeViewLeft != null) {
                    try {
                        mWindowManager.removeViewImmediate(mBackSwipeViewLeft);
                    } catch (Exception e) {
                        Log.e("Error: removeView error");
                    }
                }
                if (mBackSwipeViewRight != null) {
                    try {
                        mWindowManager.removeViewImmediate(mBackSwipeViewRight);
                    } catch (Exception e) {
                        Log.e("Error: removeView error");
                    }
                }
            }
        } catch (Exception e) {
            Log.w("removeGlobalBackSwipeView error: " + e);
        }
    }

    private WindowManager.LayoutParams initGlobalBackSwipeViewParams(boolean isLeft) {
        // 设置LayoutParam102
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

        params.format = PixelFormat.TRANSPARENT;
        params.flags = WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        params.width = CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_LEFT_RIGHT;
        params.height = mDisplayHeight;

        //显示的位置
        params.gravity = isLeft ? Gravity.START : Gravity.END;
        params.x = 0;
        params.y = mDisplayHeight / 2;
        return params;
    }

    private final SwipeBackHelper.Callback mSwipeBackHelperCallback = new SwipeBackHelper.Callback() {
        @Override
        public void onBackReleased(int type) {
            Log.d("onBackReleased: " + type);
            Intent intent = new Intent(SettingsGestureDefined.MULTI_FINGER_SWIPE_ACTION);
            if (type == SwipeBackHelper.EDGE_LEFT) {
                intent.putExtra(SettingsGestureDefined.MULTI_FINGER_SWIPE_EXTRA_KEY, SettingsCommonDefined.MultiFingerSwipe.SWIPE_FROM_LEFT);
            } else if (type == SwipeBackHelper.EDGE_RIGHT) {
                intent.putExtra(SettingsGestureDefined.MULTI_FINGER_SWIPE_EXTRA_KEY, SettingsCommonDefined.MultiFingerSwipe.SWIPE_FROM_RIGHT);
            }
            intent.putExtra(SettingsGestureDefined.TRIGGER_BY_RELEASE, true);
            sendBroadcast(intent);
        }

        @Override
        public boolean isShapeAlphaGradient() {
            return super.isShapeAlphaGradient();
        }

        @Override
        public float getShapeMaxPeak() {
            return super.getShapeMaxPeak();
        }

        @Override
        public int getArrowColor() {
            return sBackSwipeViewArrowColor;
        }

        @Override
        public int getShapeColor() {
            return sBackSwipeViewShapeColor;
        }

        @Override
        public int getShapeAlpha() {
            return sBackSwipeViewShapeAlpha;
        }
    };

    private SwipeLinearLayout initGlobalBackSwipeView() {
        return (SwipeLinearLayout) LayoutInflater.from(this).inflate(R.layout.view_back_swipe_view, null);
    }

    private void initShow() {
        if (isShowing) {
            //hideMenu();
            return;
        }
        //addGlobalBackSwipeView();
//        showMenu();
        showControlCenter();
        mControlCenterViewModel.init(this);
        initData();
        initObserver();
        return;
    }

    private void initData() {
        handleBrightness(mControlCenterViewModel.getBrightness());
        handleVolume(mControlCenterViewModel.getSystemVolume());
    }

    private void initObserver() {
        mControlCenterViewModel.getDndStatusLiveData().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean dndStatus) {
                handleDndStatus(dndStatus);
            }
        });

        mControlCenterViewModel.getIsReadLiveData().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                Log.i(",,", "----------aBoolean " + aBoolean);
                mBtnNotification.setSelected(aBoolean);
            }
        });

        mControlCenterViewModel.getSystemVolumeLiveData().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer volume) {
                handleVolume(volume);
            }
        });

        mControlCenterViewModel.addBrightnessChangedListener(mBrightnessChangedListener);
    }

    private void handleVolume(int volume) {
        if (null != mVolumeWindow) {
            mVolumeWindow.setProgress(volume);
        }
        mBtnVolume.setSelected(volume == 0);
    }

    private void handleDndStatus(boolean status) {
        if (null != mBtnDnd) {
            mBtnDnd.setBackgroundResource(status ? R.drawable.ic_dnd_on_selector : R.drawable.ic_dnd_off_selector);
        }
    }

    private static int count480 = 0;
    private boolean mIsMenuSwiping = false;


    private void sendUpdateSkipStatus() {
        Intent intent = new Intent(ComponentInfo.BROADCAST_UPDATE_SKIP_SCREEN_STATUS);
//        sendBroadcast(intent);
    }

    private void hideMenuAndControlCenter() {
        if (null != mMenuVerticalSlideLayout) {
            mMenuVerticalSlideLayout.back();
        }
        closeControlCenterDrawer();
    }

    private boolean needCheckScreenPwd(int menuType) {
        boolean isLocked = mControlCenterViewModel.getScreenLockedStatus();
        boolean isSkip = mControlCenterViewModel.getSkipScreenLockCheck();
        boolean needCheckPw = (isLocked && !isSkip);
        if (isNoSliding()) {
            hideMenuAndControlCenter();
            return false;
        }
        if (needCheckPw) {
            //锁屏了,跳转到解锁界面
            PanelJumpTool.startIdle(GlobalMenuService.this, menuType);
            hideMenuAndControlCenter();
        }
        return (!needCheckPw);
    }

    private boolean isNoSliding() {
        Log.d("on sliding control center isTalkViewShowing " + mControlCenterViewModel.isTalkViewShowing() + " isTimering " + mControlCenterViewModel.isTimering() + " isAlarming " + mControlCenterViewModel.isAlarming() + " isArming " + mControlCenterViewModel.isArming() + " isHomeCenterConflict " + mControlCenterViewModel.isHomeCenterConflict());
        return mControlCenterViewModel.isTalkViewShowing() ||
                mControlCenterViewModel.isTimering() ||
                mControlCenterViewModel.isAlarming() ||
                mControlCenterViewModel.isArming() ||
                mControlCenterViewModel.isArmingCall() ||
                mControlCenterViewModel.isUpgrading() ||
                mControlCenterViewModel.isHomeCenterConflict();
    }

    private void initControlCenterView() {
        if (null == mControlCenterView) {
            mControlCenterView = LayoutInflater.from(this).inflate(R.layout.view_control_center, null);
        }
        mControlCenterDrawer = mControlCenterView.findViewById(R.id.control_drawer);
        mControlHandle = mControlCenterDrawer.getHandle();
        mControlContent = mControlCenterDrawer.getContent();
        mControlCenterDrawer.setName("Control");
        mControlCenterDrawer.setOnDrawerCloseListener(new MultiDirectionSlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                Log.d("##### onDrawerClosed");
                showControlCenter();
//                showMenu();
                mControlCenterViewModel.unlockBack();
                mControlCenterDrawer.setBackgroundColor(getResources().getColor(R.color.transparent));
                mControlContent.setBackground(getResources().getDrawable(R.mipmap.bg_control_center));
            }
        });
        mControlCenterDrawer.setOnDrawerScrollListener(new MultiDirectionSlidingDrawer.OnDrawerScrollListener() {
            @Override
            public void onScrollStarted() {
                Log.d("##### onScrollStarted");
                activeControlWindow(true);
                mControlCenterDrawer.setBackgroundColor(getResources().getColor(R.color.transparent));
                mControlContent.setBackground(getResources().getDrawable(R.mipmap.bg_control_center));
            }

            @Override
            public void onScrollEnded(boolean isScrollFromUser) {
                Log.d("##### onScrollEnded -----isScroll:" + isScrollFromUser);
                if (isScrollFromUser && mControlCenterDrawer.isOpened()) {
                    sendUpdateSkipStatus();
                }
            }
        });

        mControlCenterDrawer.setSlideAble(new ISlideAble() {
            @Override
            public boolean isSlideAble() {
                if (getTopActivityPkgName().equals(upgradeActivityName)) {
                    return false;
                }
                return needCheckScreenPwd(MenuDefined.MenuType.MENU_TYPE_CONTROL_CENTER);
            }
        });

        mControlCenterDrawer.setOnDrawerOpenListener(new MultiDirectionSlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                Log.d("##### onDrawerOpened");
                mControlCenterViewModel.lockBack();
                activeControlWindow(true);
                mControlCenterDrawer.setBackground(getResources().getDrawable(R.mipmap.bg_control_center));
                mControlContent.setBackgroundColor(getResources().getColor(R.color.transparent));
            }
        });

        SettingsCircleProcessView circleProcessView = new SettingsCircleProcessView(this);
        circleProcessView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        SettingsSoundProcessView settingsCircleProcessView = new SettingsSoundProcessView(this);
        settingsCircleProcessView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mBtnSetting = mControlCenterView.findViewById(R.id.btn_setting);
        mBtnDnd = mControlCenterView.findViewById(R.id.btn_dnd);
        mBtnNotification = mControlCenterView.findViewById(R.id.btn_notification);
        mBtnScreenOff = mControlCenterView.findViewById(R.id.btn_screen_off);
        mBtnHome = mControlCenterView.findViewById(R.id.btn_home);
        mBtnDisplay = mControlCenterView.findViewById(R.id.btn_display);
        mBtnVolume = mControlCenterView.findViewById(R.id.btn_volume);

        mBtnSetting.setOnClickListener(mOnControlClickListener);
        mBtnNotification.setOnClickListener(mOnControlClickListener);
        mBtnDnd.setOnClickListener(mOnControlClickListener);
        mBtnHome.setOnClickListener(mOnControlClickListener);
        mBtnScreenOff.setOnClickListener(mOnControlClickListener);
        mBtnDisplay.setOnClickListener(mOnControlClickListener);
        mBtnVolume.setOnClickListener(mOnControlClickListener);
    }

    private View.OnLayoutChangeListener MenuLayoutChangeListener = new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (mMenuSlideLock) {
                mHandler.sendEmptyMessageDelayed(MSG_HANDLER_MENU_UNLOCK_SLIDE, 100);
            }
        }
    };


    private void showControlCenter() {
        isShowing = true;
        mControlSlideLock = true;
        hideControlCenter();

        if (null == mWindowManager) {
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        }

        if (null == mControlCenterView) {
            initControlCenterView();
        }
        dismissDialog();
        if (mDisplayWidth < 0 || mDisplayHeight < 0) {
            Display defaultDisplay = mWindowManager.getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getSize(point);
            mDisplayWidth = point.x;
            mDisplayHeight = point.y;
        }

        // 设置LayoutParam
        if (null == mControlLayoutParams) {
            mControlLayoutParams = new WindowManager.LayoutParams();
        }
        mControlLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

        mControlLayoutParams.format = PixelFormat.TRANSPARENT;
        mControlLayoutParams.flags = WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        mControlLayoutParams.width = mDisplayWidth;
        mControlLayoutParams.height = CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_TOP_BOTTOM;
        //mLayoutParams.height = mDisplayHeight;

        //显示的位置
        mControlLayoutParams.gravity = Gravity.CENTER;

        mControlLayoutParams.x = 0;
        mControlLayoutParams.y = -mDisplayHeight / 2;
        mControlHandle.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        //mControlHandle.setVisibility(View.GONE);
        mWindowManager.addView(mControlCenterView, mControlLayoutParams);
        mControlCenterView.addOnLayoutChangeListener(mControlLayoutChangeListener);
    }

    private View.OnLayoutChangeListener mControlLayoutChangeListener = new View.OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (mControlSlideLock) {
                mHandler.sendEmptyMessageDelayed(MSG_HANDLER_CONTROL_UNLOCK_SLIDE, 100);
            }
        }
    };

    private void activeMenuWindow(boolean isActive) {
        if (null == mWindowManager || null == mMenuView || !mMenuView.isAttachedToWindow()) {
            return;
        }

        /*if ((isActive && mMenuLayoutParams.height == mDisplayWidth) || (!isActive && mMenuLayoutParams.height == CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_TOP_BOTTOM)) {
            return;
        }*/

        if (isActive) {
            mMenuWindowLayoutParams.height = mDisplayHeight;
        } else {
            mMenuWindowLayoutParams.height = CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_TOP_BOTTOM;
        }
        mWindowManager.updateViewLayout(mMenuView, mMenuWindowLayoutParams);

        mMenuView.requestLayout();
        mMenuView.invalidate();
        mMenuVerticalSlideLayout.requestLayout();
        mMenuView.invalidate();
    }

    private void activeControlWindow(boolean isActive) {
        if (null == mWindowManager || null == mControlCenterView || !mControlCenterView.isAttachedToWindow()) {
            return;
        }

        if (isActive) {
            mControlLayoutParams.height = mDisplayHeight;
        } else {

            mControlLayoutParams.height = CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_TOP_BOTTOM;
        }

        mWindowManager.updateViewLayout(mControlCenterView, mControlLayoutParams);
        mControlCenterDrawer.updateContentHeight(mDisplayHeight - CommonDefined.GlobalMenuEdgeSize.EDGE_SIZE_TOP_BOTTOM, true);
        mControlCenterView.requestLayout();
        mControlCenterView.invalidate();
    }

    private View.OnClickListener mOnControlClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (R.id.btn_dnd == id) {
                if (!mControlCenterViewModel.getDndSwitchStatus()) {
                    showDndOnTipDialog();
                } else {
                    mControlCenterViewModel.setDndSwitchStatus(false);
                }
            } else if (R.id.btn_setting == id) {
                if (!mControlCenterViewModel.getScreenLockedStatus()
                        && !mControlCenterViewModel.getSkipSettingLockCheck()
                        && mControlCenterViewModel.getSettingScreenLockedStatus()) {
                    PanelJumpTool.startIdle(GlobalMenuService.this, MenuDefined.MenuType.MENU_TYPE_SETTING);
                } else {
                    PanelJumpTool.startSetting(GlobalMenuService.this);
                }
                closeControlCenterDrawer();
            } else if (R.id.btn_screen_off == id) {
                mControlCenterViewModel.goToSleep();
                //Explain: Close Control center drawer in InitModel
            } else if (R.id.btn_display == id) {
                showBrightnessWindow();
            } else if (R.id.btn_volume == id) {
                showVolumeWindow();
            } else if (R.id.btn_home == id) {
                PanelJumpTool.startIdle(GlobalMenuService.this);
                closeControlCenterDrawer();
            } else if (R.id.btn_notification == id) {
                // 跳转到notification, 有未读消息则进入通知界面,没有未读消息则进入安防日志界面
                // 后续如果安防日志消息也有已读/未读的概念,需对红点做区分
                Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
                intent.putExtra(ExtraKey.EXTRA_KEY_MSG_TYPE, v.isSelected() ? ExtraKey.EXTRA_KEY_NEW_NOTIFICATION_DATA : ExtraKey.EXTRA_KEY_NEW_ALARM_DATA);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                closeControlCenterDrawer();
            }
        }
    };

    /**
     * 跳转到wifi页面
     */
    private void startWifiSetting() {
        PanelJumpTool.startSettingWifi(GlobalMenuService.this);
        closeControlCenterDrawer();
    }

    /**
     * 收起控制中心
     */
    private void closeControlCenterDrawer() {
        if (null != mControlCenterDrawer && mControlCenterDrawer.isOpened()) {
            mControlCenterDrawer.close();
        }
    }

    private void initDndAlertView() {
        if (null == mDndAlertView) {
            mDndAlertView = LayoutInflater.from(this).inflate(R.layout.view_dnd_alert, null);
        }

        FrameLayout flAlertFrame = mDndAlertView.findViewById(R.id.fl_alert_frame);
        AppCompatButton btnCancel = mDndAlertView.findViewById(R.id.btn_cancel);
        AppCompatButton btnConfirm = mDndAlertView.findViewById(R.id.btn_confirm);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideDndAlert();
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mControlCenterViewModel.setDndSwitchStatus(true);
                hideDndAlert();
            }
        });
        final Rect frame = new Rect();
        mDndAlertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                btnCancel.getHitRect(frame);
                if (frame.contains((int) x, (int) y)) {
                    return false;
                }
                btnConfirm.getHitRect(frame);
                if (frame.contains((int) x, (int) y)) {
                    return false;
                }

                flAlertFrame.getHitRect(frame);
                if (frame.contains((int) x, (int) y)) {
                    return false;
                }

                return true;
            }
        });
    }

    /**
     * dnd弹窗提示
     */
    private void showDndOnTipDialog() {
        Log.d("Show Dnd on tip dialog");
        if (null == mDndAlertView) {
            initDndAlertView();
        }

        if (null == mWindowManager) {
            mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        }

        hideDndAlert();

        // 设置LayoutParam
        if (null == mDndAlertLayoutParams) {
            mDndAlertLayoutParams = new WindowManager.LayoutParams();
        }
        mDndAlertLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

        mDndAlertLayoutParams.format = PixelFormat.TRANSPARENT;
        mDndAlertLayoutParams.flags = WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        mDndAlertLayoutParams.width = mDisplayWidth;
        mDndAlertLayoutParams.height = mDisplayWidth;

        //显示的位置
        mDndAlertLayoutParams.gravity = Gravity.CENTER;

        mDndAlertLayoutParams.x = 0;
        mDndAlertLayoutParams.y = 0;
        mDndAlertLayoutParams.windowAnimations = android.R.style.Animation_Dialog;
        mWindowManager.addView(mDndAlertView, mDndAlertLayoutParams);
        if (null != mControlCenterDrawer) {
            mControlCenterDrawer.lock();
        }
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        release();
        //restart
        PanelJumpTool.startMenuService(this);
        Log.d("*******onTaskRemoved*AAA**** ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mControlCenterViewModel.removeBrightnessChangedListener(mBrightnessChangedListener);
        release();
        PanelJumpTool.startMenuService(this);
        Log.d("*******onDestroy***** ");
    }

    private void release() {
        hideMenu();
        hideControlCenter();
        removeGlobalBackSwipeView();
        if (null != mControlCenterViewModel) {
            mControlCenterViewModel.unlockBack();
        }
        mWindowManager = null;
        isShowing = false;
    }

    private void hideMenu() {
        if (null != mMenuView && null != mWindowManager) {
            try {
                mWindowManager.removeViewImmediate(mMenuView);
            } catch (Exception e) {
                Log.e("Error: removeView error");
            }
        }
    }

    private void hideControlCenter() {
        if (null != mControlCenterView && null != mWindowManager) {
            try {
                mWindowManager.removeViewImmediate(mControlCenterView);
            } catch (Exception e) {
                Log.e("Error: removeView error");
            }
        }
        hideDndAlert();
        hideErrorCodeDialog();
        mControlCenterViewModel.stopSystemVolumeTone();

        mDndAlertView = null;
    }

    private void hideErrorCodeDialog() {
        if (mErrorCodeDialog != null && mErrorCodeDialog.isShowing()) {
            mErrorCodeDialog.dismiss();
        }
    }

    private void hideDndAlert() {
        if (null != mDndAlertView && null != mWindowManager) {
            try {
                mWindowManager.removeViewImmediate(mDndAlertView);
            } catch (Exception e) {
                Log.e("Error: removeView error");
            }
        }
        if (null != mControlCenterDrawer) {
            mControlCenterDrawer.unlock();
        }
    }


    class MenuHandler extends Handler {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_HANDLER_HIDE_MENU:
//                    showMenu();
                    showControlCenter();
                    mControlCenterViewModel.unlockBack();
                    if (null != mTempToMenuItem) {
                        PanelJumpTool.startActivity(GlobalMenuService.this, mTempToMenuItem.getComponentType());
                    }
                    mTempToMenuItem = null;
                    break;
                case MSG_HANDLER_MENU_UNLOCK_SLIDE:
                    mMenuSlideLock = false;
                    break;
                case MSG_HANDLER_CONTROL_UNLOCK_SLIDE:
                    mControlSlideLock = false;
                    break;
                default:
                    break;
            }
        }
    }

    private String getTopActivityPkgName() {
        String topPkgName = "";
        //get top activity package name
        ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName comp = am.getRunningTasks(1).get(0).topActivity;
        topPkgName = comp.getPackageName();
        return topPkgName;
    }

    private void showVolumeWindow() {
        if (mVolumeWindow == null) {
            mVolumeWindow = new VolumeWindow(this, R.style.FullScreenWindow);
            mVolumeWindow.setOnProgressChanged(new VolumeWindow.OnProgressChanged() {
                @Override
                public void onProgressChange(int progress) {
                    if (mVolumeWindow != null) {
                        Log.i("volumewindow isShowing = " + mVolumeWindow.isShowing());
                        mControlCenterViewModel.showRgbValue(true, progress);
                    }
                    mControlCenterViewModel.setSystemVolume(progress);
                    mControlCenterViewModel.playSystemVolumeTone();
                }
            });

            mVolumeWindow.setOnDismissListener(dialog -> mControlCenterViewModel.showRgbValue(false, 0));
        }

        if (mVolumeWindow.isShowing()) {
            return;
        }

        mVolumeWindow.setProgress(mControlCenterViewModel.getSystemVolume());
        mControlCenterViewModel.showRgbValue(true, mControlCenterViewModel.getSystemVolume());
        mVolumeWindow.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
        mVolumeWindow.show();
    }

    private void showBrightnessWindow() {
        if (mBrightnessWindow == null) {
            mBrightnessWindow = new BrightnessWindow(this, R.style.FullScreenWindow);
            mBrightnessWindow.setOnProgressChanged(new BrightnessWindow.OnProgressChanged() {
                @Override
                public void onProgressChange(int progress) {
                    if (mBrightnessWindow != null) {
                        Log.i("brightnesswindow isShowing = " + mBrightnessWindow.isShowing());
                        mControlCenterViewModel.showRgbValue(true, progress);
                    }
                    mControlCenterViewModel.setBrightness(progress);
                    mControlCenterViewModel.setAutoBrightnessState(false);
                }
            });

            mBrightnessWindow.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    mControlCenterViewModel.showRgbValue(false, 0);
                    mControlCenterViewModel.notifyBrightnessOtherListeners(mBrightnessChangedListener, mControlCenterViewModel.getBrightness());
                }
            });
        }
        if (mBrightnessWindow.isShowing()) {
            return;
        }

        mBrightnessWindow.setProgress(mControlCenterViewModel.getBrightness());
        mControlCenterViewModel.showRgbValue(true, mControlCenterViewModel.getBrightness());
        mBrightnessWindow.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
        mBrightnessWindow.show();
    }

    private void dismissDialog() {
        if (mVolumeWindow != null && mVolumeWindow.isShowing()) {
            mVolumeWindow.dismiss();
        }
        if (mBrightnessWindow != null && mBrightnessWindow.isShowing()) {
            mBrightnessWindow.dismiss();
        }
    }

    private final BrightnessChangedListener mBrightnessChangedListener = uiValue -> handleBrightness(uiValue);

    private void handleBrightness(int brightness) {
        if (null != mBrightnessWindow) {
            mBrightnessWindow.setProgress(brightness);
        }
    }
}

New Annotation

Summary:
Author:
Mode:
Body: