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.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.Fragment;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.text.TextUtils;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.widget.EditText;
31 
32 import com.android.internal.widget.LockPatternUtils;
33 
34 public class OwnerInfoSettings extends DialogFragment implements OnClickListener {
35 
36     private static final String TAG_OWNER_INFO = "ownerInfo";
37 
38     private View mView;
39     private int mUserId;
40     private LockPatternUtils mLockPatternUtils;
41     private EditText mOwnerInfo;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         mUserId = UserHandle.myUserId();
47         mLockPatternUtils = new LockPatternUtils(getActivity());
48     }
49 
50     @Override
onCreateDialog(Bundle savedInstanceState)51     public Dialog onCreateDialog(Bundle savedInstanceState) {
52         mView = LayoutInflater.from(getActivity()).inflate(R.layout.ownerinfo, null);
53         initView();
54         return new AlertDialog.Builder(getActivity())
55                 .setTitle(R.string.owner_info_settings_title)
56                 .setView(mView)
57                 .setPositiveButton(R.string.save, this)
58                 .setNegativeButton(R.string.cancel, this)
59                 .show();
60     }
61 
initView()62     private void initView() {
63         String info = mLockPatternUtils.getOwnerInfo(mUserId);
64 
65         mOwnerInfo = (EditText) mView.findViewById(R.id.owner_info_edit_text);
66         if (!TextUtils.isEmpty(info)) {
67             mOwnerInfo.setText(info);
68         }
69     }
70 
71     @Override
onClick(DialogInterface dialog, int which)72     public void onClick(DialogInterface dialog, int which) {
73         if (which == AlertDialog.BUTTON_POSITIVE) {
74             String info = mOwnerInfo.getText().toString();
75             mLockPatternUtils.setOwnerInfoEnabled(!TextUtils.isEmpty(info), mUserId);
76             mLockPatternUtils.setOwnerInfo(info, mUserId);
77 
78             if (getTargetFragment() instanceof SecuritySettings.SecuritySubSettings) {
79                 ((SecuritySettings.SecuritySubSettings) getTargetFragment()).updateOwnerInfo();
80             }
81         }
82     }
83 
show(Fragment parent)84     public static void show(Fragment parent) {
85         if (!parent.isAdded()) return;
86 
87         final OwnerInfoSettings dialog = new OwnerInfoSettings();
88         dialog.setTargetFragment(parent, 0);
89         dialog.show(parent.getFragmentManager(), TAG_OWNER_INFO);
90     }
91 }
92