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
17package com.android.server.am;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.pm.UserInfo;
22import android.content.res.Resources;
23import android.os.Handler;
24import android.os.Message;
25import android.os.UserHandle;
26import android.os.UserManager;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewTreeObserver;
30import android.view.WindowManager;
31import android.widget.TextView;
32
33import com.android.internal.R;
34import com.android.internal.annotations.GuardedBy;
35
36/**
37 * Dialog to show when a user switch it about to happen. The intent is to snapshot the screen
38 * immediately after the dialog shows so that the user is informed that something is happening
39 * in the background rather than just freeze the screen and not know if the user-switch affordance
40 * was being handled.
41 */
42final class UserSwitchingDialog extends AlertDialog
43        implements ViewTreeObserver.OnWindowShownListener {
44    private static final String TAG = "ActivityManagerUserSwitchingDialog";
45
46    // Time to wait for the onWindowShown() callback before continuing the user switch
47    private static final int WINDOW_SHOWN_TIMEOUT_MS = 3000;
48
49    private final ActivityManagerService mService;
50    private final int mUserId;
51    private static final int MSG_START_USER = 1;
52    @GuardedBy("this")
53    private boolean mStartedUser;
54
55    public UserSwitchingDialog(ActivityManagerService service, Context context, UserInfo oldUser,
56            UserInfo newUser, boolean aboveSystem) {
57        super(context);
58
59        mService = service;
60        mUserId = newUser.id;
61
62        // Set up the dialog contents
63        setCancelable(false);
64        Resources res = getContext().getResources();
65        // Custom view due to alignment and font size requirements
66        View view = LayoutInflater.from(getContext()).inflate(R.layout.user_switching_dialog, null);
67
68        String viewMessage;
69        if (UserManager.isSplitSystemUser() && newUser.id == UserHandle.USER_SYSTEM) {
70            viewMessage = res.getString(R.string.user_logging_out_message, oldUser.name);
71        } else if (UserManager.isDeviceInDemoMode(context)) {
72            if (oldUser.isDemo()) {
73                viewMessage = res.getString(R.string.demo_restarting_message);
74            } else {
75                viewMessage = res.getString(R.string.demo_starting_message);
76            }
77        } else {
78            viewMessage = res.getString(R.string.user_switching_message, newUser.name);
79        }
80        ((TextView) view.findViewById(R.id.message)).setText(viewMessage);
81        setView(view);
82
83        if (aboveSystem) {
84            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
85        }
86        WindowManager.LayoutParams attrs = getWindow().getAttributes();
87        attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
88                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
89        getWindow().setAttributes(attrs);
90    }
91
92    @Override
93    public void show() {
94        // Slog.v(TAG, "show called");
95        super.show();
96        final View decorView = getWindow().getDecorView();
97        if (decorView != null) {
98            decorView.getViewTreeObserver().addOnWindowShownListener(this);
99        }
100        // Add a timeout as a safeguard, in case a race in screen on/off causes the window
101        // callback to never come.
102        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_USER),
103                WINDOW_SHOWN_TIMEOUT_MS);
104    }
105
106    @Override
107    public void onWindowShown() {
108        // Slog.v(TAG, "onWindowShown called");
109        startUser();
110    }
111
112    void startUser() {
113        synchronized (this) {
114            if (!mStartedUser) {
115                mService.mUserController.startUserInForeground(mUserId);
116                dismiss();
117                mStartedUser = true;
118                final View decorView = getWindow().getDecorView();
119                if (decorView != null) {
120                    decorView.getViewTreeObserver().removeOnWindowShownListener(this);
121                }
122                mHandler.removeMessages(MSG_START_USER);
123            }
124        }
125    }
126
127    private final Handler mHandler = new Handler() {
128        @Override
129        public void handleMessage(Message msg) {
130            switch (msg.what) {
131                case MSG_START_USER:
132                    startUser();
133                    break;
134            }
135        }
136    };
137}
138