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