Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Unified Diff: base/android/java/src/org/chromium/base/ApplicationStatus.java

Issue 479603003: [Android] Make ApplicationStatus thread safe (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed findbugs warning Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/ApplicationStatus.java
diff --git a/base/android/java/src/org/chromium/base/ApplicationStatus.java b/base/android/java/src/org/chromium/base/ApplicationStatus.java
index d68160010767b1c12b96f3fa8fa8714c6227b427..954415d8554ca0d9b67c0995b48e052c659715c7 100644
--- a/base/android/java/src/org/chromium/base/ApplicationStatus.java
+++ b/base/android/java/src/org/chromium/base/ApplicationStatus.java
@@ -12,9 +12,9 @@ import android.os.Bundle;
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
@@ -51,6 +51,7 @@ public class ApplicationStatus {
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). */
@@ -63,7 +64,7 @@ public class ApplicationStatus {
* 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.
@@ -187,7 +188,9 @@ public class ApplicationStatus {
}
// Invalidate the cached application state.
- sCachedApplicationState = null;
+ synchronized (sCachedApplicationStateLock) {
+ sCachedApplicationState = null;
+ }
ActivityInfo info = sActivityInfo.get(activity);
info.setStatus(newState);
@@ -235,7 +238,6 @@ public class ApplicationStatus {
* @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));
@@ -302,7 +304,11 @@ public class ApplicationStatus {
* @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();
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698