1/*
2 * Copyright (C) 2013 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.users;
18
19import android.app.Dialog;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.content.pm.UserInfo;
23import android.graphics.drawable.Drawable;
24import android.os.Bundle;
25import android.view.View;
26import android.widget.ImageView;
27import android.widget.TextView;
28
29import com.android.internal.logging.nano.MetricsProto;
30import com.android.settings.R;
31import com.android.settings.Utils;
32
33public class RestrictedProfileSettings extends AppRestrictionsFragment
34        implements EditUserInfoController.OnContentChangedCallback {
35
36    public static final String FILE_PROVIDER_AUTHORITY = "com.android.settings.files";
37    static final int DIALOG_ID_EDIT_USER_INFO = 1;
38    private static final int DIALOG_CONFIRM_REMOVE = 2;
39
40    private View mHeaderView;
41    private ImageView mUserIconView;
42    private TextView mUserNameView;
43    private ImageView mDeleteButton;
44
45    private EditUserInfoController mEditUserInfoController =
46            new EditUserInfoController();
47
48    @Override
49    public void onCreate(Bundle icicle) {
50        super.onCreate(icicle);
51
52        if (icicle != null) {
53            mEditUserInfoController.onRestoreInstanceState(icicle);
54        }
55
56        init(icicle);
57    }
58
59    @Override
60    public void onActivityCreated(Bundle savedInstanceState) {
61        mHeaderView = setPinnedHeaderView(R.layout.user_info_header);
62        mHeaderView.setOnClickListener(this);
63        mUserIconView = (ImageView) mHeaderView.findViewById(android.R.id.icon);
64        mUserNameView = (TextView) mHeaderView.findViewById(android.R.id.title);
65        mDeleteButton = (ImageView) mHeaderView.findViewById(R.id.delete);
66        mDeleteButton.setOnClickListener(this);
67        // This is going to bind the preferences.
68        super.onActivityCreated(savedInstanceState);
69    }
70
71    @Override
72    public void onSaveInstanceState(Bundle outState) {
73        super.onSaveInstanceState(outState);
74        mEditUserInfoController.onSaveInstanceState(outState);
75    }
76
77    @Override
78    public void onResume() {
79        super.onResume();
80
81        // Check if user still exists
82        UserInfo info = Utils.getExistingUser(mUserManager, mUser);
83        if (info == null) {
84            finishFragment();
85        } else {
86            ((TextView) mHeaderView.findViewById(android.R.id.title)).setText(info.name);
87            ((ImageView) mHeaderView.findViewById(android.R.id.icon)).setImageDrawable(
88                    com.android.settingslib.Utils.getUserIcon(getActivity(), mUserManager, info));
89        }
90    }
91
92    @Override
93    public void startActivityForResult(Intent intent, int requestCode) {
94        mEditUserInfoController.startingActivityForResult();
95        super.startActivityForResult(intent, requestCode);
96    }
97
98    @Override
99    public void onActivityResult(int requestCode, int resultCode, Intent data) {
100        super.onActivityResult(requestCode, resultCode, data);
101
102        mEditUserInfoController.onActivityResult(requestCode, resultCode, data);
103    }
104
105    @Override
106    public void onClick(View view) {
107        if (view == mHeaderView) {
108            showDialog(DIALOG_ID_EDIT_USER_INFO);
109        } else if (view == mDeleteButton) {
110            showDialog(DIALOG_CONFIRM_REMOVE);
111        } else {
112            super.onClick(view); // in AppRestrictionsFragment
113        }
114    }
115
116    @Override
117    public Dialog onCreateDialog(int dialogId) {
118        if (dialogId == DIALOG_ID_EDIT_USER_INFO) {
119            return mEditUserInfoController.createDialog(this, mUserIconView.getDrawable(),
120                    mUserNameView.getText(), R.string.profile_info_settings_title,
121                    this, mUser);
122        } else if (dialogId == DIALOG_CONFIRM_REMOVE) {
123            Dialog dlg =
124                    UserDialogs.createRemoveDialog(getActivity(), mUser.getIdentifier(),
125                            new DialogInterface.OnClickListener() {
126                                public void onClick(DialogInterface dialog, int which) {
127                                    removeUser();
128                                }
129                            }
130                    );
131            return dlg;
132        }
133
134        return null;
135    }
136
137    @Override
138    public int getDialogMetricsCategory(int dialogId) {
139        switch (dialogId) {
140            case DIALOG_ID_EDIT_USER_INFO:
141                return MetricsProto.MetricsEvent.DIALOG_USER_EDIT;
142            case DIALOG_CONFIRM_REMOVE:
143                return MetricsProto.MetricsEvent.DIALOG_USER_REMOVE;
144            default:
145                return 0;
146        }
147    }
148
149    private void removeUser() {
150        getView().post(new Runnable() {
151            public void run() {
152                mUserManager.removeUser(mUser.getIdentifier());
153                finishFragment();
154            }
155        });
156    }
157
158    @Override
159    public void onPhotoChanged(Drawable photo) {
160        mUserIconView.setImageDrawable(photo);
161    }
162
163    @Override
164    public void onLabelChanged(CharSequence label) {
165        mUserNameView.setText(label);
166    }
167}
168