• 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.profiles;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.UserInfo;
25 
26 import androidx.annotation.CallSuper;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceGroup;
29 
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.PreferenceController;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Objects;
36 
37 /** Shared business logic between {@link ProfilesListFragment} and
38  * {@link ChooseNewAdminFragment}.
39  */
40 public abstract class ProfilesBasePreferenceController extends
41         PreferenceController<PreferenceGroup> {
42 
43     private ProfilesPreferenceProvider mPreferenceProvider;
44     private List<Preference> mProfilesToDisplay = new ArrayList<>();
45 
46     private final BroadcastReceiver mProfileUpdateReceiver = new BroadcastReceiver() {
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             /** Update screen when profiles list is updated. */
50             refreshUi();
51         }
52     };
53 
ProfilesBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)54     public ProfilesBasePreferenceController(Context context, String preferenceKey,
55             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
56         super(context, preferenceKey, fragmentController, uxRestrictions);
57         ProfilesPreferenceProvider.ProfileClickListener profileClickListener = this::profileClicked;
58         mPreferenceProvider = new ProfilesPreferenceProvider(context, profileClickListener);
59     }
60 
61     @Override
getPreferenceType()62     protected Class<PreferenceGroup> getPreferenceType() {
63         return PreferenceGroup.class;
64     }
65 
66     /**
67      * Ensure that helper is set by the time onCreate is called. Register a listener to refresh
68      * screen on updates.
69      */
70     @Override
71     @CallSuper
onCreateInternal()72     protected void onCreateInternal() {
73         registerForProfileEvents();
74     }
75 
76     /** Unregister listener to refresh screen on updates. */
77     @Override
78     @CallSuper
onDestroyInternal()79     protected void onDestroyInternal() {
80         unregisterForProfileEvents();
81     }
82 
83     @Override
updateState(PreferenceGroup preferenceGroup)84     protected void updateState(PreferenceGroup preferenceGroup) {
85         List<Preference> newProfiles = mPreferenceProvider.createProfileList();
86         if (profileListsAreDifferent(mProfilesToDisplay, newProfiles)) {
87             mProfilesToDisplay = newProfiles;
88             preferenceGroup.removeAll();
89 
90             for (Preference preference : mProfilesToDisplay) {
91                 preferenceGroup.addPreference(preference);
92             }
93         }
94     }
95 
96     /** Handles the profile click on a preference for a certain profile. */
profileClicked(UserInfo userInfo)97     protected abstract void profileClicked(UserInfo userInfo);
98 
99 
100     /** Gets the preference provider to set additional arguments if necessary. */
getPreferenceProvider()101     protected ProfilesPreferenceProvider getPreferenceProvider() {
102         return mPreferenceProvider;
103     }
104 
getProfilesToDisplay()105     protected List<Preference> getProfilesToDisplay() {
106         return mProfilesToDisplay;
107     }
108 
profileListsAreDifferent(List<Preference> currentList, List<Preference> newList)109     private boolean profileListsAreDifferent(List<Preference> currentList,
110             List<Preference> newList) {
111         if (currentList.size() != newList.size()) {
112             return true;
113         }
114 
115         for (int i = 0; i < currentList.size(); i++) {
116             // Cannot use "compareTo" on preference, since it uses the order attribute to compare.
117             if (preferencesAreDifferent(currentList.get(i), newList.get(i))) {
118                 return true;
119             }
120         }
121 
122         return false;
123     }
124 
preferencesAreDifferent(Preference lhs, Preference rhs)125     private boolean preferencesAreDifferent(Preference lhs, Preference rhs) {
126         return !Objects.equals(lhs.getTitle(), rhs.getTitle())
127                 || !Objects.equals(lhs.getSummary(), rhs.getSummary());
128     }
129 
registerForProfileEvents()130     private void registerForProfileEvents() {
131         IntentFilter filter = new IntentFilter(Intent.ACTION_USER_INFO_CHANGED);
132         getContext().registerReceiver(mProfileUpdateReceiver, filter);
133     }
134 
unregisterForProfileEvents()135     private void unregisterForProfileEvents() {
136         getContext().unregisterReceiver(mProfileUpdateReceiver);
137     }
138 }
139