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 17 package com.android.settings.users; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.app.Fragment; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.content.pm.UserInfo; 26 import android.graphics.Bitmap; 27 import android.graphics.drawable.Drawable; 28 import android.os.AsyncTask; 29 import android.os.Bundle; 30 import android.os.UserHandle; 31 import android.os.UserManager; 32 import android.text.TextUtils; 33 import android.view.LayoutInflater; 34 import android.view.View; 35 import android.view.WindowManager; 36 import android.widget.EditText; 37 import android.widget.ImageView; 38 39 import com.android.settings.R; 40 import com.android.settings.Utils; 41 import com.android.settings.drawable.CircleFramedDrawable; 42 43 /** 44 * This class encapsulates a Dialog for editing the user nickname and photo. 45 */ 46 public class EditUserInfoController { 47 48 private static final String KEY_AWAITING_RESULT = "awaiting_result"; 49 private static final String KEY_SAVED_PHOTO = "pending_photo"; 50 51 private Dialog mEditUserInfoDialog; 52 private Bitmap mSavedPhoto; 53 private EditUserPhotoController mEditUserPhotoController; 54 private UserHandle mUser; 55 private UserManager mUserManager; 56 private boolean mWaitingForActivityResult = false; 57 58 public interface OnContentChangedCallback { 59 public void onPhotoChanged(Drawable photo); 60 public void onLabelChanged(CharSequence label); 61 } 62 63 public void clear() { 64 mEditUserInfoDialog = null; 65 mSavedPhoto = null; 66 } 67 68 public Dialog getDialog() { 69 return mEditUserInfoDialog; 70 } 71 72 public void onRestoreInstanceState(Bundle icicle) { 73 mSavedPhoto = (Bitmap) icicle.getParcelable(KEY_SAVED_PHOTO); 74 mWaitingForActivityResult = icicle.getBoolean(KEY_AWAITING_RESULT, false); 75 } 76 77 public void onSaveInstanceState(Bundle outState) { 78 if (mEditUserInfoDialog != null && mEditUserInfoDialog.isShowing() 79 && mEditUserPhotoController != null) { 80 outState.putParcelable(KEY_SAVED_PHOTO, 81 mEditUserPhotoController.getNewUserPhotoBitmap()); 82 } 83 if (mWaitingForActivityResult) { 84 outState.putBoolean(KEY_AWAITING_RESULT, 85 mWaitingForActivityResult); 86 } 87 } 88 89 public void startingActivityForResult() { 90 mWaitingForActivityResult = true; 91 } 92 93 public void onActivityResult(int requestCode, int resultCode, Intent data) { 94 mWaitingForActivityResult = false; 95 96 if (mEditUserInfoDialog != null && mEditUserInfoDialog.isShowing() 97 && mEditUserPhotoController.onActivityResult(requestCode, resultCode, data)) { 98 return; 99 } 100 } 101 102 public Dialog createDialog(final Fragment fragment, final Drawable currentUserIcon, 103 final CharSequence currentUserName, 104 int titleResId, final OnContentChangedCallback callback, UserHandle user) { 105 Activity activity = fragment.getActivity(); 106 mUser = user; 107 if (mUserManager == null) { 108 mUserManager = UserManager.get(activity); 109 } 110 LayoutInflater inflater = activity.getLayoutInflater(); 111 View content = inflater.inflate(R.layout.edit_user_info_dialog_content, null); 112 113 UserInfo info = mUserManager.getUserInfo(mUser.getIdentifier()); 114 115 final EditText userNameView = (EditText) content.findViewById(R.id.user_name); 116 userNameView.setText(info.name); 117 118 final ImageView userPhotoView = (ImageView) content.findViewById(R.id.user_photo); 119 Drawable drawable = null; 120 if (mSavedPhoto != null) { 121 drawable = CircleFramedDrawable.getInstance(activity, mSavedPhoto); 122 } else { 123 drawable = currentUserIcon; 124 if (drawable == null) { 125 drawable = Utils.getUserIcon(activity, mUserManager, info); 126 } 127 } 128 userPhotoView.setImageDrawable(drawable); 129 mEditUserPhotoController = new EditUserPhotoController(fragment, userPhotoView, 130 mSavedPhoto, drawable, mWaitingForActivityResult); 131 mEditUserInfoDialog = new AlertDialog.Builder(activity) 132 .setTitle(R.string.profile_info_settings_title) 133 .setView(content) 134 .setCancelable(true) 135 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 136 @Override 137 public void onClick(DialogInterface dialog, int which) { 138 if (which == DialogInterface.BUTTON_POSITIVE) { 139 // Update the name if changed. 140 CharSequence userName = userNameView.getText(); 141 if (!TextUtils.isEmpty(userName)) { 142 if (currentUserName == null 143 || !userName.toString().equals(currentUserName.toString())) { 144 if (callback != null) { 145 callback.onLabelChanged(userName.toString()); 146 } 147 mUserManager.setUserName(mUser.getIdentifier(), 148 userName.toString()); 149 } 150 } 151 // Update the photo if changed. 152 Drawable drawable = mEditUserPhotoController.getNewUserPhotoDrawable(); 153 Bitmap bitmap = mEditUserPhotoController.getNewUserPhotoBitmap(); 154 if (drawable != null && bitmap != null 155 && !drawable.equals(currentUserIcon)) { 156 if (callback != null) { 157 callback.onPhotoChanged(drawable); 158 } 159 new AsyncTask<Void, Void, Void>() { 160 @Override 161 protected Void doInBackground(Void... params) { 162 mUserManager.setUserIcon(mUser.getIdentifier(), 163 mEditUserPhotoController.getNewUserPhotoBitmap()); 164 return null; 165 } 166 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); 167 } 168 fragment.getActivity().removeDialog( 169 RestrictedProfileSettings.DIALOG_ID_EDIT_USER_INFO); 170 } 171 clear(); 172 } 173 }) 174 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 175 @Override 176 public void onClick(DialogInterface dialog, int which) { 177 clear(); 178 } 179 }) 180 .create(); 181 182 // Make sure the IME is up. 183 mEditUserInfoDialog.getWindow().setSoftInputMode( 184 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 185 186 return mEditUserInfoDialog; 187 } 188 } 189