MTK平台 Android11 MtkSettings简介

Posted 伟雪无痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MTK平台 Android11 MtkSettings简介相关的知识,希望对你有一定的参考价值。

一.MtkSettings启动逻辑

1.进入MtkSettings的androidManifest.xml,找启动Activity,activity-alias是标签别名,真正的启动是targetActivity中配置的Settings类

 <activity android:name="Settings"
                  android:label="@string/settings_label_launcher"
                  
                  android:taskAffinity="com.android.settings.root"
                  android:launchMode="singleTask"
                  android:configChanges="keyboard|keyboardHidden">
            <intent-filter android:priority="1">
                <action android:name="android.settings.SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
                       android:value="true" />
        </activity>
        
        <!-- Alias for launcher activity only, as this belongs to each profile. -->
        <activity-alias android:name="Settings"
                android:label="@string/settings_label_launcher"
                android:taskAffinity="com.android.settings.root"
                android:launchMode="singleTask"
                android:targetActivity="Settings">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
        </activity-alias>

2.Settings.java,Settings->SettingsActivity,Settings继承自SettingsActivity,里面所有的类都只是定义,没有实现,eg:

public class Settings extends SettingsActivity 

    /*
    * Settings subclasses for launching independently.
    */
    public static class AssistGestureSettingsActivity extends SettingsActivity  /* empty */
    public static class BluetoothSettingsActivity extends SettingsActivity  /* empty */ 
    public static class CreateShortcutActivity extends SettingsActivity  /* empty */ 
    public static class FaceSettingsActivity extends SettingsActivity  /* empty */ 
    public static class FingerprintSettingsActivity extends SettingsActivity  /* empty */ 
    public static class TetherSettingsActivity extends SettingsActivity 

3. SettingsActivity.java,查看SettingsActivity中的onCreate方法,第一次启动进入launchSettingFragment方法,这里初始化相关的Fragment,具体其他的细节可参考源码

  @Override
    protected void onCreate(Bundle savedState) 
        super.onCreate(savedState);
        Log.d(LOG_TAG, "Starting onCreate");
        long startTime = System.currentTimeMillis();

        final FeatureFactory factory = FeatureFactory.getFactory(this);

        mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);

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

        final Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_UI_OPTIONS)) 
            getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
        

        // Getting Intent properties can only be done after the super.onCreate(...)
        final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);

        // 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 = this instanceof SubSettings ||
                intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);

        // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
        // insets.
        // If this is in setup flow, don't apply theme. Because light theme needs to be applied
        // in SettingsBaseActivity#onCreate().
        if (isSubSettings && !WizardManagerHelper.isAnySetupWizard(getIntent())) 
            setTheme(R.style.Theme_SubSettings);
        

        setContentView(R.layout.settings_main_prefs);

        v188HeaderTextEmporia = (V188HeaderTextEmporia) findViewById(R.id.settings_header_text);
        getSupportFragmentManager().addOnBackStackChangedListener(this);

        if (savedState != null) 
            // We are restarting from a previous saved state; used that to initialize, instead
            // of starting fresh.
            setTitleFromIntent(intent);

            ArrayList<DashboardCategory> categories =
                    savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
            if (categories != null) 
                mCategories.clear();
                mCategories.addAll(categories);
                setTitleFromBackStack();
            
         else 
            launchSettingFragment(initialFragmentName, intent);
        
@VisibleForTesting
    void launchSettingFragment(String initialFragmentName, Intent intent) 
        if (initialFragmentName != null) 
            setTitleFromIntent(intent);

            Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
            switchToFragment(initialFragmentName, initialArguments, true,
                    mInitialTitleResId, mInitialTitle);
         else 
            // Show search icon as up affordance if we are displaying the main Dashboard
            mInitialTitleResId = R.string.dashboard_title;
            switchToFragment(TopLevelSettings.class.getName(), null /* args */, false,
                    mInitialTitleResId, mInitialTitle);
        
    

/**
     * Switch to a specific Fragment with taking care of validation, Title and BackStack
     */
    private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
            int titleResId, CharSequence title) 
        Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
        if (validate && !isValidFragment(fragmentName)) 
            throw new IllegalArgumentException("Invalid fragment for this activity: "
                    + fragmentName);
        
        Fragment f = Utils.getTargetFragment(this, fragmentName, args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_content, f);
        if (titleResId > 0) 
            transaction.setBreadCrumbTitle(titleResId);
         else if (title != null) 
            transaction.setBreadCrumbTitle(title);
        
        transaction.commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();
        Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
        return f;
    

二.MtkSettings一二级菜单添加的方法

1.MtkSettings所在的目录位置

/alps/vendor/mediatek/proprietary/packages/apps/MtkSettings

2.MtkSettings常用菜单一二级效果图

3.MtkSettings一级菜单top_level_settings.xml布局介绍

1‘.布局文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  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.
  -->

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="top_level_settings">

     <!--网络和互联网-->
     <Preference
        android:key="top_level_network"
        android:title="@string/network_dashboard_title"
        android:summary="@string/summary_placeholder"	
	    android:layout="@layout/dashboard_tile"
        android:order="-120"
        android:fragment="com.android.settings.network.NetworkDashboardFragment"
        settings:controller="com.android.settings.network.TopLevelNetworkEntryPreferenceController"/>

    <!--已连接的设备-->
    <Preference
        android:key="top_level_connected_devices"
        android:title="@string/connected_devices_dashboard_title"
        android:summary="@string/summary_placeholder"
        android:icon="@drawable/ic_homepage_connected_device"
	    android:layout="@layout/dashboard_tile"
        android:order="-110"
        android:fragment="com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment"
        settings:controller="com.android.settings.connecteddevice.TopLevelConnectedDevicesPreferenceController"/>

    <!--应用和通知-->
    <Preference
        android:key="top_level_apps_and_notifs"
        android:title="@string/app_and_notification_dashboard_title"
        android:summary="@string/app_and_notification_dashboard_summary"
        android:icon="@drawable/ic_homepage_apps"
	    android:layout="@layout/dashboard_tile"
        android:order="-100"
        android:fragment="com.android.settings.applications.AppAndNotificationDashboardFragment"/>

    <!--电池-->
    <Preference
        android:key="top_level_battery"
        android:title="@string/power_usage_summary_title"
        android:summary="@string/summary_placeholder"
        android:icon="@drawable/ic_homepage_battery"
        android:fragment="com.android.settings.fuelgauge.PowerUsageSummary"
	    android:layout="@layout/dashboard_tile"
        android:order="-90"
        settings:controller="com.android.settings.fuelgauge.TopLevelBatteryPreferenceController"/>
    
    <Preference
        android:key="date_time_settings"
        android:title="@string/date_and_time"
	    android:layout="@layout/dashboard_tile"
        android:order="-60"
        android:fragment="com.android.settings.datetime.DateTimeSettings"/>	

    <Preference
        android:key="language_input_settings"
        android:title="@string/language_settings"
	android:layout="@layout/dashboard_tile"	
        android:order="-55"
        android:fragment="com.android.settings.language.LanguageAndInputSettings"/>

    <!--存储-->
     <Preference
        android:key="top_level_storage"
        android:title="@string/storage_settings"
        android:summary="@string/summary_placeholder"
        android:icon="@drawable/ic_homepage_storage"
	    android:layout="@layout/dashboard_tile"
        android:order="-60"
        android:fragment="com.android.settings.deviceinfo.StorageSettings"
        settings:controller="com.android.settings.deviceinfo.TopLevelStoragePreferenceController"/>

    <!--隐私权-->
    <Preference
        android:key="top_level_privacy"
        android:title="@string/privacy_dashboard_title"
        android:summary="@string/privacy_dashboard_summary"
        android:icon="@drawable/ic_homepage_privacy"
	    android:layout="@layout/dashboard_tile"	
        android:order="-55"
        android:fragment="com.android.settings.privacy.PrivacyDashboardFragment"/>

    <!--位置信息-->
    <Preference
        android:key="top_level_location"
        android:title="@string/location_settings_title"
        android:summary="@string/location_settings_loading_app_permission_stats"
        android:icon="@drawable/ic_homepage_location"
        android:order="-50"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.location.LocationSettings"
        settings:controller="com.android.settings.location.TopLevelLocationPreferenceController"/>

    <!--安全-->
    <Preference
        android:key="top_level_security"
        android:title="@string/security_settings_title"
        android:summary="@string/summary_placeholder"
        android:icon="@drawable/ic_homepage_security"
        android:order="-40"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.security.SecuritySettings"
        settings:controller="com.android.settings.security.TopLevelSecurityEntryPreferenceController"/>

    <!--账号-->
    <Preference
        android:key="top_level_accounts"
        android:title="@string/account_dashboard_title"
        android:summary="@string/summary_placeholder"
        android:icon="@drawable/ic_homepage_accounts"
        android:order="-30"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.accounts.AccountDashboardFragment"
        settings:controller="com.android.settings.accounts.TopLevelAccountEntryPreferenceController"/>

    <!--无障碍-->
    <Preference
        android:key="top_level_accessibility"
        android:title="@string/accessibility_settings"
        android:summary="@string/accessibility_settings_summary"
        android:icon="@drawable/ic_homepage_accessibility"
        android:order="-20"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.accessibility.AccessibilitySettings"
        settings:controller="com.android.settings.accessibility.TopLevelAccessibilityPreferenceController"/>

    <!--系统-->
    <Preference
        android:key="top_level_system"
        android:title="@string/header_category_system"
        android:summary="@string/summary_placeholder"
        android:order="10"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.system.SystemDashboardFragment"/>

    <!--恢复出厂设置 -->
    <Preference
        android:key="factory_reset"
        android:title="@string/master_clear_title"
        settings:keywords="@string/keywords_factory_data_reset"
        settings:userRestriction="no_factory_reset"
        settings:useAdminDisabledSummary="true"
	    android:layout="@layout/dashboard_tile"	
	    android:order="15"	
        android:fragment="com.android.settings.MasterClear" />

    <!--关于手机 -->
    <Preference
        android:key="top_level_about_device"
        android:title="@string/about_settings"
        android:order="20"
	    android:layout="@layout/dashboard_tile"	
        android:fragment="com.android.settings.deviceinfo.aboutphone.MyDeviceInfoFragment"/>
    

</PreferenceScreen>

2‘.Preference元素的通用XML属性

android:key  //唯一标识,SharedPreferences也将通过此Key值进行数据保存,也可以通过key值获取保存的信息
android:defaultValue //默认值, eg: CheckPreference的默认值可为"true",默认为选中状态
android:enabled  //表示该Preference是否可用状态
android:title //每个Preference在PreferenceScreen布局上显示的标题——大标题
android:summary //每个Preference在PreferenceScreen布局上显示的概述——小标题(可以没有)
android:persistent //表示Preference元素所对应的值是否写入sharedPreferen文件中,如果是true,则表示写入;否则,则表示不写入该Preference元素的值。
android:layout //preferenceactivity的偏好布局,用于填充view
android:dependency //表示一个Preference(用A表示)的可用状态依赖另外一个Preference(用B表示,相当于是父类),B可用,则A可用;B不可用,则A不可用。
android:disableDependentsState //与android:dependency相反。B可用,则A不可用;B不可用,则A可用
android:icon //偏好图标,对于偏好选择图标必须是一个引用到另一个资源的形式或以packagetypename主题属性
android:order //表示偏好顺序,值越大,优先级越高,菜单越靠前
android:widgetLayout //可以做小部部件部分的布局
android:selectable //偏好是否是可选的
android:fragment //使用时,可以碎片化PreferenceActivity

3’.Preference配置注意项

配置文件Preference时,order优先级控制一级菜单显示,优先级值越大,优先级越高,菜单越靠前,如果需要增加一级菜单,则需要在top_level_settings.xml中添加对应的Preference项即可 

4'.MtkSettings一级菜单Preference点击跳转事件,添加key, "mobile_network_settings"

1’'.给 Preference 指定 Intent 属性,这里可以分为 action 和 targetPackage+targetClass 两种方式,eg:

<Preference
        android:key="mobile_network_settings"
        android:title="title1"
        android:summary="summary1"
        android:icon="@drawable/ic_homepage_network"
        android:order="25">
        <intent android:action="com.example.test.mobile"/>
    </Preference>


    <Preference
        android:key="mobile_network_settings"
        android:title="title2"
        android:summary="summary2"
        android:icon="@drawable/ic_homepage_network"
        android:order="30">
        <intent
            android:targetPackage="com.example.test"
            android:targetClass="com.example.test.mobile.MainActivity" />
    </Preference>

2''.DashboardFragment.java中onPreferenceTreeClick拦截点击事件处理,包名:(com.android.settings.dashboard),继承关系为:

DashboardFragment->SettingsPreferenceFragment->InstrumentedPreferenceFragment->PreferenceFragmentCompat,eg:拦截key为mobile_network_settings的事件

@Override
    public boolean onPreferenceTreeClick(Preference preference) 
        if(preference.getKey().equals("mobile_network_settings"))
            Toast.makeText(mContext,R.string.mobile_insert_sim_card,Toast.LENGTH_LONG).show();
            return true;
        

        final Collection<List<AbstractPreferenceController>> controllers =
                mPreferenceControllers.values();
        for (List<AbstractPreferenceController> controllerList : controllers) 
            for (AbstractPreferenceController controller : controllerList) 
                if (controller.handlePreferenceTreeClick(preference)) 
                    // log here since calling super.onPreferenceTreeClick will be skipped
                    writePreferenceClickMetric(preference);
                    return true;
                
            
        


        return super.onPreferenceTreeClick(preference);
    

4.MtkSettings二级菜单添加的方法,这里以display为例

1'.找到对应的二级菜单xml,display_settings.xml

2'.添加或者隐藏相应的preference菜单

<?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="display_settings_screen"
    android:title="@string/display_settings"
    settings:keywords="@string/keywords_display"
    settings:initialExpandedChildrenCount="5">

  
      <!--M: Add MiraVision Settings entrance -->
    <Preference
        android:key="mira_vision"
        android:title="@string/mira_vision_title"
        android:summary="@string/mira_vision_summary">
        <intent android:action="com.android.settings.MIRA_VISION" />
    </Preference>
  
    <com.android.settingslib.RestrictedPreference
        android:key="brightness"
        android:title="@string/brightness"
        settings:keywords="@string/keywords_display_brightness_level"
        settings:useAdminDisabledSummary="true"
        settings:userRestriction="no_config_brightness">
        <intent android:action="com.android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
    </com.android.settingslib.RestrictedPreference>
    
    <com.android.settings.display.darkmode.DarkModePreference
        android:key="dark_ui_mode"
        android:title="@string/dark_ui_mode"
        android:fragment="com.android.settings.display.darkmode.DarkModeSettingsFragment"
        android:widgetLayout="@null"
        settings:widgetLayout="@null"
        settings:controller="com.android.settings.display.DarkUIPreferenceController"
        settings:keywords="@string/keywords_dark_ui_mode"/>

    <com.android.settings.display.NightDisplayPreference
        android:key="night_display"
        android:title="@string/night_display_title"
        android:fragment="com.android.settings.display.NightDisplaySettings"
        android:widgetLayout="@null"
        settings:widgetLayout="@null"/>

    <!-- Cross-listed item, if you change this, also change it in power_usage_summary.xml -->
    <Preference
        android:key="auto_brightness_entry"
        android:title="@string/auto_brightness_title"
        android:summary="@string/summary_placeholder"
        android:fragment="com.android.settings.display.AutoBrightnessSettings"
        settings:controller="com.android.settings.display.AutoBrightnessPreferenceController"/>

    <com.android.settingslib.RestrictedPreference
        android:key="wallpaper"
        android:title="@string/wallpaper_settings_title"
        settings:keywords="@string/keywords_display_wallpaper"
        settings:useAdminDisabledSummary="true"
        settings:controller="com.android.settings.display.WallpaperPreferenceController">
    </com.android.settingslib.RestrictedPreference>

    <!-- Cross-listed item, if you change this, also change it in power_usage_summary.xml -->
    <com.android.settings.display.TimeoutListPreference
        android:key="screen_timeout"
        android:title="@string/screen_timeout"
        android:summary="@string/summary_placeholder"
        android:entries="@array/screen_timeout_entries"
        android:entryValues="@array/screen_timeout_values"
        settings:keywords="@string/keywords_screen_timeout" />

    <Preference
        android:key="adaptive_sleep_entry"
        android:title="@string/adaptive_sleep_title"
        android:summary="@string/summary_placeholder"
        android:fragment="com.android.settings.display.AdaptiveSleepSettings"
        settings:controller="com.android.settings.display.AdaptiveSleepPreferenceController" />

    <SwitchPreference
        android:key="auto_rotate"
        android:title="@string/accelerometer_title"
        settings:keywords="@string/keywords_auto_rotate"
        settings:controller="com.android.settings.display.AutoRotatePreferenceController" />

    <Preference
        android:key="color_mode"
        android:title="@string/color_mode_title"
        android:fragment="com.android.settings.display.ColorModePreferenceFragment"
        settings:controller="com.android.settings.display.ColorModePreferenceController"
        settings:keywords="@string/keywords_color_mode" />

    <SwitchPreference
        android:key="display_white_balance"
        android:title="@string/display_white_balance_title"
        android:summary="@string/display_white_balance_summary"
        settings:controller="com.android.settings.display.DisplayWhiteBalancePreferenceController" />

    <SwitchPreference
        android:key="peak_refresh_rate"
        android:title="@string/peak_refresh_rate_title"
        android:summary="@string/peak_refresh_rate_summary"
        settings:controller="com.android.settings.display.PeakRefreshRatePreferenceController" />

    <Preference
        android:key="font_size"
        android:title="@string/title_font_size"
        android:fragment="com.android.settings.display.ToggleFontSizePreferenceFragment"
        settings:controller="com.android.settings.display.FontSizePreferenceController" />

    <com.android.settings.display.ScreenZoomPreference
        android:key="display_settings_screen_zoom"
        android:title="@string/screen_zoom_title"
        android:fragment="com.android.settings.display.ScreenZoomSettings"/>

    <SwitchPreference
        android:key="show_operator_name"
        android:title="@string/show_operator_name_title"
        android:summary="@string/show_operator_name_summary" />

    <Preference
        android:key="screensaver"
        android:title="@string/screensaver_settings_title"
        android:fragment="com.android.settings.dream.DreamSettings" />

    
    <Preference
        android:key="lockscreen_from_display_settings"
        android:title="@string/lockscreen_settings_title"
        android:fragment="com.android.settings.security.LockscreenDashboardFragment"
        settings:keywords="@string/keywords_ambient_display_screen"
        settings:controller="com.android.settings.security.screenlock.LockScreenPreferenceController" />
    
	
	<!--M: Add Always on display switch -->
    <SwitchPreference
        android:key="always_on_display"
        android:title="@string/aod_title"
        android:summary="@string/aod_summary" />
		
    <SwitchPreference
        android:key="camera_gesture"
        android:title="@string/camera_gesture_title"
        android:summary="@string/camera_gesture_desc" />

    <SwitchPreference
        android:key="lift_to_wake"
        android:title="@string/lift_to_wake_title" />

    <SwitchPreference
        android:key="tap_to_wake"
        android:title="@string/tap_to_wake"
        android:summary="@string/tap_to_wake_summary" />

    <ListPreference
        android:key="theme"
        android:title="@string/device_theme"
        android:summary="@string/summary_placeholder" />

    <Preference
        android:key="vr_display_pref"
        android:title="@string/display_vr_pref_title"
        android:fragment="com.android.settings.display.VrDisplayPreferencePicker" />

    <!--M: Add Font Style -->
    <Preference
        android:key="font_setting"
        android:title="@string/font_setting_title" >
        <intent android:action="com.lovelyfonts.activity.mainTab" />
    </Preference>

    <!-- M: add for HDMI settings begin -->
    <Preference
        android:fragment="com.mediatek.hdmi.HdmiSettings"
        android:key="hdmi_settings"
        android:summary="@string/hdmi_settings_summary"
        android:title="@string/hdmi_settings" />
    <!-- M: MTK add for HDMI settings end -->
</PreferenceScreen>

以上是关于MTK平台 Android11 MtkSettings简介的主要内容,如果未能解决你的问题,请参考以下文章

Android系统之路(初识MTK) ------Android11.0添加Recents一键清除最近任务按钮

Android系统之路(初识MTK) ------Android11.0添加Recents一键清除最近任务按钮

Android系统之路(初识MTK) ------Android11.0添加Recents一键清除最近任务按钮

Android系统之路(初识MTK) ------Android11.0去掉Hotseat快捷栏的应用名称

Android系统之路(初识MTK) ------Android11.0去掉Hotseat快捷栏的应用名称

Android系统之路(初识MTK) ------Android11.0去掉Hotseat快捷栏的应用名称