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