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.users; 17 18 import android.car.userlib.CarUserManagerHelper; 19 import android.content.Intent; 20 import android.content.pm.UserInfo; 21 import android.os.Bundle; 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.Button; 28 import android.widget.EditText; 29 30 import androidx.annotation.LayoutRes; 31 import androidx.annotation.StringRes; 32 33 import com.android.car.settings.R; 34 import com.android.car.settings.common.BaseFragment; 35 import com.android.car.settingslib.util.SettingsConstants; 36 37 /** 38 * Enables user to edit their username. 39 */ 40 public class EditUsernameFragment extends BaseFragment { 41 private UserInfo mUserInfo; 42 43 private EditText mUserNameEditText; 44 private Button mOkButton; 45 private Button mCancelButton; 46 private CarUserManagerHelper mCarUserManagerHelper; 47 48 /** 49 * Creates instance of EditUsernameFragment. 50 */ newInstance(UserInfo userInfo)51 public static EditUsernameFragment newInstance(UserInfo userInfo) { 52 EditUsernameFragment 53 userSettingsFragment = new EditUsernameFragment(); 54 Bundle bundle = new Bundle(); 55 bundle.putParcelable(Intent.EXTRA_USER, userInfo); 56 userSettingsFragment.setArguments(bundle); 57 return userSettingsFragment; 58 } 59 60 @Override 61 @LayoutRes getActionBarLayoutId()62 protected int getActionBarLayoutId() { 63 return R.layout.action_bar_with_button; 64 } 65 66 @Override 67 @LayoutRes getLayoutId()68 protected int getLayoutId() { 69 return R.layout.edit_username_fragment; 70 } 71 72 @Override 73 @StringRes getTitleId()74 protected int getTitleId() { 75 return R.string.edit_user_name_title; 76 } 77 78 @Override onCreate(Bundle savedInstanceState)79 public void onCreate(Bundle savedInstanceState) { 80 super.onCreate(savedInstanceState); 81 mUserInfo = getArguments().getParcelable(Intent.EXTRA_USER); 82 } 83 84 @Override onViewCreated(View view, Bundle savedInstanceState)85 public void onViewCreated(View view, Bundle savedInstanceState) { 86 mUserNameEditText = view.findViewById(R.id.user_name_text_edit); 87 } 88 89 @Override onActivityCreated(Bundle savedInstanceState)90 public void onActivityCreated(Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 mCarUserManagerHelper = new CarUserManagerHelper(getContext()); 93 94 showOkButton(); 95 showCancelButton(); 96 configureUsernameEditing(); 97 } 98 configureUsernameEditing()99 private void configureUsernameEditing() { 100 // Set the User's name. 101 mUserNameEditText.setText(mUserInfo.name); 102 mUserNameEditText.setEnabled(true); 103 mUserNameEditText.setSelectAllOnFocus(true); 104 mUserNameEditText.addTextChangedListener(new TextWatcher() { 105 @Override 106 public void onTextChanged(CharSequence s, int start, int before, int count) { 107 if (TextUtils.isEmpty(s)) { 108 mOkButton.setEnabled(false); 109 mUserNameEditText.setError(getString(R.string.name_input_blank_error)); 110 } else if (!TextUtils.isGraphic(s)) { 111 mOkButton.setEnabled(false); 112 mUserNameEditText.setError(getString(R.string.name_input_invalid_error)); 113 } else { 114 mOkButton.setEnabled(true); 115 mUserNameEditText.setError(null); 116 } 117 } 118 119 @Override 120 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 121 } 122 123 @Override 124 public void afterTextChanged(Editable s) { 125 } 126 }); 127 } 128 showOkButton()129 private void showOkButton() { 130 // Configure OK button. 131 mOkButton = getActivity().findViewById(R.id.action_button2); 132 mOkButton.setVisibility(View.VISIBLE); 133 mOkButton.setText(android.R.string.ok); 134 mOkButton.setOnClickListener(view -> { 135 // Save new user's name. 136 mCarUserManagerHelper.setUserName(mUserInfo, mUserNameEditText.getText().toString()); 137 Settings.Secure.putInt(getActivity().getContentResolver(), 138 SettingsConstants.USER_NAME_SET, 1); 139 getActivity().onBackPressed(); 140 }); 141 } 142 showCancelButton()143 private void showCancelButton() { 144 // Configure Cancel button. 145 mCancelButton = getActivity().findViewById(R.id.action_button1); 146 mCancelButton.setVisibility(View.VISIBLE); 147 mCancelButton.setText(android.R.string.cancel); 148 mCancelButton.setOnClickListener(view -> { 149 getActivity().onBackPressed(); 150 }); 151 } 152 } 153