Brief Analysis of Android 5.0 Settings source code

Source: Internet
Author: User

Brief Analysis of Android 5.0 Settings source code
Overview:

First declare:I have been working for almost two years. I am still at the cainiao level. Sorry! It was a pity that many knowledge points were not recorded in the past. Now we have the opportunity to access the Android source code. I think we have to write something about Setting in an Android group. After all, we can only get started with Setting. Now we can only read the code for a while. Let's record some of the gains first. If we say more, It's just tears ~ This article provides a brief analysis of the normal startup process of the Settings module on platform L, and tries to analyze the implementation of an option under Settings.

Setting Introduction

On the previous KK platform, the first Activity of the Settings module is "Settings", which inherits PreferenceActivity. Each of the Settings is a corresponding Header object, the Header object allows you to display switch controls, button controls, and checkbox controls. For example, 1.1, a switch is used on WLAN and Bluetooth. But on L, the WLAN and Bluetooth switches have been removed. 1.2, the first page in the Settings module seems to be just a normal Listview, but does it use a Header? Or what is replaced? Let's continue ~


Figure-1.1

Figure-1.2

L initial interface initialization process of the Settings Module

L The first interface of the Settings module is Settings, inherited from SettingsActivity, and SettingsActivity inherited from Activity.

First, let's take a look at the Settings. java code and find that it does not override any SettingsActiviy method, nor does it add any of its own methods, but adds many static internal classes, such:

/** * 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 */ }    ...    }

As you can see from the annotations, these subclasses are created to start specific independent Settings options. For example, to set wireless in an application, you only need to start WirelessSettingsActivity.

So the Startup Process of the Settings module can be viewed directly by SettingsActiviy.
1.SettingsActivity. onCreate Method
The onCreate method is the first step in the Activity lifecycle. Let's see what SettingsActivity has done here?

 // Should happen before any call to getIntent()     getMetaData();

This method is used to obtain the additional data mFragmentClass of the Activity. If this data can be obtained, the Activity corresponding to the mFragmentClass will be displayed below. You cannot directly start the Settings module to obtain this data.

     mIsShowingDashboard = className.equals(Settings.class.getName());

This step is very important, because we came from the Activity Settings, so here the mIsShowingDashboard is 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);

This judgment is very important, but it is obvious that the value of isSubSettings is fasle, which is ignored temporarily.

  setContentView(mIsShowingDashboard ?                R.layout.settings_main_dashboard : R.layout.settings_main_prefs);

We know that the mIsShowingDashboard here is true, so the layout file used here is R. layout. settings_main_dashboard. The settings_main_dashboard.xml file is as follows:

<framelayout android:background="@color/dashboard_background_color" android:id="@+id/main_content" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"></framelayout>

Because mIsShowingDashboard is true, go directly to the following section.

  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);              }

Here we can see the switchToFragment method. We can know that we want to switch the Fragment of DashboardSummary.

Next let's take a look at what DashboardSummary is?

Dashboard is a dashboard in Chinese. Here, DashboardSummary is used to display all the Settings options.

The layout file R. layout. dashboard is loaded in onCreateView of DashboardSummary.

<code class=" hljs perl"><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"></linearlayout></scrollview></code>

After reading the layout File above, we can see that the option view of Settings should be displayed in dashboard_container.



After the onCreateView method is completed, DashboardSummary will go through onResume and then be transferred to the SettingsActivity

loadCategoriesFromResource(R.xml.dashboard_categories, categories);

This step uses R. xml. dashboard_categories to load categories. The categorys here is ArrayList mCategories. Next, let's take a look at the dashboard_categories.xml file.


  
  
      
       
   
    
Vcnlfd2lyZWxlc3NfbmV0d29ya3M = ">
            
            
             
                 
       
      
             
             
             
              
       
        
        
         
         
          
          
           
          
          
          
          
         
        
       
      
     
    
       
       
           
            
            
             
             
              
       
        
        
         
         
          
          
           
           
            
            
             
             
              
             
             
           
          
         
        
       
      
     
    
       
       
           
            
            
             
             
              
       
        
        
         
         
        
       
      
     
    
       
       
           
            
            
             
                 
       
      
             
             
             
              
       
        
        
         
         
        
       
      
     
    
   
  

We can see from this file that the so-called dashboard is an abstraction at the beginning of the Settings module. Dashboard-categorys sets the abstraction of classification sets, while dashboard-category is the abstraction of classification, and dashboard-tile is the abstraction of each option under classification. The List in the Code corresponds to dashboard-categorys, and DashboardCategory corresponds to dashboard-category, while dashboard-tile corresponds to DashboardTile in the code.

After these objects are loaded, SettingsActivity returns the obtained mCategories to DashboardSummary to initialize Settings.

The following code initializes the interface in DashboardSummary. rebuildUI ().

   long start = System.currentTimeMillis();        final Resources res = getResources();        mDashboard.removeAllViews();        List
  
    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);        }
  

I will not analyze this Code. The logic is very simple. traverse the list of categories to obtain the DashboardCategory object, convert all DashboardCategory objects and DashboardCategory objects into view objects and add them to the mDashboard of the main view object.

Here, the onCreate method of SettingsActivity is finished. To sum up,

1. The onCreate task is to switch the Fragment of DashboardSmmary, and then read the pre-configured file from dashboard_categories.xml to initialize the first interface view of Settings.
2. L removes the Header class and replaces it with the DashboardCategory and DashboardTile classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.