blob: 6f7695ce8c34ae112dd635c31fe0c7769f471b6f [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;
29import android.os.UserHandle;
30import android.os.UserManager;
31import android.util.Log;
Ricky Wai7f1ca4f2016-06-15 11:36:23 +010032import android.view.Window;
Rubin Xu58d25992016-01-21 17:47:13 +000033
34import com.android.internal.R;
35
36/**
37 * A dialog shown to the user when they try to launch an app from a quiet profile
Sudheer Shanka7a9c34b2016-03-11 12:25:51 -080038 * ({@link UserManager#isQuietModeEnabled(UserHandle)}.
Rubin Xu58d25992016-01-21 17:47:13 +000039 */
40public 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;
Rubin Xu58d25992016-01-21 17:47:13 +000045 private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason";
46
47 private int mUserId;
48 private int mReason;
Rubin Xue420c552016-04-06 19:04:30 +010049 private IntentSender mTarget;
Rubin Xu58d25992016-01-21 17:47:13 +000050
51 @Override
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
Ricky Wai7f1ca4f2016-06-15 11:36:23 +010054 // 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);
Rubin Xu58d25992016-01-21 17:47:13 +000057 Intent intent = getIntent();
58 mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
59 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
Rubin Xue420c552016-04-06 19:04:30 +010060 mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
Rubin Xu58d25992016-01-21 17:47:13 +000061
62 if (mUserId == UserHandle.USER_NULL) {
63 Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping.");
64 finish();
65 return;
66 }
67
68 String dialogTitle;
Rubin Xu945fd002016-01-29 15:37:05 +000069 String dialogMessage = null;
Rubin Xu58d25992016-01-21 17:47:13 +000070 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);
Rubin Xu58d25992016-01-21 17:47:13 +000073 } else {
74 Log.wtf(TAG, "Invalid unlaunchable type: " + mReason);
75 finish();
76 return;
77 }
78
Rubin Xu58d25992016-01-21 17:47:13 +000079 AlertDialog.Builder builder = new AlertDialog.Builder(this)
Tony Mak894f6272018-02-02 16:49:04 +000080 .setTitle(dialogTitle)
81 .setMessage(dialogMessage)
Rubin Xu58d25992016-01-21 17:47:13 +000082 .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
93 public void onDismiss(DialogInterface dialog) {
94 finish();
95 }
96
97 @Override
98 public void onClick(DialogInterface dialog, int which) {
99 if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) {
Tony Makbece85d2018-01-12 12:10:17 +0000100 UserManager.get(this).requestQuietModeEnabled(false, UserHandle.of(mUserId), mTarget);
Rubin Xu58d25992016-01-21 17:47:13 +0000101 }
102 }
103
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
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 }
Rubin Xue420c552016-04-06 19:04:30 +0100117
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 }
Rubin Xu58d25992016-01-21 17:47:13 +0000123}