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