summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOwen Lin <owenlin@google.com>2012-09-04 16:53:42 +0800
committerOwen Lin <owenlin@google.com>2012-09-11 12:21:59 +0800
commit5251514287e41c57076029b08484d20beb5967b5 (patch)
tree355dc94f09d8963c5ba2c0c3abb5b45878e4b5a5 /src
parent960306bb25cb79ded4f5cb0f3b329ff042e0e837 (diff)
downloadandroid_packages_apps_Snap-5251514287e41c57076029b08484d20beb5967b5.tar.gz
android_packages_apps_Snap-5251514287e41c57076029b08484d20beb5967b5.tar.bz2
android_packages_apps_Snap-5251514287e41c57076029b08484d20beb5967b5.zip
Ensure peekObject is called in the syncrhonization block.
bug: 7019598 Change-Id: If153287078826afc9827ea64364b4fb64f2b3a5f
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/app/AlbumDataLoader.java20
-rw-r--r--src/com/android/gallery3d/app/AlbumSetDataLoader.java47
-rw-r--r--src/com/android/gallery3d/app/PhotoDataAdapter.java83
-rw-r--r--src/com/android/gallery3d/data/ClusterAlbumSet.java11
-rw-r--r--src/com/android/gallery3d/data/DataManager.java41
-rw-r--r--src/com/android/gallery3d/data/LocalAlbum.java18
-rw-r--r--src/com/android/gallery3d/data/MediaSource.java15
-rw-r--r--src/com/android/gallery3d/data/MtpDevice.java16
-rw-r--r--src/com/android/gallery3d/data/Path.java2
9 files changed, 125 insertions, 128 deletions
diff --git a/src/com/android/gallery3d/app/AlbumDataLoader.java b/src/com/android/gallery3d/app/AlbumDataLoader.java
index ff950522a..8093bb983 100644
--- a/src/com/android/gallery3d/app/AlbumDataLoader.java
+++ b/src/com/android/gallery3d/app/AlbumDataLoader.java
@@ -22,7 +22,6 @@ import android.os.Process;
import com.android.gallery3d.common.Utils;
import com.android.gallery3d.data.ContentListener;
-import com.android.gallery3d.data.DataManager;
import com.android.gallery3d.data.MediaItem;
import com.android.gallery3d.data.MediaObject;
import com.android.gallery3d.data.MediaSet;
@@ -347,21 +346,16 @@ public class AlbumDataLoader {
}
mDirty = false;
updateLoading(true);
- long version;
- synchronized (DataManager.LOCK) {
- version = mSource.reload();
- }
+ long version = mSource.reload();
UpdateInfo info = executeAndWait(new GetUpdateInfo(version));
updateComplete = info == null;
if (updateComplete) continue;
- synchronized (DataManager.LOCK) {
- if (info.version != version) {
- info.size = mSource.getMediaItemCount();
- info.version = version;
- }
- if (info.reloadCount > 0) {
- info.items = mSource.getMediaItem(info.reloadStart, info.reloadCount);
- }
+ if (info.version != version) {
+ info.size = mSource.getMediaItemCount();
+ info.version = version;
+ }
+ if (info.reloadCount > 0) {
+ info.items = mSource.getMediaItem(info.reloadStart, info.reloadCount);
}
executeAndWait(new UpdateContent(info));
}
diff --git a/src/com/android/gallery3d/app/AlbumSetDataLoader.java b/src/com/android/gallery3d/app/AlbumSetDataLoader.java
index 80bc48a46..9f7014ecd 100644
--- a/src/com/android/gallery3d/app/AlbumSetDataLoader.java
+++ b/src/com/android/gallery3d/app/AlbumSetDataLoader.java
@@ -19,11 +19,9 @@ package com.android.gallery3d.app;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
-import android.os.SystemClock;
import com.android.gallery3d.common.Utils;
import com.android.gallery3d.data.ContentListener;
-import com.android.gallery3d.data.DataManager;
import com.android.gallery3d.data.MediaItem;
import com.android.gallery3d.data.MediaObject;
import com.android.gallery3d.data.MediaSet;
@@ -353,39 +351,28 @@ public class AlbumSetDataLoader {
mDirty = false;
updateLoading(true);
- long version;
- synchronized (DataManager.LOCK) {
- long start = SystemClock.uptimeMillis();
- version = mSource.reload();
- long duration = SystemClock.uptimeMillis() - start;
- if (duration > 20) {
- Log.v("DebugLoadingTime", "finish reload - " + duration);
- }
- }
+ long version = mSource.reload();
UpdateInfo info = executeAndWait(new GetUpdateInfo(version));
updateComplete = info == null;
if (updateComplete) continue;
-
- synchronized (DataManager.LOCK) {
- if (info.version != version) {
- info.version = version;
- info.size = mSource.getSubMediaSetCount();
-
- // If the size becomes smaller after reload(), we may
- // receive from GetUpdateInfo an index which is too
- // big. Because the main thread is not aware of the size
- // change until we call UpdateContent.
- if (info.index >= info.size) {
- info.index = INDEX_NONE;
- }
- }
- if (info.index != INDEX_NONE) {
- info.item = mSource.getSubMediaSet(info.index);
- if (info.item == null) continue;
- info.cover = info.item.getCoverMediaItem();
- info.totalCount = info.item.getTotalMediaItemCount();
+ if (info.version != version) {
+ info.version = version;
+ info.size = mSource.getSubMediaSetCount();
+
+ // If the size becomes smaller after reload(), we may
+ // receive from GetUpdateInfo an index which is too
+ // big. Because the main thread is not aware of the size
+ // change until we call UpdateContent.
+ if (info.index >= info.size) {
+ info.index = INDEX_NONE;
}
}
+ if (info.index != INDEX_NONE) {
+ info.item = mSource.getSubMediaSet(info.index);
+ if (info.item == null) continue;
+ info.cover = info.item.getCoverMediaItem();
+ info.totalCount = info.item.getTotalMediaItemCount();
+ }
executeAndWait(new UpdateContent(info));
}
updateLoading(false);
diff --git a/src/com/android/gallery3d/app/PhotoDataAdapter.java b/src/com/android/gallery3d/app/PhotoDataAdapter.java
index abe4e8aa3..5ba2afceb 100644
--- a/src/com/android/gallery3d/app/PhotoDataAdapter.java
+++ b/src/com/android/gallery3d/app/PhotoDataAdapter.java
@@ -25,7 +25,6 @@ import com.android.gallery3d.common.BitmapUtils;
import com.android.gallery3d.common.Utils;
import com.android.gallery3d.data.BitmapPool;
import com.android.gallery3d.data.ContentListener;
-import com.android.gallery3d.data.DataManager;
import com.android.gallery3d.data.LocalMediaItem;
import com.android.gallery3d.data.MediaItem;
import com.android.gallery3d.data.MediaObject;
@@ -1007,58 +1006,56 @@ public class PhotoDataAdapter implements PhotoPage.Model {
}
mDirty = false;
UpdateInfo info = executeAndWait(new GetUpdateInfo());
- synchronized (DataManager.LOCK) {
- updateLoading(true);
- long version = mSource.reload();
- if (info.version != version) {
- info.reloadContent = true;
- info.size = mSource.getMediaItemCount();
- }
- if (!info.reloadContent) continue;
- info.items = mSource.getMediaItem(
- info.contentStart, info.contentEnd);
-
- int index = MediaSet.INDEX_NOT_FOUND;
+ updateLoading(true);
+ long version = mSource.reload();
+ if (info.version != version) {
+ info.reloadContent = true;
+ info.size = mSource.getMediaItemCount();
+ }
+ if (!info.reloadContent) continue;
+ info.items = mSource.getMediaItem(
+ info.contentStart, info.contentEnd);
- // First try to focus on the given hint path if there is one.
- if (mFocusHintPath != null) {
- index = findIndexOfPathInCache(info, mFocusHintPath);
- mFocusHintPath = null;
- }
+ int index = MediaSet.INDEX_NOT_FOUND;
- // Otherwise try to see if the currently focused item can be found.
- if (index == MediaSet.INDEX_NOT_FOUND) {
- MediaItem item = findCurrentMediaItem(info);
- if (item != null && item.getPath() == info.target) {
- index = info.indexHint;
- } else {
- index = findIndexOfTarget(info);
- }
- }
+ // First try to focus on the given hint path if there is one.
+ if (mFocusHintPath != null) {
+ index = findIndexOfPathInCache(info, mFocusHintPath);
+ mFocusHintPath = null;
+ }
- // The image has been deleted. Focus on the next image (keep
- // mCurrentIndex unchanged) or the previous image (decrease
- // mCurrentIndex by 1). In page mode we want to see the next
- // image, so we focus on the next one. In film mode we want the
- // later images to shift left to fill the empty space, so we
- // focus on the previous image (so it will not move). In any
- // case the index needs to be limited to [0, mSize).
- if (index == MediaSet.INDEX_NOT_FOUND) {
+ // Otherwise try to see if the currently focused item can be found.
+ if (index == MediaSet.INDEX_NOT_FOUND) {
+ MediaItem item = findCurrentMediaItem(info);
+ if (item != null && item.getPath() == info.target) {
index = info.indexHint;
- if (mFocusHintDirection == FOCUS_HINT_PREVIOUS
- && index > 0) {
- index--;
- }
+ } else {
+ index = findIndexOfTarget(info);
}
+ }
- // Don't change index if mSize == 0
- if (mSize > 0) {
- if (index >= mSize) index = mSize - 1;
+ // The image has been deleted. Focus on the next image (keep
+ // mCurrentIndex unchanged) or the previous image (decrease
+ // mCurrentIndex by 1). In page mode we want to see the next
+ // image, so we focus on the next one. In film mode we want the
+ // later images to shift left to fill the empty space, so we
+ // focus on the previous image (so it will not move). In any
+ // case the index needs to be limited to [0, mSize).
+ if (index == MediaSet.INDEX_NOT_FOUND) {
+ index = info.indexHint;
+ if (mFocusHintDirection == FOCUS_HINT_PREVIOUS
+ && index > 0) {
+ index--;
}
+ }
- info.indexHint = index;
+ // Don't change index if mSize == 0
+ if (mSize > 0) {
+ if (index >= mSize) index = mSize - 1;
}
+ info.indexHint = index;
+
executeAndWait(new UpdateContent(info));
}
}
diff --git a/src/com/android/gallery3d/data/ClusterAlbumSet.java b/src/com/android/gallery3d/data/ClusterAlbumSet.java
index a32ba87f4..cb212ba36 100644
--- a/src/com/android/gallery3d/data/ClusterAlbumSet.java
+++ b/src/com/android/gallery3d/data/ClusterAlbumSet.java
@@ -112,10 +112,13 @@ public class ClusterAlbumSet extends MediaSet implements ContentListener {
} else {
childPath = mPath.getChild(i);
}
- ClusterAlbum album = (ClusterAlbum) dataManager.peekMediaObject(
- childPath);
- if (album == null) {
- album = new ClusterAlbum(childPath, dataManager, this);
+
+ ClusterAlbum album;
+ synchronized (DataManager.LOCK) {
+ album = (ClusterAlbum) dataManager.peekMediaObject(childPath);
+ if (album == null) {
+ album = new ClusterAlbum(childPath, dataManager, this);
+ }
}
album.setMediaItems(clustering.getCluster(i));
album.setName(childName);
diff --git a/src/com/android/gallery3d/data/DataManager.java b/src/com/android/gallery3d/data/DataManager.java
index 0b4dae2c2..eeab8a885 100644
--- a/src/com/android/gallery3d/data/DataManager.java
+++ b/src/com/android/gallery3d/data/DataManager.java
@@ -156,29 +156,38 @@ public class DataManager {
mSourceMap.put(source.getPrefix(), source);
}
+ // A common usage of this method is:
+ // synchronized (DataManager.LOCK) {
+ // MediaObject object = peekMediaObject(path);
+ // if (object == null) {
+ // object = createMediaObject(...);
+ // }
+ // }
public MediaObject peekMediaObject(Path path) {
return path.getObject();
}
public MediaObject getMediaObject(Path path) {
- MediaObject obj = path.getObject();
- if (obj != null) return obj;
-
- MediaSource source = mSourceMap.get(path.getPrefix());
- if (source == null) {
- Log.w(TAG, "cannot find media source for path: " + path);
- return null;
- }
+ synchronized (LOCK) {
+ MediaObject obj = path.getObject();
+ if (obj != null) return obj;
+
+ MediaSource source = mSourceMap.get(path.getPrefix());
+ if (source == null) {
+ Log.w(TAG, "cannot find media source for path: " + path);
+ return null;
+ }
- try {
- MediaObject object = source.createMediaObject(path);
- if (object == null) {
- Log.w(TAG, "cannot create media object: " + path);
+ try {
+ MediaObject object = source.createMediaObject(path);
+ if (object == null) {
+ Log.w(TAG, "cannot create media object: " + path);
+ }
+ return object;
+ } catch (Throwable t) {
+ Log.w(TAG, "exception in creating media object: " + path, t);
+ return null;
}
- return object;
- } catch (Throwable t) {
- Log.w(TAG, "exception in creating media object: " + path, t);
- return null;
}
}
diff --git a/src/com/android/gallery3d/data/LocalAlbum.java b/src/com/android/gallery3d/data/LocalAlbum.java
index eaf1e54fc..4682e7792 100644
--- a/src/com/android/gallery3d/data/LocalAlbum.java
+++ b/src/com/android/gallery3d/data/LocalAlbum.java
@@ -140,17 +140,19 @@ public class LocalAlbum extends MediaSet {
private static MediaItem loadOrUpdateItem(Path path, Cursor cursor,
DataManager dataManager, GalleryApp app, boolean isImage) {
- LocalMediaItem item = (LocalMediaItem) dataManager.peekMediaObject(path);
- if (item == null) {
- if (isImage) {
- item = new LocalImage(path, app, cursor);
+ synchronized (DataManager.LOCK) {
+ LocalMediaItem item = (LocalMediaItem) dataManager.peekMediaObject(path);
+ if (item == null) {
+ if (isImage) {
+ item = new LocalImage(path, app, cursor);
+ } else {
+ item = new LocalVideo(path, app, cursor);
+ }
} else {
- item = new LocalVideo(path, app, cursor);
+ item.updateContent(cursor);
}
- } else {
- item.updateContent(cursor);
+ return item;
}
- return item;
}
// The pids array are sorted by the (path) id.
diff --git a/src/com/android/gallery3d/data/MediaSource.java b/src/com/android/gallery3d/data/MediaSource.java
index 9f7561ec6..95901283b 100644
--- a/src/com/android/gallery3d/data/MediaSource.java
+++ b/src/com/android/gallery3d/data/MediaSource.java
@@ -77,12 +77,15 @@ public abstract class MediaSource {
int n = list.size();
for (int i = 0; i < n; i++) {
PathId pid = list.get(i);
- MediaObject obj = pid.path.getObject();
- if (obj == null) {
- try {
- obj = createMediaObject(pid.path);
- } catch (Throwable th) {
- Log.w(TAG, "cannot create media object: " + pid.path, th);
+ MediaObject obj;
+ synchronized (DataManager.LOCK) {
+ obj = pid.path.getObject();
+ if (obj == null) {
+ try {
+ obj = createMediaObject(pid.path);
+ } catch (Throwable th) {
+ Log.w(TAG, "cannot create media object: " + pid.path, th);
+ }
}
}
if (obj != null) {
diff --git a/src/com/android/gallery3d/data/MtpDevice.java b/src/com/android/gallery3d/data/MtpDevice.java
index 999f55420..25ffbc8ba 100644
--- a/src/com/android/gallery3d/data/MtpDevice.java
+++ b/src/com/android/gallery3d/data/MtpDevice.java
@@ -127,14 +127,16 @@ public class MtpDevice extends MediaSet {
for (int i = begin; i < end; i++) {
MtpObjectInfo child = mJpegChildren.get(i);
Path childPath = mItemPath.getChild(child.getObjectHandle());
- MtpImage image = (MtpImage) dataManager.peekMediaObject(childPath);
- if (image == null) {
- image = new MtpImage(
- childPath, mApplication, mDeviceId, child, mMtpContext);
- } else {
- image.updateContent(child);
+ synchronized (DataManager.LOCK) {
+ MtpImage image = (MtpImage) dataManager.peekMediaObject(childPath);
+ if (image == null) {
+ image = new MtpImage(
+ childPath, mApplication, mDeviceId, child, mMtpContext);
+ } else {
+ image.updateContent(child);
+ }
+ result.add(image);
}
- result.add(image);
}
return result;
}
diff --git a/src/com/android/gallery3d/data/Path.java b/src/com/android/gallery3d/data/Path.java
index dfb91315e..a4840bdbf 100644
--- a/src/com/android/gallery3d/data/Path.java
+++ b/src/com/android/gallery3d/data/Path.java
@@ -72,7 +72,7 @@ public class Path {
}
}
- public MediaObject getObject() {
+ MediaObject getObject() {
synchronized (Path.class) {
return (mObject == null) ? null : mObject.get();
}