• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.internal.app;
18 
19 import static android.app.admin.DevicePolicyResources.Strings.Core.UNLAUNCHABLE_APP_WORK_PAUSED_MESSAGE;
20 import static android.app.admin.DevicePolicyResources.Strings.Core.UNLAUNCHABLE_APP_WORK_PAUSED_TITLE;
21 import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
22 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
23 
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.IntentSender;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.Looper;
34 import android.os.UserHandle;
35 import android.os.UserManager;
36 import android.util.Log;
37 import android.view.Window;
38 
39 import com.android.internal.R;
40 
41 /**
42  * A dialog shown to the user when they try to launch an app from a quiet profile
43  * ({@link UserManager#isQuietModeEnabled(UserHandle)}.
44  */
45 public class UnlaunchableAppActivity extends Activity
46         implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
47     private static final String TAG = "UnlaunchableAppActivity";
48 
49     private static final int UNLAUNCHABLE_REASON_QUIET_MODE = 1;
50     private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason";
51 
52     private int mUserId;
53     private int mReason;
54     private IntentSender mTarget;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         // As this activity has nothing to show, we should hide the title bar also
60         // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog
61         requestWindowFeature(Window.FEATURE_NO_TITLE);
62         Intent intent = getIntent();
63         mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
64         mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
65         mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
66 
67         if (mUserId == UserHandle.USER_NULL) {
68             Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping.");
69             finish();
70             return;
71         }
72 
73         String dialogTitle;
74         String dialogMessage = null;
75         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
76             dialogTitle = getDialogTitle();
77             dialogMessage = getDialogMessage();
78         } else {
79             Log.wtf(TAG, "Invalid unlaunchable type: " + mReason);
80             finish();
81             return;
82         }
83 
84         AlertDialog.Builder builder = new AlertDialog.Builder(this)
85                 .setTitle(dialogTitle)
86                 .setMessage(dialogMessage)
87                 .setOnDismissListener(this);
88         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
89             builder.setPositiveButton(R.string.work_mode_turn_on, this)
90                     .setNegativeButton(R.string.cancel, null);
91         } else {
92             builder.setPositiveButton(R.string.ok, null);
93         }
94         final AlertDialog dialog = builder.create();
95         dialog.create();
96         // Prevents screen overlay attack.
97         getWindow().setHideOverlayWindows(true);
98         dialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
99         dialog.show();
100     }
101 
getDialogTitle()102     private String getDialogTitle() {
103         return getSystemService(DevicePolicyManager.class).getResources().getString(
104                 UNLAUNCHABLE_APP_WORK_PAUSED_TITLE, () -> getString(R.string.work_mode_off_title));
105     }
106 
getDialogMessage()107     private String getDialogMessage() {
108         return getSystemService(DevicePolicyManager.class).getResources().getString(
109                 UNLAUNCHABLE_APP_WORK_PAUSED_MESSAGE,
110                 () -> getString(R.string.work_mode_off_message));
111     }
112 
113     @Override
onDismiss(DialogInterface dialog)114     public void onDismiss(DialogInterface dialog) {
115         finish();
116     }
117 
118     @Override
onClick(DialogInterface dialog, int which)119     public void onClick(DialogInterface dialog, int which) {
120         if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) {
121             UserManager userManager = UserManager.get(this);
122             new Handler(Looper.getMainLooper()).post(
123                     () -> userManager.requestQuietModeEnabled(
124                             /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget));
125         }
126     }
127 
createBaseIntent()128     private static final Intent createBaseIntent() {
129         Intent intent = new Intent();
130         intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName()));
131         intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
132         return intent;
133     }
134 
createInQuietModeDialogIntent(int userId)135     public static Intent createInQuietModeDialogIntent(int userId) {
136         Intent intent = createBaseIntent();
137         intent.putExtra(EXTRA_UNLAUNCHABLE_REASON, UNLAUNCHABLE_REASON_QUIET_MODE);
138         intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
139         return intent;
140     }
141 
createInQuietModeDialogIntent(int userId, IntentSender target)142     public static Intent createInQuietModeDialogIntent(int userId, IntentSender target) {
143         Intent intent = createInQuietModeDialogIntent(userId);
144         intent.putExtra(Intent.EXTRA_INTENT, target);
145         return intent;
146     }
147 }
148