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.cts.verifier.managedprovisioning; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.util.Log; 23 24 /** 25 * A {@link BroadcastReceiver} used to receive just one intent - it just logs the intent and 26 * unregisters itself. 27 * 28 * <p>Useful for debugging purposes. 29 */ 30 public final class LogAndSelfUnregisterBroadcastReceiver extends BroadcastReceiver { 31 32 private static final String TAG = LogAndSelfUnregisterBroadcastReceiver.class.getSimpleName(); 33 34 /** 35 * Registers the broadcast to receive the given intent. 36 */ register(Context context, String action)37 public static void register(Context context, String action) { 38 context.getApplicationContext().registerReceiver( 39 new LogAndSelfUnregisterBroadcastReceiver(), 40 new IntentFilter(action)); 41 } 42 43 @Override onReceive(Context context, Intent intent)44 public void onReceive(Context context, Intent intent) { 45 Log.i(TAG, "onReceive(): " + intent); 46 context.getApplicationContext().unregisterReceiver(this); 47 } 48 } 49