OwnerInfoSettings.java revision 62775bf756a8eca9423bb4fab3fc7b71ab22d1a7
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
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.app.Fragment;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
25import android.os.Bundle;
26import android.os.UserHandle;
27import android.text.TextUtils;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.widget.EditText;
31
32import com.android.internal.widget.LockPatternUtils;
33
34public 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
44    public void onCreate(Bundle savedInstanceState) {
45        super.onCreate(savedInstanceState);
46        mUserId = UserHandle.myUserId();
47        mLockPatternUtils = new LockPatternUtils(getActivity());
48    }
49
50    @Override
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
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
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
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