Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 05da3f52 authored by Hung-ying Tyan's avatar Hung-ying Tyan
Browse files

Show sync error toast only when both loading and syncing are done.

Bug: 7427597
Change-Id: Ie1fe8510e2a2225424b3c2892b2c0a8b4021324c
parent 38c5d92c
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -72,6 +72,8 @@ public class AlbumDataLoader {
    private LoadingListener mLoadingListener;

    private ReloadTask mReloadTask;
    // the data version on which last loading failed
    private long mFailedVersion = MediaObject.INVALID_DATA_VERSION;

    public AlbumDataLoader(AbstractGalleryActivity context, MediaSet mediaSet) {
        mSource = mediaSet;
@@ -93,7 +95,11 @@ public class AlbumDataLoader {
                        if (mLoadingListener != null) mLoadingListener.onLoadingStarted();
                        return;
                    case MSG_LOAD_FINISH:
                        if (mLoadingListener != null) mLoadingListener.onLoadingFinished();
                        if (mLoadingListener != null) {
                            boolean loadingFailed =
                                    (mFailedVersion != MediaObject.INVALID_DATA_VERSION);
                            mLoadingListener.onLoadingFinished(loadingFailed);
                        }
                        return;
                }
            }
@@ -245,6 +251,10 @@ public class AlbumDataLoader {

        @Override
        public UpdateInfo call() throws Exception {
            if (mFailedVersion == mVersion) {
                // previous loading failed, return null to pause loading
                return null;
            }
            UpdateInfo info = new UpdateInfo();
            long version = mVersion;
            info.version = mSourceVersion;
@@ -283,7 +293,14 @@ public class AlbumDataLoader {

            ArrayList<MediaItem> items = info.items;

            if (items == null) return null;
            mFailedVersion = MediaObject.INVALID_DATA_VERSION;
            if ((items == null) || items.isEmpty()) {
                if (info.reloadCount > 0) {
                    mFailedVersion = info.version;
                    Log.d(TAG, "loading failed: " + mFailedVersion);
                }
                return null;
            }
            int start = Math.max(info.reloadStart, mContentStart);
            int end = Math.min(info.reloadStart + items.size(), mContentEnd);

@@ -340,11 +357,17 @@ public class AlbumDataLoader {
                synchronized (this) {
                    if (mActive && !mDirty && updateComplete) {
                        updateLoading(false);
                        if (mFailedVersion != MediaObject.INVALID_DATA_VERSION) {
                            Log.d(TAG, "reload pause");
                        }
                        Utils.waitWithoutInterrupt(this);
                        continue;
                        if (mActive && (mFailedVersion != MediaObject.INVALID_DATA_VERSION)) {
                            Log.d(TAG, "reload resume");
                        }
                        continue;
                    }
                    mDirty = false;
                }
                updateLoading(true);
                long version = mSource.reload();
                UpdateInfo info = executeAndWait(new GetUpdateInfo(version));
+22 −7
Original line number Diff line number Diff line
@@ -108,6 +108,8 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster

    private int mLoadingBits = 0;
    private boolean mInitialSynced = false;
    private int mSyncResult;
    private boolean mLoadingFailed;
    private RelativePosition mOpenCenter = new RelativePosition();

    private Handler mHandler;
@@ -419,6 +421,7 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster

        // Set the reload bit here to prevent it exit this page in clearLoadingBit().
        setLoadingBit(BIT_LOADING_RELOAD);
        mLoadingFailed = false;
        mAlbumDataAdapter.resume();

        mAlbumView.resume();
@@ -693,17 +696,13 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
            public void run() {
                GLRoot root = mActivity.getGLRoot();
                root.lockRenderThread();
                mSyncResult = resultCode;
                try {
                    if (resultCode == MediaSet.SYNC_RESULT_SUCCESS) {
                        mInitialSynced = true;
                    }
                    clearLoadingBit(BIT_LOADING_SYNC);
                    if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive
                            && (mAlbumDataAdapter.size() == 0)) {
                        // show error toast only if the album is empty
                        Toast.makeText(mActivity, R.string.sync_album_error,
                                Toast.LENGTH_LONG).show();
                    }
                    showSyncErrorIfNecessary(mLoadingFailed);
                } finally {
                    root.unlockRenderThread();
                }
@@ -711,6 +710,19 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
        });
    }

    // Show sync error toast when all the following conditions are met:
    // (1) both loading and sync are done,
    // (2) sync result is error,
    // (3) the page is still active, and
    // (4) no photo is shown or loading fails.
    private void showSyncErrorIfNecessary(boolean loadingFailed) {
        if ((mLoadingBits == 0) && (mSyncResult == MediaSet.SYNC_RESULT_ERROR) && mIsActive
                && (loadingFailed || (mAlbumDataAdapter.size() == 0))) {
            Toast.makeText(mActivity, R.string.sync_album_error,
                    Toast.LENGTH_LONG).show();
        }
    }

    private void setLoadingBit(int loadTaskBit) {
        mLoadingBits |= loadTaskBit;
    }
@@ -731,11 +743,14 @@ public class AlbumPage extends ActivityState implements GalleryActionBar.Cluster
        @Override
        public void onLoadingStarted() {
            setLoadingBit(BIT_LOADING_RELOAD);
            mLoadingFailed = false;
        }

        @Override
        public void onLoadingFinished() {
        public void onLoadingFinished(boolean loadingFailed) {
            clearLoadingBit(BIT_LOADING_RELOAD);
            mLoadingFailed = loadingFailed;
            showSyncErrorIfNecessary(loadingFailed);
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ public class AlbumSetDataLoader {
                        if (mLoadingListener != null) mLoadingListener.onLoadingStarted();
                        return;
                    case MSG_LOAD_FINISH:
                        if (mLoadingListener != null) mLoadingListener.onLoadingFinished();
                        if (mLoadingListener != null) mLoadingListener.onLoadingFinished(false);
                        return;
                }
            }
+1 −1
Original line number Diff line number Diff line
@@ -734,7 +734,7 @@ public class AlbumSetPage extends ActivityState implements
        }

        @Override
        public void onLoadingFinished() {
        public void onLoadingFinished(boolean loadingFailed) {
            clearLoadingBit(BIT_LOADING_RELOAD);
        }
    }
+6 −1
Original line number Diff line number Diff line
@@ -18,5 +18,10 @@ package com.android.gallery3d.app;

public interface LoadingListener {
    public void onLoadingStarted();
    public void onLoadingFinished();
    /**
     * Called when loading is complete or no further progress can be made.
     *
     * @param loadingFailed true if data source cannot provide requested data
     */
    public void onLoadingFinished(boolean loadingFailed);
}
Loading