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 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 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(mView); 64 return mView; 65 } 66 67 private void initView(View view) { 68 final ContentResolver res = getActivity().getContentResolver(); 69 String info = mLockPatternUtils.getOwnerInfo(mUserId); 70 boolean enabled = mLockPatternUtils.isOwnerInfoEnabled(); 71 mCheckbox = (CheckBox) mView.findViewById(R.id.show_owner_info_on_lockscreen_checkbox); 72 mOwnerInfo = (EditText) mView.findViewById(R.id.owner_info_edit_text); 73 mOwnerInfo.setText(info); 74 mOwnerInfo.setEnabled(enabled); 75 mNickname = (EditText) mView.findViewById(R.id.owner_info_nickname); 76 if (!mShowNickname) { 77 mNickname.setVisibility(View.GONE); 78 } else { 79 mNickname.setText(UserManager.get(getActivity()).getUserName()); 80 mNickname.setSelected(true); 81 } 82 mCheckbox.setChecked(enabled); 83 if (UserHandle.myUserId() != UserHandle.USER_OWNER) { 84 if (UserManager.get(getActivity()).isLinkedUser()) { 85 mCheckbox.setText(R.string.show_profile_info_on_lockscreen_label); 86 } else { 87 mCheckbox.setText(R.string.show_user_info_on_lockscreen_label); 88 } 89 } 90 mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 91 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 92 mLockPatternUtils.setOwnerInfoEnabled(isChecked); 93 mOwnerInfo.setEnabled(isChecked); // disable text field if not enabled 94 } 95 }); 96 } 97 98 @Override 99 public void onPause() { 100 super.onPause(); 101 saveChanges(); 102 } 103 104 void saveChanges() { 105 ContentResolver res = getActivity().getContentResolver(); 106 String info = mOwnerInfo.getText().toString(); 107 mLockPatternUtils.setOwnerInfo(info, mUserId); 108 if (mShowNickname) { 109 String oldName = UserManager.get(getActivity()).getUserName(); 110 CharSequence newName = mNickname.getText(); 111 if (!TextUtils.isEmpty(newName) && !newName.equals(oldName)) { 112 UserManager.get(getActivity()).setUserName(UserHandle.myUserId(), 113 newName.toString()); 114 } 115 } 116 } 117 118 } 119