1 /* 2 * Copyright (C) 2021 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 package com.android.bedstead.dpmwrapper; 17 18 import static com.android.bedstead.dpmwrapper.Utils.isCurrentUserOnHeadlessSystemUser; 19 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.IntentFilter; 23 import android.util.Log; 24 25 import androidx.localbroadcastmanager.content.LocalBroadcastManager; 26 27 /** 28 * Helper class used by the test apps. 29 */ 30 public final class TestAppHelper { 31 32 private static final String TAG = TestAppHelper.class.getSimpleName(); 33 34 /** 35 * Called by test case to register a {@link BrodcastReceiver} to receive intents sent by the 36 * device owner's {@link android.app.admin.DeviceAdminReceiver}. 37 */ registerTestCaseReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter)38 public static void registerTestCaseReceiver(Context context, BroadcastReceiver receiver, 39 IntentFilter filter) { 40 if (isCurrentUserOnHeadlessSystemUser(context)) { 41 TestAppCallbacksReceiver.registerReceiver(context, receiver, filter); 42 return; 43 } 44 Log.d(TAG, "Registering " + receiver + " to receive " + Utils.toString(filter) 45 + " locally on user " + context.getUserId()); 46 LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter); 47 } 48 49 /** 50 * Called by test case to unregister a {@link BrodcastReceiver} that receive intents sent by the 51 * device owner's {@link android.app.admin.DeviceAdminReceiver}. 52 */ unregisterTestCaseReceiver(Context context, BroadcastReceiver receiver)53 public static void unregisterTestCaseReceiver(Context context, BroadcastReceiver receiver) { 54 if (isCurrentUserOnHeadlessSystemUser(context)) { 55 TestAppCallbacksReceiver.unregisterReceiver(context, receiver); 56 return; 57 } 58 Log.d(TAG, "Unegistering " + receiver + " locally on user " + context.getUserId()); 59 LocalBroadcastManager.getInstance(context).unregisterReceiver(receiver); 60 } 61 TestAppHelper()62 private TestAppHelper() { 63 throw new UnsupportedOperationException("contains only static methods"); 64 } 65 } 66