1 /*
2  * Copyright (C) 2015 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.cts.verifier.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Notification;
22 import android.app.NotificationChannel;
23 import android.app.NotificationManager;
24 import android.app.admin.DevicePolicyManager;
25 import android.content.ActivityNotFoundException;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.util.Log;
34 import android.widget.Toast;
35 
36 import com.android.cts.verifier.IntentDrivenTestActivity;
37 import com.android.cts.verifier.IntentDrivenTestActivity.ButtonInfo;
38 import com.android.cts.verifier.R;
39 import com.android.cts.verifier.TestListAdapter.TestListItem;
40 
41 public class Utils {
42 
43     private static final String TAG = "CtsVerifierByodUtils";
44     static final int BUGREPORT_NOTIFICATION_ID = 12345;
45     private static final String CHANNEL_ID = "BugReport";
46     static final String FILE_PROVIDER_AUTHORITY =
47             "com.android.cts.verifier.managedprovisioning.fileprovider";
48 
createInteractiveTestItem(Activity activity, String id, int titleRes, int infoRes, ButtonInfo[] buttonInfos)49     static TestListItem createInteractiveTestItem(Activity activity, String id, int titleRes,
50             int infoRes, ButtonInfo[] buttonInfos) {
51         return TestListItem.newTest(activity, titleRes,
52                 id, new Intent(activity, IntentDrivenTestActivity.class)
53                 .putExtra(IntentDrivenTestActivity.EXTRA_ID, id)
54                 .putExtra(IntentDrivenTestActivity.EXTRA_TITLE, titleRes)
55                 .putExtra(IntentDrivenTestActivity.EXTRA_INFO, infoRes)
56                 .putExtra(IntentDrivenTestActivity.EXTRA_BUTTONS, buttonInfos),
57                 null);
58     }
59 
createInteractiveTestItem(Activity activity, String id, int titleRes, int infoRes, ButtonInfo buttonInfo)60     static TestListItem createInteractiveTestItem(Activity activity, String id, int titleRes,
61             int infoRes, ButtonInfo buttonInfo) {
62         return createInteractiveTestItem(activity, id, titleRes, infoRes,
63                 new ButtonInfo[] { buttonInfo });
64     }
65 
requestDeleteManagedProfile(Context context)66     static void requestDeleteManagedProfile(Context context) {
67         try {
68             Intent intent = new Intent(ByodHelperActivity.ACTION_REMOVE_MANAGED_PROFILE);
69             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
70             context.startActivity(intent);
71         }
72         catch (ActivityNotFoundException e) {
73             Log.d(TAG, "requestDeleteProfileOwner: ActivityNotFoundException", e);
74         }
75     }
76 
provisionManagedProfile(Activity activity, ComponentName admin, int requestCode)77     static void provisionManagedProfile(Activity activity, ComponentName admin, int requestCode) {
78         Intent sending = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
79         sending.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, admin);
80         if (sending.resolveActivity(activity.getPackageManager()) != null) {
81             activity.startActivityForResult(sending, requestCode);
82         } else {
83             showToast(activity, R.string.provisioning_byod_disabled);
84         }
85     }
86 
showBugreportNotification(Context context, String msg, int notificationId)87     static void showBugreportNotification(Context context, String msg, int notificationId) {
88         NotificationManager notificationManager = getNotificationManager(context);
89         NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
90                 CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH);
91         notificationManager.createNotificationChannel(channel);
92         CharSequence title = context.getString(R.string.device_owner_requesting_bugreport_tests);
93         Notification notification = new Notification.Builder(context)
94                 .setChannelId(CHANNEL_ID)
95                 .setSmallIcon(R.drawable.icon)
96                 .setContentTitle(title)
97                 .setContentText(msg)
98                 .setStyle(new Notification.BigTextStyle().bigText(msg))
99                 .extend(new Notification.TvExtender())
100                 .build();
101         Log.d(TAG, "Sending notification: id=" + notificationId + ", title='" + title
102                 + "' text='" + msg + "'");
103         notificationManager.notify(notificationId, notification);
104     }
105 
getNotificationManager(Context context)106     private static NotificationManager getNotificationManager(Context context) {
107         if (UserManager.isHeadlessSystemUserMode()) {
108             Log.d(TAG, "getNotificationManager(): using context for current user");
109             context = context.createContextAsUser(UserHandle.CURRENT, /* flags= */ 0);
110         }
111         return context.getSystemService(NotificationManager.class);
112     }
113 
showToast(Context context, int messageId)114     static void showToast(Context context, int messageId) {
115         CharSequence msg = context.getString(messageId);
116         Log.d(TAG, "showToast(): " + msg);
117         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
118     }
119 
120     /**
121      * Prompts the tester to set a screen lock credential, or change it if one exists.
122      *
123      * An instruction dialog is shown before the tester is sent to the ChooseLockGeneric activity
124      * in Settings.
125      *
126      * @param activity The calling activity where the result is handled
127      * @param requestCode The callback request code when the lock is set
128      */
setScreenLock(Activity activity, int requestCode)129     static void setScreenLock(Activity activity, int requestCode) {
130         final Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
131         new AlertDialog.Builder(activity)
132                 .setTitle(R.string.provisioning_byod)
133                 .setMessage(R.string.provisioning_byod_set_screen_lock_dialog_message)
134                 .setPositiveButton(R.string.go_button_text, (DialogInterface dialog, int which) ->
135                         activity.startActivityForResult(intent, requestCode))
136                 .show();
137     }
138 
139     /**
140      * Prompts the tester to remove the current screen lock credential.
141      *
142      * An instruction dialog is shown before the tester is sent to the ChooseLockGeneric activity
143      * in Settings.
144      *
145      * @param activity The calling activity
146      */
removeScreenLock(Activity activity)147     static void removeScreenLock(Activity activity) {
148         final Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
149         new AlertDialog.Builder(activity)
150                 .setTitle(R.string.provisioning_byod)
151                 .setMessage(R.string.provisioning_byod_remove_screen_lock_dialog_message)
152                 .setPositiveButton(R.string.go_button_text, (DialogInterface dialog, int which) ->
153                         activity.startActivity(intent))
154                 .show();
155     }
156 
isLockscreenSupported(Context context)157     static boolean isLockscreenSupported(Context context) {
158         return context.getPackageManager().hasSystemFeature(
159                 PackageManager.FEATURE_SECURE_LOCK_SCREEN);
160     }
161 }
162