Rabbit-R1/switch port/java/sources/androidx/localbroadcastmanager/content/LocalBroadcastManager.java
2024-05-21 17:08:36 -04:00

255 lines
10 KiB
Java

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 java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/* loaded from: classes2.dex */
public final class LocalBroadcastManager {
private static final boolean DEBUG = false;
static final int MSG_EXEC_PENDING_BROADCASTS = 1;
private static final String TAG = "LocalBroadcastManager";
private static LocalBroadcastManager mInstance;
private static final Object mLock = new Object();
private final Context mAppContext;
private final Handler mHandler;
private final HashMap<BroadcastReceiver, ArrayList<ReceiverRecord>> mReceivers = new HashMap<>();
private final HashMap<String, ArrayList<ReceiverRecord>> mActions = new HashMap<>();
private final ArrayList<BroadcastRecord> mPendingBroadcasts = new ArrayList<>();
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public static final class ReceiverRecord {
boolean broadcasting;
boolean dead;
final IntentFilter filter;
final BroadcastReceiver receiver;
ReceiverRecord(IntentFilter intentFilter, BroadcastReceiver broadcastReceiver) {
this.filter = intentFilter;
this.receiver = broadcastReceiver;
}
public String toString() {
StringBuilder sb = new StringBuilder(128);
sb.append("Receiver{");
sb.append(this.receiver);
sb.append(" filter=");
sb.append(this.filter);
if (this.dead) {
sb.append(" DEAD");
}
sb.append("}");
return sb.toString();
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public static final class BroadcastRecord {
final Intent intent;
final ArrayList<ReceiverRecord> receivers;
BroadcastRecord(Intent intent, ArrayList<ReceiverRecord> arrayList) {
this.intent = intent;
this.receivers = arrayList;
}
}
public static LocalBroadcastManager getInstance(Context context) {
LocalBroadcastManager localBroadcastManager;
synchronized (mLock) {
if (mInstance == null) {
mInstance = new LocalBroadcastManager(context.getApplicationContext());
}
localBroadcastManager = mInstance;
}
return localBroadcastManager;
}
private LocalBroadcastManager(Context context) {
this.mAppContext = context;
this.mHandler = new Handler(context.getMainLooper()) { // from class: androidx.localbroadcastmanager.content.LocalBroadcastManager.1
@Override // android.os.Handler
public void handleMessage(Message message) {
if (message.what == 1) {
LocalBroadcastManager.this.executePendingBroadcasts();
} else {
super.handleMessage(message);
}
}
};
}
public void registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
synchronized (this.mReceivers) {
ReceiverRecord receiverRecord = new ReceiverRecord(intentFilter, broadcastReceiver);
ArrayList<ReceiverRecord> arrayList = this.mReceivers.get(broadcastReceiver);
if (arrayList == null) {
arrayList = new ArrayList<>(1);
this.mReceivers.put(broadcastReceiver, arrayList);
}
arrayList.add(receiverRecord);
for (int i = 0; i < intentFilter.countActions(); i++) {
String action = intentFilter.getAction(i);
ArrayList<ReceiverRecord> arrayList2 = this.mActions.get(action);
if (arrayList2 == null) {
arrayList2 = new ArrayList<>(1);
this.mActions.put(action, arrayList2);
}
arrayList2.add(receiverRecord);
}
}
}
public void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
synchronized (this.mReceivers) {
ArrayList<ReceiverRecord> remove = this.mReceivers.remove(broadcastReceiver);
if (remove == null) {
return;
}
for (int size = remove.size() - 1; size >= 0; size--) {
ReceiverRecord receiverRecord = remove.get(size);
receiverRecord.dead = true;
for (int i = 0; i < receiverRecord.filter.countActions(); i++) {
String action = receiverRecord.filter.getAction(i);
ArrayList<ReceiverRecord> arrayList = this.mActions.get(action);
if (arrayList != null) {
for (int size2 = arrayList.size() - 1; size2 >= 0; size2--) {
ReceiverRecord receiverRecord2 = arrayList.get(size2);
if (receiverRecord2.receiver == broadcastReceiver) {
receiverRecord2.dead = true;
arrayList.remove(size2);
}
}
if (arrayList.size() <= 0) {
this.mActions.remove(action);
}
}
}
}
}
}
/* JADX WARN: Multi-variable type inference failed */
public boolean sendBroadcast(Intent intent) {
boolean z;
ArrayList<ReceiverRecord> arrayList;
int i;
String str;
boolean z2;
synchronized (this.mReceivers) {
String action = intent.getAction();
String resolveTypeIfNeeded = intent.resolveTypeIfNeeded(this.mAppContext.getContentResolver());
Uri data = intent.getData();
String scheme = intent.getScheme();
Set<String> categories = intent.getCategories();
boolean z3 = true;
Object[] objArr = (intent.getFlags() & 8) != 0;
if (objArr != false) {
Log.v(TAG, "Resolving type " + resolveTypeIfNeeded + " scheme " + scheme + " of intent " + intent);
}
ArrayList<ReceiverRecord> arrayList2 = this.mActions.get(intent.getAction());
if (arrayList2 != null) {
if (objArr != false) {
Log.v(TAG, "Action list: " + arrayList2);
}
ArrayList arrayList3 = null;
int i2 = 0;
while (i2 < arrayList2.size()) {
ReceiverRecord receiverRecord = arrayList2.get(i2);
if (objArr != false) {
Log.v(TAG, "Matching against filter " + receiverRecord.filter);
}
if (receiverRecord.broadcasting) {
if (objArr != false) {
Log.v(TAG, " Filter's target already added");
}
arrayList = arrayList2;
i = i2;
str = action;
z2 = z3;
} else {
String str2 = action;
arrayList = arrayList2;
i = i2;
str = action;
z2 = z3;
int match = receiverRecord.filter.match(str2, resolveTypeIfNeeded, scheme, data, categories, TAG);
if (match >= 0) {
if (objArr != false) {
Log.v(TAG, " Filter matched! match=0x" + Integer.toHexString(match));
}
if (arrayList3 == null) {
arrayList3 = new ArrayList();
}
arrayList3.add(receiverRecord);
receiverRecord.broadcasting = z2;
} else if (objArr != false) {
Log.v(TAG, " Filter did not match: " + (match != -4 ? match != -3 ? match != -2 ? match != -1 ? "unknown reason" : "type" : "data" : "action" : "category"));
}
}
i2 = i + 1;
z3 = z2;
arrayList2 = arrayList;
action = str;
}
boolean z4 = z3;
if (arrayList3 != null) {
for (int i3 = 0; i3 < arrayList3.size(); i3++) {
((ReceiverRecord) arrayList3.get(i3)).broadcasting = false;
}
this.mPendingBroadcasts.add(new BroadcastRecord(intent, arrayList3));
if (!this.mHandler.hasMessages(z4 ? 1 : 0)) {
this.mHandler.sendEmptyMessage(z4 ? 1 : 0);
}
return z4;
}
z = false;
} else {
z = false;
}
return z;
}
}
public void sendBroadcastSync(Intent intent) {
if (sendBroadcast(intent)) {
executePendingBroadcasts();
}
}
void executePendingBroadcasts() {
int size;
BroadcastRecord[] broadcastRecordArr;
while (true) {
synchronized (this.mReceivers) {
size = this.mPendingBroadcasts.size();
if (size <= 0) {
return;
}
broadcastRecordArr = new BroadcastRecord[size];
this.mPendingBroadcasts.toArray(broadcastRecordArr);
this.mPendingBroadcasts.clear();
}
for (int i = 0; i < size; i++) {
BroadcastRecord broadcastRecord = broadcastRecordArr[i];
int size2 = broadcastRecord.receivers.size();
for (int i2 = 0; i2 < size2; i2++) {
ReceiverRecord receiverRecord = broadcastRecord.receivers.get(i2);
if (!receiverRecord.dead) {
receiverRecord.receiver.onReceive(this.mAppContext, broadcastRecord.intent);
}
}
}
}
}
}