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 
31 import com.android.gallery3d.app.GalleryActivity;
32 import com.android.photos.adapters.PhotoThumbnailAdapter;
33 import com.android.photos.data.PhotoSetLoader;
34 import com.android.photos.shims.LoaderCompatShim;
35 import com.android.photos.shims.MediaItemsLoader;
36 
37 import java.util.ArrayList;
38 
39 public class PhotoSetFragment extends MultiSelectGridFragment implements LoaderCallbacks<Cursor> {
40 
41     private static final int LOADER_PHOTOSET = 1;
42 
43     private LoaderCompatShim<Cursor> mLoaderCompatShim;
44     private PhotoThumbnailAdapter mAdapter;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         Context context = getActivity();
50         mAdapter = new PhotoThumbnailAdapter(context);
51     }
52 
53     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)54     public View onCreateView(LayoutInflater inflater, ViewGroup container,
55             Bundle savedInstanceState) {
56         View root = super.onCreateView(inflater, container, savedInstanceState);
57         getLoaderManager().initLoader(LOADER_PHOTOSET, null, this);
58         return root;
59     }
60 
61     @Override
onViewCreated(View view, Bundle savedInstanceState)62     public void onViewCreated(View view, Bundle savedInstanceState) {
63         super.onViewCreated(view, savedInstanceState);
64         // TODO: Remove once UI stabilizes
65         getGridView().setColumnWidth(MediaItemsLoader.getThumbnailSize());
66     }
67 
68     @Override
onGridItemClick(GridView g, View v, int position, long id)69     public void onGridItemClick(GridView g, View v, int position, long id) {
70         if (mLoaderCompatShim == null) {
71             // Not fully initialized yet, discard
72             return;
73         }
74         Cursor item = (Cursor) getItemAtPosition(position);
75         Uri uri = mLoaderCompatShim.uriForItem(item);
76         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
77         intent.setClass(getActivity(), GalleryActivity.class);
78         startActivity(intent);
79     }
80 
81     @Override
onCreateLoader(int id, Bundle args)82     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
83         // TODO: Switch to PhotoSetLoader
84         MediaItemsLoader loader = new MediaItemsLoader(getActivity());
85         mLoaderCompatShim = loader;
86         mAdapter.setDrawableFactory(mLoaderCompatShim);
87         return loader;
88     }
89 
90     @Override
onLoadFinished(Loader<Cursor> loader, Cursor data)91     public void onLoadFinished(Loader<Cursor> loader,
92             Cursor data) {
93         mAdapter.swapCursor(data);
94         setAdapter(mAdapter);
95     }
96 
97     @Override
onLoaderReset(Loader<Cursor> loader)98     public void onLoaderReset(Loader<Cursor> loader) {
99     }
100 
101     @Override
getItemMediaType(Object item)102     public int getItemMediaType(Object item) {
103         return ((Cursor) item).getInt(PhotoSetLoader.INDEX_MEDIA_TYPE);
104     }
105 
106     @Override
getItemSupportedOperations(Object item)107     public int getItemSupportedOperations(Object item) {
108         return ((Cursor) item).getInt(PhotoSetLoader.INDEX_SUPPORTED_OPERATIONS);
109     }
110 
111     private ArrayList<Uri> mSubItemUriTemp = new ArrayList<Uri>(1);
112     @Override
getSubItemUrisForItem(Object item)113     public ArrayList<Uri> getSubItemUrisForItem(Object item) {
114         mSubItemUriTemp.clear();
115         mSubItemUriTemp.add(mLoaderCompatShim.uriForItem((Cursor) item));
116         return mSubItemUriTemp;
117     }
118 
119     @Override
deleteItemWithPath(Object itemPath)120     public void deleteItemWithPath(Object itemPath) {
121         mLoaderCompatShim.deleteItemWithPath(itemPath);
122     }
123 
124     @Override
getItemUri(Object item)125     public Uri getItemUri(Object item) {
126         return mLoaderCompatShim.uriForItem((Cursor) item);
127     }
128 
129     @Override
getPathForItem(Object item)130     public Object getPathForItem(Object item) {
131         return mLoaderCompatShim.getPathForItem((Cursor) item);
132     }
133 }
134