Skip to content

Commit

Permalink
bug fixed.
Browse files Browse the repository at this point in the history
1. check for NPE
2. protection for 'Receiver not registered'
  • Loading branch information
kissonchan committed Mar 3, 2020
1 parent 84f2463 commit e728e0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,39 @@ public abstract class StateUpdateListenerRegister<StateT> {

private final StateUpdatedReceiver receiver = new StateUpdatedReceiver(this);

private final Object mLock = new Object();

protected StateUpdateListenerRegister(PlayCore playCore, IntentFilter intentFilter, Context context) {
this.playCore = playCore;
this.intentFilter = intentFilter;
this.context = context;
}

public final synchronized void registerListener(StateUpdatedListener<StateT> listener) {
this.playCore.debug("registerListener");
if (mStateUpdatedListeners.contains(listener)) {
this.playCore.debug("listener has been registered!");
return;
}
mStateUpdatedListeners.add(listener);
if (mStateUpdatedListeners.size() == 1) {
context.registerReceiver(receiver, intentFilter);
public final void registerListener(StateUpdatedListener<StateT> listener) {
synchronized (mLock) {
this.playCore.debug("registerListener");
if (mStateUpdatedListeners.contains(listener)) {
this.playCore.debug("listener has been registered!");
return;
}
mStateUpdatedListeners.add(listener);
if (mStateUpdatedListeners.size() == 1) {
context.registerReceiver(receiver, intentFilter);
}
}
}

public final synchronized void unregisterListener(StateUpdatedListener<StateT> listener) {
this.playCore.debug("unregisterListener");
mStateUpdatedListeners.remove(listener);
if (mStateUpdatedListeners.isEmpty()) {
context.unregisterReceiver(receiver);
public final void unregisterListener(StateUpdatedListener<StateT> listener) {
synchronized (mLock) {
this.playCore.debug("unregisterListener");
boolean contained = mStateUpdatedListeners.remove(listener);
if (mStateUpdatedListeners.isEmpty() && contained) {
try {
context.unregisterReceiver(receiver);
} catch (IllegalArgumentException e) {
playCore.error(e, "Receiver not registered: " + intentFilter.getAction(0));
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ private static void installSplitResDirs(Context context, Resources preResources,
}
for (Map.Entry<Object, WeakReference<Resources>> entry : activeResources.entrySet()) {
Resources res = entry.getValue().get();
if (res == null) {
continue;
}
if (res == preResources) {
activeResources.put(entry.getKey(), new WeakReference<>(newResources));
SplitLog.i(TAG, "pre-resources found in @mActiveResources");
Expand All @@ -257,6 +260,9 @@ private static void installSplitResDirs(Context context, Resources preResources,
(Map<String, WeakReference<Object>>) mPackagesInActivityThread().get(activityThread);
for (Map.Entry<String, WeakReference<Object>> entry : instance_mPackages.entrySet()) {
Object packageInfo = entry.getValue().get();
if (packageInfo == null) {
continue;
}
Resources resources = (Resources) mResourcesInLoadedApk().get(packageInfo);
if (resources == preResources) {
SplitLog.i(TAG, "pre-resources found in @mPackages");
Expand All @@ -268,6 +274,9 @@ private static void installSplitResDirs(Context context, Resources preResources,
(Map<String, WeakReference<Object>>) mResourcePackagesInActivityThread().get(activityThread);
for (Map.Entry<String, WeakReference<Object>> entry : instance_mResourcePackages.entrySet()) {
Object packageInfo = entry.getValue().get();
if (packageInfo == null) {
continue;
}
Resources resources = (Resources) mResourcesInLoadedApk().get(packageInfo);
if (resources == preResources) {
SplitLog.i(TAG, "pre-resources found in @mResourcePackages");
Expand Down

0 comments on commit e728e0f

Please sign in to comment.