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.systemui; 18 19 import android.app.AlertDialog; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.DialogInterface; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.pm.UserInfo; 26 import android.os.UserHandle; 27 import android.util.Log; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.internal.logging.UiEventLogger; 31 import com.android.systemui.broadcast.BroadcastDispatcher; 32 import com.android.systemui.qs.QSUserSwitcherEvent; 33 import com.android.systemui.settings.UserTracker; 34 import com.android.systemui.statusbar.phone.SystemUIDialog; 35 import com.android.systemui.statusbar.policy.UserSwitcherController; 36 import com.android.systemui.util.settings.SecureSettings; 37 38 /** 39 * Manages notification when a guest session is resumed. 40 */ 41 public class GuestResumeSessionReceiver extends BroadcastReceiver { 42 43 private static final String TAG = "GuestResumeSessionReceiver"; 44 45 @VisibleForTesting 46 public static final String SETTING_GUEST_HAS_LOGGED_IN = "systemui.guest_has_logged_in"; 47 48 @VisibleForTesting 49 public AlertDialog mNewSessionDialog; 50 private final UserTracker mUserTracker; 51 private final UserSwitcherController mUserSwitcherController; 52 private final UiEventLogger mUiEventLogger; 53 private final SecureSettings mSecureSettings; 54 GuestResumeSessionReceiver(UserSwitcherController userSwitcherController, UserTracker userTracker, UiEventLogger uiEventLogger, SecureSettings secureSettings)55 public GuestResumeSessionReceiver(UserSwitcherController userSwitcherController, 56 UserTracker userTracker, UiEventLogger uiEventLogger, 57 SecureSettings secureSettings) { 58 mUserSwitcherController = userSwitcherController; 59 mUserTracker = userTracker; 60 mUiEventLogger = uiEventLogger; 61 mSecureSettings = secureSettings; 62 } 63 64 /** 65 * Register this receiver with the {@link BroadcastDispatcher} 66 * 67 * @param broadcastDispatcher to register the receiver. 68 */ register(BroadcastDispatcher broadcastDispatcher)69 public void register(BroadcastDispatcher broadcastDispatcher) { 70 IntentFilter f = new IntentFilter(Intent.ACTION_USER_SWITCHED); 71 broadcastDispatcher.registerReceiver(this, f, null /* handler */, UserHandle.SYSTEM); 72 } 73 74 @Override onReceive(Context context, Intent intent)75 public void onReceive(Context context, Intent intent) { 76 String action = intent.getAction(); 77 78 if (Intent.ACTION_USER_SWITCHED.equals(action)) { 79 cancelDialog(); 80 81 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); 82 if (userId == UserHandle.USER_NULL) { 83 Log.e(TAG, intent + " sent to " + TAG + " without EXTRA_USER_HANDLE"); 84 return; 85 } 86 87 UserInfo currentUser = mUserTracker.getUserInfo(); 88 if (!currentUser.isGuest()) { 89 return; 90 } 91 92 int notFirstLogin = mSecureSettings.getIntForUser( 93 SETTING_GUEST_HAS_LOGGED_IN, 0, userId); 94 if (notFirstLogin != 0) { 95 mNewSessionDialog = new ResetSessionDialog(context, mUserSwitcherController, 96 mUiEventLogger, userId); 97 mNewSessionDialog.show(); 98 } else { 99 mSecureSettings.putIntForUser(SETTING_GUEST_HAS_LOGGED_IN, 1, userId); 100 } 101 } 102 } 103 cancelDialog()104 private void cancelDialog() { 105 if (mNewSessionDialog != null && mNewSessionDialog.isShowing()) { 106 mNewSessionDialog.cancel(); 107 mNewSessionDialog = null; 108 } 109 } 110 111 /** 112 * Dialog shown when user when asking for confirmation before deleting guest user. 113 */ 114 @VisibleForTesting 115 public static class ResetSessionDialog extends SystemUIDialog implements 116 DialogInterface.OnClickListener { 117 118 @VisibleForTesting 119 public static final int BUTTON_WIPE = BUTTON_NEGATIVE; 120 @VisibleForTesting 121 public static final int BUTTON_DONTWIPE = BUTTON_POSITIVE; 122 123 private final UserSwitcherController mUserSwitcherController; 124 private final UiEventLogger mUiEventLogger; 125 private final int mUserId; 126 ResetSessionDialog(Context context, UserSwitcherController userSwitcherController, UiEventLogger uiEventLogger, int userId)127 ResetSessionDialog(Context context, 128 UserSwitcherController userSwitcherController, 129 UiEventLogger uiEventLogger, 130 int userId) { 131 super(context); 132 133 setTitle(context.getString(R.string.guest_wipe_session_title)); 134 setMessage(context.getString(R.string.guest_wipe_session_message)); 135 setCanceledOnTouchOutside(false); 136 137 setButton(BUTTON_WIPE, 138 context.getString(R.string.guest_wipe_session_wipe), this); 139 setButton(BUTTON_DONTWIPE, 140 context.getString(R.string.guest_wipe_session_dontwipe), this); 141 142 mUserSwitcherController = userSwitcherController; 143 mUiEventLogger = uiEventLogger; 144 mUserId = userId; 145 } 146 147 @Override onClick(DialogInterface dialog, int which)148 public void onClick(DialogInterface dialog, int which) { 149 if (which == BUTTON_WIPE) { 150 mUiEventLogger.log(QSUserSwitcherEvent.QS_USER_GUEST_WIPE); 151 mUserSwitcherController.removeGuestUser(mUserId, UserHandle.USER_NULL); 152 dismiss(); 153 } else if (which == BUTTON_DONTWIPE) { 154 mUiEventLogger.log(QSUserSwitcherEvent.QS_USER_GUEST_CONTINUE); 155 cancel(); 156 } 157 } 158 } 159 } 160