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.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Bundle; 24 25 import androidx.annotation.XmlRes; 26 27 import com.android.car.settings.R; 28 import com.android.internal.annotations.VisibleForTesting; 29 30 /** 31 * Shows details for a user with the ability to remove user and edit current user. 32 */ 33 public class UserDetailsFragment extends UserDetailsBaseFragment { 34 35 private boolean mIsStarted; 36 37 /** Creates instance of UserDetailsFragment. */ newInstance(int userId)38 public static UserDetailsFragment newInstance(int userId) { 39 return (UserDetailsFragment) UserDetailsBaseFragment.addUserIdToFragmentArguments( 40 new UserDetailsFragment(), userId); 41 } 42 43 @VisibleForTesting 44 final BroadcastReceiver mUserUpdateReceiver = new BroadcastReceiver() { 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 // Update the user info value, as it may have changed. 48 refreshUserInfo(); 49 if (mIsStarted) { 50 // Update the text in the action bar when there is a user update. 51 getToolbar().setTitle(getTitleText()); 52 } 53 } 54 }; 55 56 @Override 57 @XmlRes getPreferenceScreenResId()58 protected int getPreferenceScreenResId() { 59 return R.xml.user_details_fragment; 60 } 61 62 @Override onAttach(Context context)63 public void onAttach(Context context) { 64 super.onAttach(context); 65 use(EditUserNameEntryPreferenceController.class, 66 R.string.pk_edit_user_name_entry).setUserInfo(getUserInfo()); 67 } 68 69 @Override onCreate(Bundle savedInstanceState)70 public void onCreate(Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 registerForUserEvents(); 73 } 74 75 @Override onStart()76 public void onStart() { 77 super.onStart(); 78 mIsStarted = true; 79 getToolbar().setTitle(getTitleText()); 80 } 81 82 @Override onStop()83 public void onStop() { 84 mIsStarted = false; 85 super.onStop(); 86 } 87 88 @Override onDestroy()89 public void onDestroy() { 90 unregisterForUserEvents(); 91 super.onDestroy(); 92 } 93 94 @Override getTitleText()95 protected String getTitleText() { 96 return UserUtils.getUserDisplayName(getContext(), getUserInfo()); 97 } 98 registerForUserEvents()99 private void registerForUserEvents() { 100 IntentFilter filter = new IntentFilter(Intent.ACTION_USER_INFO_CHANGED); 101 getContext().registerReceiver(mUserUpdateReceiver, filter); 102 } 103 unregisterForUserEvents()104 private void unregisterForUserEvents() { 105 getContext().unregisterReceiver(mUserUpdateReceiver); 106 } 107 } 108