Initial commit
This commit is contained in:
@@ -0,0 +1,240 @@
|
||||
package androidx.localbroadcastmanager.content;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.jimu.robotapi.PeripheralType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public final class LocalBroadcastManager {
|
||||
private static final Object f = new Object();
|
||||
private static LocalBroadcastManager g;
|
||||
private final Context a;
|
||||
private final HashMap<BroadcastReceiver, ArrayList<ReceiverRecord>> b = new HashMap<>();
|
||||
private final HashMap<String, ArrayList<ReceiverRecord>> c = new HashMap<>();
|
||||
private final ArrayList<BroadcastRecord> d = new ArrayList<>();
|
||||
private final Handler e;
|
||||
|
||||
private static final class BroadcastRecord {
|
||||
final Intent a;
|
||||
final ArrayList<ReceiverRecord> b;
|
||||
|
||||
BroadcastRecord(Intent intent, ArrayList<ReceiverRecord> arrayList) {
|
||||
this.a = intent;
|
||||
this.b = arrayList;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ReceiverRecord {
|
||||
final IntentFilter a;
|
||||
final BroadcastReceiver b;
|
||||
boolean c;
|
||||
boolean d;
|
||||
|
||||
ReceiverRecord(IntentFilter intentFilter, BroadcastReceiver broadcastReceiver) {
|
||||
this.a = intentFilter;
|
||||
this.b = broadcastReceiver;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder(PeripheralType.SERVO);
|
||||
sb.append("Receiver{");
|
||||
sb.append(this.b);
|
||||
sb.append(" filter=");
|
||||
sb.append(this.a);
|
||||
if (this.d) {
|
||||
sb.append(" DEAD");
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private LocalBroadcastManager(Context context) {
|
||||
this.a = context;
|
||||
this.e = new Handler(context.getMainLooper()) { // from class: androidx.localbroadcastmanager.content.LocalBroadcastManager.1
|
||||
@Override // android.os.Handler
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what != 1) {
|
||||
super.handleMessage(message);
|
||||
} else {
|
||||
LocalBroadcastManager.this.a();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static LocalBroadcastManager a(Context context) {
|
||||
LocalBroadcastManager localBroadcastManager;
|
||||
synchronized (f) {
|
||||
if (g == null) {
|
||||
g = new LocalBroadcastManager(context.getApplicationContext());
|
||||
}
|
||||
localBroadcastManager = g;
|
||||
}
|
||||
return localBroadcastManager;
|
||||
}
|
||||
|
||||
public void a(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
|
||||
synchronized (this.b) {
|
||||
ReceiverRecord receiverRecord = new ReceiverRecord(intentFilter, broadcastReceiver);
|
||||
ArrayList<ReceiverRecord> arrayList = this.b.get(broadcastReceiver);
|
||||
if (arrayList == null) {
|
||||
arrayList = new ArrayList<>(1);
|
||||
this.b.put(broadcastReceiver, arrayList);
|
||||
}
|
||||
arrayList.add(receiverRecord);
|
||||
for (int i = 0; i < intentFilter.countActions(); i++) {
|
||||
String action = intentFilter.getAction(i);
|
||||
ArrayList<ReceiverRecord> arrayList2 = this.c.get(action);
|
||||
if (arrayList2 == null) {
|
||||
arrayList2 = new ArrayList<>(1);
|
||||
this.c.put(action, arrayList2);
|
||||
}
|
||||
arrayList2.add(receiverRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void a(BroadcastReceiver broadcastReceiver) {
|
||||
synchronized (this.b) {
|
||||
ArrayList<ReceiverRecord> remove = this.b.remove(broadcastReceiver);
|
||||
if (remove == null) {
|
||||
return;
|
||||
}
|
||||
for (int size = remove.size() - 1; size >= 0; size--) {
|
||||
ReceiverRecord receiverRecord = remove.get(size);
|
||||
receiverRecord.d = true;
|
||||
for (int i = 0; i < receiverRecord.a.countActions(); i++) {
|
||||
String action = receiverRecord.a.getAction(i);
|
||||
ArrayList<ReceiverRecord> arrayList = this.c.get(action);
|
||||
if (arrayList != null) {
|
||||
for (int size2 = arrayList.size() - 1; size2 >= 0; size2--) {
|
||||
ReceiverRecord receiverRecord2 = arrayList.get(size2);
|
||||
if (receiverRecord2.b == broadcastReceiver) {
|
||||
receiverRecord2.d = true;
|
||||
arrayList.remove(size2);
|
||||
}
|
||||
}
|
||||
if (arrayList.size() <= 0) {
|
||||
this.c.remove(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean a(Intent intent) {
|
||||
String str;
|
||||
ArrayList arrayList;
|
||||
int i;
|
||||
ArrayList<ReceiverRecord> arrayList2;
|
||||
String str2;
|
||||
synchronized (this.b) {
|
||||
String action = intent.getAction();
|
||||
String resolveTypeIfNeeded = intent.resolveTypeIfNeeded(this.a.getContentResolver());
|
||||
Uri data = intent.getData();
|
||||
String scheme = intent.getScheme();
|
||||
Set<String> categories = intent.getCategories();
|
||||
boolean z = (intent.getFlags() & 8) != 0;
|
||||
if (z) {
|
||||
Log.v("LocalBroadcastManager", "Resolving type " + resolveTypeIfNeeded + " scheme " + scheme + " of intent " + intent);
|
||||
}
|
||||
ArrayList<ReceiverRecord> arrayList3 = this.c.get(intent.getAction());
|
||||
if (arrayList3 != null) {
|
||||
if (z) {
|
||||
Log.v("LocalBroadcastManager", "Action list: " + arrayList3);
|
||||
}
|
||||
ArrayList arrayList4 = null;
|
||||
int i2 = 0;
|
||||
while (i2 < arrayList3.size()) {
|
||||
ReceiverRecord receiverRecord = arrayList3.get(i2);
|
||||
if (z) {
|
||||
Log.v("LocalBroadcastManager", "Matching against filter " + receiverRecord.a);
|
||||
}
|
||||
if (receiverRecord.c) {
|
||||
if (z) {
|
||||
Log.v("LocalBroadcastManager", " Filter's target already added");
|
||||
}
|
||||
i = i2;
|
||||
arrayList2 = arrayList3;
|
||||
str = action;
|
||||
str2 = resolveTypeIfNeeded;
|
||||
arrayList = arrayList4;
|
||||
} else {
|
||||
str = action;
|
||||
arrayList = arrayList4;
|
||||
i = i2;
|
||||
arrayList2 = arrayList3;
|
||||
str2 = resolveTypeIfNeeded;
|
||||
int match = receiverRecord.a.match(action, resolveTypeIfNeeded, scheme, data, categories, "LocalBroadcastManager");
|
||||
if (match >= 0) {
|
||||
if (z) {
|
||||
Log.v("LocalBroadcastManager", " Filter matched! match=0x" + Integer.toHexString(match));
|
||||
}
|
||||
arrayList4 = arrayList == null ? new ArrayList() : arrayList;
|
||||
arrayList4.add(receiverRecord);
|
||||
receiverRecord.c = true;
|
||||
i2 = i + 1;
|
||||
action = str;
|
||||
arrayList3 = arrayList2;
|
||||
resolveTypeIfNeeded = str2;
|
||||
} else if (z) {
|
||||
Log.v("LocalBroadcastManager", " Filter did not match: " + (match != -4 ? match != -3 ? match != -2 ? match != -1 ? "unknown reason" : "type" : "data" : "action" : "category"));
|
||||
}
|
||||
}
|
||||
arrayList4 = arrayList;
|
||||
i2 = i + 1;
|
||||
action = str;
|
||||
arrayList3 = arrayList2;
|
||||
resolveTypeIfNeeded = str2;
|
||||
}
|
||||
ArrayList arrayList5 = arrayList4;
|
||||
if (arrayList5 != null) {
|
||||
for (int i3 = 0; i3 < arrayList5.size(); i3++) {
|
||||
((ReceiverRecord) arrayList5.get(i3)).c = false;
|
||||
}
|
||||
this.d.add(new BroadcastRecord(intent, arrayList5));
|
||||
if (!this.e.hasMessages(1)) {
|
||||
this.e.sendEmptyMessage(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void a() {
|
||||
BroadcastRecord[] broadcastRecordArr;
|
||||
while (true) {
|
||||
synchronized (this.b) {
|
||||
int size = this.d.size();
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
broadcastRecordArr = new BroadcastRecord[size];
|
||||
this.d.toArray(broadcastRecordArr);
|
||||
this.d.clear();
|
||||
}
|
||||
for (BroadcastRecord broadcastRecord : broadcastRecordArr) {
|
||||
int size2 = broadcastRecord.b.size();
|
||||
for (int i = 0; i < size2; i++) {
|
||||
ReceiverRecord receiverRecord = broadcastRecord.b.get(i);
|
||||
if (!receiverRecord.d) {
|
||||
receiverRecord.b.onReceive(this.a, broadcastRecord.a);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user