前文
Android 7.1 设置添加一级目录设置项
新增需求
新增一级项, 打开指定Activity, 如下图, 在系统下添加 定时开关机, 点击后, 打开指定app(非设置内部)
步骤
packages/apps/Settings/src/com/android/settings/Settings.java
diff --git a/packages/apps/Settings/src/com/android/settings/Settings.java b/packages/apps/Settings/src/com/android/settings/Settings.java index 9711e4a..1cd29d0 100755 --- a/packages/apps/Settings/src/com/android/settings/Settings.java +++ b/packages/apps/Settings/src/com/android/settings/Settings.java @@ -28,6 +28,7 @@ public class Settings extends SettingsActivity { /* * Settings subclasses for launching independently. */ + public static class TimingBootSettingsActivity extends SettingsActivity { /* empty */ } public static class BluetoothSettingsActivity extends SettingsActivity { /* empty */ } public static class WirelessSettingsActivity extends SettingsActivity { /* empty */ } public static class SimSettingsActivity extends SettingsActivity { /* empty */ }
新增 TimingBootSettingsActivity, 下面AndroidManifest.xml使用
packages/apps/Settings/AndroidManifest.xml
diff --git a/packages/apps/Settings/AndroidManifest.xml b/packages/apps/Settings/AndroidManifest.xml index 6031292..a503166 100755 --- a/packages/apps/Settings/AndroidManifest.xml +++ b/packages/apps/Settings/AndroidManifest.xml @@ -420,6 +420,21 @@ android:value="com.android.settings.ApnSettings" /> </activity> + <!--AnsonCode --> + <activity android:name="Settings$TimingBootSettingsActivity" + android:label="定时开关机" + android:icon="@drawable/ic_settings_bluetooth" + android:taskAffinity="" + > + <intent-filter android:priority="3"> + <action android:name="com.android.settings.action.SETTINGS" /> + </intent-filter> + <meta-data android:name="com.android.settings.category" + android:value="com.android.settings.category.system" /> + <meta-data android:name="com.android.settings.FRAGMENT_CLASS" + android:value="com.android.settings.TimingBootSettings" /> + </activity> + <activity android:name="Settings$BluetoothSettingsActivity" android:label="@string/bluetooth_settings_title" android:icon="@drawable/ic_settings_bluetooth"
packages/apps/Settings/src/com/android/settings/SettingsActivity.java
diff --git a/packages/apps/Settings/src/com/android/settings/SettingsActivity.java b/packages/apps/Settings/src/com/android/settings/SettingsActivity.java index 60f6ca0..3cbd7a7 100755 --- a/packages/apps/Settings/src/com/android/settings/SettingsActivity.java +++ b/packages/apps/Settings/src/com/android/settings/SettingsActivity.java @@ -262,6 +262,7 @@ public class SettingsActivity extends SettingsDrawerActivity Settings.AccessibilitySettingsActivity.class.getName(), Settings.PrintSettingsActivity.class.getName(), Settings.PaymentSettingsActivity.class.getName(), + Settings.TimingBootSettingsActivity.class.getName(), Settings.HdmiSettingsActivity.class.getName() }; @@ -1037,6 +1038,18 @@ public class SettingsActivity extends SettingsDrawerActivity private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate, boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) { if (validate && !isValidFragment(fragmentName)) { + //AnsonCode 由于com.android.settings.TimingBootSettings是虚构出来的, 导致产生Invalid fragment + //在打开应用后, 返回, 并finish(), 让设置保持在初始界面, 否则, 重新回到设置时会显示空白的内容 + if("com.android.settings.TimingBootSettings".equals(fragmentName)){ + Intent tbs = new Intent(); + tbs.setComponent(new ComponentName("com.android.timingboot", "com.android.timingboot.BootTimeSettings")); + tbs.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(tbs); + setResult(RESULT_CANCELED, getResultIntentData()); + finish(); + return null; + } + throw new IllegalArgumentException("Invalid fragment for this activity: " + fragmentName); } @@ -1169,6 +1182,11 @@ public class SettingsActivity extends SettingsDrawerActivity BackupSettingsActivity.class.getName()), hasBackupActivity, isAdmin || Utils.isCarrierDemoUser(this), pm); + //AnsonCode add TimingBoot, 使用设置项 + setTileEnabled(new ComponentName(packageName, + Settings.TimingBootSettingsActivity.class.getName()), + true, isAdmin, pm); + }
注解
注1
frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/SettingsDrawerActivity.java
//调用: TileUtils.getCategories
frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java
public static List<DashboardCategory> getCategories(Context context, HashMap<Pair<String, String>, Tile> cache) { final long startTime = System.currentTimeMillis(); boolean setup = Global.getInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0) != 0; ArrayList<Tile> tiles = new ArrayList<>(); UserManager userManager = UserManager.get(context); for (UserHandle user : userManager.getUserProfiles()) { // TODO: Needs much optimization, too many PM queries going on here. if (user.getIdentifier() == ActivityManager.getCurrentUser()) { // Only add Settings for this user. getTilesForAction(context, user, SETTINGS_ACTION, cache, null, tiles, true); getTilesForAction(context, user, OPERATOR_SETTINGS, cache, OPERATOR_DEFAULT_CATEGORY, tiles, false); getTilesForAction(context, user, MANUFACTURER_SETTINGS, cache, MANUFACTURER_DEFAULT_CATEGORY, tiles, false); } if (setup) { getTilesForAction(context, user, EXTRA_SETTINGS_ACTION, cache, null, tiles, false); } } boolean isSupportBluetooth = "true".equals(SystemProperties.get("ro.rk.bt_enable")); HashMap<String, DashboardCategory> categoryMap = new HashMap<>(); for (Tile tile : tiles) { DashboardCategory category = categoryMap.get(tile.category); if (category == null) { category = createCategory(context, tile.category); if (category == null) { Log.w(LOG_TAG, "Couldn't find category " + tile.category); continue; } categoryMap.put(category.key, category); } Intent tileIntent = tile.intent; if(!isSupportBluetooth && tileIntent != null && tileIntent.toString().contains("com.android.settings/.Settings$BluetoothSettingsActivity")) continue; category.addTile(tile); } ArrayList<DashboardCategory> categories = new ArrayList<>(categoryMap.values()); for (DashboardCategory category : categories) { Collections.sort(category.tiles, TILE_COMPARATOR); } Collections.sort(categories, CATEGORY_COMPARATOR); if (DEBUG_TIMING) Log.d(LOG_TAG, "getCategories took " + (System.currentTimeMillis() - startTime) + " ms"); return categories; }