FileListFragment.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2013 Paul Burke
  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. package com.ipaulpro.afilechooser;
  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.os.Environment;
  20. import android.support.v4.app.ListFragment;
  21. import android.support.v4.app.LoaderManager;
  22. import android.support.v4.content.Loader;
  23. import android.view.View;
  24. import android.widget.ListView;
  25. import java.io.File;
  26. import java.util.List;
  27. /**
  28. * Fragment that displays a list of Files in a given path.
  29. *
  30. * @version 2013-12-11
  31. * @author paulburke (ipaulpro)
  32. */
  33. public class FileListFragment extends ListFragment implements
  34. LoaderManager.LoaderCallbacks<List<File>> {
  35. /**
  36. * Interface to listen for events.
  37. */
  38. public interface Callbacks {
  39. /**
  40. * Called when a file is selected from the list.
  41. *
  42. * @param file The file selected
  43. */
  44. public void onFileSelected(File file);
  45. }
  46. private static final int LOADER_ID = 0;
  47. private FileListAdapter mAdapter;
  48. private String mPath;
  49. private Callbacks mListener;
  50. /**
  51. * Create a new instance with the given file path.
  52. *
  53. * @param path The absolute path of the file (directory) to display.
  54. * @return A new Fragment with the given file path.
  55. */
  56. public static FileListFragment newInstance(String path) {
  57. FileListFragment fragment = new FileListFragment();
  58. Bundle args = new Bundle();
  59. args.putString(FileChooserActivity.PATH, path);
  60. fragment.setArguments(args);
  61. return fragment;
  62. }
  63. @Override
  64. public void onAttach(Activity activity) {
  65. super.onAttach(activity);
  66. try {
  67. mListener = (Callbacks) activity;
  68. } catch (ClassCastException e) {
  69. throw new ClassCastException(activity.toString()
  70. + " must implement FileListFragment.Callbacks");
  71. }
  72. }
  73. @Override
  74. public void onCreate(Bundle savedInstanceState) {
  75. super.onCreate(savedInstanceState);
  76. mAdapter = new FileListAdapter(getActivity());
  77. mPath = getArguments() != null ? getArguments().getString(
  78. FileChooserActivity.PATH) : Environment
  79. .getExternalStorageDirectory().getAbsolutePath();
  80. }
  81. @Override
  82. public void onActivityCreated(Bundle savedInstanceState) {
  83. setEmptyText(getString(R.string.empty_directory));
  84. setListAdapter(mAdapter);
  85. setListShown(false);
  86. getLoaderManager().initLoader(LOADER_ID, null, this);
  87. super.onActivityCreated(savedInstanceState);
  88. }
  89. @Override
  90. public void onListItemClick(ListView l, View v, int position, long id) {
  91. FileListAdapter adapter = (FileListAdapter) l.getAdapter();
  92. if (adapter != null) {
  93. File file = (File) adapter.getItem(position);
  94. mPath = file.getAbsolutePath();
  95. mListener.onFileSelected(file);
  96. }
  97. }
  98. @Override
  99. public Loader<List<File>> onCreateLoader(int id, Bundle args) {
  100. return new FileLoader(getActivity(), mPath);
  101. }
  102. @Override
  103. public void onLoadFinished(Loader<List<File>> loader, List<File> data) {
  104. mAdapter.setListItems(data);
  105. if (isResumed())
  106. setListShown(true);
  107. else
  108. setListShownNoAnimation(true);
  109. }
  110. @Override
  111. public void onLoaderReset(Loader<List<File>> loader) {
  112. mAdapter.clear();
  113. }
  114. }