PhotoSetFragment.java revision 5f30b98d6ac98c6aa863d48f47e9b8e2b77a1820
15f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck/*
25f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * Copyright (C) 2013 The Android Open Source Project
35f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck *
45f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * Licensed under the Apache License, Version 2.0 (the "License");
55f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * you may not use this file except in compliance with the License.
65f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * You may obtain a copy of the License at
75f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck *
85f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck *      http://www.apache.org/licenses/LICENSE-2.0
95f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck *
105f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * Unless required by applicable law or agreed to in writing, software
115f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * distributed under the License is distributed on an "AS IS" BASIS,
125f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * See the License for the specific language governing permissions and
145f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck * limitations under the License.
155f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck */
165f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
175f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckpackage com.android.photos;
185f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
195f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.app.Fragment;
205f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.app.LoaderManager.LoaderCallbacks;
215f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.content.Loader;
225f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.database.Cursor;
235f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.os.Bundle;
245f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.provider.MediaStore.Files.FileColumns;
255f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.view.LayoutInflater;
265f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.view.View;
275f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.view.ViewGroup;
285f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.widget.CursorAdapter;
295f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.widget.ListView;
305f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport android.widget.SimpleCursorAdapter;
315f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
325f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport com.android.gallery3d.R;
335f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckimport com.android.photos.data.PhotoSetLoader;
345f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
355f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
365f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reckpublic class PhotoSetFragment extends Fragment implements LoaderCallbacks<Cursor> {
375f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
385f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    private static final int LOADER_PHOTOSET = 1;
395f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
405f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    private ListView mPhotoSetView;
415f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    private View mEmptyView;
425f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    private CursorAdapter mAdapter;
435f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
445f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    @Override
455f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    public View onCreateView(LayoutInflater inflater, ViewGroup container,
465f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck            Bundle savedInstanceState) {
475f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        View root = inflater.inflate(R.layout.photo_set, container, false);
485f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mPhotoSetView = (ListView) root.findViewById(android.R.id.list);
495f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mEmptyView = root.findViewById(android.R.id.empty);
505f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mEmptyView.setVisibility(View.GONE);
515f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mAdapter = new SimpleCursorAdapter(getActivity(),
525f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck                android.R.layout.simple_list_item_1, null,
535f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck                new String[] { FileColumns.DATA },
545f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck                new int[] { android.R.id.text1 }, 0);
555f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mPhotoSetView.setAdapter(mAdapter);
565f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        getLoaderManager().initLoader(LOADER_PHOTOSET, null, this);
575f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        updateEmptyStatus();
585f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        return root;
595f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    }
605f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
615f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    private void updateEmptyStatus() {
625f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        boolean empty = (mAdapter == null || mAdapter.getCount() == 0);
635f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mPhotoSetView.setVisibility(empty ? View.GONE : View.VISIBLE);
645f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mEmptyView.setVisibility(empty ? View.VISIBLE : View.GONE);
655f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    }
665f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
675f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    @Override
685f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
695f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        return new PhotoSetLoader(getActivity());
705f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    }
715f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
725f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    @Override
735f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    public void onLoadFinished(Loader<Cursor> loader,
745f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck            Cursor data) {
755f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        mAdapter.swapCursor(data);
765f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck        updateEmptyStatus();
775f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    }
785f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck
795f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    @Override
805f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    public void onLoaderReset(Loader<Cursor> loader) {
815f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck    }
825f30b98d6ac98c6aa863d48f47e9b8e2b77a1820John Reck}
83