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 {
72            viewMessage = res.getString(R.string.user_switching_message, newUser.name);
73        }
74        ((TextView) view.findViewById(R.id.message)).setText(viewMessage);
75        setView(view);
76
77        if (aboveSystem) {
78            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
79        }
80        WindowManager.LayoutParams attrs = getWindow().getAttributes();
81        attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
82                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
83        getWindow().setAttributes(attrs);
84    }
85
86    @Override
87    public void show() {
88        // Slog.v(TAG, "show called");
89        super.show();
90        final View decorView = getWindow().getDecorView();
91        if (decorView != null) {
92            decorView.getViewTreeObserver().addOnWindowShownListener(this);
93        }
94        // Add a timeout as a safeguard, in case a race in screen on/off causes the window
95        // callback to never come.
96        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_USER),
97                WINDOW_SHOWN_TIMEOUT_MS);
98    }
99
100    @Override
101    public void onWindowShown() {
102        // Slog.v(TAG, "onWindowShown called");
103        startUser();
104    }
105
106    void startUser() {
107        synchronized (this) {
108            if (!mStartedUser) {
109                mService.mUserController.startUserInForeground(mUserId, this);
110                mStartedUser = true;
111                final View decorView = getWindow().getDecorView();
112                if (decorView != null) {
113                    decorView.getViewTreeObserver().removeOnWindowShownListener(this);
114                }
115                mHandler.removeMessages(MSG_START_USER);
116            }
117        }
118    }
119
120    private final Handler mHandler = new Handler() {
121        @Override
122        public void handleMessage(Message msg) {
123            switch (msg.what) {
124                case MSG_START_USER:
125                    startUser();
126                    break;
127            }
128        }
129    };
130}
131