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.Handler; 30 import android.os.Looper; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 import android.util.Log; 34 import android.view.Window; 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 40 * ({@link UserManager#isQuietModeEnabled(UserHandle)}. 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; 47 private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason"; 48 49 private int mUserId; 50 private int mReason; 51 private IntentSender mTarget; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 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); 59 Intent intent = getIntent(); 60 mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1); 61 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); 62 mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT); 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; 71 String dialogMessage = null; 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); 75 } else { 76 Log.wtf(TAG, "Invalid unlaunchable type: " + mReason); 77 finish(); 78 return; 79 } 80 81 AlertDialog.Builder builder = new AlertDialog.Builder(this) 82 .setTitle(dialogTitle) 83 .setMessage(dialogMessage) 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 onDismiss(DialogInterface dialog)95 public void onDismiss(DialogInterface dialog) { 96 finish(); 97 } 98 99 @Override onClick(DialogInterface dialog, int which)100 public void onClick(DialogInterface dialog, int which) { 101 if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) { 102 UserManager userManager = UserManager.get(this); 103 new Handler(Looper.getMainLooper()).post( 104 () -> userManager.requestQuietModeEnabled( 105 /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget)); 106 } 107 } 108 createBaseIntent()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 createInQuietModeDialogIntent(int userId)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 } 122 createInQuietModeDialogIntent(int userId, IntentSender target)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 } 128 } 129