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