【笔记】Android Settings 应用设置菜单的界面代码介绍

简介

Settings应用中,提供多类设置菜单入口,每个菜单内又有各模块功能的实现。

那么各个模块基于Settings 基础的界面Fragment去实现UI,层层按不同业务进行封装继承实现子类:

  • DashboardFragment
  • SettingsPreferenceFragment

功能设置页中的菜单又是通过Controller去实现业务并进行UI动态更新控制。

代码

基于 Android U 平台的实现进行介绍。

公用基类

DashboardFragment

packages/apps/Settings/src/com/android/settings/dashboard/DashboardFragment.java

package com.android.settings.dashboard;/*** Base fragment for dashboard style UI containing a list of static and dynamic setting items.*/
public abstract class DashboardFragment extends SettingsPreferenceFragmentimplements CategoryListener, Indexable, PreferenceGroup.OnExpandButtonClickListener,BasePreferenceController.UiBlockListener {public static final String CATEGORY = "category";private static final String TAG = "DashboardFragment";private static final long TIMEOUT_MILLIS = 50L;/*** Get a list of {@link AbstractPreferenceController} for this fragment.*/protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {return null;}
 

模块案例

继承关系:(自上而下,底层是父类,除了MobileNetwork,其他都是抽象类,直到Fragment)

  • MobileNetworkSettings --- 移动网络设置
  • AbstractMobileNetworkSettings
  • RestrictedDashboardFragment
  • DashboardFragment
  • SettingsPreferenceFragment
  • InstrumentedPreferenceFragment -- /com/android/settings/core/
  • ObservablePreferenceFragment -- /frameworks/base/packages/SettingsLib
  • PreferenceFragmentCompat -- classes.jar/androidx.preference
  • Fragment

AbstractMobileNetworkSettings 是一个抽象类,用于提供移动网络设置相关的基本功能和行为。

RestrictedDashboardFragment 是在DashboardFragment的基础上添加了一些限制或额外的安全措施,以限制用户对某些设置或操作的访问或更改。可能会对一些敏感的设置进行限制,例如网络设置、安全设置等。也可能会对某些功能进行权限控制,只允许特定用户访问。

DashboardFragment 是基础的仪表盘样式 UI Fragment,用于显示静态和动态的设置项列表。通常不会加入对设置的限制,而是专注于提供用户友好的设置界面和功能。可能包含一些基本的设置项处理和展示逻辑,以便用户能够轻松地进行配置和操作。

MobileNetworkSettings 移动网络设置(界面逻辑)

packages/apps/Settings/src/com/android/settings/network/telephony/MobileNetworkSettings.java

界面布局在mobile_network_settings.xml

MobileNetworkSettings API
APIFunction
onPreferenceTreeClick定义子功能pref点击事件(override)
createPreferenceControllers创建子功能pref的控制器(override)
onAttach生命周期,use 各类 Controller(override)
onSubscriptionDetailChanged响应注册状态变化进行的操作
package com.android.settings.network.telephony;@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class MobileNetworkSettings extends AbstractMobileNetworkSettings implementsMobileNetworkRepository.MobileNetworkCallback {private static final String LOG_TAG = "NetworkSettings";//处理子菜单点击事件/*** Invoked on each preference click in this hierarchy, overrides* PreferenceActivity's implementation.  Used to make sure we track the* preference click events.*/@Overridepublic boolean onPreferenceTreeClick(Preference preference) {if (super.onPreferenceTreeClick(preference)) {return true;}final String key = preference.getKey();if (TextUtils.equals(key, BUTTON_CDMA_SYSTEM_SELECT_KEY)|| TextUtils.equals(key, BUTTON_CDMA_SUBSCRIPTION_KEY)) {if (mTelephonyManager.getEmergencyCallbackMode()) {startActivityForResult(new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),REQUEST_CODE_EXIT_ECM);mClickedPrefKey = key;}return true;}//【客制化】可通过else if 定制其他keyt的点击行为return false;}//创建各子菜单/功能选项的Controller@Overrideprotected List<AbstractPreferenceController> createPreferenceControllers(Context context) {if (!SubscriptionUtil.isSimHardwareVisible(context)) {finish();return Arrays.asList();}if (getArguments() == null) {Intent intent = getIntent();if (intent != null) {mSubId = intent.getIntExtra(Settings.EXTRA_SUB_ID,MobileNetworkUtils.getSearchableSubscriptionId(context));Log.d(LOG_TAG, "display subId from intent: " + mSubId);} else {Log.d(LOG_TAG, "intent is null, can not get subId " + mSubId + " from intent.");}} else {mSubId = getArguments().getInt(Settings.EXTRA_SUB_ID,MobileNetworkUtils.getSearchableSubscriptionId(context));Log.d(LOG_TAG, "display subId from getArguments(): " + mSubId);}mMobileNetworkRepository = MobileNetworkRepository.getInstance(context);mExecutor.execute(() -> {mSubscriptionInfoEntity = mMobileNetworkRepository.getSubInfoById(String.valueOf(mSubId));mMobileNetworkInfoEntity =mMobileNetworkRepository.queryMobileNetworkInfoBySubId(String.valueOf(mSubId));});return Arrays.asList(new DataUsageSummaryPreferenceController(getActivity(), getSettingsLifecycle(),this, mSubId),new RoamingPreferenceController(context, KEY_ROAMING_PREF, getSettingsLifecycle(),this, mSubId),new CallsDefaultSubscriptionController(context, KEY_CALLS_PREF,getSettingsLifecycle(), this),new SmsDefaultSubscriptionController(context, KEY_SMS_PREF, getSettingsLifecycle(),this),new MobileDataPreferenceController(context, KEY_MOBILE_DATA_PREF,getSettingsLifecycle(), this, mSubId),new ConvertToEsimPreferenceController(context, KEY_CONVERT_TO_ESIM_PREF,getSettingsLifecycle(), this, mSubId));}@Overridepublic void onAttach(Context context) {super.onAttach(context);if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {Log.d(LOG_TAG, "Invalid subId, get the default subscription to show.");SubscriptionInfo info = SubscriptionUtil.getSubscriptionOrDefault(context, mSubId);if (info == null) {Log.d(LOG_TAG, "Invalid subId request " + mSubId);return;}mSubId = info.getSubscriptionId();Log.d(LOG_TAG, "Show NetworkSettings fragment for subId" + mSubId);}Intent intent = getIntent();if (intent != null) {int updateSubscriptionIndex = intent.getIntExtra(Settings.EXTRA_SUB_ID,SubscriptionManager.INVALID_SUBSCRIPTION_ID);if (updateSubscriptionIndex != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {int oldSubId = mSubId;mSubId = updateSubscriptionIndex;// If the subscription has changed or the new intent does not contain the opt in// action,// remove the old discovery dialog. If the activity is being recreated, we will see// onCreate -> onNewIntent, so the dialog will first be recreated for the old// subscription// and then removed.if (updateSubscriptionIndex != oldSubId|| !MobileNetworkActivity.doesIntentContainOptInAction(intent)) {removeContactDiscoveryDialog(oldSubId);}// evaluate showing the new discovery dialog if this intent contains an action to// show the// opt-in.if (MobileNetworkActivity.doesIntentContainOptInAction(intent)) {showContactDiscoveryDialog();}}}final DataUsageSummaryPreferenceController dataUsageSummaryPreferenceController =use(DataUsageSummaryPreferenceController.class);if (dataUsageSummaryPreferenceController != null) {dataUsageSummaryPreferenceController.init(mSubId);}use(MobileNetworkSwitchController.class).init(mSubId);use(CarrierSettingsVersionPreferenceController.class).init(mSubId);use(BillingCyclePreferenceController.class).init(mSubId);use(MmsMessagePreferenceController.class).init(mSubId);use(AutoDataSwitchPreferenceController.class).init(mSubId);use(DisabledSubscriptionController.class).init(mSubId);use(DeleteSimProfilePreferenceController.class).init(mSubId, this,REQUEST_CODE_DELETE_SUBSCRIPTION);use(DisableSimFooterPreferenceController.class).init(mSubId);use(NrDisabledInDsdsFooterPreferenceController.class).init(mSubId);final MobileDataPreferenceController mobileDataPreferenceController =use(MobileDataPreferenceController.class);if (mobileDataPreferenceController != null) {mobileDataPreferenceController.init(getFragmentManager(), mSubId,mSubscriptionInfoEntity, mMobileNetworkInfoEntity);mobileDataPreferenceController.setWifiPickerTrackerHelper(new WifiPickerTrackerHelper(getSettingsLifecycle(), context,null /* WifiPickerTrackerCallback */));}final RoamingPreferenceController roamingPreferenceController =use(RoamingPreferenceController.class);if (roamingPreferenceController != null) {roamingPreferenceController.init(getFragmentManager(), mSubId,mMobileNetworkInfoEntity);}use(ApnPreferenceController.class).init(mSubId);use(CarrierPreferenceController.class).init(mSubId);use(DataUsagePreferenceController.class).init(mSubId);use(PreferredNetworkModePreferenceController.class).init(mSubId);use(EnabledNetworkModePreferenceController.class).init(mSubId);use(DataServiceSetupPreferenceController.class).init(mSubId);use(Enable2gPreferenceController.class).init(mSubId);use(CarrierWifiTogglePreferenceController.class).init(getLifecycle(), mSubId);final WifiCallingPreferenceController wifiCallingPreferenceController =use(WifiCallingPreferenceController.class).init(mSubId);final OpenNetworkSelectPagePreferenceController openNetworkSelectPagePreferenceController =use(OpenNetworkSelectPagePreferenceController.class).init(mSubId);final AutoSelectPreferenceController autoSelectPreferenceController =use(AutoSelectPreferenceController.class).init(getLifecycle(), mSubId).addListener(openNetworkSelectPagePreferenceController);use(NetworkPreferenceCategoryController.class).init(mSubId).setChildren(Arrays.asList(autoSelectPreferenceController));mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);mCdmaSubscriptionPreferenceController = use(CdmaSubscriptionPreferenceController.class);mCdmaSubscriptionPreferenceController.init(getPreferenceManager(), mSubId);final VideoCallingPreferenceController videoCallingPreferenceController =use(VideoCallingPreferenceController.class).init(mSubId);use(CallingPreferenceCategoryController.class).setChildren(Arrays.asList(wifiCallingPreferenceController, videoCallingPreferenceController));use(Enhanced4gLtePreferenceController.class).init(mSubId).addListener(videoCallingPreferenceController);use(Enhanced4gCallingPreferenceController.class).init(mSubId).addListener(videoCallingPreferenceController);use(Enhanced4gAdvancedCallingPreferenceController.class).init(mSubId).addListener(videoCallingPreferenceController);use(ContactDiscoveryPreferenceController.class).init(getParentFragmentManager(), mSubId);use(NrAdvancedCallingPreferenceController.class).init(mSubId);use(TransferEsimPreferenceController.class).init(mSubId, mSubscriptionInfoEntity);final ConvertToEsimPreferenceController convertToEsimPreferenceController =use(ConvertToEsimPreferenceController.class);if (convertToEsimPreferenceController != null) {convertToEsimPreferenceController.init(mSubId, mSubscriptionInfoEntity);}List<AbstractSubscriptionPreferenceController> subscriptionPreferenceControllers =useAll(AbstractSubscriptionPreferenceController.class);for (AbstractSubscriptionPreferenceController controller :subscriptionPreferenceControllers) {controller.init(mSubId);}}private void onSubscriptionDetailChanged() {if (mSubscriptionInfoEntity != null) {/*** Update the title when SIM stats got changed*/final Consumer<Activity> renameTitle = activity -> {if (activity != null && !activity.isFinishing()) {if (activity instanceof SettingsActivity) {((SettingsActivity) activity).setTitle(mSubscriptionInfoEntity.uniqueName);}}};ThreadUtils.postOnMainThread(() -> {renameTitle.accept(getActivity());redrawPreferenceControllers();});}}//界面布局在mobile_network_settings.xml@Overrideprotected int getPreferenceScreenResId() {return R.xml.mobile_network_settings;}}

mobile_network_settings 布局

packages/apps/Settings/res/xml/mobile_network_settings.xml

  • MobileNetworkSwitchController SIM卡移动网络总开关逻辑
  • use_sim_switch 界面的资源key
  • mobile_network_use_sim_on 开关名称title

以下仅展示部分功能选项/菜单:

<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"xmlns:settings="http://schemas.android.com/apk/res-auto"android:key="mobile_network_pref_screen"><com.android.settings.widget.SettingsMainSwitchPreferenceandroid:key="use_sim_switch"android:title="@string/mobile_network_use_sim_on"settings:controller="com.android.settings.network.telephony.MobileNetworkSwitchController"/><PreferenceCategoryandroid:key="enabled_state_container"android:title="@string/summary_placeholder"settings:controller="com.android.settings.network.telephony.DisabledSubscriptionController"android:layout="@layout/preference_category_no_label"><!--智能数据切换开关--><SwitchPreferenceandroid:key="auto_data_switch"android:title="@string/auto_data_switch_title"android:summary="@string/auto_data_switch_summary"settings:controller="com.android.settings.network.telephony.AutoDataSwitchPreferenceController"/><!--数据漫游开关--><com.android.settingslib.RestrictedSwitchPreferenceandroid:key="button_roaming_key"android:title="@string/roaming"android:persistent="false"android:summaryOn="@string/roaming_enable"android:summaryOff="@string/roaming_disable"settings:userRestriction="no_data_roaming"settings:controller="com.android.settings.network.telephony.RoamingPreferenceController"/><!--数据流量菜单入口--><Preferenceandroid:key="data_usage_summary"android:title="@string/mobile_data_usage_title"settings:controller="com.android.settings.network.telephony.DataUsagePreferenceController"/><!--网络模式列表选择--><ListPreferenceandroid:key="enabled_networks_key"android:title="@string/preferred_network_mode_title"android:summary="@string/preferred_network_mode_summary"android:entries="@array/enabled_networks_choices"android:entryValues="@array/enabled_networks_values"android:dialogTitle="@string/preferred_network_mode_dialogtitle"settings:controller="com.android.settings.network.telephony.EnabledNetworkModePreferenceController"/><!--Network目录--><PreferenceCategoryandroid:key="network_operators_category_key"android:title="@string/network_operator_category"settings:controller="com.android.settings.network.telephony.NetworkPreferenceCategoryController"><!--自动选网开关--><SwitchPreferenceandroid:key="auto_select_key"android:title="@string/select_automatically"settings:controller="com.android.settings.network.telephony.gsm.AutoSelectPreferenceController"/><!--如果上述自动选网关闭,那么此手动选网菜单,可跳转到网络列表页--><Preferenceandroid:key="choose_network_key"android:title="@string/choose_network_title"settings:controller="com.android.settings.network.telephony.gsm.OpenNetworkSelectPagePreferenceController"/></PreferenceCategory><!--APN设置入口--><!--We want separate APN setting from reset of settings because we want user to change it with caution--><com.android.settingslib.RestrictedPreferenceandroid:key="telephony_apn_key"android:persistent="false"android:title="@string/mobile_network_apn_title"settings:allowDividerAbove="true"settings:keywords="@string/keywords_access_point_names"settings:controller="com.android.settings.network.telephony.ApnPreferenceController"/></PreferenceCategory></PreferenceScreen>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/367279.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言快速学习笔记

学习网站&#xff1a;C 语言教程 | 菜鸟教程 (runoob.com)C 语言教程 | 菜鸟教程 (runoob.com)C 语言教程 | 菜鸟教程 (runoob.com) 这个网站知识完整&#xff0c;讲解清晰。 在线C语言编程工具&#xff1a;菜鸟教程在线编辑器 (runoob.com) 国外学习网站&#xff1a;C语言介…

难道 Java 已经过时了?

当一门技术已经存在许多年了&#xff0c;它可能会失去竞争力&#xff0c;而后黯然退场&#xff0c;默默地离开&#xff0c;这对大部分的人来说就已经算是过时了。 Java 于 1995 年正式上线&#xff0c;至今已经走过了 27 个年头&#xff0c;在众多编程技术里算是年龄比较大的语…

【C++】开源:量化金融计算库QuantLib配置与使用

&#x1f60f;★,:.☆(&#xffe3;▽&#xffe3;)/$:.★ &#x1f60f; 这篇文章主要介绍量化交易库QuantLib配置与使用。 无专精则不能成&#xff0c;无涉猎则不能通。——梁启超 欢迎来到我的博客&#xff0c;一起学习&#xff0c;共同进步。 喜欢的朋友可以关注一下&#…

【高中数学/基本不等式】已知:a,b皆为正实数,且3a+2b=10 求:3a开方+2b开方的最大值?

【题目】 已知&#xff1a;a,b皆为正实数&#xff0c;且3a2b10 求&#xff1a;3a开方2b开方的最大值&#xff1f; 【解答】 解法一&#xff1a;&#xff08;基本不等式&#xff09; 原式^23a2*根号下(3a*2b)2b102*根号下(3a*2b)<103a2b101020 答&#xff1a;3a开方2b…

[漏洞复现] MetInfo5.0.4文件包含漏洞

[漏洞复现] MetInfo5.0.4文件包含漏洞 MetInfo5.0.4 漏洞代码审计 漏洞出现在about/index.php中&#xff0c;因为利用了动态地址&#xff0c;所以存在漏洞。 漏洞检查语句&#xff08;&#xff01;192.168.109.100是我的服务器ip&#xff0c;需要换成自己的&#xff09;&…

双曲方程初值问题的差分逼近(迎风格式)

稳定性: 数值例子 例一 例二 代码 % function chap4_hyperbolic_1st0rder_1D % test the upwind scheme for 1D hyperbolic equation % u_t + a*u_x = 0,0<x<L,O<t<T, % u(x,0) = |x-1|,0<X<L, % u(0,t) = 1% foundate = 2015-4-22’; % chgedate = 202…

SpringBoot 如何处理跨域请求?你说的出几种方法?

引言&#xff1a;在现代的Web开发中&#xff0c;跨域请求&#xff08;Cross-Origin Resource Sharing&#xff0c;CORS&#xff09;是一个常见的挑战。随着前后端分离架构的流行&#xff0c;前端应用通常运行在一个与后端 API 不同的域名或端口上&#xff0c;这就导致了浏览器的…

方法的用法

一.简介 目前为止我给出的所有的案例都是将代码放在main方法中&#xff0c;就会产生一些问题&#xff1a; 代码冗长&#xff0c;不利于维护变量过多&#xff0c;想不出那么多的变量名没有重用性 那么该如何解决呢&#xff1f; 我们可以编写功能性的代码块&#xff0c;来被ma…

华为DCN之:SDN和NFV

1. SDN概述 1.1 SDN的起源 SDN&#xff08;Software Defined Network&#xff09;即软件定义网络。是由斯坦福大学Clean Slate研究组提出的一种新型网络创新架构。其核心理念通过将网络设备控制平面与数据平面分离&#xff0c;从而实现了网络控制平面的集中控制&#xff0c;为…

【STM32 RTC实时时钟如何配置!超详细的解析和超简单的配置,附上寄存器操作】

STM32 里面RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域&#xff0c;即在系统复位或从待机模式唤醒后&#xff0c;RTC的设置和时间维持不变。因为系统对后备寄存器和RTC相关寄存器有写保护&#xff0c;所以如果想要对后备寄存器和RTC进行访问&#xff0c;则需要通过操作…

PHP校园论坛-计算机毕业设计源码08586

摘 要 本项目旨在基于PHP技术设计与实现一个校园论坛系统&#xff0c;以提供一个功能丰富、用户友好的交流平台。该论坛系统将包括用户注册与登录、帖子发布与回复、个人信息管理等基本功能&#xff0c;并结合社交化特点&#xff0c;增强用户之间的互动性。通过利用PHP语言及其…

14-15 为什么我们现在对阅读如此难以接受

写出来感觉很奇怪&#xff0c;但最近我感觉自己失去了阅读能力。长篇文本对我来说尤其具有挑战性。句子很难读完。更别提章节了。章节有很多段落&#xff0c;而段落又由许多句子组成。 啊。 即使在极少数情况下&#xff0c;我读完了一章&#xff0c;下一页上已经有另一章等着…

什么是自动气象站呢

自动气象站&#xff0c;作为现代气象观测的重要工具&#xff0c;已经深入到我们生活的各个领域&#xff0c;从气象预报到农业生产&#xff0c;再到环境保护&#xff0c;自动气象站都发挥着不可或缺的作用。 自动气象站&#xff0c;顾名思义&#xff0c;是一种能够自动收集、处理…

153. 寻找旋转排序数组中的最小值(中等)

153. 寻找旋转排序数组中的最小值 1. 题目描述2.详细题解3.代码实现3.1 Python3.2 Java 1. 题目描述 题目中转&#xff1a;153. 寻找旋转排序数组中的最小值 2.详细题解 如果不考虑 O ( l o g n ) O(log n) O(logn)的时间复杂度&#xff0c;直接 O ( n ) O(n) O(n)时间复杂…

基于Spring Boot的先进时尚室内管理系统

1 项目介绍 1.1 研究背景 随着21世纪信息技术革命的到来&#xff0c;互联网的普及与发展对人类社会的演变产生了深远影响&#xff0c;跨越了物质生活的丰盈边界&#xff0c;更深层次地滋养了人类的精神文化生活。在过去&#xff0c;囿于地理位置和技术条件的限制&#xff0c;…

【网络】网络基础(一)

网络基础&#xff08;一&#xff09; 文章目录 一、计算机网络背景1.1网络发展1.2认识“协议” 二、网络协议初识2.1OSI七层模型2.2OSI五层模型 三、网络传输基本流程3.1局域网通信3.2网络传输流程不跨子网的网络传输跨子网的网络传输 3.3网络中的地址管理IP地址MAC地址 一、计…

使用conda安装第三方包报错CondaSSLError

使用conda安装第三方包报错CondaSSLError 1. 报错信息2. 解决方法 1. 报错信息 错误描述&#xff1a;刚刚下载的 anaconda 在使用 conda 安装 pytorch 时报错&#xff08;CondaSSLError: OpenSSL appears to be unavailable on this machine. OpenSSL is required to download …

LeetCode题练习与总结:二叉树的后序遍历--145

一、题目描述 给你一棵二叉树的根节点 root &#xff0c;返回其节点值的 后序遍历 。 示例 1&#xff1a; 输入&#xff1a;root [1,null,2,3] 输出&#xff1a;[3,2,1]示例 2&#xff1a; 输入&#xff1a;root [] 输出&#xff1a;[]示例 3&#xff1a; 输入&#xff1a…

2002-2022年各省老年人口抚养比(人口抽样调查)数据

2002-2022年各省老年人口抚养比(人口抽样调查)数据 1、时间&#xff1a;2002-2022年 2、指标&#xff1a;老年人口抚养比 3、来源&#xff1a;国家统计局、统计年鉴 4、范围&#xff1a;31省&#xff0c; 5、缺失情况&#xff1a;无缺失&#xff0c;其中2010年的值取2009、…

Swift 中强大的 Key Paths(键路径)机制趣谈(下)

概览 在上一篇博文 Swift 中强大的 Key Paths(键路径)机制趣谈(上)中,我们介绍了 Swift 语言中键路径机制的基础知识,并举了若干例子讨论了它的一些用武之地。 而在本文中我们将再接再厉,继续有趣的键路径大冒险,为 KeyPaths 画上一个圆满的句号。 在本篇博文中,您将…