1f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin/*
2f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Copyright (C) 2010 The Android Open Source Project
3f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
4f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Licensed under the Apache License, Version 2.0 (the "License");
5f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * you may not use this file except in compliance with the License.
6f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * You may obtain a copy of the License at
7f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
8f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *      http://www.apache.org/licenses/LICENSE-2.0
9f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
10f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Unless required by applicable law or agreed to in writing, software
11f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * distributed under the License is distributed on an "AS IS" BASIS,
12f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * See the License for the specific language governing permissions and
14f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * limitations under the License.
15f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin */
16f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
17f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linpackage com.android.gallery3d.app;
18f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
192b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.app.Activity;
202b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.content.res.Configuration;
212b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.os.Bundle;
222b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.os.Handler;
232b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.os.Message;
242b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.text.format.Formatter;
252b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.view.LayoutInflater;
262b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.view.View;
272b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.view.View.OnClickListener;
282b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.widget.FrameLayout;
292b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.widget.ProgressBar;
302b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.widget.TextView;
312b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport android.widget.Toast;
322b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Lin
33f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.R;
348e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.common.Utils;
35f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.data.MediaObject;
36f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.data.MediaSet;
37f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.data.Path;
388e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.ui.CacheStorageUsageInfo;
39f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.GLCanvas;
4048ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Linimport com.android.gallery3d.ui.GLRoot;
41f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.GLView;
42f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.ManageCacheDrawer;
43f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.MenuExecutor;
44f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.SelectionManager;
45f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.ui.SlotView;
468e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.ui.SynchronizedHandler;
478e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.util.Future;
48f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport com.android.gallery3d.util.GalleryUtils;
498e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.util.ThreadPool.Job;
508e565703e8d482421714383a8ad40a67929ce2daOwen Linimport com.android.gallery3d.util.ThreadPool.JobContext;
51f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
52f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport java.util.ArrayList;
53f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
54f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linpublic class ManageCachePage extends ActivityState implements
558e565703e8d482421714383a8ad40a67929ce2daOwen Lin        SelectionManager.SelectionListener, MenuExecutor.ProgressListener,
568e565703e8d482421714383a8ad40a67929ce2daOwen Lin        EyePosition.EyePositionListener, OnClickListener {
57f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public static final String KEY_MEDIA_PATH = "media-path";
588e565703e8d482421714383a8ad40a67929ce2daOwen Lin
59f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final String TAG = "ManageCachePage";
60f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
61f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final int DATA_CACHE_SIZE = 256;
628e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private static final int MSG_REFRESH_STORAGE_INFO = 1;
638e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private static final int MSG_REQUEST_LAYOUT = 2;
648e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private static final int PROGRESS_BAR_MAX = 10000;
65f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
667260f6f74d465520e4497b23fe56f98abb0c15a2Owen Lin    private SlotView mSlotView;
67f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private MediaSet mMediaSet;
68f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
69f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    protected SelectionManager mSelectionManager;
7031cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    protected ManageCacheDrawer mSelectionDrawer;
71c3d8ac3b9504346dafc49e006b5f732dd1db21e8Owen Lin    private AlbumSetDataLoader mAlbumSetDataAdapter;
72f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
73f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private EyePosition mEyePosition;
74f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
75f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    // The eyes' position of the user, the origin is at the center of the
76f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    // device and the unit is in pixels.
77f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private float mX;
78f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private float mY;
79f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private float mZ;
80f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
81f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private int mAlbumCountToMakeAvailableOffline;
828e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private View mFooterContent;
838e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private CacheStorageUsageInfo mCacheStorageInfo;
848e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private Future<Void> mUpdateStorageInfo;
858e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private Handler mHandler;
868e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private boolean mLayoutReady = false;
87f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
88f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private GLView mRootPane = new GLView() {
89f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        private float mMatrix[] = new float[16];
90f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
91f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        @Override
92ff951a85c3c9015ef7ccd8902e69ce88360a0cb8Chih-Chung Chang        protected void renderBackground(GLCanvas view) {
93ff951a85c3c9015ef7ccd8902e69ce88360a0cb8Chih-Chung Chang            view.clearBuffer();
94ff951a85c3c9015ef7ccd8902e69ce88360a0cb8Chih-Chung Chang        }
95ff951a85c3c9015ef7ccd8902e69ce88360a0cb8Chih-Chung Chang
96ff951a85c3c9015ef7ccd8902e69ce88360a0cb8Chih-Chung Chang        @Override
97f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        protected void onLayout(
98f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                boolean changed, int left, int top, int right, int bottom) {
998e565703e8d482421714383a8ad40a67929ce2daOwen Lin            // Hack: our layout depends on other components on the screen.
1008e565703e8d482421714383a8ad40a67929ce2daOwen Lin            // We assume the other components will complete before we get a change
1018e565703e8d482421714383a8ad40a67929ce2daOwen Lin            // to run a message in main thread.
1028e565703e8d482421714383a8ad40a67929ce2daOwen Lin            if (!mLayoutReady) {
1038e565703e8d482421714383a8ad40a67929ce2daOwen Lin                mHandler.sendEmptyMessage(MSG_REQUEST_LAYOUT);
1048e565703e8d482421714383a8ad40a67929ce2daOwen Lin                return;
1058e565703e8d482421714383a8ad40a67929ce2daOwen Lin            }
1068e565703e8d482421714383a8ad40a67929ce2daOwen Lin            mLayoutReady = false;
1078e565703e8d482421714383a8ad40a67929ce2daOwen Lin
108f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            mEyePosition.resetPosition();
1098e565703e8d482421714383a8ad40a67929ce2daOwen Lin            Activity activity = (Activity) mActivity;
1108cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            int slotViewTop = mActivity.getGalleryActionBar().getHeight();
1118e565703e8d482421714383a8ad40a67929ce2daOwen Lin            int slotViewBottom = bottom - top;
1128e565703e8d482421714383a8ad40a67929ce2daOwen Lin
113bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            View footer = activity.findViewById(R.id.footer);
114bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            if (footer != null) {
1158e565703e8d482421714383a8ad40a67929ce2daOwen Lin                int location[] = {0, 0};
116bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang                footer.getLocationOnScreen(location);
1178e565703e8d482421714383a8ad40a67929ce2daOwen Lin                slotViewBottom = location[1];
1188e565703e8d482421714383a8ad40a67929ce2daOwen Lin            }
119f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
1207260f6f74d465520e4497b23fe56f98abb0c15a2Owen Lin            mSlotView.layout(0, slotViewTop, right - left, slotViewBottom);
121f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
122f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
123f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        @Override
124f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        protected void render(GLCanvas canvas) {
125f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
126f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            GalleryUtils.setViewPointMatrix(mMatrix,
127f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                        getWidth() / 2 + mX, getHeight() / 2 + mY, mZ);
128f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            canvas.multiplyMatrix(mMatrix, 0);
129f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            super.render(canvas);
130f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            canvas.restore();
131f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
132f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    };
133f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
13431cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    @Override
135f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onEyePositionChanged(float x, float y, float z) {
136f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mRootPane.lockRendering();
137f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mX = x;
138f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mY = y;
139f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mZ = z;
140f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mRootPane.unlockRendering();
141f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mRootPane.invalidate();
142f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
143f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
144bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang    private void onDown(int index) {
14531cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer.setPressedIndex(index);
146bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang    }
147bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang
148bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang    private void onUp() {
14931cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer.setPressedIndex(-1);
150bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang    }
151bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang
152f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onSingleTapUp(int slotIndex) {
153f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        MediaSet targetSet = mAlbumSetDataAdapter.getMediaSet(slotIndex);
154f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (targetSet == null) return; // Content is dirty, we shall reload soon
155f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
156f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        // ignore selection action if the target set does not support cache
157f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        // operation (like a local album).
158f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if ((targetSet.getSupportedOperations()
159f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                & MediaSet.SUPPORT_CACHE) == 0) {
160f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            showToastForLocalAlbum();
161f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            return;
162f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
163f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
164f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        Path path = targetSet.getPath();
165f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        boolean isFullyCached =
166f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                (targetSet.getCacheFlag() == MediaObject.CACHE_FLAG_FULL);
167f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        boolean isSelected = mSelectionManager.isItemSelected(path);
168f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
169f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (!isFullyCached) {
170f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            // We only count the media sets that will be made available offline
171f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            // in this session.
172f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (isSelected) {
173f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                --mAlbumCountToMakeAvailableOffline;
174f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            } else {
175f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                ++mAlbumCountToMakeAvailableOffline;
176f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
177f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
178f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
179f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        long sizeOfTarget = targetSet.getCacheSize();
1808e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mCacheStorageInfo.increaseTargetCacheSize(
1818e565703e8d482421714383a8ad40a67929ce2daOwen Lin                (isFullyCached ^ isSelected) ? -sizeOfTarget : sizeOfTarget);
1828e565703e8d482421714383a8ad40a67929ce2daOwen Lin        refreshCacheStorageInfo();
183f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
184f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager.toggle(path);
1857260f6f74d465520e4497b23fe56f98abb0c15a2Owen Lin        mSlotView.invalidate();
186f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
187f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
188f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    @Override
189f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onCreate(Bundle data, Bundle restoreState) {
1908e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mCacheStorageInfo = new CacheStorageUsageInfo(mActivity);
191f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        initializeViews();
192f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        initializeData(data);
193f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mEyePosition = new EyePosition(mActivity.getAndroidContext(), this);
1948e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
1958e565703e8d482421714383a8ad40a67929ce2daOwen Lin            @Override
1968e565703e8d482421714383a8ad40a67929ce2daOwen Lin            public void handleMessage(Message message) {
1978e565703e8d482421714383a8ad40a67929ce2daOwen Lin                switch (message.what) {
1988e565703e8d482421714383a8ad40a67929ce2daOwen Lin                    case MSG_REFRESH_STORAGE_INFO:
1998e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        refreshCacheStorageInfo();
2008e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        break;
2018e565703e8d482421714383a8ad40a67929ce2daOwen Lin                    case MSG_REQUEST_LAYOUT: {
2028e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        mLayoutReady = true;
2038e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        removeMessages(MSG_REQUEST_LAYOUT);
2048e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        mRootPane.requestLayout();
2058e565703e8d482421714383a8ad40a67929ce2daOwen Lin                        break;
2068e565703e8d482421714383a8ad40a67929ce2daOwen Lin                    }
2078e565703e8d482421714383a8ad40a67929ce2daOwen Lin                }
2088e565703e8d482421714383a8ad40a67929ce2daOwen Lin            }
2098e565703e8d482421714383a8ad40a67929ce2daOwen Lin        };
2108e565703e8d482421714383a8ad40a67929ce2daOwen Lin    }
2118e565703e8d482421714383a8ad40a67929ce2daOwen Lin
2128e565703e8d482421714383a8ad40a67929ce2daOwen Lin    @Override
2138e565703e8d482421714383a8ad40a67929ce2daOwen Lin    public void onConfigurationChanged(Configuration config) {
2148e565703e8d482421714383a8ad40a67929ce2daOwen Lin        // We use different layout resources for different configs
2158e565703e8d482421714383a8ad40a67929ce2daOwen Lin        initializeFooterViews();
2168e565703e8d482421714383a8ad40a67929ce2daOwen Lin        FrameLayout layout = (FrameLayout) ((Activity) mActivity).findViewById(R.id.footer);
2178e565703e8d482421714383a8ad40a67929ce2daOwen Lin        if (layout.getVisibility() == View.VISIBLE) {
2188e565703e8d482421714383a8ad40a67929ce2daOwen Lin            layout.removeAllViews();
2198e565703e8d482421714383a8ad40a67929ce2daOwen Lin            layout.addView(mFooterContent);
2208e565703e8d482421714383a8ad40a67929ce2daOwen Lin        }
221f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
222f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
223f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    @Override
224f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onPause() {
225f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        super.onPause();
226f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mAlbumSetDataAdapter.pause();
22731cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer.pause();
228f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mEyePosition.pause();
2298e565703e8d482421714383a8ad40a67929ce2daOwen Lin
2308e565703e8d482421714383a8ad40a67929ce2daOwen Lin        if (mUpdateStorageInfo != null) {
2318e565703e8d482421714383a8ad40a67929ce2daOwen Lin            mUpdateStorageInfo.cancel();
2328e565703e8d482421714383a8ad40a67929ce2daOwen Lin            mUpdateStorageInfo = null;
2338e565703e8d482421714383a8ad40a67929ce2daOwen Lin        }
2348e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mHandler.removeMessages(MSG_REFRESH_STORAGE_INFO);
2358e565703e8d482421714383a8ad40a67929ce2daOwen Lin
2368e565703e8d482421714383a8ad40a67929ce2daOwen Lin        FrameLayout layout = (FrameLayout) ((Activity) mActivity).findViewById(R.id.footer);
2378e565703e8d482421714383a8ad40a67929ce2daOwen Lin        layout.removeAllViews();
2388e565703e8d482421714383a8ad40a67929ce2daOwen Lin        layout.setVisibility(View.INVISIBLE);
239f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
240f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
2418e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private Job<Void> mUpdateStorageInfoJob = new Job<Void>() {
2428e565703e8d482421714383a8ad40a67929ce2daOwen Lin        @Override
2438e565703e8d482421714383a8ad40a67929ce2daOwen Lin        public Void run(JobContext jc) {
2448e565703e8d482421714383a8ad40a67929ce2daOwen Lin            mCacheStorageInfo.loadStorageInfo(jc);
2458e565703e8d482421714383a8ad40a67929ce2daOwen Lin            if (!jc.isCancelled()) {
2468e565703e8d482421714383a8ad40a67929ce2daOwen Lin                mHandler.sendEmptyMessage(MSG_REFRESH_STORAGE_INFO);
2478e565703e8d482421714383a8ad40a67929ce2daOwen Lin            }
2488e565703e8d482421714383a8ad40a67929ce2daOwen Lin            return null;
2498e565703e8d482421714383a8ad40a67929ce2daOwen Lin        }
2508e565703e8d482421714383a8ad40a67929ce2daOwen Lin    };
2518e565703e8d482421714383a8ad40a67929ce2daOwen Lin
252f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    @Override
253f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onResume() {
254f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        super.onResume();
255f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        setContentPane(mRootPane);
256f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mAlbumSetDataAdapter.resume();
25731cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer.resume();
258f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mEyePosition.resume();
2598e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mUpdateStorageInfo = mActivity.getThreadPool().submit(mUpdateStorageInfoJob);
2608e565703e8d482421714383a8ad40a67929ce2daOwen Lin        FrameLayout layout = (FrameLayout) ((Activity) mActivity).findViewById(R.id.footer);
2618e565703e8d482421714383a8ad40a67929ce2daOwen Lin        layout.addView(mFooterContent);
2628e565703e8d482421714383a8ad40a67929ce2daOwen Lin        layout.setVisibility(View.VISIBLE);
263f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
264f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
265f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void initializeData(Bundle data) {
266f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        String mediaPath = data.getString(ManageCachePage.KEY_MEDIA_PATH);
267f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mMediaSet = mActivity.getDataManager().getMediaSet(mediaPath);
268f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager.setSourceMediaSet(mMediaSet);
269f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
270f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        // We will always be in selection mode in this page.
271f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager.setAutoLeaveSelectionMode(false);
272f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager.enterSelectionMode();
273f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
274c3d8ac3b9504346dafc49e006b5f732dd1db21e8Owen Lin        mAlbumSetDataAdapter = new AlbumSetDataLoader(
275f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                mActivity, mMediaSet, DATA_CACHE_SIZE);
27631cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer.setModel(mAlbumSetDataAdapter);
277f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
278f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
279f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void initializeViews() {
2808e565703e8d482421714383a8ad40a67929ce2daOwen Lin        Activity activity = (Activity) mActivity;
2818e565703e8d482421714383a8ad40a67929ce2daOwen Lin
282f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager = new SelectionManager(mActivity, true);
283f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mSelectionManager.setSelectionListener(this);
284f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
2858e565703e8d482421714383a8ad40a67929ce2daOwen Lin        Config.ManageCachePage config = Config.ManageCachePage.get(activity);
28648ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin        mSlotView = new SlotView(mActivity, config.slotViewSpec);
28731cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSelectionDrawer = new ManageCacheDrawer(mActivity, mSelectionManager, mSlotView,
28831cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin                config.labelSpec, config.cachePinSize, config.cachePinMargin);
28931cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin        mSlotView.setSlotRenderer(mSelectionDrawer);
2907260f6f74d465520e4497b23fe56f98abb0c15a2Owen Lin        mSlotView.setListener(new SlotView.SimpleListener() {
291f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            @Override
292bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            public void onDown(int index) {
293bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang                ManageCachePage.this.onDown(index);
294bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            }
295bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang
296bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            @Override
29790fe70f11b0961e1e5ae353cdf563236a55f502dYuli Huang            public void onUp(boolean followedByLongPress) {
298bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang                ManageCachePage.this.onUp();
299bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            }
300bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang
301bc2154199aee16762e3758ad0cfed685323d9a9fChih-Chung Chang            @Override
302f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            public void onSingleTapUp(int slotIndex) {
303f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                ManageCachePage.this.onSingleTapUp(slotIndex);
304f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
305f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        });
3067260f6f74d465520e4497b23fe56f98abb0c15a2Owen Lin        mRootPane.addComponent(mSlotView);
3078e565703e8d482421714383a8ad40a67929ce2daOwen Lin        initializeFooterViews();
3088e565703e8d482421714383a8ad40a67929ce2daOwen Lin    }
309f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
3108e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private void initializeFooterViews() {
3118e565703e8d482421714383a8ad40a67929ce2daOwen Lin        Activity activity = (Activity) mActivity;
312f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
3138e565703e8d482421714383a8ad40a67929ce2daOwen Lin        LayoutInflater inflater = activity.getLayoutInflater();
3148e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mFooterContent = inflater.inflate(R.layout.manage_offline_bar, null);
315f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
3168e565703e8d482421714383a8ad40a67929ce2daOwen Lin        mFooterContent.findViewById(R.id.done).setOnClickListener(this);
3178e565703e8d482421714383a8ad40a67929ce2daOwen Lin        refreshCacheStorageInfo();
318f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
319f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
3208e565703e8d482421714383a8ad40a67929ce2daOwen Lin    @Override
3218e565703e8d482421714383a8ad40a67929ce2daOwen Lin    public void onClick(View view) {
3228e565703e8d482421714383a8ad40a67929ce2daOwen Lin        Utils.assertTrue(view.getId() == R.id.done);
32348ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin        GLRoot root = mActivity.getGLRoot();
32448ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin        root.lockRenderThread();
32548ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin        try {
32648ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            ArrayList<Path> ids = mSelectionManager.getSelected(false);
32748ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            if (ids.size() == 0) {
32848ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin                onBackPressed();
32948ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin                return;
33048ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            }
33148ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            showToast();
3328e565703e8d482421714383a8ad40a67929ce2daOwen Lin
33348ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            MenuExecutor menuExecutor = new MenuExecutor(mActivity, mSelectionManager);
33448ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            menuExecutor.startAction(R.id.action_toggle_full_caching,
33548ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin                    R.string.process_caching_requests, this);
33648ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin        } finally {
33748ba94ae713dbf57898cfa84ae69517da50cf7a0Owen Lin            root.unlockRenderThread();
338f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
339f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
340f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
341f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void showToast() {
342f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mAlbumCountToMakeAvailableOffline > 0) {
343f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            Activity activity = (Activity) mActivity;
344f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            Toast.makeText(activity, activity.getResources().getQuantityString(
345f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                    R.plurals.make_albums_available_offline,
346f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                    mAlbumCountToMakeAvailableOffline),
347f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                    Toast.LENGTH_SHORT).show();
348f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
349f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
350f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
351f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void showToastForLocalAlbum() {
352f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        Activity activity = (Activity) mActivity;
353f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        Toast.makeText(activity, activity.getResources().getString(
354f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            R.string.try_to_set_local_album_available_offline),
355f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            Toast.LENGTH_SHORT).show();
356f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
357f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
3588e565703e8d482421714383a8ad40a67929ce2daOwen Lin    private void refreshCacheStorageInfo() {
3598e565703e8d482421714383a8ad40a67929ce2daOwen Lin        ProgressBar progressBar = (ProgressBar) mFooterContent.findViewById(R.id.progress);
3608e565703e8d482421714383a8ad40a67929ce2daOwen Lin        TextView status = (TextView) mFooterContent.findViewById(R.id.status);
3618e565703e8d482421714383a8ad40a67929ce2daOwen Lin        progressBar.setMax(PROGRESS_BAR_MAX);
3628e565703e8d482421714383a8ad40a67929ce2daOwen Lin        long totalBytes = mCacheStorageInfo.getTotalBytes();
3638e565703e8d482421714383a8ad40a67929ce2daOwen Lin        long usedBytes = mCacheStorageInfo.getUsedBytes();
3648e565703e8d482421714383a8ad40a67929ce2daOwen Lin        long expectedBytes = mCacheStorageInfo.getExpectedUsedBytes();
3658e565703e8d482421714383a8ad40a67929ce2daOwen Lin        long freeBytes = mCacheStorageInfo.getFreeBytes();
3668e565703e8d482421714383a8ad40a67929ce2daOwen Lin
3678e565703e8d482421714383a8ad40a67929ce2daOwen Lin        Activity activity = (Activity) mActivity;
3688e565703e8d482421714383a8ad40a67929ce2daOwen Lin        if (totalBytes == 0) {
3698e565703e8d482421714383a8ad40a67929ce2daOwen Lin            progressBar.setProgress(0);
3708e565703e8d482421714383a8ad40a67929ce2daOwen Lin            progressBar.setSecondaryProgress(0);
3718e565703e8d482421714383a8ad40a67929ce2daOwen Lin
3728e565703e8d482421714383a8ad40a67929ce2daOwen Lin            // TODO: get the string translated
3738e565703e8d482421714383a8ad40a67929ce2daOwen Lin            String label = activity.getString(R.string.free_space_format, "-");
3748e565703e8d482421714383a8ad40a67929ce2daOwen Lin            status.setText(label);
3758e565703e8d482421714383a8ad40a67929ce2daOwen Lin        } else {
3768e565703e8d482421714383a8ad40a67929ce2daOwen Lin            progressBar.setProgress((int) (usedBytes * PROGRESS_BAR_MAX / totalBytes));
3778e565703e8d482421714383a8ad40a67929ce2daOwen Lin            progressBar.setSecondaryProgress(
3788e565703e8d482421714383a8ad40a67929ce2daOwen Lin                    (int) (expectedBytes * PROGRESS_BAR_MAX / totalBytes));
3798e565703e8d482421714383a8ad40a67929ce2daOwen Lin            String label = activity.getString(R.string.free_space_format,
3808e565703e8d482421714383a8ad40a67929ce2daOwen Lin                    Formatter.formatFileSize(activity, freeBytes));
3818e565703e8d482421714383a8ad40a67929ce2daOwen Lin            status.setText(label);
3828e565703e8d482421714383a8ad40a67929ce2daOwen Lin        }
3838e565703e8d482421714383a8ad40a67929ce2daOwen Lin    }
3848e565703e8d482421714383a8ad40a67929ce2daOwen Lin
38531cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    @Override
386f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onProgressComplete(int result) {
387f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        onBackPressed();
388f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
389f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
39031cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    @Override
391f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onProgressUpdate(int index) {
392f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
393f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
39431cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    @Override
395f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onSelectionModeChange(int mode) {
396f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
397f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
39831cac30e46a293f6d0901e22c6ab0d083cae7021Owen Lin    @Override
399f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void onSelectionChange(Path path, boolean selected) {
400f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
401d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin
402d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    @Override
403d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    public void onConfirmDialogDismissed(boolean confirmed) {
404d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    }
405d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin
406d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    @Override
407d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    public void onConfirmDialogShown() {
408d759b7c1fc449f4f0ef91997738bbef1bbd96677Owen Lin    }
409f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin}
410