1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.profiles; 17 18 import android.content.Intent; 19 import android.content.pm.UserInfo; 20 import android.os.Bundle; 21 import android.os.UserManager; 22 import android.provider.Settings; 23 import android.text.Editable; 24 import android.text.TextUtils; 25 import android.text.TextWatcher; 26 import android.view.View; 27 import android.widget.EditText; 28 29 import androidx.annotation.LayoutRes; 30 import androidx.annotation.StringRes; 31 32 import com.android.car.settings.R; 33 import com.android.car.settings.common.BaseFragment; 34 import com.android.car.ui.toolbar.MenuItem; 35 import com.android.internal.annotations.VisibleForTesting; 36 37 import java.util.Collections; 38 import java.util.List; 39 40 /** 41 * Enables user to edit their profile name. 42 */ 43 public class EditProfileNameFragment extends BaseFragment { 44 45 private static final String PROFILE_NAME_SET = "profile_name_set"; 46 47 private UserInfo mUserInfo; 48 49 private EditText mProfileNameEditText; 50 @VisibleForTesting UserManager mUserManager; 51 private MenuItem mSaveButton; 52 53 /** 54 * Creates instance of EditProfileNameFragment. 55 */ newInstance(UserInfo userInfo)56 public static EditProfileNameFragment newInstance(UserInfo userInfo) { 57 EditProfileNameFragment 58 profileSettingsFragment = new EditProfileNameFragment(); 59 Bundle bundle = new Bundle(); 60 bundle.putParcelable(Intent.EXTRA_USER, userInfo); 61 profileSettingsFragment.setArguments(bundle); 62 return profileSettingsFragment; 63 } 64 65 @Override getToolbarMenuItems()66 public List<MenuItem> getToolbarMenuItems() { 67 return Collections.singletonList(mSaveButton); 68 } 69 70 @Override 71 @LayoutRes getLayoutId()72 protected int getLayoutId() { 73 return R.layout.edit_profile_name_fragment; 74 } 75 76 @Override 77 @StringRes getTitleId()78 protected int getTitleId() { 79 return R.string.edit_user_name_title; 80 } 81 82 @Override onCreate(Bundle savedInstanceState)83 public void onCreate(Bundle savedInstanceState) { 84 super.onCreate(savedInstanceState); 85 mUserInfo = getArguments().getParcelable(Intent.EXTRA_USER); 86 87 mSaveButton = new MenuItem.Builder(getContext()) 88 .setTitle(android.R.string.ok) 89 .setOnClickListener(i -> { 90 // Save new user's name. 91 mUserManager.setUserName(mUserInfo.id, 92 mProfileNameEditText.getText().toString()); 93 Settings.Secure.putInt(getActivity().getContentResolver(), 94 PROFILE_NAME_SET, 1); 95 getActivity().onBackPressed(); 96 }) 97 .build(); 98 } 99 100 @Override onViewCreated(View view, Bundle savedInstanceState)101 public void onViewCreated(View view, Bundle savedInstanceState) { 102 super.onViewCreated(view, savedInstanceState); 103 mProfileNameEditText = view.findViewById(R.id.profile_name_text_edit); 104 } 105 106 @Override onActivityCreated(Bundle savedInstanceState)107 public void onActivityCreated(Bundle savedInstanceState) { 108 super.onActivityCreated(savedInstanceState); 109 110 if (mUserManager == null) { 111 mUserManager = UserManager.get(getContext()); 112 } 113 114 configureProfileNameEditing(); 115 } 116 configureProfileNameEditing()117 private void configureProfileNameEditing() { 118 // Set the profile's name. 119 mProfileNameEditText.setText(mUserInfo.name); 120 mProfileNameEditText.setEnabled(true); 121 mProfileNameEditText.setSelectAllOnFocus(true); 122 mProfileNameEditText.addTextChangedListener(new TextWatcher() { 123 @Override 124 public void onTextChanged(CharSequence s, int start, int before, int count) { 125 if (TextUtils.isEmpty(s)) { 126 mSaveButton.setEnabled(false); 127 mProfileNameEditText.setError(getString(R.string.name_input_blank_error)); 128 } else if (!TextUtils.isGraphic(s)) { 129 mSaveButton.setEnabled(false); 130 mProfileNameEditText.setError(getString(R.string.name_input_invalid_error)); 131 } else { 132 mSaveButton.setEnabled(true); 133 mProfileNameEditText.setError(null); 134 } 135 } 136 137 @Override 138 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 139 } 140 141 @Override 142 public void afterTextChanged(Editable s) { 143 } 144 }); 145 } 146 } 147