首页 > 代码库 > Android Setting显示开发者选项的逻辑

Android Setting显示开发者选项的逻辑

默认User版本没有“开发者选项”,我们可以通过点击版本号7次来显示开发者模式,今天来看下代码中是如何处理的:

首先是packages/apps/Settings/src/com/android/settings/Settings.java的初始化处理:

布尔值showDev是否默认显示“开发者选项”:

 final boolean showDev = mDevelopmentPreferences.getBoolean(
                DevelopmentSettings.PREF_SHOW,
                android.os.Build.TYPE.equals("eng"));

如果想让开发者选项默认显示,不需要点击7次版本号,可直接将showDev置为true即可:

final boolean showDev = true; 

如果showDev为false,则在如下代码中隐藏“开发者选项”:

if (id == R.id.development_settings) {
                if (!showDev) {
                    target.remove(i);
                }
            } 


下面看一下点击版本号显示开发者选项的部分:

代码路径:packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java

首先是定义点击次数:

static final int TAPS_TO_BE_A_DEVELOPER = 7;


点击版本号的处理逻辑,若mDevHitCountdown小于0,则表示”开发者选项“已经显示,参考Resume函数的处理:

if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
            // Don't enable developer options for secondary users.
            if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;

            if (mDevHitCountdown > 0) {
                if (mDevHitCountdown == 1) {
                    if (super.ensurePinRestrictedPreference(preference)) {
                        return true;
                    }
                }
                mDevHitCountdown--;
                if (mDevHitCountdown == 0) {
                    getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
                            Context.MODE_PRIVATE).edit().putBoolean(
                                    DevelopmentSettings.PREF_SHOW, true).apply();
                    if (mDevHitToast != null) {
                        mDevHitToast.cancel();
                    }
                    mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
                            Toast.LENGTH_LONG);
                    mDevHitToast.show();
                } else if (mDevHitCountdown > 0
                        && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
                    if (mDevHitToast != null) {
                        mDevHitToast.cancel();
                    }
                    mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
                            R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
                            Toast.LENGTH_SHORT);
                    mDevHitToast.show();
                }
            } else if (mDevHitCountdown < 0) {
                if (mDevHitToast != null) {
                    mDevHitToast.cancel();
                }
                mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
                        Toast.LENGTH_LONG); 
                mDevHitToast.show();
            }
        } 

Resume函数:

@Override
    public void onResume() {
        super.onResume();
        PreferenceGroup parentPreference = getPreferenceScreen();
        mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
                Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW,
                        android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER;
        mDevHitToast = null;
    }



转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui

我的GitHub:周木水的GitHub https://github.com/zhoumushui





Android Setting显示开发者选项的逻辑