Home | History | Annotate | Download | only in am
      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 
     17 package com.android.server.am;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.ViewTreeObserver;
     25 import android.view.WindowManager;
     26 import android.widget.TextView;
     27 
     28 import com.android.internal.R;
     29 
     30 /**
     31  * Dialog to show when a user switch it about to happen. The intent is to snapshot the screen
     32  * immediately after the dialog shows so that the user is informed that something is happening
     33  * in the background rather than just freeze the screen and not know if the user-switch affordance
     34  * was being handled.
     35  */
     36 final class UserSwitchingDialog extends AlertDialog
     37         implements ViewTreeObserver.OnWindowShownListener {
     38     private static final String TAG = "ActivityManagerUserSwitchingDialog";
     39 
     40     private final ActivityManagerService mService;
     41     private final int mUserId;
     42 
     43     public UserSwitchingDialog(ActivityManagerService service, Context context,
     44             int userId, String userName, boolean aboveSystem) {
     45         super(context);
     46 
     47         mService = service;
     48         mUserId = userId;
     49 
     50         // Set up the dialog contents
     51         setCancelable(false);
     52         Resources res = getContext().getResources();
     53         // Custom view due to alignment and font size requirements
     54         View view = LayoutInflater.from(getContext()).inflate(R.layout.user_switching_dialog, null);
     55         ((TextView) view.findViewById(R.id.message)).setText(
     56                 res.getString(com.android.internal.R.string.user_switching_message, userName));
     57         setView(view);
     58 
     59         if (aboveSystem) {
     60             getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
     61         }
     62         WindowManager.LayoutParams attrs = getWindow().getAttributes();
     63         attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
     64                 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
     65         getWindow().setAttributes(attrs);
     66     }
     67 
     68     @Override
     69     public void show() {
     70         // Slog.v(TAG, "show called");
     71         super.show();
     72         final View decorView = getWindow().getDecorView();
     73         if (decorView != null) {
     74             decorView.getViewTreeObserver().addOnWindowShownListener(this);
     75         }
     76     }
     77 
     78     @Override
     79     public void onWindowShown() {
     80         // Slog.v(TAG, "onWindowShown called");
     81         mService.startUserInForeground(mUserId, this);
     82         final View decorView = getWindow().getDecorView();
     83         if (decorView != null) {
     84             decorView.getViewTreeObserver().removeOnWindowShownListener(this);
     85         }
     86     }
     87 }
     88