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 String targetPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); 71 Log.i(TAG, "Unlaunchable activity for target package: " + targetPackageName); 72 final UserManager userManager = UserManager.get(this); 73 74 if (mUserId == UserHandle.USER_NULL) { 75 Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping."); 76 finish(); 77 return; 78 } 79 80 if (android.os.Flags.allowPrivateProfile() 81 && android.multiuser.Flags.enablePrivateSpaceFeatures() 82 && !userManager.isManagedProfile(mUserId)) { 83 Log.e(TAG, "Unlaunchable activity for target package " + targetPackageName 84 + " called for a non-managed-profile " + mUserId); 85 finish(); 86 return; 87 } 88 89 if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) { 90 Log.wtf(TAG, "Invalid unlaunchable type: " + mReason); 91 finish(); 92 return; 93 } 94 95 boolean showEmergencyCallButton = 96 (targetPackageName != null && targetPackageName.equals( 97 mTelecomManager.getDefaultDialerPackage(UserHandle.of(mUserId)))); 98 99 final AlertDialog.Builder builder; 100 if (showEmergencyCallButton) { 101 builder = new AlertDialog.Builder(this, R.style.AlertDialogWithEmergencyButton); 102 builder.setNeutralButton(R.string.work_mode_emergency_call_button, this); 103 } else { 104 builder = new AlertDialog.Builder(this); 105 } 106 builder.setTitle(getDialogTitle()) 107 .setOnDismissListener(this) 108 .setPositiveButton(R.string.work_mode_turn_on, this) 109 .setNegativeButton(R.string.cancel, null); 110 111 final AlertDialog dialog = builder.create(); 112 dialog.create(); 113 if (showEmergencyCallButton) { 114 dialog.getWindow().findViewById(R.id.parentPanel).setPadding(0, 0, 0, 30); 115 dialog.getWindow().findViewById(R.id.button3).setOutlineProvider(null); 116 } 117 118 // Prevents screen overlay attack. 119 getWindow().setHideOverlayWindows(true); 120 dialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true); 121 dialog.show(); 122 } 123 getDialogTitle()124 private String getDialogTitle() { 125 return getSystemService(DevicePolicyManager.class).getResources().getString( 126 UNLAUNCHABLE_APP_WORK_PAUSED_TITLE, () -> getString(R.string.work_mode_off_title)); 127 } 128 129 @Override onDismiss(DialogInterface dialog)130 public void onDismiss(DialogInterface dialog) { 131 finish(); 132 } 133 134 @Override onClick(DialogInterface dialog, int which)135 public void onClick(DialogInterface dialog, int which) { 136 if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) { 137 return; 138 } 139 if (which == DialogInterface.BUTTON_POSITIVE) { 140 UserManager userManager = UserManager.get(this); 141 new Handler(Looper.getMainLooper()).post( 142 () -> userManager.requestQuietModeEnabled( 143 /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget)); 144 } else if (which == DialogInterface.BUTTON_NEUTRAL) { 145 launchEmergencyDialer(); 146 } 147 } 148 launchEmergencyDialer()149 private void launchEmergencyDialer() { 150 startActivity(mTelecomManager.createLaunchEmergencyDialerIntent( 151 null /* number*/) 152 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 153 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS 154 | Intent.FLAG_ACTIVITY_CLEAR_TOP)); 155 } 156 createBaseIntent()157 private static final Intent createBaseIntent() { 158 Intent intent = new Intent(); 159 intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName())); 160 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 161 return intent; 162 } 163 createInQuietModeDialogIntent(int userId)164 public static Intent createInQuietModeDialogIntent(int userId) { 165 Intent intent = createBaseIntent(); 166 intent.putExtra(EXTRA_UNLAUNCHABLE_REASON, UNLAUNCHABLE_REASON_QUIET_MODE); 167 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId); 168 return intent; 169 } 170 createInQuietModeDialogIntent(int userId, IntentSender target, ResolveInfo resolveInfo)171 public static Intent createInQuietModeDialogIntent(int userId, IntentSender target, 172 ResolveInfo resolveInfo) { 173 Intent intent = createInQuietModeDialogIntent(userId); 174 intent.putExtra(Intent.EXTRA_INTENT, target); 175 if (resolveInfo != null) { 176 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, resolveInfo.getComponentInfo().packageName); 177 } 178 return intent; 179 } 180 } 181