Android12 (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

  1. 参考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的布局文件,目前只添加了虚拟摄像头开关这一个选项,后续需要增加可以参考添加

  1. 虚拟摄像头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;
+    }
+}
  1. 添加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 */ }
  1. 注册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(),
  1. 添加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主界面中。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容