Android S Setting添加新项

根据项目要求,需要在setting中添加一项专门管理直播设置的内容,添加步骤如下(参考displaysetting 进行添加):

1. 添加liveSetting到SettingsHomepageActivity

--- a/packages/apps/Settings/res/xml/top_level_settings.xml
+++ b/packages/apps/Settings/res/xml/top_level_settings.xml
@@ -54,14 +54,23 @@
android:title="@string/configure_notification_settings"
android:summary="@string/notification_dashboard_summary"/>

+<!--    <Preference-->
+<!--        android:fragment="com.android.settings.fuelgauge.PowerUsageSummary"-->
+<!--        android:icon="@drawable/ic_settings_battery_white"-->
+<!--        android:key="top_level_battery"-->
+<!--        android:order="-110"-->
+<!--        android:title="@string/power_usage_summary_title"-->
+<!--        android:summary="@string/summary_placeholder"-->
+<!--        settings:controller="com.android.settings.fuelgauge.TopLevelBatteryPreferenceController"/>-->
+
<Preference
-        android:fragment="com.android.settings.fuelgauge.PowerUsageSummary"
-        android:icon="@drawable/ic_settings_battery_white"
-        android:key="top_level_battery"
+        android:fragment="com.android.settings.LiveSettings"
+        android:icon="@drawable/ic_settings_display_white"
+        android:key="top_level_live"
android:order="-110"
-        android:title="@string/power_usage_summary_title"
-        android:summary="@string/summary_placeholder"
-        settings:controller="com.android.settings.fuelgauge.TopLevelBatteryPreferenceController"/>
+        android:title="@string/live_settings"
+        android:summary="@string/live_settings"
+        settings:controller="com.android.settings.klive.TopLevelLivePreferenceController"/>

<Preference
         android:fragment="com.android.settings.deviceinfo.StorageDashboardFragment"


参考displaysetting,将电池相关的setting更改为livesetting,提供livesetting的fragment:com.android.settings.LiveSettings ,controller:com.android.settings.klive.TopLevelLivePreferenceController

2. 参考displaysetting 新建LiveSettings 、TopLevelLivePreferenceController和R.xml.live_settings


+++ b/packages/apps/Settings/src/com/android/settings/LiveSettings.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import android.os.Bundle;
+
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.klive.FakeCameraSwitchPreferenceController;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.search.SearchIndexable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
+public class LiveSettings extends DashboardFragment {
+    private static final String TAG = "LiveSettings";
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.LIVE;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.live_settings;
+    }
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+    }
+
+    @Override
+    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
+        return buildPreferenceControllers(context, getSettingsLifecycle());
+    }
+
+    @Override
+    public int getHelpResource() {
+        return R.string.help_uri_display;
+    }
+
+    private static List<AbstractPreferenceController> buildPreferenceControllers(
+            Context context, Lifecycle lifecycle) {
+        final List<AbstractPreferenceController> controllers = new ArrayList<>();
         //controller中的key"fake_camera" 必须和xml中的key保持一致不然getPreferenceKey()方法会返回空,导致空指针
+        controllers.add(new FakeCameraSwitchPreferenceController(context,"fake_camera"));
+        return controllers;
+    }
+
+    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider(R.xml.live_settings) {
+
+                @Override
+                public List<AbstractPreferenceController> createPreferenceControllers(
+                        Context context) {
+                    return buildPreferenceControllers(context, null);
+                }
+            };
+}

+++ b/packages/apps/Settings/src/com/android/settings/klive/TopLevelLivePreferenceController.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.klive;
+
+import android.content.Context;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+public class TopLevelLivePreferenceController extends BasePreferenceController {
+
+    public TopLevelLivePreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return  AVAILABLE;
+
+    }
+}

+++ b/packages/apps/Settings/res/xml/live_settings.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res-auto"
+    android:key="live_settings_screen"
+    android:title="@string/live_settings"
+    settings:keywords="@string/keywords_live">
+
+    <PreferenceCategory
         //一个组的名称
+        android:title="@string/category_name_fake_camera">
+
+        <SwitchPreference
             //PreferenceKey 很重要 加载布局时需要根据这个来加载
+            android:key="fake_camera"
+            android:title="@string/fake_camera_switch"
             //关键字
+            settings:keywords="@string/category_name_fake_camera"
             //forwork 设置为true代表这个功能一直可用,不会置为灰色
+            settings:forWork="true"
             //该界面具体功能逻辑实现
+            settings:controller="com.android.settings.klive.FakeCameraSwitchPreferenceController"/>
+
+    </PreferenceCategory>
+
+
+
+</PreferenceScreen>


此xml即为livesetting的布局文件,目前只添加了虚拟摄像头开关这一个选项,后续需要增加可以参考添加

3. 虚拟摄像头controller 添加

要实现虚拟摄像头开关 相关逻辑需要添加到controller当中


+++ b/packages/apps/Settings/src/com/android/settings/klive/FakeCameraSwitchPreferenceController.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.settings.klive;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import android.os.Build;
+import android.provider.Settings;
+import android.text.TextUtils;
+
+import androidx.annotation.RequiresApi;
+import androidx.preference.Preference;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+
+public class FakeCameraSwitchPreferenceController extends TogglePreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver{
+
+    private final MetricsFeatureProvider mMetricsFeatureProvider;
+    private Preference mPreference;
+    //此处的key必须和xml里面的key保持一致 不然会导致报错 空指针
+    public FakeCameraSwitchPreferenceController(Context context, String key) {
+        super(context, key);
+        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        mPreference = preference;
+        super.updateState(preference);
+    }
+
+
+
+    @Override
+    public boolean isSliceable() {
+        return TextUtils.equals(getPreferenceKey(), "fake_camera");
+    }
+
+    @Override
+    public boolean isPublicSlice() {
+        return true;
+    }
+
+    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
+    @Override
+    public boolean isChecked() {
+        return Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.USE_FAKE_CAMERA,1)==1;
+    }
+
+    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        final int useFakeCamera=isChecked?1:0;
+        Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.USE_FAKE_CAMERA,useFakeCamera);
+        return true;
+    }
+}

4. 添加liveSetting activity (感觉非必须,但没有测试)


diff --git a/packages/apps/Settings/AndroidManifest.xml b/packages/apps/Settings/AndroidManifest.xml
old mode 100644
new mode 100755
index 5649d2e..50ed7d7
--- a/packages/apps/Settings/AndroidManifest.xml
+++ b/packages/apps/Settings/AndroidManifest.xml
@@ -974,6 +974,24 @@
                 android:value="true" />
         </activity>
 
+        <!-- Live -->
+        <activity
+            android:name="Settings$LiveSettingsActivity"
+            android:label="@string/live_settings"
+            android:taskAffinity=""
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.VOICE_LAUNCH" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
+                android:value="com.android.settings.klive.LiveSettings" />
+            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
+                android:value="true" />
+        </activity>
+
         <activity
             android:name="Settings$DisplaySettingsActivity"
             android:label="@string/display_settings"

diff --git a/packages/apps/Settings/src/com/android/settings/Settings.java b/packages/apps/Settings/src/com/android/settings/Settings.java
old mode 100644
new mode 100755
index 00e07ed..b4a04ab
--- a/packages/apps/Settings/src/com/android/settings/Settings.java
+++ b/packages/apps/Settings/src/com/android/settings/Settings.java
@@ -103,6 +103,7 @@ public class Settings extends SettingsActivity {
     public static class UserDictionarySettingsActivity extends SettingsActivity { /* empty */ }
     public static class DarkThemeSettingsActivity extends SettingsActivity { /* empty */ }
     public static class DisplaySettingsActivity extends SettingsActivity { /* empty */ }
+    public static class LiveSettingsActivity extends SettingsActivity { /* empty */ }
     public static class NightDisplaySettingsActivity extends SettingsActivity { /* empty */ }
     public static class NightDisplaySuggestionActivity extends NightDisplaySettingsActivity { /* empty */ }
     public static class SmartAutoRotateSettingsActivity extends SettingsActivity { /* empty */ }

5. 注册LiveSettings到DashboardFragment


diff --git a/packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java b/packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java
old mode 100644
new mode 100755
index 0316fba..ac02d12
--- a/packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/packages/apps/Settings/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -19,6 +19,7 @@ package com.android.settings.core.gateway;
 import com.android.settings.AllInOneTetherSettings;
 import com.android.settings.DisplaySettings;
 import com.android.settings.IccLockSettings;
+import com.android.settings.LiveSettings;
 import com.android.settings.MainClear;
 import com.android.settings.MainClearConfirm;
 import com.android.settings.Settings;
@@ -199,6 +200,7 @@ public class SettingsGateway {
             UserDictionaryList.class.getName(),
             UserDictionarySettings.class.getName(),
             DisplaySettings.class.getName(),
+            LiveSettings.class.getName(),
             MyDeviceInfoFragment.class.getName(),
             ModuleLicensesDashboard.class.getName(),
             ManageApplications.class.getName(),
@@ -331,6 +333,7 @@ public class SettingsGateway {
             Settings.NetworkDashboardActivity.class.getName(),
             Settings.ConnectedDeviceDashboardActivity.class.getName(),
             Settings.AppDashboardActivity.class.getName(),
+            Settings.LiveSettingsActivity.class.getName(),
             Settings.DisplaySettingsActivity.class.getName(),
             Settings.SoundSettingsActivity.class.getName(),
             Settings.StorageDashboardActivity.class.getName(),
diff --git a/packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragmentRegistry.java b/packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragmentRegistry.java
old mode 100644
new mode 100755
index 8ca2bee..6864a68
--- a/packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragmentRegistry.java
+++ b/packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragmentRegistry.java
@@ -20,6 +20,7 @@ import android.util.ArrayMap;
 
 import com.android.settings.DisplaySettings;
 import com.android.settings.LegalSettings;
+import com.android.settings.LiveSettings;
 import com.android.settings.accounts.AccountDashboardFragment;
 import com.android.settings.accounts.AccountDetailDashboardFragment;
 import com.android.settings.applications.AppDashboardFragment;
@@ -79,6 +80,8 @@ public class DashboardFragmentRegistry {
                 CategoryKey.CATEGORY_DEVICE);
         PARENT_TO_CATEGORY_KEY_MAP.put(AppDashboardFragment.class.getName(),
                 CategoryKey.CATEGORY_APPS);
+        PARENT_TO_CATEGORY_KEY_MAP.put(LiveSettings.class.getName(),
+                CategoryKey.CATEGORY_LIVE);
         PARENT_TO_CATEGORY_KEY_MAP.put(PowerUsageSummary.class.getName(),
                 CategoryKey.CATEGORY_BATTERY);
         PARENT_TO_CATEGORY_KEY_MAP.put(DisplaySettings.class.getName(),

6. 添加livesetting相关的枚举


diff --git a/frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryKey.java b/frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryKey.java
old mode 100644
new mode 100755
index 0d544be..fdc6334
--- a/frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryKey.java
+++ b/frameworks/base/packages/SettingsLib/src/com/android/settingslib/drawer/CategoryKey.java
@@ -30,6 +30,7 @@ public final class CategoryKey {
     public static final String CATEGORY_APPS = "com.android.settings.category.ia.apps";
     public static final String CATEGORY_APPS_DEFAULT =
             "com.android.settings.category.ia.apps.default";
+    public static final String CATEGORY_LIVE = "com.android.settings.category.ia.live";
     public static final String CATEGORY_BATTERY = "com.android.settings.category.ia.battery";
     public static final String CATEGORY_DISPLAY = "com.android.settings.category.ia.display";
     public static final String CATEGORY_SOUND = "com.android.settings.category.ia.sound";
diff --git a/frameworks/proto_logging/stats/enums/app/settings_enums.proto b/frameworks/proto_logging/stats/enums/app/settings_enums.proto
old mode 100644
new mode 100755
index 173cc72..556fd23
--- a/frameworks/proto_logging/stats/enums/app/settings_enums.proto
+++ b/frameworks/proto_logging/stats/enums/app/settings_enums.proto
@@ -3180,6 +3180,11 @@ enum PageId {
     // CATEGORY: SETTINGS
     // OS: S
     SETTINGS_BUTTON_NAV_DLG = 1907;
+    
+    // OPEN: Settings >Live
+    // CATEGORY: SETTINGS
+    // OS: S
+    LIVE = 1908;
 }
 
 // Battery Saver schedule types.


通过以上步骤 能顺利添加新的setting项到 setting主界面中。
 

猜你喜欢

转载自blog.csdn.net/weixin_40774418/article/details/124123576
今日推荐