Android 5.1 Settings源代码简要分析
转载请注明出处,谢谢~http://blog.csdn.net/u011974987/article/details/51004854。
概述:
先声明:本人工作快两年了,仍是菜鸟级别的。羞愧啊!曾经遇到好多知识点都没有记录下来,感觉挺可惜的。如今有机会接触Android 源代码。
我们一个Android组的搞Setting,我认为是得写得东西。毕竟才接触。如今仅仅能看一段时间代码,就先记录下一些收获吧,说多了就是泪~本文主要针对L平台上Settings模块正常启动流程做一个简要分析,并试着分析一下Settings以下某选项的实现。
Setting 简单介绍
在之前的KK平台上Settings模块的第一个Activity名字为Settings,其继承的是PreferenceActivity,设置的每个选项都是相应的一个Header对象,而且Header对象同意显示switch控件,button控件,checkbox控件等。
例如以下图1.1,WLAN和蓝牙上使用到了switch开关。但在L上面,WLAN和蓝牙的这两个开关已经去掉了。如图1.2,在Settings模块的首个页面似乎就仅仅是一个普通的Listview,那它用的还是不是Header呢?或者说取而代之的是什么呢?继续往下看吧~
图片-1.1
图片-1.2
L Settings 模块首界面初始化流程
L Settings模块首界面为Settings。继承自SettingsActivity,SettingsActivity继承自Activity。
首先看一下Settings.java代码能够发现它没有重写不论什么SettingsActiviy的方法。也没有添加不论什么自己的方法,只有添加了很多静态内部类,如:
/**
* Top-level Settings activity
*/
public class Settings extends SettingsActivity {
/*
* Settings subclasses for launching independently.
*/
public static class BluetoothSettingsActivity extends SettingsActivity { /* empty */ }
public static class WirelessSettingsActivity extends SettingsActivity { /* empty */ }
public static class SimSettingsActivity extends SettingsActivity { /* empty */ }
public static class TetherSettingsActivity extends SettingsActivity { /* empty */ }
public static class VpnSettingsActivity extends SettingsActivity { /* empty */ }
public static class DateTimeSettingsActivity extends SettingsActivity { /* empty */ }
public static class StorageSettingsActivity extends SettingsActivity { /* empty */ }
public static class WifiSettingsActivity extends SettingsActivity { /* empty */ }
public static class WifiP2pSettingsActivity extends SettingsActivity { /* empty */ }
public static class InputMethodAndLanguageSettingsActivity extends SettingsActivity { /* empty */ }
public static class KeyboardLayoutPickerActivity extends SettingsActivity { /* empty */ }
public static class InputMethodAndSubtypeEnablerActivity extends SettingsActivity { /* empty */ }
public static class VoiceInputSettingsActivity extends SettingsActivity { /* empty */ }
public static class SpellCheckersSettingsActivity extends SettingsActivity { /* empty */ }
public static class LocalePickerActivity extends SettingsActivity { /* empty */ }
public static class UserDictionarySettingsActivity extends SettingsActivity { /* empty */ }
public static class HomeSettingsActivity extends SettingsActivity { /* empty */ }
...
}
看凝视能够知道,这些子类是为了启动特定独立的Settings选项而创建的,比如在某个应用里须要设置无线那么仅仅须要启动 WirelessSettingsActivity 就能够了。
所以Settings模块的启动流程直接看SettingsActiviy即可了。
1. SettingsActivity.onCreate方法
onCreate方法是Activity的生命周期第一步。看看 SettingsActivity在这里都做了些什么?
// Should happen before any call to getIntent()
getMetaData();
这种方法用来获得Activity的额外数据mFragmentClass。假设能够获得这个数据。那么以下会去显示mFragmentClass相应的Activity。直接启动Settings模块不会获得这个数据。
mIsShowingDashboard = className.equals(Settings.class.getName());
这一步非常重要,由于我们是从Settings这个Activity过来的,所以这里的 mIsShowingDashboard 为 true 。
// This is a "Sub Settings" when:
// - this is a real SubSettings
// - or :settings:show_fragment_as_subsetting is passed to the Intent
final boolean isSubSettings = className.equals(SubSettings.class.getName()) ||
intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
这个推断非常重要但非常明显这时isSubSettings的值是fasle,临时忽略。
setContentView(mIsShowingDashboard ?
R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
前面知道这里的 mIsShowingDashboard为true,所以这里使用的布局文件为R.layout.settings_main_dashboard。settings_main_dashboard.xml文件例如以下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_content"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/dashboard_background_color"
/>
由于mIsShowingDashboard为true,直接走到以下这段
else {
// No UP affordance if we are displaying the main Dashboard
mDisplayHomeAsUpEnabled = false;
// Show Search affordance
mDisplaySearch = true;
mInitialTitleResId = R.string.dashboard_title;
switchToFragment(DashboardSummary.class.getName(), null, false, false,
mInitialTitleResId, mInitialTitle, false);
}
这里看到switchToFragment这种方法,能够知道这里是要切换DashboardSummary这个Fragment.
接下来就看看DashboardSummary是个什么玩意?
dashboard中文意思是仪表盘,这里是指DashboardSummary就是用来显示Settings全部选项的。
在DashboardSummary的onCreateView里载入了这个布局文件R.layout.dashboard
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:clipToPadding="false">
<LinearLayout
android:id="@+id/dashboard_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:paddingStart="@dimen/dashboard_padding_start"
android:paddingEnd="@dimen/dashboard_padding_end"
android:paddingTop="@dimen/dashboard_padding_top"
android:paddingBottom="@dimen/dashboard_padding_bottom"
android:orientation="vertical"
/>
</ScrollView>
看了上面的布局文件能够知道Settings的选项视图应该就是显示在dashboard_container中了。
DashboardSummary走完onCreateView方法后会走onResume,然后一路下来又会调到SettingsActivity的
loadCategoriesFromResource(R.xml.dashboard_categories, categories);
这一步是通过 R.xml.dashboard_categories来载入categories,这里的categorys为ArrayList mCategories。
接着来看看dashboard_categories.xml这个文件吧
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 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.
-->
<dashboard-categories
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- WIRELESS and NETWORKS -->
<dashboard-category
android:id="@+id/wireless_section"
android:title="@string/header_category_wireless_networks" >
<!-- Wifi -->
<dashboard-tile
android:id="@+id/wifi_settings"
android:title="@string/wifi_settings_title"
android:fragment="com.android.settings.wifi.WifiSettings"
android:icon="@drawable/ic_settings_wireless"
/>
<!--HetComm-->
<dashboard-tile
android:id="@+id/hetcomm_settings"
android:icon="@drawable/ic_settings_hetcomm"
android:title="@string/hetcom_setting_title">
<intent android:action="com.android.settings.HETCOMM_SETTINGS" />
</dashboard-tile>
<!-- Bluetooth -->
<dashboard-tile
android:id="@+id/bluetooth_settings"
android:title="@string/bluetooth_settings_title"
android:fragment="com.android.settings.bluetooth.BluetoothSettings"
android:icon="@drawable/ic_settings_bluetooth2"
/>
<!-- Hotknot -->
<dashboard-tile
android:id="@+id/hotknot_settings"
android:title="@string/hotknot_settings_title"
android:fragment="com.mediatek.settings.hotknot.HotKnotSettings"
android:icon="@drawable/ic_settings_hotknot"
/>
<!-- SIM Cards -->
<dashboard-tile
android:id="@+id/sim_settings"
android:title="@string/sim_settings_title"
android:fragment="com.android.settings.sim.SimSettings"
android:icon="@drawable/ic_sim_sd"
/>
<!-- Data Usage -->
<dashboard-tile
android:id="@+id/data_usage_settings"
android:title="@string/data_usage_summary_title"
android:fragment="com.android.settings.DataUsageSummary"
android:icon="@drawable/ic_settings_data_usage"
/>
<!-- Operator hook -->
<dashboard-tile
android:id="@+id/operator_settings"
android:fragment="com.android.settings.WirelessSettings" >
<intent android:action="com.android.settings.OPERATOR_APPLICATION_SETTING" />
</dashboard-tile>
<!-- Other wireless and network controls -->
<dashboard-tile
android:id="@+id/wireless_settings"
android:title="@string/radio_controls_title"
android:fragment="com.android.settings.WirelessSettings"
android:icon="@drawable/ic_settings_more"
/>
</dashboard-category>
<!-- DEVICE -->
<dashboard-category
android:id="@+id/device_section"
android:title="@string/header_category_device" >
<!-- Home -->
<dashboard-tile
android:id="@+id/home_settings"
android:title="@string/home_settings"
android:fragment="com.android.settings.HomeSettings"
android:icon="@drawable/ic_settings_home"
/>
<!-- Display -->
<dashboard-tile
android:id="@+id/display_settings"
android:title="@string/display_settings"
android:fragment="com.android.settings.DisplaySettings"
android:icon="@drawable/ic_settings_display"
/>
<!-- Notifications -->
<dashboard-tile
android:id="@+id/notification_settings"
android:title="@string/notification_settings"
android:fragment="com.mediatek.audioprofile.AudioProfileSettings"
android:icon="@drawable/ic_settings_notifications"
/>
<!-- Storage -->
<dashboard-tile
android:id="@+id/storage_settings"
android:title="@string/storage_settings"
android:fragment="com.android.settings.deviceinfo.Memory"
android:icon="@drawable/ic_settings_storage"
/>
<!-- Battery -->
<dashboard-tile
android:id="@+id/battery_settings"
android:title="@string/power_usage_summary_title"
android:fragment="com.android.settings.fuelgauge.PowerUsageSummary"
android:icon="@drawable/ic_settings_battery"
/>
<!-- Application Settings -->
<dashboard-tile
android:id="@+id/application_settings"
android:title="@string/applications_settings"
android:fragment="com.android.settings.applications.ManageApplications"
android:icon="@drawable/ic_settings_applications"
/>
<!-- Manage users -->
<dashboard-tile
android:id="@+id/user_settings"
android:title="@string/user_settings_title"
android:fragment="com.android.settings.users.UserSettings"
android:icon="@drawable/ic_settings_multiuser"
/>
<!-- Manage NFC payment apps -->
<dashboard-tile
android:id="@+id/nfc_payment_settings"
android:title="@string/nfc_payment_settings_title"
android:fragment="com.android.settings.nfc.PaymentSettings"
android:icon="@drawable/ic_settings_nfc_payment"
/>
<!-- Manufacturer hook -->
<dashboard-tile
android:id="@+id/manufacturer_settings"
android:fragment="com.android.settings.WirelessSettings">
<intent android:action="com.android.settings.MANUFACTURER_APPLICATION_SETTING" />
</dashboard-tile>
</dashboard-category>
<!-- PERSONAL -->
<dashboard-category
android:id="@+id/personal_section"
android:title="@string/header_category_personal" >
<!-- Location -->
<dashboard-tile
android:id="@+id/location_settings"
android:title="@string/location_settings_title"
android:fragment="com.android.settings.location.LocationSettings"
android:icon="@drawable/ic_settings_location"
/>
<!-- Security -->
<dashboard-tile
android:id="@+id/security_settings"
android:title="@string/security_settings_title"
android:fragment="com.android.settings.SecuritySettings"
android:icon="@drawable/ic_settings_security"
/>
<!-- Account -->
<dashboard-tile
android:id="@+id/account_settings"
android:title="@string/account_settings_title"
android:fragment="com.android.settings.accounts.AccountSettings"
android:icon="@drawable/ic_settings_accounts"
/>
<!-- Language -->
<dashboard-tile
android:id="@+id/language_settings"
android:title="@string/language_settings"
android:fragment="com.android.settings.inputmethod.InputMethodAndLanguageSettings"
android:icon="@drawable/ic_settings_language"
/>
<!-- Backup and reset -->
<dashboard-tile
android:id="@+id/privacy_settings"
android:title="@string/privacy_settings"
android:fragment="com.android.settings.PrivacySettings"
android:icon="@drawable/ic_settings_backup"
/>
</dashboard-category>
<!-- SYSTEM -->
<dashboard-category
android:id="@+id/system_section"
android:title="@string/header_category_system" >
<!-- Date & Time -->
<dashboard-tile
android:id="@+id/date_time_settings"
android:title="@string/date_and_time_settings_title"
android:fragment="com.android.settings.DateTimeSettings"
android:icon="@drawable/ic_settings_date_time"
/>
<!--Scheduled power on&off-->
<dashboard-tile
android:id="@+id/power_settings"
android:icon="@drawable/ic_settings_schpwronoff"
android:title="@string/schedule_power_on_off_settings_title">
<intent android:action="com.android.settings.SCHEDULE_POWER_ON_OFF_SETTING" />
</dashboard-tile>
<!-- Accessibility feedback -->
<dashboard-tile
android:id="@+id/accessibility_settings"
android:title="@string/accessibility_settings"
android:fragment="com.android.settings.accessibility.AccessibilitySettings"
android:icon="@drawable/ic_settings_accessibility"
/>
<!-- Print -->
<dashboard-tile
android:id="@+id/print_settings"
android:title="@string/print_settings"
android:fragment="com.android.settings.print.PrintSettingsFragment"
android:icon="@drawable/ic_settings_print"
/>
<!-- Development -->
<dashboard-tile
android:id="@+id/development_settings"
android:title="@string/development_settings_title"
android:fragment="com.android.settings.DevelopmentSettings"
android:icon="@drawable/ic_settings_development"
/>
<!-- About Device -->
<dashboard-tile
android:id="@+id/about_settings"
android:title="@string/about_settings"
android:fragment="com.android.settings.DeviceInfoSettings"
android:icon="@drawable/ic_settings_about"
/>
</dashboard-category>
</dashboard-categories>
依据这个文件我们能够知道了,所谓的dashboard就是Settings模块首界面的一个抽象。而dashboard-categorys则是设置分类集合的抽象,而dashboard-category是分类的抽象,dashboard-tile就是分类下每个选项的抽象了。代码中的List相应dashboard-categorys, DashboardCategory相应dashboard-category。而dashboard-tile则对因代码中的DashboardTile。
当载入完这些对象后SettingsActivity会将得到的 mCategories 返回给DashboardSummary来初始化Settings的设置选项。
以下这段代码就是DashboardSummary.rebuildUI()中完毕界面的初始化
long start = System.currentTimeMillis();
final Resources res = getResources();
mDashboard.removeAllViews();
List<DashboardCategory> categories =
((SettingsActivity) context).getDashboardCategories(true);
final int count = categories.size();
for (int n = 0; n < count; n++) {
DashboardCategory category = categories.get(n);
View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard,
false);
TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
categoryLabel.setText(category.getTitle(res));
ViewGroup categoryContent =
(ViewGroup) categoryView.findViewById(R.id.category_content);
final int tilesCount = category.getTilesCount();
for (int i = 0; i < tilesCount; i++) {
DashboardTile tile = category.getTile(i);
DashboardTileView tileView = new DashboardTileView(context);
updateTileView(context, res, tile, tileView.getImageView(),
tileView.getTitleTextView(), tileView.getStatusTextView());
tileView.setTile(tile);
categoryContent.addView(tileView);
}
// Add the category
mDashboard.addView(categoryView);
}
这段代码我就不详细分析了,逻辑非常easy,遍历categories这个列表来获取DashboardCategory对象。将全部DashboardCategory对象和DashboardCategory对象中的DashboardTile对象转化为视图对象并加入到主视图对象mDashboard中。
到这里SettingsActivity的onCreate方法就算结束了。总结一下,
1.onCreate完毕的任务是切换DashboardSmmary这个Fragment,然后从dashboard_categories.xml中读取预先配置好的文件来初始化Settings的首界面视图。
2.L中舍弃了Header类,取而代之的是DashboardCategory和DashboardTile类。
Android 5.1 Settings源代码简要分析的更多相关文章
- Android日志系统Logcat源代码简要分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6606957 在前面两篇文章Android日志系 ...
- Android初级教程通过简要分析“土司”源码,来自实现定义土司理论探讨
由于系统自带的土司瞬间即逝,而且非常难看.因此我们就希望自定义自己的土司风格.有些实例就是基于自定义土司完成的,例如金山卫士的火箭发射,基本原理就是个土司.但是在做出自己的土司风格之前,还是要简要分析 ...
- 《Android系统源代码情景分析》连载回忆录:灵感之源
上个月,在花了一年半时间之后,写了55篇文章,分析完成了Chromium在Android上的实现,以及Android基于Chromium实现的WebView.学到了很多东西,不过也挺累的,平均不到两个 ...
- Android Hal层简要分析
Android Hal层简要分析 Android Hal层(即 Hardware Abstraction Layer)是Google开发的Android系统里上层应用对底层硬件操作屏蔽的一个软件层次, ...
- Android `AsyncTask`简要分析
AsyncTask简要分析 经典异步任务:AsyncTask,使用场景有:批量下载,批量拷贝等.官方文档就直接给出了一个批量下载的示例. private class DownloadFilesTask ...
- FFmpeg源代码简单分析:configure
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- Android 4.4 全套源代码及子模块源代码的下载方法
博文<Android源代码下载--用git clone实现单个文件夹下载>介绍了採用git clone方法下载Android单个文件夹源代码的方法,这篇文章已经有四年的历史,这期间Goog ...
- Activity源码简要分析总结
Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...
- android 常见死机问题--log分析
http://blog.csdn.net/fangchongbory/article/details/7645815 android 常见死机问题--log分析============ ...
随机推荐
- Method Dispatch in Protocol Extensions
We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different d ...
- day24-1 元类
目录 元类 类的组成 内置函数 exec() class关键字创建类原理 自定义元类控制类的创建 自定义元类控制类实例化 自定义元类后对象属性查找顺序 元类 在python中一切皆对象,name我们用 ...
- 最后一个非零数字(POJ 1604、POJ 1150、POJ 3406)
POJ中有些问题给出了一个长数字序列(即序列中的数字非常多),这个长数字序列的生成有一定的规律,要求求出这个长数字序列中某个位上的数字是多少.这种问题通过分析,找出规律就容易解决. 例如,N!是一个非 ...
- Eclipse使用入门指南及技巧
Java是必须的 安装一个JDK就可以了,比如jdk-6u39-windows-x64.exe,安装完毕,会自行安装JRE. 如果不用IDE,这个时候用记事本也是可以写程序,然后用javac编译, ...
- JAVA基础——设计模式之简单工厂模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简单工厂模式是由一个工厂 ...
- Chrome插件:本地程序实现验证码破解(浏览器与本地进程通信)
利用chrome调用本地程序破解图片验证码background.js var port = null, tabId, fname = "vcode.chrome.v1.item.01&quo ...
- 用js 转化大小写
function capitalize(string){ var words =string.split(" "); for(var i=0;i<words.length;i ...
- php 日常问题
1.isset.empty.is_null的区别 isset 判断变量是否定义或者是否为空 变量存在返回ture,否则返回false 变量定义不赋值返回false unset一个变量,返回false ...
- Python之数字
Python之数字 int(数字)===>在Python3中,int没有范围,在Python2中,int超出范围就叫长整型(Long). 浮点运算:单精度 float 双精度 double a: ...
- LeetCode(60) Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...