• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.user.CarUserManagerHelper;
19 import android.content.pm.UserInfo;
20 import android.os.Bundle;
21 import android.provider.Settings;
22 import android.support.annotation.VisibleForTesting;
23 import android.support.design.widget.TextInputEditText;
24 import android.view.View;
25 import android.widget.Button;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.BaseFragment;
29 import com.android.car.settingslib.util.SettingsConstants;
30 
31 /**
32  * Enables user to edit their username.
33  */
34 public class EditUsernameFragment extends BaseFragment {
35     public static final String EXTRA_USER_INFO = "extra_user_info";
36     private UserInfo mUserInfo;
37 
38     private TextInputEditText mUserNameEditText;
39     private Button mOkButton;
40     private Button mCancelButton;
41 
42     @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
43     CarUserManagerHelper mCarUserManagerHelper;
44 
45     /**
46      * Creates instance of EditUsernameFragment.
47      */
newInstance(UserInfo userInfo)48     public static EditUsernameFragment newInstance(UserInfo userInfo) {
49         EditUsernameFragment
50                 userSettingsFragment = new EditUsernameFragment();
51         Bundle bundle = BaseFragment.getBundle();
52         bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button);
53         bundle.putInt(EXTRA_TITLE_ID, R.string.edit_user_name_title);
54         bundle.putParcelable(EXTRA_USER_INFO, userInfo);
55         bundle.putInt(EXTRA_LAYOUT, R.layout.edit_username_fragment);
56         userSettingsFragment.setArguments(bundle);
57         return userSettingsFragment;
58     }
59 
60     @Override
onCreate(Bundle savedInstanceState)61     public void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         mUserInfo = getArguments().getParcelable(EXTRA_USER_INFO);
64     }
65 
66     @Override
onViewCreated(View view, Bundle savedInstanceState)67     public void onViewCreated(View view, Bundle savedInstanceState) {
68         mUserNameEditText = (TextInputEditText) view.findViewById(R.id.user_name_text_edit);
69     }
70 
71     @Override
onActivityCreated(Bundle savedInstanceState)72     public void onActivityCreated(Bundle savedInstanceState) {
73         super.onActivityCreated(savedInstanceState);
74         createCarUserManagerHelper();
75 
76         configureUsernameEditing();
77         showOkButton();
78         showCancelButton();
79     }
80 
createCarUserManagerHelper()81     private void createCarUserManagerHelper() {
82         // Null check for testing. Don't want to override it if already set by a test.
83         if (mCarUserManagerHelper == null) {
84             mCarUserManagerHelper = new CarUserManagerHelper(getContext());
85         }
86     }
87 
configureUsernameEditing()88     private void configureUsernameEditing() {
89         // Set the User's name.
90         mUserNameEditText.setText(mUserInfo.name);
91         mUserNameEditText.setEnabled(true);
92         mUserNameEditText.setSelectAllOnFocus(true);
93     }
94 
showOkButton()95     private void showOkButton() {
96         // Configure OK button.
97         mOkButton = (Button) getActivity().findViewById(R.id.action_button2);
98         mOkButton.setVisibility(View.VISIBLE);
99         mOkButton.setText(android.R.string.ok);
100         mOkButton.setOnClickListener(view -> {
101             // Save new user's name.
102             mCarUserManagerHelper.setUserName(mUserInfo, mUserNameEditText.getText().toString());
103             Settings.Secure.putInt(getActivity().getContentResolver(),
104                     SettingsConstants.USER_NAME_SET, 1);
105             getActivity().onBackPressed();
106         });
107     }
108 
showCancelButton()109     private void showCancelButton() {
110         // Configure Cancel button.
111         mCancelButton = (Button) getActivity().findViewById(R.id.action_button1);
112         mCancelButton.setVisibility(View.VISIBLE);
113         mCancelButton.setText(android.R.string.cancel);
114         mCancelButton.setOnClickListener(view -> {
115             getActivity().onBackPressed();
116         });
117     }
118 }
119