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