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