Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android系統移植與調試之-------)如何修改Android系統默認顯示

Android系統移植與調試之-------)如何修改Android系統默認顯示

編輯:關於Android編程

 

1、首先解決【設置】界面默認顯示【開發者選項】的問題

查看源代碼:packages/apps/Settings/src/com/android/settings/SettingsActivity.java

在updateTilesList(List 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的話,還是會彈出窗口要我設置【未知來源】選項,如下所示,因此該方案被放棄。

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022313575755.png

點擊【設置】按鈕進入到界面是,【未知來源】按鈕默認勾選了,這個時候退出去,繼續安裝apk的話就可以安裝了,如下所示:

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022313575725.png

 



因為這個添加的代碼是在SecuritySettings的createPreferenceHierarchy()方法中,也就是說要進入到該Activity才生效。這樣明顯沒達到目的。

因此要和設置【USB調試】一樣,在一個接收開機廣播的廣播接收者中將Settings.Global.INSTALL_NON_MARKET_APPS設置為1。

 

4、找Settings裡面定義的一個接收開機廣播的廣播接收者,在AndroidManifest.xml中找到一個,當然你也可以自己定義一個這樣的廣播接收者。

       
        
            
            	             
            	         
            
        


因此在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了。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved