Skip to content

Commit

Permalink
[Android] Make ApplicationStatus thread safe
Browse files Browse the repository at this point in the history
- Make sActivityInfo a ConcurrentHashMap instead of just a HashMap.

BUG=403951

Review URL: https://codereview.chromium.org/479603003

Cr-Commit-Position: refs/heads/master@{#290379}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@290379 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dtrainor@chromium.org committed Aug 18, 2014
1 parent c9b3972 commit cbc581e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions base/android/java/src/org/chromium/base/ApplicationStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Provides information about the current activity's status, and a way
Expand Down Expand Up @@ -51,6 +51,7 @@ public ObserverList<ActivityStateListener> getListeners() {

private static Application sApplication;

private static Object sCachedApplicationStateLock = new Object();
private static Integer sCachedApplicationState;

/** Last activity that was shown (or null if none or it was destroyed). */
Expand All @@ -63,7 +64,7 @@ public ObserverList<ActivityStateListener> getListeners() {
* A map of which observers listen to state changes from which {@link Activity}.
*/
private static final Map<Activity, ActivityInfo> sActivityInfo =
new HashMap<Activity, ActivityInfo>();
new ConcurrentHashMap<Activity, ActivityInfo>();

/**
* A list of observers to be notified when any {@link Activity} has a state change.
Expand Down Expand Up @@ -187,7 +188,9 @@ private static void onStateChange(Activity activity, int newState) {
}

// Invalidate the cached application state.
sCachedApplicationState = null;
synchronized (sCachedApplicationStateLock) {
sCachedApplicationState = null;
}

ActivityInfo info = sActivityInfo.get(activity);
info.setStatus(newState);
Expand Down Expand Up @@ -235,7 +238,6 @@ public static Activity getLastTrackedFocusedActivity() {
* @return A {@link List} of all non-destroyed {@link Activity}s.
*/
public static List<WeakReference<Activity>> getRunningActivities() {
ThreadUtils.assertOnUiThread();
List<WeakReference<Activity>> activities = new ArrayList<WeakReference<Activity>>();
for (Activity activity : sActivityInfo.keySet()) {
activities.add(new WeakReference<Activity>(activity));
Expand Down Expand Up @@ -302,7 +304,11 @@ public static int getStateForActivity(Activity activity) {
* @return The state of the application (see {@link ApplicationState}).
*/
public static int getStateForApplication() {
if (sCachedApplicationState == null) sCachedApplicationState = determineApplicationState();
synchronized (sCachedApplicationStateLock) {
if (sCachedApplicationState == null) {
sCachedApplicationState = determineApplicationState();
}
}

return sCachedApplicationState.intValue();
}
Expand Down

0 comments on commit cbc581e

Please sign in to comment.