• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.settings.common;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceGroup;
27 
28 import java.util.Map;
29 
30 /**
31  * Injects preferences from other system applications at a placeholder location. The placeholder
32  * should be a {@link PreferenceGroup} which sets the controller attribute to the fully qualified
33  * name of this class. The preference should contain an intent which will be passed to
34  * {@link ExtraSettingsLoader#loadPreferences(Intent)}.
35  *
36  * <p>For example:
37  * <pre>{@code
38  * <PreferenceCategory
39  *     android:key="@string/pk_system_extra_settings"
40  *     android:title="@string/system_extra_settings_title"
41  *     settings:controller="com.android.settings.common.ExtraSettingsPreferenceController">
42  *     <intent android:action="com.android.settings.action.EXTRA_SETTINGS">
43  *         <extra android:name="com.android.settings.category"
44  *                android:value="com.android.settings.category.system"/>
45  *     </intent>
46  * </PreferenceCategory>
47  * }</pre>
48  *
49  * @see ExtraSettingsLoader
50  */
51 // TODO: investigate using SettingsLib Tiles.
52 public class ExtraSettingsPreferenceController extends PreferenceController<PreferenceGroup> {
53 
54     private ExtraSettingsLoader mExtraSettingsLoader;
55     private boolean mSettingsLoaded;
56 
ExtraSettingsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions restrictionInfo)57     public ExtraSettingsPreferenceController(Context context, String preferenceKey,
58             FragmentController fragmentController, CarUxRestrictions restrictionInfo) {
59         super(context, preferenceKey, fragmentController, restrictionInfo);
60         mExtraSettingsLoader = new ExtraSettingsLoader(context);
61     }
62 
63     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
setExtraSettingsLoader(ExtraSettingsLoader extraSettingsLoader)64     public void setExtraSettingsLoader(ExtraSettingsLoader extraSettingsLoader) {
65         mExtraSettingsLoader = extraSettingsLoader;
66     }
67 
68     @Override
getPreferenceType()69     protected Class<PreferenceGroup> getPreferenceType() {
70         return PreferenceGroup.class;
71     }
72 
73     @Override
updateState(PreferenceGroup preference)74     protected void updateState(PreferenceGroup preference) {
75         Map<Preference, Bundle> preferenceBundleMap = mExtraSettingsLoader.loadPreferences(
76                 preference.getIntent());
77         if (!mSettingsLoaded) {
78             addExtraSettings(preferenceBundleMap);
79             mSettingsLoaded = true;
80         }
81         preference.setVisible(preference.getPreferenceCount() > 0);
82     }
83 
84     /**
85      * Adds the extra settings from the system based on the intent that is passed in the preference
86      * group. All the preferences that resolve these intents will be added in the preference group.
87      *
88      * @param preferenceBundleMap a map of {@link Preference} and {@link Bundle} representing
89      * settings injected from system apps and their metadata.
90      */
addExtraSettings(Map<Preference, Bundle> preferenceBundleMap)91     protected void addExtraSettings(Map<Preference, Bundle> preferenceBundleMap) {
92         for (Preference setting : preferenceBundleMap.keySet()) {
93             getPreference().addPreference(setting);
94         }
95     }
96 }
97