blob: 27b7419f227a321fb2ab0bf5fcf31f9f17c2a6bc [file] [log] [blame]
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001/*
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
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090017package com.android.internal.util.test;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070018
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.ContextWrapper;
22import android.content.Intent;
23import android.content.IntentFilter;
Dianne Hackborne0e413e2015-12-09 17:22:26 -080024import android.os.Bundle;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070025import android.os.Handler;
Jeff Sharkey3671b1e2013-01-31 17:22:26 -080026import android.os.UserHandle;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070027
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090028import java.util.ArrayList;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070029import java.util.Iterator;
30import java.util.List;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070031import java.util.concurrent.ExecutionException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070032import java.util.concurrent.Future;
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090033import java.util.concurrent.FutureTask;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070034import java.util.concurrent.TimeUnit;
35import java.util.concurrent.TimeoutException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070036
37/**
38 * {@link ContextWrapper} that can attach listeners for upcoming
39 * {@link Context#sendBroadcast(Intent)}.
40 */
41public class BroadcastInterceptingContext extends ContextWrapper {
42 private static final String TAG = "WatchingContext";
43
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090044 private final List<BroadcastInterceptor> mInterceptors = new ArrayList<>();
Jeff Sharkey9599cc52011-05-22 14:59:31 -070045
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090046 public abstract class FutureIntent extends FutureTask<Intent> {
47 public FutureIntent() {
48 super(
49 () -> { throw new IllegalStateException("Cannot happen"); }
50 );
51 }
52 }
53
54 public class BroadcastInterceptor extends FutureIntent {
Jeff Sharkey9599cc52011-05-22 14:59:31 -070055 private final BroadcastReceiver mReceiver;
56 private final IntentFilter mFilter;
57
58 public BroadcastInterceptor(BroadcastReceiver receiver, IntentFilter filter) {
59 mReceiver = receiver;
60 mFilter = filter;
61 }
62
63 public boolean dispatchBroadcast(Intent intent) {
64 if (mFilter.match(getContentResolver(), intent, false, TAG) > 0) {
65 if (mReceiver != null) {
66 final Context context = BroadcastInterceptingContext.this;
67 mReceiver.onReceive(context, intent);
68 return false;
69 } else {
70 set(intent);
71 return true;
72 }
73 } else {
74 return false;
75 }
76 }
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070077
78 @Override
79 public Intent get() throws InterruptedException, ExecutionException {
80 try {
81 return get(5, TimeUnit.SECONDS);
82 } catch (TimeoutException e) {
83 throw new RuntimeException(e);
84 }
85 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -070086 }
87
88 public BroadcastInterceptingContext(Context base) {
89 super(base);
90 }
91
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090092 public FutureIntent nextBroadcastIntent(String action) {
Jeff Sharkey9599cc52011-05-22 14:59:31 -070093 return nextBroadcastIntent(new IntentFilter(action));
94 }
95
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090096 public FutureIntent nextBroadcastIntent(IntentFilter filter) {
Jeff Sharkey9599cc52011-05-22 14:59:31 -070097 final BroadcastInterceptor interceptor = new BroadcastInterceptor(null, filter);
98 synchronized (mInterceptors) {
99 mInterceptors.add(interceptor);
100 }
101 return interceptor;
102 }
103
104 @Override
105 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
106 synchronized (mInterceptors) {
107 mInterceptors.add(new BroadcastInterceptor(receiver, filter));
108 }
109 return null;
110 }
111
112 @Override
113 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
114 String broadcastPermission, Handler scheduler) {
115 return registerReceiver(receiver, filter);
116 }
117
118 @Override
119 public void unregisterReceiver(BroadcastReceiver receiver) {
120 synchronized (mInterceptors) {
121 final Iterator<BroadcastInterceptor> i = mInterceptors.iterator();
122 while (i.hasNext()) {
123 final BroadcastInterceptor interceptor = i.next();
124 if (receiver.equals(interceptor.mReceiver)) {
125 i.remove();
126 }
127 }
128 }
129 }
130
131 @Override
132 public void sendBroadcast(Intent intent) {
133 synchronized (mInterceptors) {
134 final Iterator<BroadcastInterceptor> i = mInterceptors.iterator();
135 while (i.hasNext()) {
136 final BroadcastInterceptor interceptor = i.next();
137 if (interceptor.dispatchBroadcast(intent)) {
138 i.remove();
139 }
140 }
141 }
142 }
143
144 @Override
Jeff Sharkey3671b1e2013-01-31 17:22:26 -0800145 public void sendBroadcast(Intent intent, String receiverPermission) {
146 sendBroadcast(intent);
147 }
148
149 @Override
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700150 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
151 sendBroadcast(intent);
152 }
153
154 @Override
Jeff Sharkey3671b1e2013-01-31 17:22:26 -0800155 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
156 sendBroadcast(intent);
157 }
158
159 @Override
160 public void sendBroadcastAsUser(Intent intent, UserHandle user,
161 String receiverPermission) {
162 sendBroadcast(intent);
163 }
164
165 @Override
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700166 public void sendStickyBroadcast(Intent intent) {
167 sendBroadcast(intent);
168 }
169
170 @Override
Jeff Sharkey3671b1e2013-01-31 17:22:26 -0800171 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700172 sendBroadcast(intent);
173 }
174
175 @Override
Dianne Hackborne0e413e2015-12-09 17:22:26 -0800176 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
177 sendBroadcast(intent);
178 }
179
180 @Override
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700181 public void removeStickyBroadcast(Intent intent) {
182 // ignored
183 }
184}