blob: 3a8536b8127e0f07ffb24e2b666a06b384d541d7 [file] [log] [blame]
Adrian Roos50052442014-07-17 23:35:18 +02001/*
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
17package com.android.systemui;
18
Sudheer Shankadc589ac2016-11-10 15:30:17 -080019import android.app.ActivityManager;
Adrian Roos50052442014-07-17 23:35:18 +020020import android.app.Dialog;
21import android.content.BroadcastReceiver;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.content.pm.UserInfo;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.os.UserManager;
31import android.provider.Settings;
32import android.util.Log;
33import android.view.WindowManagerGlobal;
34
Winsonc0d70582016-01-29 10:24:39 -080035import com.android.systemui.statusbar.phone.SystemUIDialog;
36
Adrian Roos50052442014-07-17 23:35:18 +020037/**
38 * Manages notification when a guest session is resumed.
39 */
40public class GuestResumeSessionReceiver extends BroadcastReceiver {
41
42 private static final String TAG = "GuestResumeSessionReceiver";
43
44 private static final String SETTING_GUEST_HAS_LOGGED_IN = "systemui.guest_has_logged_in";
45
46 private Dialog mNewSessionDialog;
47
48 public void register(Context context) {
49 IntentFilter f = new IntentFilter(Intent.ACTION_USER_SWITCHED);
Xiaohui Chen87d0e252015-07-30 15:38:16 -070050 context.registerReceiverAsUser(this, UserHandle.SYSTEM,
Adrian Roos50052442014-07-17 23:35:18 +020051 f, null /* permission */, null /* scheduler */);
52 }
53
54 @Override
55 public void onReceive(Context context, Intent intent) {
56 String action = intent.getAction();
57
58 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
59 cancelDialog();
60
61 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
62 if (userId == UserHandle.USER_NULL) {
63 Log.e(TAG, intent + " sent to " + TAG + " without EXTRA_USER_HANDLE");
64 return;
65 }
66
67 UserInfo currentUser;
68 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -080069 currentUser = ActivityManager.getService().getCurrentUser();
Adrian Roos50052442014-07-17 23:35:18 +020070 } catch (RemoteException e) {
71 return;
72 }
73 if (!currentUser.isGuest()) {
74 return;
75 }
76
77 ContentResolver cr = context.getContentResolver();
78 int notFirstLogin = Settings.System.getIntForUser(
79 cr, SETTING_GUEST_HAS_LOGGED_IN, 0, userId);
80 if (notFirstLogin != 0) {
81 mNewSessionDialog = new ResetSessionDialog(context, userId);
82 mNewSessionDialog.show();
83 } else {
84 Settings.System.putIntForUser(
85 cr, SETTING_GUEST_HAS_LOGGED_IN, 1, userId);
86 }
87 }
88 }
89
90 /**
91 * Wipes the guest session.
92 *
93 * The guest must be the current user and its id must be {@param userId}.
94 */
95 private static void wipeGuestSession(Context context, int userId) {
96 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
97 UserInfo currentUser;
98 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -080099 currentUser = ActivityManager.getService().getCurrentUser();
Adrian Roos50052442014-07-17 23:35:18 +0200100 } catch (RemoteException e) {
101 Log.e(TAG, "Couldn't wipe session because ActivityManager is dead");
102 return;
103 }
104 if (currentUser.id != userId) {
105 Log.w(TAG, "User requesting to start a new session (" + userId + ")"
106 + " is not current user (" + currentUser.id + ")");
107 return;
108 }
109 if (!currentUser.isGuest()) {
110 Log.w(TAG, "User requesting to start a new session (" + userId + ")"
111 + " is not a guest");
112 return;
113 }
114
Amith Yamasani1df14732014-08-29 21:37:27 -0700115 boolean marked = userManager.markGuestForDeletion(currentUser.id);
116 if (!marked) {
117 Log.w(TAG, "Couldn't mark the guest for deletion for user " + userId);
118 return;
119 }
Adrian Roos50052442014-07-17 23:35:18 +0200120 UserInfo newGuest = userManager.createGuest(context, currentUser.name);
121
122 try {
123 if (newGuest == null) {
Xiaohui Chen87d0e252015-07-30 15:38:16 -0700124 Log.e(TAG, "Could not create new guest, switching back to system user");
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800125 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Amith Yamasani1df14732014-08-29 21:37:27 -0700126 userManager.removeUser(currentUser.id);
Adrian Roos50052442014-07-17 23:35:18 +0200127 WindowManagerGlobal.getWindowManagerService().lockNow(null /* options */);
128 return;
129 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800130 ActivityManager.getService().switchUser(newGuest.id);
Amith Yamasani1df14732014-08-29 21:37:27 -0700131 userManager.removeUser(currentUser.id);
Adrian Roos50052442014-07-17 23:35:18 +0200132 } catch (RemoteException e) {
133 Log.e(TAG, "Couldn't wipe session because ActivityManager or WindowManager is dead");
134 return;
135 }
136 }
137
138 private void cancelDialog() {
139 if (mNewSessionDialog != null && mNewSessionDialog.isShowing()) {
140 mNewSessionDialog.cancel();
141 mNewSessionDialog = null;
142 }
143 }
144
145 private static class ResetSessionDialog extends SystemUIDialog implements
146 DialogInterface.OnClickListener {
147
148 private static final int BUTTON_WIPE = BUTTON_NEGATIVE;
149 private static final int BUTTON_DONTWIPE = BUTTON_POSITIVE;
150
151 private final int mUserId;
152
153 public ResetSessionDialog(Context context, int userId) {
154 super(context);
155
156 setTitle(context.getString(R.string.guest_wipe_session_title));
157 setMessage(context.getString(R.string.guest_wipe_session_message));
158 setCanceledOnTouchOutside(false);
159
160 setButton(BUTTON_WIPE,
161 context.getString(R.string.guest_wipe_session_wipe), this);
162 setButton(BUTTON_DONTWIPE,
163 context.getString(R.string.guest_wipe_session_dontwipe), this);
164
165 mUserId = userId;
166 }
167
168 @Override
169 public void onClick(DialogInterface dialog, int which) {
170 if (which == BUTTON_WIPE) {
171 wipeGuestSession(getContext(), mUserId);
172 dismiss();
173 } else if (which == BUTTON_DONTWIPE) {
174 cancel();
175 }
176 }
177 }
178}