• 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.users;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.car.userlib.CarUserManagerHelper;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.pm.UserInfo;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 
32 /**
33  * Common setup of all preference controllers related to user details.
34  *
35  * @param <V> the upper bound on the type of {@link Preference} on which the controller
36  *            expects to operate.
37  */
38 public abstract class UserDetailsBasePreferenceController<V extends Preference> extends
39         PreferenceController<V> {
40 
41     private final CarUserManagerHelper mCarUserManagerHelper;
42     private UserInfo mUserInfo;
43 
44     private final BroadcastReceiver mUserUpdateReceiver = new BroadcastReceiver() {
45         @Override
46         public void onReceive(Context context, Intent intent) {
47             refreshUserInfo();
48             refreshUi();
49         }
50     };
51 
UserDetailsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)52     public UserDetailsBasePreferenceController(Context context, String preferenceKey,
53             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
54         super(context, preferenceKey, fragmentController, uxRestrictions);
55         mCarUserManagerHelper = new CarUserManagerHelper(getContext());
56     }
57 
58     /** Sets the user info for which this preference controller operates. */
setUserInfo(UserInfo userInfo)59     public void setUserInfo(UserInfo userInfo) {
60         mUserInfo = userInfo;
61     }
62 
63     /** Gets the current user info. */
getUserInfo()64     public UserInfo getUserInfo() {
65         return mUserInfo;
66     }
67 
68     /** Refreshes the user info, since it might have changed. */
refreshUserInfo()69     protected void refreshUserInfo() {
70         mUserInfo = UserUtils.getUserInfo(getContext(), mUserInfo.id);
71     }
72 
73     @Override
checkInitialized()74     protected void checkInitialized() {
75         if (mUserInfo == null) {
76             throw new IllegalStateException("UserInfo should be non-null by this point");
77         }
78     }
79 
80     /** Registers a listener which updates the displayed user name when a user is modified. */
81     @Override
onCreateInternal()82     protected void onCreateInternal() {
83         registerForUserEvents();
84     }
85 
86     /** Unregisters a listener which updates the displayed user name when a user is modified. */
87     @Override
onDestroyInternal()88     protected void onDestroyInternal() {
89         unregisterForUserEvents();
90     }
91 
registerForUserEvents()92     private void registerForUserEvents() {
93         IntentFilter filter = new IntentFilter(Intent.ACTION_USER_INFO_CHANGED);
94         getContext().registerReceiver(mUserUpdateReceiver, filter);
95     }
96 
unregisterForUserEvents()97     private void unregisterForUserEvents() {
98         getContext().unregisterReceiver(mUserUpdateReceiver);
99     }
100 
101     /** Gets the car user manager helper. */
getCarUserManagerHelper()102     protected CarUserManagerHelper getCarUserManagerHelper() {
103         return mCarUserManagerHelper;
104     }
105 }
106