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
17package com.android.photos;
18
19import android.app.LoaderManager.LoaderCallbacks;
20import android.content.Context;
21import android.content.Intent;
22import android.content.Loader;
23import android.database.Cursor;
24import android.net.Uri;
25import android.os.Bundle;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.GridView;
30
31import com.android.gallery3d.app.Gallery;
32import com.android.photos.adapters.PhotoThumbnailAdapter;
33import com.android.photos.data.PhotoSetLoader;
34import com.android.photos.shims.LoaderCompatShim;
35import com.android.photos.shims.MediaItemsLoader;
36
37import java.util.ArrayList;
38
39public 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
47    public void onCreate(Bundle savedInstanceState) {
48        super.onCreate(savedInstanceState);
49        Context context = getActivity();
50        mAdapter = new PhotoThumbnailAdapter(context);
51    }
52
53    @Override
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
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
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(), Gallery.class);
78        startActivity(intent);
79    }
80
81    @Override
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
91    public void onLoadFinished(Loader<Cursor> loader,
92            Cursor data) {
93        mAdapter.swapCursor(data);
94        setAdapter(mAdapter);
95    }
96
97    @Override
98    public void onLoaderReset(Loader<Cursor> loader) {
99    }
100
101    @Override
102    public int getItemMediaType(Object item) {
103        return ((Cursor) item).getInt(PhotoSetLoader.INDEX_MEDIA_TYPE);
104    }
105
106    @Override
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
113    public ArrayList<Uri> getSubItemUrisForItem(Object item) {
114        mSubItemUriTemp.clear();
115        mSubItemUriTemp.add(mLoaderCompatShim.uriForItem((Cursor) item));
116        return mSubItemUriTemp;
117    }
118
119    @Override
120    public void deleteItemWithPath(Object itemPath) {
121        mLoaderCompatShim.deleteItemWithPath(itemPath);
122    }
123
124    @Override
125    public Uri getItemUri(Object item) {
126        return mLoaderCompatShim.uriForItem((Cursor) item);
127    }
128
129    @Override
130    public Object getPathForItem(Object item) {
131        return mLoaderCompatShim.getPathForItem((Cursor) item);
132    }
133}
134