• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISION_RESULT_INTENT));
72         mIsRegistered = true;
73     }
74 
unregister()75     public void unregister() {
76         if (mIsRegistered) {
77             mContext.unregisterReceiver(mReceiver);
78             mIsRegistered = false;
79         }
80     }
81 
await(long timeoutSeconds)82     public boolean await(long timeoutSeconds) throws InterruptedException {
83         return mLatch.await(timeoutSeconds, TimeUnit.SECONDS);
84     }
85 
setPreprovisioningActivityResult(boolean result)86     public void setPreprovisioningActivityResult(boolean result) {
87         mPreprovisioningActivityResult.set(result);
88         mLatch.countDown();
89     }
90 
getResult()91     public boolean getResult() {
92         Log.i(TAG, "mBroadcastResult: " + mBroadcastResult.get());
93         Log.i(TAG, "mIntentResult: " + mIntentResult.get());
94         Log.i(TAG, "mPreprovisioningActivityResult: " + mPreprovisioningActivityResult.get());
95         return mBroadcastResult.get() && mIntentResult.get()
96                 && mPreprovisioningActivityResult.get();
97     }
98 }
99