1 /* 2 * Copyright (C) 2014 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.Dialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.content.pm.UserInfo; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 import android.preference.Preference; 27 import android.preference.SwitchPreference; 28 29 import com.android.settings.R; 30 import com.android.settings.SettingsPreferenceFragment; 31 import com.android.settings.Utils; 32 33 import java.util.List; 34 35 /** 36 * Settings screen for configuring a specific user. It can contain user restrictions 37 * and deletion controls. It is shown when you tap on the settings icon in the 38 * user management (UserSettings) screen. 39 * 40 * Arguments to this fragment must include the userId of the user (in EXTRA_USER_ID) for whom 41 * to display controls, or should contain the EXTRA_USER_GUEST = true. 42 */ 43 public class UserDetailsSettings extends SettingsPreferenceFragment 44 implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener { 45 46 private static final String TAG = UserDetailsSettings.class.getSimpleName(); 47 48 private static final String KEY_ENABLE_TELEPHONY = "enable_calling"; 49 private static final String KEY_REMOVE_USER = "remove_user"; 50 51 /** Integer extra containing the userId to manage */ 52 static final String EXTRA_USER_ID = "user_id"; 53 /** Boolean extra to indicate guest preferences */ 54 static final String EXTRA_USER_GUEST = "guest_user"; 55 56 private static final int DIALOG_CONFIRM_REMOVE = 1; 57 private static final int DIALOG_CONFIRM_ENABLE_CALLING = 2; 58 private static final int DIALOG_CONFIRM_ENABLE_CALLING_SMS = 3; 59 60 private UserManager mUserManager; 61 private SwitchPreference mPhonePref; 62 private Preference mRemoveUserPref; 63 64 private UserInfo mUserInfo; 65 private boolean mGuestUser; 66 private Bundle mDefaultGuestRestrictions; 67 68 @Override 69 public void onCreate(Bundle icicle) { 70 super.onCreate(icicle); 71 72 final Context context = getActivity(); 73 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 74 75 addPreferencesFromResource(R.xml.user_details_settings); 76 mPhonePref = (SwitchPreference) findPreference(KEY_ENABLE_TELEPHONY); 77 mRemoveUserPref = findPreference(KEY_REMOVE_USER); 78 79 mGuestUser = getArguments().getBoolean(EXTRA_USER_GUEST, false); 80 81 if (!mGuestUser) { 82 // Regular user. Get the user id from the caller. 83 final int userId = getArguments().getInt(EXTRA_USER_ID, -1); 84 if (userId == -1) { 85 throw new RuntimeException("Arguments to this fragment must contain the user id"); 86 } 87 mUserInfo = mUserManager.getUserInfo(userId); 88 mPhonePref.setChecked(!mUserManager.hasUserRestriction( 89 UserManager.DISALLOW_OUTGOING_CALLS, new UserHandle(userId))); 90 mRemoveUserPref.setOnPreferenceClickListener(this); 91 } else { 92 // These are not for an existing user, just general Guest settings. 93 removePreference(KEY_REMOVE_USER); 94 // Default title is for calling and SMS. Change to calling-only here 95 mPhonePref.setTitle(R.string.user_enable_calling); 96 mDefaultGuestRestrictions = mUserManager.getDefaultGuestRestrictions(); 97 mPhonePref.setChecked( 98 !mDefaultGuestRestrictions.getBoolean(UserManager.DISALLOW_OUTGOING_CALLS)); 99 } 100 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER)) { 101 removePreference(KEY_REMOVE_USER); 102 } 103 mPhonePref.setOnPreferenceChangeListener(this); 104 } 105 106 @Override 107 public boolean onPreferenceClick(Preference preference) { 108 if (preference == mRemoveUserPref) { 109 if (UserHandle.myUserId() != UserHandle.USER_OWNER) { 110 throw new RuntimeException("Only the owner can remove a user"); 111 } 112 showDialog(DIALOG_CONFIRM_REMOVE); 113 return true; 114 } 115 return false; 116 } 117 118 @Override 119 public boolean onPreferenceChange(Preference preference, Object newValue) { 120 if (mGuestUser) { 121 // TODO: Show confirmation dialog: b/15761405 122 mDefaultGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, 123 !((Boolean) newValue)); 124 // SMS is always disabled for guest 125 mDefaultGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); 126 mUserManager.setDefaultGuestRestrictions(mDefaultGuestRestrictions); 127 // Update the guest's restrictions, if there is a guest 128 List<UserInfo> users = mUserManager.getUsers(true); 129 for (UserInfo user: users) { 130 if (user.isGuest()) { 131 UserHandle userHandle = new UserHandle(user.id); 132 Bundle userRestrictions = mUserManager.getUserRestrictions(userHandle); 133 userRestrictions.putAll(mDefaultGuestRestrictions); 134 mUserManager.setUserRestrictions(userRestrictions, userHandle); 135 } 136 } 137 } else { 138 // TODO: Show confirmation dialog: b/15761405 139 UserHandle userHandle = new UserHandle(mUserInfo.id); 140 mUserManager.setUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, 141 !((Boolean) newValue), userHandle); 142 mUserManager.setUserRestriction(UserManager.DISALLOW_SMS, 143 !((Boolean) newValue), userHandle); 144 } 145 return true; 146 } 147 148 @Override 149 public Dialog onCreateDialog(int dialogId) { 150 Context context = getActivity(); 151 if (context == null) return null; 152 switch (dialogId) { 153 case DIALOG_CONFIRM_REMOVE: { 154 Dialog dlg = Utils.createRemoveConfirmationDialog(getActivity(), mUserInfo.id, 155 new DialogInterface.OnClickListener() { 156 public void onClick(DialogInterface dialog, int which) { 157 removeUser(); 158 } 159 }); 160 return dlg; 161 } 162 case DIALOG_CONFIRM_ENABLE_CALLING: 163 case DIALOG_CONFIRM_ENABLE_CALLING_SMS: 164 // TODO: b/15761405 165 } 166 return null; 167 } 168 169 void removeUser() { 170 mUserManager.removeUser(mUserInfo.id); 171 finishFragment(); 172 } 173 } 174