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