今天有个用户对【设置】有个特殊的要求,即:
1、开机的时候默认显示【开发者选项】并打开【USB调试】开关 (【Developer options】-->【USB debugging】)
2、开机的时候默认打开【安全】-->【未知来源】的开关 (【Security】--->【Unknown sources】)
1、首先解决【设置】界面默认显示【开发者选项】的问题
查看源代码:packages/apps/Settings/src/com/android/settings/SettingsActivity.java
在updateTilesList(List<DashboardCategory> target)方法中,可以找到【开发者选项】相关的代码:
else if (id == R.id.development_settings) { Log.d(LOG_TAG, "=================oyp K="+showDev); if (!showDev || um.hasUserRestriction( UserManager.DISALLOW_DEBUGGING_FEATURES)) { removeTile = true; } }
其中showDev变量定义在该方法最开始处
final boolean showDev = mDevelopmentPreferences.getBoolean( DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"));
因此要想特定客户直接显示【开发者选项】,则将showDev的值改变即可,代码如下:
//added by ouyang 2015-11-10 set DevelopmentSettings.PREF_SHOW=true if model is J5 if (android.os.SystemProperties.isJ5Version()) { SharedPreferences.Editor editor=mDevelopmentPreferences.edit(); editor.putBoolean(DevelopmentSettings.PREF_SHOW, true); editor.commit(); Log.d(LOG_TAG, "=================oyp set DevelopmentSettings.PREF_SHOW=true if model is J5 "); } //added by ouyang 2015-11-10 end final boolean showDev = mDevelopmentPreferences.getBoolean( DevelopmentSettings.PREF_SHOW, android.os.Build.TYPE.equals("eng"));
即在初始化showDev变量之前,将DevelopmentSettings.PREF_SHOW的值设置为true,那么showDev也会是true。这样【开发者选项】就会默认打开。那个判断是J5客户的方法在/frameworks/base/core/java/android/os/SystemProperties.java中定义
public static boolean isJ5Version(){ return SystemProperties.get("ro.product.model").contains("J5"); }
2、解决【开发者选项】-->【USB调试】开关
首先查看packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java
在updateAllOptions()方法中,有关于【USB调试】的逻辑处理。
/// M: CR ALPS00244115. Lock and unlock screen, the "USB debugging" is unchecked. boolean isChecked = (mAdbDialog != null && mAdbDialog.isShowing()) ? true : (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0); updateSwitchPreference(mEnableAdb, isChecked); /// M: update usb preference again mExt.customUSBPreference(mEnableAdb);
void updateSwitchPreference(SwitchPreference switchPreference, boolean value) { switchPreference.setChecked(value); mHaveDebugSettings |= value; }
其中mEnableAdb即【USB调试】开关控件。一开始我直接强制的把isChecked变量设置为true,如下代码所示
/// M: CR ALPS00244115. Lock and unlock screen, the "USB debugging" is unchecked. boolean isChecked = (mAdbDialog != null && mAdbDialog.isShowing()) ? true : (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0); - //added by ouyang 2015-11-10 set mEnableAdb=true if model is J5 - if (android.os.SystemProperties.isJ5Version()) { - isChecked=true; - } - //added by ouyang 2015-11-10 end updateSwitchPreference(mEnableAdb, isChecked); /// M: update usb preference again mExt.customUSBPreference(mEnableAdb);
但是这样的话,直接刷机后开机的话还是不能直接连接USB调试,还要重新关闭该开关再打开该开关才能进行USB调试,因此该方案被放弃。
因为是否真正打开【USB调试】功能,是看Settings.Global.ADB_ENABLED这个属性的值是0还是1。因此真正的方法是在一个接受开机广播的广播接收者中将Settings.Global.ADB_ENABLED设置为1。
在下面和【未知来源】一起修改,在第4步骤中解决。
3、解决【安全】-->【未知来源】的开关 默认打开的问题
首先查看packages/apps/Settings/src/com/android/settings/SecuritySettings.java文件,在createPreferenceHierarchy()方法中找到【未知来源】的控件,403行附近
mToggleAppInstallation = (SwitchPreference) findPreference( KEY_TOGGLE_INSTALL_APPLICATIONS); mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
其中isNonMarketAppsAllowed()方法代码如下
private boolean isNonMarketAppsAllowed() { return Settings.Global.getInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS, 0) > 0; }
setNonMarketAppsAllowed(boolean enabled)方法代码如下
private void setNonMarketAppsAllowed(boolean enabled) { final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); if (um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)) { return; } // Change the system setting Settings.Global.putInt(getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS, enabled ? 1 : 0); }
一开始我在判断isNonMarketAppsAllowed()方法之前,先调用setNonMarketAppsAllowed()方法,这样一进入这个界面的话,【未知来源】选项是被勾选的。
mToggleAppInstallation = (SwitchPreference) findPreference( KEY_TOGGLE_INSTALL_APPLICATIONS); - - //added by ouyang 2015-11-10 setChecked(true) if the model is J5 - if (android.os.SystemProperties.isJ5Version()) { - setNonMarketAppsAllowed(true); - } mToggleAppInstallation.setChecked(isNonMarketAppsAllowed()); - //added by ouyang 2015-11-10 end
但是,刷完机直接安装一个第三方的apk的话,还是会弹出窗口要我设置【未知来源】选项,如下所示,因此该方案被放弃。
点击【设置】按钮进入到界面是,【未知来源】按钮默认勾选了,这个时候退出去,继续安装apk的话就可以安装了,如下所示:
因为这个添加的代码是在SecuritySettings的createPreferenceHierarchy()方法中,也就是说要进入到该Activity才生效。这样明显没达到目的。
因此要和设置【USB调试】一样,在一个接收开机广播的广播接收者中将Settings.Global.INSTALL_NON_MARKET_APPS设置为1。
4、找Settings里面定义的一个接收开机广播的广播接收者,在AndroidManifest.xml中找到一个,当然你也可以自己定义一个这样的广播接收者。
<!-- Restore Rotation Receiver --> <receiver android:name="com.mediatek.settings.RestoreRotationReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.ACTION_BOOT_IPO" /> </intent-filter> </receiver>
因此在packages/apps/Settings/src/com/mediatek/settings/RestoreRotationReceiver.java的onReceive(Context context, Intent intent)方法中加入以下代码:
//added by ouyang 2015-11-13 if (android.os.SystemProperties.isJ5Version()) { // 开机启动时,默认设置 [未知来源] 勾选 Settings.Global.putInt(context.getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS,1); // 开机启动时,默认设置 [USB调试] 勾选 Settings.Global.putInt(context.getContentResolver(),Settings.Global.ADB_ENABLED, 1); } //added by ouyang 2015-11-13 end
而一开始添加的代码可以注释掉了。通过git diff可以看出来
diff --git a/packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java b/packages/apps/Settings/src/com/android/settings/Devel index 0a987df..503270a 100755 --- a/packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java +++ b/packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java @@ -508,15 +508,9 @@ public class DevelopmentSettings extends SettingsPreferenceFragment final Context context = getActivity(); final ContentResolver cr = context.getContentResolver(); mHaveDebugSettings = false; - /// M: CR ALPS00244115. Lock and unlock screen, the "USB debugging" is unchecked. boolean isChecked = (mAdbDialog != null && mAdbDialog.isShowing()) ? true : (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) != 0); - //added by ouyang 2015-11-10 set mEnableAdb=true if model is J5 - if (android.os.SystemProperties.isJ5Version()) { - isChecked=true; - } - //added by ouyang 2015-11-10 end updateSwitchPreference(mEnableAdb, isChecked); /// M: update usb preference again mExt.customUSBPreference(mEnableAdb);
diff --git a/packages/apps/Settings/src/com/android/settings/SecuritySettings.java b/packages/apps/Settings/src/com/android/settings/Security index 4de173a..392e6ee 100755 --- a/packages/apps/Settings/src/com/android/settings/SecuritySettings.java +++ b/packages/apps/Settings/src/com/android/settings/SecuritySettings.java @@ -402,14 +402,7 @@ public class SecuritySettings extends SettingsPreferenceFragment root.findPreference(KEY_DEVICE_ADMIN_CATEGORY); mToggleAppInstallation = (SwitchPreference) findPreference( KEY_TOGGLE_INSTALL_APPLICATIONS); - - //added by ouyang 2015-11-10 setChecked(true) if the model is J5 - if (android.os.SystemProperties.isJ5Version()) { - setNonMarketAppsAllowed(true); - } mToggleAppInstallation.setChecked(isNonMarketAppsAllowed()); - //added by ouyang 2015-11-10 end - // Side loading of apps. // Disable for restricted profiles. For others, check if policy disallows it. mToggleAppInstallation.setEnabled(!um.getUserInfo(UserHandle.myUserId()).isRestricted());
这样改为后,编译,刷机后,插上USB数据线就可以开始进行adb调试了,而且可以直接安装未知来源的apk了。
====================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
====================================================================================
版权声明:本文为欧阳鹏原创文章,欢迎转载,转载请注明出处! http://blog.csdn.net/ouyang_peng