1def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu/*
2def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * Copyright (C) 2013 The Android Open Source Project
3def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu *
4def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * Licensed under the Apache License, Version 2.0 (the "License");
5def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * you may not use this file except in compliance with the License.
6def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * You may obtain a copy of the License at
7def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu *
8def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu *      http://www.apache.org/licenses/LICENSE-2.0
9def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu *
10def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * Unless required by applicable law or agreed to in writing, software
11def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * distributed under the License is distributed on an "AS IS" BASIS,
12def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * See the License for the specific language governing permissions and
14def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu * limitations under the License.
15def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu */
16def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
17def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescupackage com.android.photos;
18def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
19def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.app.LoaderManager.LoaderCallbacks;
20def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.content.Context;
21def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.content.Intent;
22def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.content.Loader;
23def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.database.Cursor;
24def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.net.Uri;
25def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.os.Bundle;
26def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.view.LayoutInflater;
27def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.view.View;
28def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.view.ViewGroup;
29def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport android.widget.GridView;
30559535174c8c70f12a342fe46f00647c31080383Bobby Georgescuimport android.widget.ImageView;
31559535174c8c70f12a342fe46f00647c31080383Bobby Georgescuimport android.widget.TextView;
32def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
33559535174c8c70f12a342fe46f00647c31080383Bobby Georgescuimport com.android.gallery3d.R;
3406c027675948259368a902fd6f8c2ecac5db5167Mangesh Ghiwareimport com.android.gallery3d.app.GalleryActivity;
35def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport com.android.photos.adapters.PhotoThumbnailAdapter;
36def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport com.android.photos.data.PhotoSetLoader;
37def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport com.android.photos.shims.LoaderCompatShim;
38def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport com.android.photos.shims.MediaItemsLoader;
39559535174c8c70f12a342fe46f00647c31080383Bobby Georgescuimport com.android.photos.views.HeaderGridView;
40def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
41def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescuimport java.util.ArrayList;
42def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
43c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescupublic class AlbumFragment extends MultiSelectGridFragment implements LoaderCallbacks<Cursor> {
44def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
45def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    protected static final String KEY_ALBUM_URI = "AlbumUri";
46559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu    protected static final String KEY_ALBUM_TITLE = "AlbumTitle";
47def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    private static final int LOADER_ALBUM = 1;
48def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
49def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    private LoaderCompatShim<Cursor> mLoaderCompatShim;
50def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    private PhotoThumbnailAdapter mAdapter;
51def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    private String mAlbumPath;
52559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu    private String mAlbumTitle;
53559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu    private View mHeaderView;
54def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
55def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
56def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public void onCreate(Bundle savedInstanceState) {
57def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        super.onCreate(savedInstanceState);
58def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        Context context = getActivity();
59def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mAdapter = new PhotoThumbnailAdapter(context);
60def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        Bundle args = getArguments();
61def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        if (args != null) {
62def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu            mAlbumPath = args.getString(KEY_ALBUM_URI, null);
63559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            mAlbumTitle = args.getString(KEY_ALBUM_TITLE, null);
64def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        }
65def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
66def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
67def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
68def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public View onCreateView(LayoutInflater inflater, ViewGroup container,
69def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu            Bundle savedInstanceState) {
70def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        getLoaderManager().initLoader(LOADER_ALBUM, null, this);
71559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        return inflater.inflate(R.layout.album_content, container, false);
72def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
73def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
74def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
75c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu    public void onViewCreated(View view, Bundle savedInstanceState) {
76c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        super.onViewCreated(view, savedInstanceState);
77c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        // TODO: Remove once UI stabilizes
78c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        getGridView().setColumnWidth(MediaItemsLoader.getThumbnailSize());
79ef43b57345a7fbb39e22289328f52f16dcd45635Bobby Georgescu    }
80ef43b57345a7fbb39e22289328f52f16dcd45635Bobby Georgescu
81559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu    private void updateHeaderView() {
82559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        if (mHeaderView == null) {
83559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            mHeaderView = LayoutInflater.from(getActivity())
84559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu                    .inflate(R.layout.album_header, getGridView(), false);
85559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            ((HeaderGridView) getGridView()).addHeaderView(mHeaderView, null, false);
86559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu
87559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            // TODO remove this when the data model stabilizes
88559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            mHeaderView.setMinimumHeight(200);
89559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        }
90559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        ImageView iv = (ImageView) mHeaderView.findViewById(R.id.album_header_image);
91559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        TextView title = (TextView) mHeaderView.findViewById(R.id.album_header_title);
92559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        TextView subtitle = (TextView) mHeaderView.findViewById(R.id.album_header_subtitle);
93559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        title.setText(mAlbumTitle);
94559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        int count = mAdapter.getCount();
95559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        subtitle.setText(getActivity().getResources().getQuantityString(
96559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu                R.plurals.number_of_photos, count, count));
97559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        if (count > 0) {
98559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu            iv.setImageDrawable(mLoaderCompatShim.drawableForItem(mAdapter.getItem(0), null));
99559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        }
100559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu    }
101559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu
102ef43b57345a7fbb39e22289328f52f16dcd45635Bobby Georgescu    @Override
103c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu    public void onGridItemClick(GridView g, View v, int position, long id) {
104def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        if (mLoaderCompatShim == null) {
105def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu            // Not fully initialized yet, discard
106def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu            return;
107def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        }
108c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        Cursor item = (Cursor) getItemAtPosition(position);
109def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        Uri uri = mLoaderCompatShim.uriForItem(item);
110def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
11106c027675948259368a902fd6f8c2ecac5db5167Mangesh Ghiware        intent.setClass(getActivity(), GalleryActivity.class);
112def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        startActivity(intent);
113def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
114def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
115def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
116def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
117def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        // TODO: Switch to PhotoSetLoader
118def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        MediaItemsLoader loader = new MediaItemsLoader(getActivity(), mAlbumPath);
119def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mLoaderCompatShim = loader;
120def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mAdapter.setDrawableFactory(mLoaderCompatShim);
121def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        return loader;
122def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
123def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
124def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
125def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public void onLoadFinished(Loader<Cursor> loader,
126def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu            Cursor data) {
127def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mAdapter.swapCursor(data);
128559535174c8c70f12a342fe46f00647c31080383Bobby Georgescu        updateHeaderView();
129c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        setAdapter(mAdapter);
130def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
131def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
132def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
133def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public void onLoaderReset(Loader<Cursor> loader) {
134def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
135def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
136def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
137def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public int getItemMediaType(Object item) {
138def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        return ((Cursor) item).getInt(PhotoSetLoader.INDEX_MEDIA_TYPE);
139def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
140def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
141def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
142def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public int getItemSupportedOperations(Object item) {
143def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        return ((Cursor) item).getInt(PhotoSetLoader.INDEX_SUPPORTED_OPERATIONS);
144def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
145def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
146def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    private ArrayList<Uri> mSubItemUriTemp = new ArrayList<Uri>(1);
147def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
148def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public ArrayList<Uri> getSubItemUrisForItem(Object item) {
149def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mSubItemUriTemp.clear();
150def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mSubItemUriTemp.add(mLoaderCompatShim.uriForItem((Cursor) item));
151def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        return mSubItemUriTemp;
152def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
153def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
154def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
155def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    public void deleteItemWithPath(Object itemPath) {
156def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu        mLoaderCompatShim.deleteItemWithPath(itemPath);
157def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
158def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
159def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
160c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu    public Uri getItemUri(Object item) {
161c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        return mLoaderCompatShim.uriForItem((Cursor) item);
162def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
163def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu
164def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    @Override
165c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu    public Object getPathForItem(Object item) {
166c1a015f3b9a896179d2b4778e50988460cfd34fbBobby Georgescu        return mLoaderCompatShim.getPathForItem((Cursor) item);
167def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu    }
168def3606a1e7ffa20171c473d792af7a0bbec4ea8Bobby Georgescu}
169