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.app.admin.DevicePolicyResources.Strings.Core.UNLAUNCHABLE_APP_WORK_PAUSED_TITLE; 20 import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS; 21 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 22 23 import android.app.Activity; 24 import android.app.AlertDialog; 25 import android.app.admin.DevicePolicyManager; 26 import android.content.ComponentName; 27 import android.content.DialogInterface; 28 import android.content.Intent; 29 import android.content.IntentSender; 30 import android.content.pm.ResolveInfo; 31 import android.os.Bundle; 32 import android.os.Handler; 33 import android.os.Looper; 34 import android.os.UserHandle; 35 import android.os.UserManager; 36 import android.telecom.TelecomManager; 37 import android.util.Log; 38 import android.view.Window; 39 40 import com.android.internal.R; 41 42 /** 43 * A dialog shown to the user when they try to launch an app from a quiet profile 44 * ({@link UserManager#isQuietModeEnabled(UserHandle)}. 45 */ 46 public class UnlaunchableAppActivity extends Activity 47 implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener { 48 private static final String TAG = "UnlaunchableAppActivity"; 49 50 private static final int UNLAUNCHABLE_REASON_QUIET_MODE = 1; 51 private static final String EXTRA_UNLAUNCHABLE_REASON = "unlaunchable_reason"; 52 53 private int mUserId; 54 private int mReason; 55 private IntentSender mTarget; 56 private TelecomManager mTelecomManager; 57 58 @Override onCreate(Bundle savedInstanceState)59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 // As this activity has nothing to show, we should hide the title bar also 62 // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog 63 requestWindowFeature(Window.FEATURE_NO_TITLE); 64 Intent intent = getIntent(); 65 mTelecomManager = getSystemService(TelecomManager.class); 66 mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1); 67 mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); 68 mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT, 69 android.content.IntentSender.class); 70 71 if (mUserId == UserHandle.USER_NULL) { 72 Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping."); 73 finish(); 74 return; 75 } 76 77 if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) { 78 Log.wtf(TAG, "Invalid unlaunchable type: " + mReason); 79 finish(); 80 return; 81 } 82 83 String targetPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); 84 boolean showEmergencyCallButton = 85 (targetPackageName != null && targetPackageName.equals( 86 mTelecomManager.getDefaultDialerPackage(UserHandle.of(mUserId)))); 87 88 final AlertDialog.Builder builder; 89 if (showEmergencyCallButton) { 90 builder = new AlertDialog.Builder(this, R.style.AlertDialogWithEmergencyButton); 91 builder.setNeutralButton(R.string.work_mode_emergency_call_button, this); 92 } else { 93 builder = new AlertDialog.Builder(this); 94 } 95 builder.setTitle(getDialogTitle()) 96 .setOnDismissListener(this) 97 .setPositiveButton(R.string.work_mode_turn_on, this) 98 .setNegativeButton(R.string.cancel, null); 99 100 final AlertDialog dialog = builder.create(); 101 dialog.create(); 102 if (showEmergencyCallButton) { 103 dialog.getWindow().findViewById(R.id.parentPanel).setPadding(0, 0, 0, 30); 104 dialog.getWindow().findViewById(R.id.button3).setOutlineProvider(null); 105 } 106 107 // Prevents screen overlay attack. 108 getWindow().setHideOverlayWindows(true); 109 dialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true); 110 dialog.show(); 111 } 112 getDialogTitle()113 private String getDialogTitle() { 114 return getSystemService(DevicePolicyManager.class).getResources().getString( 115 UNLAUNCHABLE_APP_WORK_PAUSED_TITLE, () -> getString(R.string.work_mode_off_title)); 116 } 117 118 @Override onDismiss(DialogInterface dialog)119 public void onDismiss(DialogInterface dialog) { 120 finish(); 121 } 122 123 @Override onClick(DialogInterface dialog, int which)124 public void onClick(DialogInterface dialog, int which) { 125 if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) { 126 return; 127 } 128 if (which == DialogInterface.BUTTON_POSITIVE) { 129 UserManager userManager = UserManager.get(this); 130 new Handler(Looper.getMainLooper()).post( 131 () -> userManager.requestQuietModeEnabled( 132 /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget)); 133 } else if (which == DialogInterface.BUTTON_NEUTRAL) { 134 launchEmergencyDialer(); 135 } 136 } 137 launchEmergencyDialer()138 private void launchEmergencyDialer() { 139 startActivity(mTelecomManager.createLaunchEmergencyDialerIntent( 140 null /* number*/) 141 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 142 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 143 | Intent.FLAG_ACTIVITY_CLEAR_TOP)); 144 } 145 createBaseIntent()146 private static final Intent createBaseIntent() { 147 Intent intent = new Intent(); 148 intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName())); 149 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 150 return intent; 151 } 152 createInQuietModeDialogIntent(int userId)153 public static Intent createInQuietModeDialogIntent(int userId) { 154 Intent intent = createBaseIntent(); 155 intent.putExtra(EXTRA_UNLAUNCHABLE_REASON, UNLAUNCHABLE_REASON_QUIET_MODE); 156 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); 157 return intent; 158 } 159 createInQuietModeDialogIntent(int userId, IntentSender target, ResolveInfo resolveInfo)160 public static Intent createInQuietModeDialogIntent(int userId, IntentSender target, 161 ResolveInfo resolveInfo) { 162 Intent intent = createInQuietModeDialogIntent(userId); 163 intent.putExtra(Intent.EXTRA_INTENT, target); 164 if (resolveInfo != null) { 165 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, resolveInfo.getComponentInfo().packageName); 166 } 167 return intent; 168 } 169 } 170