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.app.Service;
21import android.content.ActivityNotFoundException;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.res.Resources;
26import android.os.Handler;
27import android.os.Message;
28import android.util.Slog;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.WindowManager;
32import android.widget.TextView;
33
34import com.android.internal.R;
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    private static final String TAG = "ActivityManagerUserSwitchingDialog";
44
45    private static final int MSG_START_USER = 1;
46
47    private final ActivityManagerService mService;
48    private final int mUserId;
49
50    public UserSwitchingDialog(ActivityManagerService service, Context context,
51            int userId, String userName, boolean aboveSystem) {
52        super(context);
53
54        mService = service;
55        mUserId = userId;
56
57        // Set up the dialog contents
58        setCancelable(false);
59        Resources res = getContext().getResources();
60        // Custom view due to alignment and font size requirements
61        View view = LayoutInflater.from(getContext()).inflate(R.layout.user_switching_dialog, null);
62        ((TextView) view.findViewById(R.id.message)).setText(
63                res.getString(com.android.internal.R.string.user_switching_message, userName));
64        setView(view);
65
66        if (aboveSystem) {
67            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
68        }
69        WindowManager.LayoutParams attrs = getWindow().getAttributes();
70        attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
71                WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
72        getWindow().setAttributes(attrs);
73    }
74
75    @Override
76    public void show() {
77        super.show();
78        // TODO: Instead of just an arbitrary delay, wait for a signal that the window was fully
79        // displayed by the window manager
80        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_USER), 250);
81    }
82
83    private final Handler mHandler = new Handler() {
84        public void handleMessage(Message msg) {
85            switch (msg.what) {
86                case MSG_START_USER:
87                    mService.startUserInForeground(mUserId, UserSwitchingDialog.this);
88                    break;
89            }
90        }
91    };
92}
93