Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 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; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 24 | import android.content.ComponentName; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 25 | import android.content.DialogInterface; |
| 26 | import android.content.Intent; |
Rubin Xu | e420c55 | 2016-04-06 19:04:30 +0100 | [diff] [blame] | 27 | import android.content.IntentSender; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 28 | import android.os.Bundle; |
Jonathan Scott | 3e7c684 | 2020-06-19 18:10:07 +0100 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 31 | import android.os.UserHandle; |
| 32 | import android.os.UserManager; |
| 33 | import android.util.Log; |
Ricky Wai | 7f1ca4f | 2016-06-15 11:36:23 +0100 | [diff] [blame] | 34 | import android.view.Window; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 35 | |
| 36 | import 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 Shanka | 7a9c34b | 2016-03-11 12:25:51 -0800 | [diff] [blame] | 40 | * ({@link UserManager#isQuietModeEnabled(UserHandle)}. |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 41 | */ |
| 42 | public 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 Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 47 | private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason"; |
| 48 | |
| 49 | private int mUserId; |
| 50 | private int mReason; |
Rubin Xu | e420c55 | 2016-04-06 19:04:30 +0100 | [diff] [blame] | 51 | private IntentSender mTarget; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | protected void onCreate(Bundle savedInstanceState) { |
| 55 | super.onCreate(savedInstanceState); |
Ricky Wai | 7f1ca4f | 2016-06-15 11:36:23 +0100 | [diff] [blame] | 56 | // 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 Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 59 | Intent intent = getIntent(); |
| 60 | mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1); |
| 61 | mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
Rubin Xu | e420c55 | 2016-04-06 19:04:30 +0100 | [diff] [blame] | 62 | mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT); |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 63 | |
| 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 Xu | 945fd00 | 2016-01-29 15:37:05 +0000 | [diff] [blame] | 71 | String dialogMessage = null; |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 72 | 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 Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 75 | } else { |
| 76 | Log.wtf(TAG, "Invalid unlaunchable type: " + mReason); |
| 77 | finish(); |
| 78 | return; |
| 79 | } |
| 80 | |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 81 | AlertDialog.Builder builder = new AlertDialog.Builder(this) |
Tony Mak | 894f627 | 2018-02-02 16:49:04 +0000 | [diff] [blame] | 82 | .setTitle(dialogTitle) |
| 83 | .setMessage(dialogMessage) |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 84 | .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 Scott | 3e7c684 | 2020-06-19 18:10:07 +0100 | [diff] [blame] | 102 | UserManager userManager = UserManager.get(this); |
| 103 | new Handler(Looper.getMainLooper()).post( |
| 104 | () -> userManager.requestQuietModeEnabled( |
| 105 | /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget)); |
Rubin Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 106 | } |
| 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 Xu | e420c55 | 2016-04-06 19:04:30 +0100 | [diff] [blame] | 122 | |
| 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 Xu | 58d2599 | 2016-01-21 17:47:13 +0000 | [diff] [blame] | 128 | } |