Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2011 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.server;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.ContextWrapper;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.Handler;
     25 
     26 import com.google.common.collect.Lists;
     27 import com.google.common.util.concurrent.AbstractFuture;
     28 
     29 import java.util.Iterator;
     30 import java.util.List;
     31 import java.util.concurrent.ExecutionException;
     32 import java.util.concurrent.Future;
     33 import java.util.concurrent.TimeUnit;
     34 import java.util.concurrent.TimeoutException;
     35 
     36 /**
     37  * {@link ContextWrapper} that can attach listeners for upcoming
     38  * {@link Context#sendBroadcast(Intent)}.
     39  */
     40 public class BroadcastInterceptingContext extends ContextWrapper {
     41     private static final String TAG = "WatchingContext";
     42 
     43     private final List<BroadcastInterceptor> mInterceptors = Lists.newArrayList();
     44 
     45     public class BroadcastInterceptor extends AbstractFuture<Intent> {
     46         private final BroadcastReceiver mReceiver;
     47         private final IntentFilter mFilter;
     48 
     49         public BroadcastInterceptor(BroadcastReceiver receiver, IntentFilter filter) {
     50             mReceiver = receiver;
     51             mFilter = filter;
     52         }
     53 
     54         public boolean dispatchBroadcast(Intent intent) {
     55             if (mFilter.match(getContentResolver(), intent, false, TAG) > 0) {
     56                 if (mReceiver != null) {
     57                     final Context context = BroadcastInterceptingContext.this;
     58                     mReceiver.onReceive(context, intent);
     59                     return false;
     60                 } else {
     61                     set(intent);
     62                     return true;
     63                 }
     64             } else {
     65                 return false;
     66             }
     67         }
     68 
     69         @Override
     70         public Intent get() throws InterruptedException, ExecutionException {
     71             try {
     72                 return get(5, TimeUnit.SECONDS);
     73             } catch (TimeoutException e) {
     74                 throw new RuntimeException(e);
     75             }
     76         }
     77     }
     78 
     79     public BroadcastInterceptingContext(Context base) {
     80         super(base);
     81     }
     82 
     83     public Future<Intent> nextBroadcastIntent(String action) {
     84         return nextBroadcastIntent(new IntentFilter(action));
     85     }
     86 
     87     public Future<Intent> nextBroadcastIntent(IntentFilter filter) {
     88         final BroadcastInterceptor interceptor = new BroadcastInterceptor(null, filter);
     89         synchronized (mInterceptors) {
     90             mInterceptors.add(interceptor);
     91         }
     92         return interceptor;
     93     }
     94 
     95     @Override
     96     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
     97         synchronized (mInterceptors) {
     98             mInterceptors.add(new BroadcastInterceptor(receiver, filter));
     99         }
    100         return null;
    101     }
    102 
    103     @Override
    104     public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
    105             String broadcastPermission, Handler scheduler) {
    106         return registerReceiver(receiver, filter);
    107     }
    108 
    109     @Override
    110     public void unregisterReceiver(BroadcastReceiver receiver) {
    111         synchronized (mInterceptors) {
    112             final Iterator<BroadcastInterceptor> i = mInterceptors.iterator();
    113             while (i.hasNext()) {
    114                 final BroadcastInterceptor interceptor = i.next();
    115                 if (receiver.equals(interceptor.mReceiver)) {
    116                     i.remove();
    117                 }
    118             }
    119         }
    120     }
    121 
    122     @Override
    123     public void sendBroadcast(Intent intent) {
    124         synchronized (mInterceptors) {
    125             final Iterator<BroadcastInterceptor> i = mInterceptors.iterator();
    126             while (i.hasNext()) {
    127                 final BroadcastInterceptor interceptor = i.next();
    128                 if (interceptor.dispatchBroadcast(intent)) {
    129                     i.remove();
    130                 }
    131             }
    132         }
    133     }
    134 
    135     @Override
    136     public void sendStickyBroadcast(Intent intent) {
    137         sendBroadcast(intent);
    138     }
    139 
    140     @Override
    141     public void sendBroadcast(Intent intent, String receiverPermission) {
    142         sendBroadcast(intent);
    143     }
    144 
    145     @Override
    146     public void removeStickyBroadcast(Intent intent) {
    147         // ignored
    148     }
    149 }
    150