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