Home | History | Annotate | Download | only in tools
      1 /*
      2  * Copyright (C) 2017 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 android.app.cts.android.app.cts.tools;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.os.Handler;
     24 import android.os.SystemClock;
     25 
     26 /**
     27  * Helper for sending a broadcast and waiting for a result from it.
     28  */
     29 public final class SyncOrderedBroadcast {
     30     boolean mHasResult;
     31     int mReceivedCode;
     32     String mReceivedData;
     33     Bundle mReceivedExtras;
     34 
     35     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
     36         @Override public void onReceive(Context context, Intent intent) {
     37             synchronized (SyncOrderedBroadcast.this) {
     38                 mReceivedCode = getResultCode();
     39                 mReceivedData = getResultData();
     40                 mReceivedExtras = getResultExtras(false);
     41                 mHasResult = true;
     42                 SyncOrderedBroadcast.this.notifyAll();
     43             }
     44         }
     45     };
     46 
     47     public int sendAndWait(Context context, Intent broadcast, int initialCode,
     48             String initialData, Bundle initialExtras, long timeout) {
     49         mHasResult = false;
     50         context.sendOrderedBroadcast(broadcast, null, mReceiver,
     51                 new Handler(context.getMainLooper()), initialCode, initialData, initialExtras);
     52 
     53         final long endTime = SystemClock.uptimeMillis() + timeout;
     54 
     55         synchronized (this) {
     56             while (!mHasResult) {
     57                 final long now = SystemClock.uptimeMillis();
     58                 if (now >= endTime) {
     59                     throw new IllegalStateException("Timed out waiting for broadcast " + broadcast);
     60                 }
     61                 try {
     62                     wait(endTime - now);
     63                 } catch (InterruptedException e) {
     64                 }
     65             }
     66             return mReceivedCode;
     67         }
     68     }
     69 
     70     public int getReceivedCode() {
     71         return mReceivedCode;
     72     }
     73 
     74     public String getReceivedData() {
     75         return mReceivedData;
     76     }
     77 
     78     public Bundle getReceivedExtras() {
     79         return mReceivedExtras;
     80     }
     81 }
     82