1 /*
2  * Copyright (C) 2010 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.settings;
18 
19 import android.app.Fragment;
20 import android.content.ContentResolver;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.CheckBox;
30 import android.widget.CompoundButton;
31 import android.widget.EditText;
32 import android.widget.CompoundButton.OnCheckedChangeListener;
33 
34 import com.android.internal.widget.LockPatternUtils;
35 
36 public class OwnerInfoSettings extends Fragment {
37 
38     public static final String EXTRA_SHOW_NICKNAME = "show_nickname";
39 
40     private View mView;
41     private CheckBox mCheckbox;
42     private int mUserId;
43     private LockPatternUtils mLockPatternUtils;
44     private EditText mOwnerInfo;
45     private EditText mNickname;
46     private boolean mShowNickname;
47 
48     @Override
onCreate(Bundle savedInstanceState)49     public void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         Bundle args = getArguments();
52         if (args != null && args.containsKey(EXTRA_SHOW_NICKNAME)) {
53             mShowNickname = args.getBoolean(EXTRA_SHOW_NICKNAME);
54         }
55     }
56 
57     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)58     public View onCreateView(LayoutInflater inflater, ViewGroup container,
59             Bundle savedInstanceState) {
60         mView = inflater.inflate(R.layout.ownerinfo, container, false);
61         mUserId = UserHandle.myUserId();
62         mLockPatternUtils = new LockPatternUtils(getActivity());
63         initView();
64         return mView;
65     }
66 
initView()67     private void initView() {
68         mNickname = (EditText) mView.findViewById(R.id.owner_info_nickname);
69         if (!mShowNickname) {
70             mNickname.setVisibility(View.GONE);
71         } else {
72             mNickname.setText(UserManager.get(getActivity()).getUserName());
73             mNickname.setSelected(true);
74         }
75 
76         final boolean enabled = mLockPatternUtils.isOwnerInfoEnabled();
77 
78         mCheckbox = (CheckBox) mView.findViewById(R.id.show_owner_info_on_lockscreen_checkbox);
79         mCheckbox.setChecked(enabled);
80         if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
81             if (UserManager.get(getActivity()).isLinkedUser()) {
82                 mCheckbox.setText(R.string.show_profile_info_on_lockscreen_label);
83             } else {
84                 mCheckbox.setText(R.string.show_user_info_on_lockscreen_label);
85             }
86         }
87         mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
88             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
89                 mLockPatternUtils.setOwnerInfoEnabled(isChecked);
90                 mOwnerInfo.setEnabled(isChecked); // disable text field if not enabled
91             }
92         });
93 
94         String info = mLockPatternUtils.getOwnerInfo(mUserId);
95 
96         mOwnerInfo = (EditText) mView.findViewById(R.id.owner_info_edit_text);
97         mOwnerInfo.setEnabled(enabled);
98         if (!TextUtils.isEmpty(info)) {
99             mOwnerInfo.setText(info);
100         }
101     }
102 
103     @Override
onPause()104     public void onPause() {
105         super.onPause();
106         saveChanges();
107     }
108 
saveChanges()109     void saveChanges() {
110         String info = mOwnerInfo.getText().toString();
111         mLockPatternUtils.setOwnerInfo(info, mUserId);
112         if (mShowNickname) {
113             String oldName = UserManager.get(getActivity()).getUserName();
114             CharSequence newName = mNickname.getText();
115             if (!TextUtils.isEmpty(newName) && !newName.equals(oldName)) {
116                 UserManager.get(getActivity()).setUserName(UserHandle.myUserId(),
117                         newName.toString());
118             }
119         }
120     }
121 }
122