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