Android14系统开机添加自定义广播

更改路径:

路径一:

 frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java

CentralSurfacesImpl 是 Android SystemUI 的核心类,负责管理状态栏(Status Bar)、锁屏(Keyguard) 和通知面板(Notification Shade) 的协同工作。它是系统状态栏功能的中央控制器,处理用户交互、状态切换、动画协调以及与底层系统服务的通信。

新增文件一:

frameworks/base/packages/SystemUI/src/com/sky/systemui/K2OtherBroadcastReceiver.java

新增文件二:

frameworks/base/packages/SystemUI/src/com/sky/systemui/SkyApiManager.java

路径二:

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java(AMS)

修改一:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java

diff --git a/u_sys/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java b/u_sys/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
index 9edb414ff93..f0d02f4e747 100644
--- a/u_sys/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
+++ b/u_sys/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
@@ -274,6 +274,8 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Provider;
 
+import com.sky.systemui.SkyApiManager;
+
 /**
  * A class handling initialization and coordination between some of the key central surfaces in
  * System UI: The notification shade, the keyguard (lockscreen), and the status bar.
@@ -324,6 +326,7 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
     private float mTransitionToFullShadeProgress = 0f;
     private NotificationListContainer mNotifListContainer;
     private boolean mIsShortcutListSearchEnabled;
+    private SkyApiManager mSkyApiManager;
 
     private final KeyguardStateController.Callback mKeyguardStateControllerCallback =
             new KeyguardStateController.Callback() {
@@ -1280,6 +1283,11 @@ public class CentralSurfacesImpl implements CoreStartable, CentralSurfaces {
         updateResources();
         updateTheme();
 
+	// add by xll
+	mSkyApiManager = new SkyApiManager(mContext);
+	mSkyApiManager.init();
+	// add end
+
         inflateStatusBarWindow();
         mNotificationShadeWindowView.setOnTouchListener(getStatusBarWindowTouchListener());
         mWallpaperController.setRootView(mNotificationShadeWindowView);


makeStatusBarView`是SystemUI初始化视图(状态栏、通知面板等)的关键方法。在这个方法中初始化`SkyApiManager`并间接注册广播,是为了确保在SystemUI界面准备好后,再开始处理与这些界面相关的广播(如护眼弹窗需要显示在SystemUI的窗口上)。

修改二:新增SkyApiManager.java文件

package com.sky.systemui;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.app.AlertDialog;
import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.KeyguardManager;
import android.os.Handler;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;

import java.util.HashMap;
import java.util.List;

import android.util.Log;
import android.provider.Settings;
import android.os.Message;
import android.os.IPowerManager;
import android.os.ServiceManager;
import android.os.RemoteException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.*;

import com.android.systemui.R;

public class SkyApiManager {
    private Context mContext;
    public static final String TAG = "SkyApiManager";
    private final boolean DEBUG = true;
    private SkyApiInstallBroadcastReceiver mInstallReceiver;
    private SkyApiUninstallBroadcastReceiver mUninstallReceiver;
    private K2OtherBroadcastReceiver mK2OtherReceiver;

    public SkyApiManager(Context context) {
        mContext = context;
    }

    public void init() {
        initSettingsData();
        initInstallReceiver();//定义广播接收器,用于处理静默安装APK的请求
        initUninstallReceiver();//卸载APK
        initK2OtherReceiver();//重点
        mK2OtherReceiver.initSensor(mContext);
        NetworkStatusCallback listener = new NetworkStatusCallback(mContext);
        ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connMgr != null) {
            NetworkRequest nr = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                    .build();
            connMgr.registerNetworkCallback(nr, listener);
        }
    }

    private void initSettingsData() {
    }

    private void initInstallReceiver() {
        IntentFilter installIntentFilter = new IntentFilter();
        installIntentFilter.addAction(SkyApiInstallBroadcastReceiver.ACTION_SILENCE_INSTALL);
        mInstallReceiver = new SkyApiInstallBroadcastReceiver();
        mContext.registerReceiver(mInstallReceiver, installIntentFilter);
    }

    private void initUninstallReceiver() {
        IntentFilter uninstallIntentFilter = new IntentFilter();
        uninstallIntentFilter.addAction(SkyApiUninstallBroadcastReceiver.ACTION_SILENCE_UNINSTALL);
        mUninstallReceiver = new SkyApiUninstallBroadcastReceiver();
        mContext.registerReceiver(mUninstallReceiver, uninstallIntentFilter);
    }

//注册了多个广播Action,包括系统事件(开机完成、屏幕关闭)和自定义事件(护眼模式、功能键等)。 
    private void initK2OtherReceiver() {
        IntentFilter K2IntentFilter = new IntentFilter();
        K2IntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        K2IntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_EYE_PROTECT);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_SYS_F17);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_SYS_F18);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_SYS_INFO);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_SET_AUDIO_MODE);
        K2IntentFilter.addAction(K2OtherBroadcastReceiver.ACTION_GET_AUDIO_MODE);
        mK2OtherReceiver = new K2OtherBroadcastReceiver();
        mContext.registerReceiver(mK2OtherReceiver, K2IntentFilter);
    }

    // Read node
    private static String getString(String path) {
        String prop = "0";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            prop = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }

    // Write node
    private static boolean setString(String path, String value) {
        try {
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(path));
            Log.d(TAG, "set value:" + value);
            bufWriter.write(value);
            bufWriter.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "can't write the " + path);
            return false;
        }
    }

    public void unInit() {
        if (mInstallReceiver != null) {
            mContext.unregisterReceiver(mInstallReceiver);
        }
        if (mUninstallReceiver != null) {
            mContext.unregisterReceiver(mUninstallReceiver);
        }
        if (mK2OtherReceiver != null) {
            mContext.unregisterReceiver(mK2OtherReceiver);
        }
    }

    private void logd(String str) {
        if (DEBUG) {
            Log.d(TAG, str);
        }
    }
}

修改三:新增文件K2OtherBroadcastReceiver.java

package com.sky.systemui;

import java.util.concurrent.SynchronousQueue;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IIntentSender;
import android.content.IIntentReceiver;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.content.ComponentName;
import android.net.wifi.WifiManager;
import android.bluetooth.BluetoothAdapter;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.SubscriptionInfo;
import android.os.Build;
import android.os.Handler;
import android.os.SystemProperties;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.provider.Settings;
import android.view.Window;
import android.view.Gravity;
import android.view.WindowManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.os.PowerManager;
import android.os.SystemClock;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import com.android.sky.ITofSensorService;
import com.android.sky.ITofSensorListener;

import com.android.systemui.R;


/**
 * Created by Luanne on 2024/11/13.
 */
public class K2OtherBroadcastReceiver extends BroadcastReceiver {

    private final String TAG = SkyApiManager.TAG;
    private final boolean DEBUG = true;

    public final static String ACTION_EYE_PROTECT = "android.intent.action.eyeprotect";
    public final static String ACTION_SYS_F17 = "com.intent.action.F17";
    public final static String ACTION_SYS_F18 = "com.intent.action.F18";
    public final static String ACTION_SYS_INFO = "com.intent.action.info";
    public final static String ACTION_SET_AUDIO_MODE = "android.intent.action.SET_AUDIO_MODE";
    public final static String ACTION_GET_AUDIO_MODE = "android.intent.action.GET_AUDIO_MODE";
    private static final String TOF_OFFSET_RESULT = "/sys/bus/i2c/drivers/dao-tof/2-0041/app0/offset_sync";
    private static final String TOF_XTALK_RESULT = "/sys/bus/i2c/drivers/dao-tof/2-0041/app0/xtalk_sync";
    private static Context mContext;
    private ITofSensorService tofSensorService;
    private ITofSensorListener.Stub tofSensorListener;
    // Trigger proximity if distance is less than 5 cm.
    private static final float SHOW_PROXIMITY_THRESHOLD = 20.0f;
    private static final float DISMISS_PROXIMITY_THRESHOLD = 25.0f;
    private static final float KIDDING_PROXIMITY_THRESHOLD = 2.0f;
    private static final int EYEPROTECT_TIME = 12000;
    private static final int HALLSENSOR_TIME = 10000;

    private static long eyeProtectLastTime = 0;
    private static long hallSensorLastTime = 0;
    private static boolean isEyeProtectCounting = false;
    private static boolean isHallSensorCounting = false;
    private static boolean isEyeProtectDialogShowing = false;
    private static boolean isHallSensorTriggered = false;

    private static boolean mProximitySensorEnabled;
    private Handler mhandle = new Handler();
    CommonDialog mDisDialog;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        logd("onReceive action: " + action);
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            // 处理开机完成
            upateSystemInfo();
            SystemProperties.set("persist.speech.support", "false");
            int audio_mode = Settings.System.getInt(mContext.getContentResolver(),
                    "aispeech.force", 0);
            if (audio_mode == 0) {
                SystemProperties.set("vendor.audio.ais.algo_force_bypass_enable", "1");
            } else {
                SystemProperties.set("vendor.audio.ais.algo_force_bypass_enable", "0");
            }
            String defLauncher = SystemProperties.get("persist.system.default.launcher3");
            Log.d("Luanne", "defLauncher: " + defLauncher);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (defLauncher != null && defLauncher.equals("1")) {
                            enableApplication(context, "com.android.launcher3");
                            Thread.sleep(1000);
                            disableApplication(context, "com.hssenglish.redbooklauncher");
                        } else {
                            enableApplication(context, "com.hssenglish.redbooklauncher");
                            Thread.sleep(1000);
                            disableApplication(context, "com.android.launcher3");
                        }
                    } catch (Exception e) {
                    }
                }
            }).start();
        } else if (action.equals(ACTION_EYE_PROTECT)) {
            // 护眼模式开关
            boolean isChecked = Settings.System.getString(mContext.getContentResolver(),
                    "ep_protect").equals("true");
            setProximitySensorEnabled(isChecked);
        } else if (action.equals(ACTION_SYS_F17)) {
            // 切换夜间模式
            boolean isNightActive = Settings.Secure.getInt(mContext.getContentResolver(),
                    "night_display_activated", 0) == 1;
            Settings.Secure.putInt(mContext.getContentResolver(),
                    "night_display_activated", isNightActive ? 0 : 1);
        } else if (action.equals(ACTION_SYS_F18)) {
            // 切换飞行模式
            boolean isAirModeOn = Settings.Global.getInt(mContext.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
            Settings.Global.putInt(mContext.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, isAirModeOn ? 0 : 1);
            Intent air_intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            air_intent.putExtra("state", isAirModeOn ? false : true);
            mContext.sendBroadcast(air_intent);//发送飞行模式变更广播
        } else if (action.equals(ACTION_SYS_INFO)) {
            // 更新系统信息
            upateSystemInfo();
        } else if (action.equals(ACTION_SET_AUDIO_MODE)) {
            String mode = intent.getStringExtra("audiomode");
            Log.d("Luanne", "systemui ACTION_SET_AUDIO_MODE audiomode:" + mode);
            /*
            if (mode != null) {
                if (mode.equals("3")) {
                    //Noise Reduction Mode
                    mode = "true";
                    Settings.System.putInt(mContext.getContentResolver(), "aispeech.force", 1);
                    SystemProperties.set("vendor.audio.ais.algo_force_bypass_enable", "0");
                    if (!ifFileExist("/data/aispeech/OfflineProfile.profile")) {
                        Intent authintent = new Intent("android.sky.aispeech.SET_AUDIO_MODE");
                        authintent.putExtra("audiomode", mode);
                        authintent.setComponent(new ComponentName("com.aispeech.duihalauth",
                                "com.aispeech.duihalauth.MyBroadcastReceiver"));
                        mContext.sendBroadcast(authintent);
                    }
                } else if (mode.equals("1")) {
                    //Quiet mode
                    mode = "false";
                    Settings.System.putInt(mContext.getContentResolver(), "aispeech.force", 0);
                    SystemProperties.set("vendor.audio.ais.algo_force_bypass_enable", "1");
                } else {
                    return;
                }
            }
            */
        } else if (action.equals(ACTION_GET_AUDIO_MODE)) {
            Log.d("Luanne", "systemui ACTION_GET_AUDIO_MODE");
            boolean mForce = Settings.System.getInt(context.getContentResolver(),
                    "aispeech.force", 1) == 1;
            String audio = "";
            /*
            if (mForce) {
                audio = "3";
            } else {
                audio = "1";
            }
            if (audio != null && !audio.equals("")) {
                Intent audtioModeintent = new Intent();
                audtioModeintent.setAction("android.intent.aciont.AUDIOMODE");
                audtioModeintent.setPackage("com.hssenglish.redbooklauncher");
                audtioModeintent.putExtra("audio", audio);
                mContext.sendBroadcast(audtioModeintent);
            }
            */
        } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            resetEyeProtect();
            resetHallSensor();
        }
    }

    //调用`upateSystemInfo()`更新系统信息(序列号、MAC地址、IMEI等)
    private void upateSystemInfo() {
        //String props = SystemProperties.get("ro.serialno");
        // 获取并设置设备序列号
        String props = Build.getSerial().toString();
        logd("upateSystemInfo getSerial: " + props);
        SystemProperties.set("persist.sys.hrSerial", props);
        boolean isAirplaneMode = Settings.System.getInt(mContext.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
        logd("isAirplaneMode:" + isAirplaneMode);
        // 若非飞行模式,则获取并设置WiFi MAC地址、蓝牙地址、IMEI等。
        if (!isAirplaneMode) {
            // 获取WiFi MAC地址
            WifiManager mWifiManager = mContext.getSystemService(WifiManager.class);
            final String[] macAddresses = mWifiManager.getFactoryMacAddresses();
            String macAddress = null;
            if (macAddresses != null && macAddresses.length > 0) {
                macAddress = macAddresses[0];
                SystemProperties.set("persist.sys.MacAddress", macAddress);
            }
            //获取蓝牙地址
            BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
            if (bluetooth != null) {
                String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
                if (address != null) {
                    SystemProperties.set("persist.sys.BtAddress", address.toLowerCase());
                }
            }
            // 获取双卡IMEI
            TelephonyManager mTelephonyManager = (TelephonyManager)
                    mContext.getSystemService(Context.TELEPHONY_SERVICE);
            if (mTelephonyManager != null) {
                final int phoneType = getPhoneType(0, mTelephonyManager);
                String imei1 = phoneType == mTelephonyManager.PHONE_TYPE_CDMA ?
                        mTelephonyManager.getMeid(0) :
                        mTelephonyManager.getImei(0);
                if (imei1 != null) {
                    SystemProperties.set("persist.sys.IMEI01", imei1);
                }

                final int phoneType2 = getPhoneType(1, mTelephonyManager);
                String imei2 = phoneType2 == mTelephonyManager.PHONE_TYPE_CDMA ?
                        mTelephonyManager.getMeid(1) :
                        mTelephonyManager.getImei(1);
                if (imei2 != null) {
                    SystemProperties.set("persist.sys.IMEI02", imei2);
                }
            }
        }
        // 设置TOF传感器校准数据
        String tofOffset = SystemProperties.get("ro.boot.tof_offset");
        String tofPosition = SystemProperties.get("ro.boot.tof_position");
        String tofThreshold = SystemProperties.get("ro.boot.tof_threshold");
        Log.d("Luanne", "upateSystemInfo tofOffset: " + tofOffset + ", tofPosition: " + tofPosition + ", tofThreshold: " + tofThreshold);
        if(!TextUtils.isEmpty(tofOffset)) {
            setString(TOF_OFFSET_RESULT, tofOffset);
        }
        if(!TextUtils.isEmpty(tofPosition) && !TextUtils.isEmpty(tofThreshold)) {
            setString(TOF_XTALK_RESULT, tofPosition + "," + tofThreshold);
        }
    }

    private boolean ifFileExist(String filePath) {
        File file = new File(filePath);
        return (file.exists());
    }

    //禁用应用
    private void disableApplication(Context context, String packageName) {
        PackageManager mPackageManager = context.getPackageManager();
        mPackageManager.setApplicationEnabledSetting(
                packageName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                0);
    }

    //启用应用
    private void enableApplication(Context context, String packageName) {
        PackageManager mPackageManager = context.getPackageManager();
        mPackageManager.setApplicationEnabledSetting(
                packageName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                0);
    }

    private int getPhoneType(int slotIndex, TelephonyManager mTelephonyManager) {
        SubscriptionInfo subInfo = SubscriptionManager.from(mContext)
                .getActiveSubscriptionInfoForSimSlotIndex(slotIndex);
        return mTelephonyManager.getCurrentPhoneType(subInfo != null ? subInfo.getSubscriptionId()
                : SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
    }

    public void initSensor(Context context) {
        logd("initSensor!!!");
        mContext = context;
        tofSensorService = ITofSensorService.Stub.asInterface(ServiceManager.getService("ext_tofservice"));
        tofSensorListener = new ITofSensorListener.Stub() {
            @Override
            public void onTofDataChanged(float value) throws RemoteException {
                // 护眼弹窗逻辑(受 mProximitySensorEnabled 控制)
                if (mProximitySensorEnabled) {
                    if (value > KIDDING_PROXIMITY_THRESHOLD && value < SHOW_PROXIMITY_THRESHOLD) {
                        if (!isEyeProtectCounting) {
                            eyeProtectLastTime = System.currentTimeMillis();
                            isEyeProtectCounting = true;
                            mhandle.postDelayed(eyeProtectRunnable, 1000);
                        }
                    } else if (value >= DISMISS_PROXIMITY_THRESHOLD || value <= 0) {
                        // 远离或极近都要消除弹窗
                        resetEyeProtect();
                    }
                } else {
                    resetEyeProtect();
                }

                // hallsensor休眠逻辑(不受 mProximitySensorEnabled 控制,始终有效)
                if (value > 0 && value <= KIDDING_PROXIMITY_THRESHOLD) {
                    if (!isHallSensorCounting && !isHallSensorTriggered) {
                        hallSensorLastTime = System.currentTimeMillis();
                        isHallSensorCounting = true;
                        mhandle.postDelayed(hallSensorRunnable, 1000);
                    }
                } else {
                    resetHallSensor();
                }
            }
        };
        String isModeOn = Settings.System.getString(mContext.getContentResolver(), "ep_protect");
        if (isModeOn == null || isModeOn.equals("true")) {
            Settings.System.putString(mContext.getContentResolver(), "ep_protect", "true");
            mProximitySensorEnabled = true;
        } else {
            mProximitySensorEnabled = false;
        }
        try {
            tofSensorService.registerListener(tofSensorListener);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to register listener", e);
        }
    }

    public void setProximitySensorEnabled(boolean enable) {
        logd("setProximitySensorEnabled = " + enable);
        mProximitySensorEnabled = enable;
        if (!enable) {
            resetEyeProtect();
        }
    }

    private Runnable eyeProtectRunnable = new Runnable() {
        @Override
        public void run() {
            if (isEyeProtectCounting && mProximitySensorEnabled) {
                if ((System.currentTimeMillis() - eyeProtectLastTime) >= (EYEPROTECT_TIME - 1000)) {
                    if (!isEyeProtectDialogShowing) {
                        showEyeProtectDialog();
                    }
                    // 弹窗弹出后,停止计时
                    isEyeProtectCounting = false;
                } else {
                    mhandle.postDelayed(this, 1000);
                }
            }
        }
    };

    private void showEyeProtectDialog() {
        if (mProximitySensorEnabled) {
            mDisDialog = new CommonDialog(mContext);
            mDisDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            mDisDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            Window dialogWindow = mDisDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            lp.x = 200;
            lp.y = 0;
            dialogWindow.setAttributes(lp);
            mDisDialog.setMessage(R.string.msg_protect_eye).show();
            isEyeProtectDialogShowing = true;
        }
    }

    private void resetEyeProtect() {
        isEyeProtectCounting = false;
        eyeProtectLastTime = 0;
        if (isEyeProtectDialogShowing && mDisDialog != null && mDisDialog.isShowing()) {
            mDisDialog.dismiss();
            isEyeProtectDialogShowing = false;
        }
    }

    private Runnable hallSensorRunnable = new Runnable() {
        @Override
        public void run() {
            if (isHallSensorCounting && !isHallSensorTriggered) {
                if ((System.currentTimeMillis() - hallSensorLastTime) >= (HALLSENSOR_TIME - 1000)) {
                    triggerSleep();
                } else {
                    mhandle.postDelayed(this, 1000);
                }
            }
        }
    };

    private void triggerSleep() {
        isHallSensorTriggered = true;
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        if (pm != null) {
            pm.goToSleep(SystemClock.uptimeMillis());
        }
        // 休眠后立即复位,防止重复触发
        resetHallSensor();
    }

    private void resetHallSensor() {
        isHallSensorCounting = false;
        hallSensorLastTime = 0;
        isHallSensorTriggered = false;
    }

    private static boolean setString(String path, String value) {
        try {
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(path));
            Log.d("Luanne", "set value:" + value);
            bufWriter.write(value);
            bufWriter.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("Luanne", "can't write the " + path);
            return false;
        }
    }

    private void logd(String str) {
        if (DEBUG) {
            Log.d(TAG, str);
        }
    }
}

总结一下:

在 SystemUI 的核心初始化阶段,CentralSurfacesImpl 的 makeStatusBarView() 方法作为视图创建的关键入口,负责初始化 SkyApiManager 这一系统功能管理中心。该管理器通过其 init() 方法统一协调多个关键模块的初始化工作。
其中,initK2OtherReceiver() 作为核心初始化步骤之一,精心构建了 K2OtherBroadcastReceiver 广播接收器。

当系统广播事件触发时,K2OtherBroadcastReceiver 的 onReceive() 方法作为统一入口,通过高效的事件路由机制分发到具体业务模块。这种设计不仅实现了功能需求,更体现了良好的系统架构设计原则,包括单一职责、开闭原则和接口隔离,为后续功能扩展和维护奠定了坚实基础。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值