1 /* 2 * Copyright (C) 2016 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.managedprovisioning.e2eui; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.util.Log; 24 25 import java.util.concurrent.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 import java.util.concurrent.atomic.AtomicBoolean; 28 29 /** 30 * Listen to provisioning result from DPC running in test process 31 */ 32 public class ProvisioningResultListener { 33 private static final String TAG = ProvisioningResultListener.class.getSimpleName(); 34 35 public static final String ACTION_PROVISION_RESULT_BROADCAST = 36 "com.android.managedprovisioning.e2eui.ACTION_PROVISION_RESULT_BROADCAST"; 37 public static final String ACTION_PROVISION_RESULT_INTENT = 38 "com.android.managedprovisioning.e2eui.ACTION_PROVISION_RESULT_INTENT"; 39 public static final String EXTRA_RESULT = "result"; 40 41 private final Context mContext; 42 private final CountDownLatch mLatch = new CountDownLatch(3); 43 private final AtomicBoolean mBroadcastResult = new AtomicBoolean(false); 44 private final AtomicBoolean mPreprovisioningActivityResult = new AtomicBoolean(false); 45 private final AtomicBoolean mIntentResult = new AtomicBoolean(false); 46 private final ResultReceiver mReceiver = new ResultReceiver(); 47 private boolean mIsRegistered = false; 48 49 private class ResultReceiver extends BroadcastReceiver { 50 @Override onReceive(Context context, Intent intent)51 public void onReceive(Context context, Intent intent) { 52 switch(intent.getAction()) { 53 case ACTION_PROVISION_RESULT_BROADCAST: 54 mBroadcastResult.set(intent.getBooleanExtra(EXTRA_RESULT, false)); 55 mLatch.countDown(); 56 break; 57 case ACTION_PROVISION_RESULT_INTENT: 58 mIntentResult.set(intent.getBooleanExtra(EXTRA_RESULT, false)); 59 mLatch.countDown(); 60 break; 61 } 62 } 63 } 64 ProvisioningResultListener(Context context)65 public ProvisioningResultListener(Context context) { 66 mContext = context; 67 } 68 register()69 public void register() { 70 mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISION_RESULT_BROADCAST), 71 Context.RECEIVER_EXPORTED/*UNAUDITED*/); 72 mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISION_RESULT_INTENT), 73 Context.RECEIVER_EXPORTED/*UNAUDITED*/); 74 mIsRegistered = true; 75 } 76 unregister()77 public void unregister() { 78 if (mIsRegistered) { 79 mContext.unregisterReceiver(mReceiver); 80 mIsRegistered = false; 81 } 82 } 83 await(long timeoutSeconds)84 public boolean await(long timeoutSeconds) throws InterruptedException { 85 return mLatch.await(timeoutSeconds, TimeUnit.SECONDS); 86 } 87 setPreprovisioningActivityResult(boolean result)88 public void setPreprovisioningActivityResult(boolean result) { 89 mPreprovisioningActivityResult.set(result); 90 mLatch.countDown(); 91 } 92 getResult()93 public boolean getResult() { 94 Log.i(TAG, "mBroadcastResult: " + mBroadcastResult.get()); 95 Log.i(TAG, "mIntentResult: " + mIntentResult.get()); 96 Log.i(TAG, "mPreprovisioningActivityResult: " + mPreprovisioningActivityResult.get()); 97 return mBroadcastResult.get() && mIntentResult.get() 98 && mPreprovisioningActivityResult.get(); 99 } 100 } 101