1 /*
2  * Copyright (C) 2020 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 android.content.Intent;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 import android.util.Slog;
25 
26 import com.android.internal.R;
27 
28 /**
29  * A dialog shown to the user when they try to launch an app that is not allowed in lock task
30  * mode. The intent to start this activity must be created with the static factory method provided
31  * below.
32  */
33 public class BlockedAppActivity extends AlertActivity {
34 
35     private static final String TAG = "BlockedAppActivity";
36     private static final String PACKAGE_NAME = "com.android.internal.app";
37     private static final String EXTRA_BLOCKED_PACKAGE = PACKAGE_NAME + ".extra.BLOCKED_PACKAGE";
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         Intent intent = getIntent();
44         int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, /* defaultValue= */ -1);
45         if (userId < 0) {
46             Slog.wtf(TAG, "Invalid user: " + userId);
47             finish();
48             return;
49         }
50 
51         String packageName = intent.getStringExtra(EXTRA_BLOCKED_PACKAGE);
52         if (TextUtils.isEmpty(packageName)) {
53             Slog.wtf(TAG, "Invalid package: " + packageName);
54             finish();
55             return;
56         }
57 
58         CharSequence appLabel = getAppLabel(userId, packageName);
59 
60         mAlertParams.mTitle = getString(R.string.app_blocked_title);
61         mAlertParams.mMessage = getString(R.string.app_blocked_message, appLabel);
62         mAlertParams.mPositiveButtonText = getString(android.R.string.ok);
63         setupAlert();
64     }
65 
getAppLabel(int userId, String packageName)66     private CharSequence getAppLabel(int userId, String packageName) {
67         PackageManager pm = getPackageManager();
68         try {
69             ApplicationInfo aInfo =
70                     pm.getApplicationInfoAsUser(packageName, /* flags= */ 0, userId);
71             return aInfo.loadLabel(pm);
72         } catch (PackageManager.NameNotFoundException ne) {
73             Slog.e(TAG, "Package " + packageName + " not found", ne);
74         }
75         return packageName;
76     }
77 
78 
79     /** Creates an intent that launches {@link BlockedAppActivity}. */
createIntent(int userId, String packageName)80     public static Intent createIntent(int userId, String packageName) {
81         return new Intent()
82                 .setClassName("android", BlockedAppActivity.class.getName())
83                 .putExtra(Intent.EXTRA_USER_ID, userId)
84                 .putExtra(EXTRA_BLOCKED_PACKAGE, packageName);
85     }
86 }
87