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.VisibleForTesting;
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 profile 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 ProfileDetailsBasePreferenceController<V extends Preference> extends
39         PreferenceController<V> {
40 
41     private UserInfo mUserInfo;
42     private boolean mNeedToRefreshUserInfo = false;
43 
44     @VisibleForTesting
45     final BroadcastReceiver mProfileUpdateReceiver = new BroadcastReceiver() {
46         @Override
47         public void onReceive(Context context, Intent intent) {
48             refreshUserInfo();
49             refreshUi();
50         }
51     };
52 
ProfileDetailsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)53     public ProfileDetailsBasePreferenceController(Context context, String preferenceKey,
54             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
55         super(context, preferenceKey, fragmentController, uxRestrictions);
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 = ProfileUtils.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 profile name when a profile is modified. */
81     @Override
onStartInternal()82     protected void onStartInternal() {
83         registerForProfileEvents();
84 
85         /* refresh UserInfo and UI only when restarting */
86         if (mNeedToRefreshUserInfo) {
87             refreshUserInfo();
88             refreshUi();
89         }
90     }
91 
92     /**
93      * Unregisters a listener which updates the displayed profile name when a profile is modified.
94      */
95     @Override
onStopInternal()96     protected void onStopInternal() {
97         unregisterForProfileEvents();
98         mNeedToRefreshUserInfo = true;
99     }
100 
registerForProfileEvents()101     private void registerForProfileEvents() {
102         IntentFilter filter = new IntentFilter(Intent.ACTION_USER_INFO_CHANGED);
103         getContext().registerReceiver(mProfileUpdateReceiver, filter);
104     }
105 
unregisterForProfileEvents()106     private void unregisterForProfileEvents() {
107         getContext().unregisterReceiver(mProfileUpdateReceiver);
108     }
109 }
110