Android Q - Settings 页面添加一级菜单

Android Q - Settings页面添加二级选项_感觉不怎么会的博客-CSDN博客

本次修改基本同上一篇文章,在一级菜单中的最后添加 OTG 选项,简单记录下。

文件清单,依旧位于 packages/apps/Settings :

  • res/xml/top_level_settings.xml
  • res/xml/otg_settings.xml
  • src/com/android/settings/otg/OTGPreferenceController.java
  • src/com/android/settings/otg/OTGSettingFragment.java
  • src/com/android/settings/otg/TopLevelOTGPreferenceController.java

下面简单说明。

1、修改布局文件

路径:res/xml/top_level_settings.xml

说明:这个文件就是打开 Settings 应用的首页

...
    <Preference
        android:key="top_level_otg"
        android:summary="otg"
        android:title="@string/settings_otg_tittle"
        android:order="150"
        android:icon="@drawable/ic_homepage_otg"
        android:fragment="com.android.settings.otg.OTGSettingFragment"
        settings:controller="com.android.settings.otg.TopLevelOTGPreferenceController"/>

</PreferenceScreen>

路径:res/xml/otg_settings.xml

说明:进入 OTG 菜单后的布局文件,领导只说需要一个开关选项,功能实现可仿照上一篇文章实现。


<?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="otg_setting"
    android:title="@string/settings_otg_tittle"
    settings:initialExpandedChildrenCount="1">
    <!--lichang add OTG-->
    <com.android.settingslib.RestrictedSwitchPreference
        android:key="usb_otg"
        android:title="@string/settings_otg_text"
        android:summary="@string/settings_otg_summary"
        android:order="-25"
        settings:controller="com.android.settings.otg.OTGPreferenceController"
        />
</PreferenceScreen>

 2、功能实现

路径:src/com/android/settings/otg/TopLevelOTGPreferenceController.java
说明:菜单选项


package com.android.settings.otg;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class TopLevelOTGPreferenceController extends BasePreferenceController {
    public TopLevelOTGPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }
    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
    @Override
    public CharSequence getSummary() {
        return mContext.getString(R.string.settings_otg_text);
    }
}

路径:src/com/android/settings/otg/OTGSettingFragment.java

说明:这个就是点击一级菜单后进入的页面,加载二级页面。(有的参数不太清楚怎么添加,清楚之后再更新)


/*
 * 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.otg;
import static com.android.settings.network.MobilePlanPreferenceController.MANAGE_MOBILE_PLAN_DIALOG_ID;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.development.featureflags.FeatureFlagPersistent;
import com.android.settings.network.MobilePlanPreferenceController.MobilePlanPreferenceHost;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.wifi.WifiMasterSwitchPreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import com.android.settings.deviceinfo.BuildNumberPreferenceController;
import com.android.settings.otg.OTGPreferenceController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SearchIndexable
public class OTGSettingFragment extends DashboardFragment {
    private static final String TAG = "OTGDashboardFrag";
    public static final String ARG_SHOW_EXPAND_BUTTON = "showExpandButton";
    public static final String EXTRA_SHOW_AWARE_DISABLED = "show_aware_dialog_disabled";
    private static final int COUNT_INVALID = -1;
    private Fragment mFragment;
    /*UNISOC: bug for 966475 @{*/
    private AlertDialog mMobilePlanDialog;
    /*UNISOC: @}*/
    private int mChildrenCount = COUNT_INVALID;
    @Override
    public int getMetricsCategory() {
        //应该是这种格式的 SettingsEnums.SETTINGS_NETWORK_CATEGORY;
        //但是我不知道如何添加,暂时默认这个数值吧~
        return 9232;
    }
    @Override
    protected String getLogTag() {
        return TAG;
    }
    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.otg_settings;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        /*UNISOC: Modify for bug1145683, We do not want to display an advanced button if
         * linking to this screen from condition @{*/
        Bundle args = getArguments();
        if (args != null) {
            if (!args.getBoolean(ARG_SHOW_EXPAND_BUTTON, true)) {
                mChildrenCount = Integer.MAX_VALUE;
            }
        }
        use(OTGPreferenceController.class).setFragment(this);
    }
    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getSettingsLifecycle(),
                this /* fragment */);
    }
    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
                                                                                 Lifecycle lifecycle, Fragment fragment) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        return controllers;
    }
}

路径:src/com/android/settings/otg/OTGPreferenceController.java

说明:这个基本同上一篇文章中的以太网开关控制一样,甚至比之前写的更少。


/*
 * 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.otg;
import android.os.Bundle;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.utils.ThreadPool;
import com.android.settings.utils.ReflectUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class OTGPreferenceController extends TogglePreferenceController {
    private static final String TAG = "OTGPreferenceController";
    public OTGPreferenceController(Context context, String key) {
        super(context, key);
    }
    private Fragment mFragment;
    private SwitchPreference mOTGPreference;
    private SharedPreferences mSharedPreference;
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        android.util.Log.d(TAG, "handlePreferenceTreeClick: 測試" + preference.getKey());
        changeState(!isChecked());
        return false;
    }
    @Override
    @AvailabilityStatus
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        android.util.Log.d(TAG, "displayPreference: " );
        if (isAvailable()) {
            mOTGPreference = screen.findPreference(getPreferenceKey());
            if (isChecked())
                mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_open));
            else
                mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_close));
            mOTGPreference.setChecked(isChecked());
        }
    }
    public void init(DashboardFragment fragment) {
        final Context context = fragment.getContext();
    }
    public void setFragment(Fragment hostFragment) {
        mFragment = hostFragment;
    }
    @Override
    public boolean setChecked(boolean isChecked) {
        android.util.Log.d(TAG, "setChecked: " + isChecked);
        return false;
    }
    public void changeState(boolean state) {
        android.util.Log.d(TAG, "changeState to be = " + state);
        if (state) {
            mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_open));
            mOTGPreference.setChecked(true);
            ReflectUtil.setProperty("persist.flyscale.otg", "1");
        } else {
            mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_close));
            mOTGPreference.setChecked(false);
            ReflectUtil.setProperty("persist.flyscale.otg", "0");
        }
        android.util.Log.d(TAG, "changeState: currentState is = " + isChecked());
    }
    @Override
    public boolean isChecked() {
        return TextUtils.equals(ReflectUtil.getProperty("persist.flyscale.otg", "0"), "1");
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值