blob: ca0856238b90d1197868754e4390d1388924df13 [file] [log] [blame]
Rubin Xu58d25992016-01-21 17:47:13 +00001/*
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
17package com.android.internal.app;
18
19import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
20import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
21
22import android.app.Activity;
23import android.app.AlertDialog;
Rubin Xu58d25992016-01-21 17:47:13 +000024import android.content.ComponentName;
Rubin Xu58d25992016-01-21 17:47:13 +000025import android.content.DialogInterface;
26import android.content.Intent;
Rubin Xue420c552016-04-06 19:04:30 +010027import android.content.IntentSender;
Rubin Xu58d25992016-01-21 17:47:13 +000028import android.os.Bundle;
Jonathan Scott3e7c6842020-06-19 18:10:07 +010029import android.os.Handler;
30import android.os.Looper;
Rubin Xu58d25992016-01-21 17:47:13 +000031import android.os.UserHandle;
32import android.os.UserManager;
33import android.util.Log;
Ricky Wai7f1ca4f2016-06-15 11:36:23 +010034import android.view.Window;
Rubin Xu58d25992016-01-21 17:47:13 +000035
36import com.android.internal.R;
37
38/**
39 * A dialog shown to the user when they try to launch an app from a quiet profile
Sudheer Shanka7a9c34b2016-03-11 12:25:51 -080040 * ({@link UserManager#isQuietModeEnabled(UserHandle)}.
Rubin Xu58d25992016-01-21 17:47:13 +000041 */
42public class UnlaunchableAppActivity extends Activity
43 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
44 private static final String TAG = "UnlaunchableAppActivity";
45
46 private static final int UNLAUNCHABLE_REASON_QUIET_MODE = 1;
Rubin Xu58d25992016-01-21 17:47:13 +000047 private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason";
48
49 private int mUserId;
50 private int mReason;
Rubin Xue420c552016-04-06 19:04:30 +010051 private IntentSender mTarget;
Rubin Xu58d25992016-01-21 17:47:13 +000052
53 @Override
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
Ricky Wai7f1ca4f2016-06-15 11:36:23 +010056 // As this activity has nothing to show, we should hide the title bar also
57 // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog
58 requestWindowFeature(Window.FEATURE_NO_TITLE);
Rubin Xu58d25992016-01-21 17:47:13 +000059 Intent intent = getIntent();
60 mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
61 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
Rubin Xue420c552016-04-06 19:04:30 +010062 mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
Rubin Xu58d25992016-01-21 17:47:13 +000063
64 if (mUserId == UserHandle.USER_NULL) {
65 Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping.");
66 finish();
67 return;
68 }
69
70 String dialogTitle;
Rubin Xu945fd002016-01-29 15:37:05 +000071 String dialogMessage = null;
Rubin Xu58d25992016-01-21 17:47:13 +000072 if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
73 dialogTitle = getResources().getString(R.string.work_mode_off_title);
74 dialogMessage = getResources().getString(R.string.work_mode_off_message);
Rubin Xu58d25992016-01-21 17:47:13 +000075 } else {
76 Log.wtf(TAG, "Invalid unlaunchable type: " + mReason);
77 finish();
78 return;
79 }
80
Rubin Xu58d25992016-01-21 17:47:13 +000081 AlertDialog.Builder builder = new AlertDialog.Builder(this)
Tony Mak894f6272018-02-02 16:49:04 +000082 .setTitle(dialogTitle)
83 .setMessage(dialogMessage)
Rubin Xu58d25992016-01-21 17:47:13 +000084 .setOnDismissListener(this);
85 if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
86 builder.setPositiveButton(R.string.work_mode_turn_on, this)
87 .setNegativeButton(R.string.cancel, null);
88 } else {
89 builder.setPositiveButton(R.string.ok, null);
90 }
91 builder.show();
92 }
93
94 @Override
95 public void onDismiss(DialogInterface dialog) {
96 finish();
97 }
98
99 @Override
100 public void onClick(DialogInterface dialog, int which) {
101 if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) {
Jonathan Scott3e7c6842020-06-19 18:10:07 +0100102 UserManager userManager = UserManager.get(this);
103 new Handler(Looper.getMainLooper()).post(
104 () -> userManager.requestQuietModeEnabled(
105 /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget));
Rubin Xu58d25992016-01-21 17:47:13 +0000106 }
107 }
108
109 private static final Intent createBaseIntent() {
110 Intent intent = new Intent();
111 intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName()));
112 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
113 return intent;
114 }
115
116 public static Intent createInQuietModeDialogIntent(int userId) {
117 Intent intent = createBaseIntent();
118 intent.putExtra(EXTRA_UNLAUNCHABLE_REASON, UNLAUNCHABLE_REASON_QUIET_MODE);
119 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
120 return intent;
121 }
Rubin Xue420c552016-04-06 19:04:30 +0100122
123 public static Intent createInQuietModeDialogIntent(int userId, IntentSender target) {
124 Intent intent = createInQuietModeDialogIntent(userId);
125 intent.putExtra(Intent.EXTRA_INTENT, target);
126 return intent;
127 }
Rubin Xu58d25992016-01-21 17:47:13 +0000128}