FileChooserActivity.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.ActionBar;
  18. import android.content.BroadcastReceiver;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.content.IntentFilter;
  22. import android.net.Uri;
  23. import android.os.Build;
  24. import android.os.Bundle;
  25. import android.os.Environment;
  26. import android.support.v4.app.FragmentActivity;
  27. import android.support.v4.app.FragmentManager;
  28. import android.support.v4.app.FragmentManager.BackStackEntry;
  29. import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
  30. import android.support.v4.app.FragmentTransaction;
  31. import android.view.Menu;
  32. import android.view.MenuItem;
  33. import android.widget.Toast;
  34. import java.io.File;
  35. /**
  36. * Main Activity that handles the FileListFragments
  37. *
  38. * @version 2013-06-25
  39. * @author paulburke (ipaulpro)
  40. */
  41. public class FileChooserActivity extends FragmentActivity implements
  42. OnBackStackChangedListener, FileListFragment.Callbacks {
  43. public static final String PATH = "path";
  44. public static final String EXTERNAL_BASE_PATH = Environment
  45. .getExternalStorageDirectory().getAbsolutePath();
  46. private static final boolean HAS_ACTIONBAR = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
  47. private FragmentManager mFragmentManager;
  48. private BroadcastReceiver mStorageListener = new BroadcastReceiver() {
  49. @Override
  50. public void onReceive(Context context, Intent intent) {
  51. Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show();
  52. finishWithResult(null);
  53. }
  54. };
  55. private String mPath;
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59. mFragmentManager = getSupportFragmentManager();
  60. mFragmentManager.addOnBackStackChangedListener(this);
  61. if (savedInstanceState == null) {
  62. mPath = EXTERNAL_BASE_PATH;
  63. addFragment();
  64. } else {
  65. mPath = savedInstanceState.getString(PATH);
  66. }
  67. setTitle(mPath);
  68. }
  69. @Override
  70. protected void onPause() {
  71. super.onPause();
  72. unregisterStorageListener();
  73. }
  74. @Override
  75. protected void onResume() {
  76. super.onResume();
  77. registerStorageListener();
  78. }
  79. @Override
  80. protected void onSaveInstanceState(Bundle outState) {
  81. super.onSaveInstanceState(outState);
  82. outState.putString(PATH, mPath);
  83. }
  84. @Override
  85. public void onBackStackChanged() {
  86. int count = mFragmentManager.getBackStackEntryCount();
  87. if (count > 0) {
  88. BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1);
  89. mPath = fragment.getName();
  90. } else {
  91. mPath = EXTERNAL_BASE_PATH;
  92. }
  93. setTitle(mPath);
  94. if (HAS_ACTIONBAR)
  95. invalidateOptionsMenu();
  96. }
  97. @Override
  98. public boolean onCreateOptionsMenu(Menu menu) {
  99. if (HAS_ACTIONBAR) {
  100. boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;
  101. ActionBar actionBar = getActionBar();
  102. actionBar.setDisplayHomeAsUpEnabled(hasBackStack);
  103. actionBar.setHomeButtonEnabled(hasBackStack);
  104. }
  105. return true;
  106. }
  107. @Override
  108. public boolean onOptionsItemSelected(MenuItem item) {
  109. switch (item.getItemId()) {
  110. case android.R.id.home:
  111. mFragmentManager.popBackStack();
  112. return true;
  113. }
  114. return super.onOptionsItemSelected(item);
  115. }
  116. /**
  117. * Add the initial Fragment with given path.
  118. */
  119. private void addFragment() {
  120. FileListFragment fragment = FileListFragment.newInstance(mPath);
  121. mFragmentManager.beginTransaction()
  122. .add(android.R.id.content, fragment).commit();
  123. }
  124. /**
  125. * "Replace" the existing Fragment with a new one using given path. We're
  126. * really adding a Fragment to the back stack.
  127. *
  128. * @param file The file (directory) to display.
  129. */
  130. private void replaceFragment(File file) {
  131. mPath = file.getAbsolutePath();
  132. FileListFragment fragment = FileListFragment.newInstance(mPath);
  133. mFragmentManager.beginTransaction()
  134. .replace(android.R.id.content, fragment)
  135. .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
  136. .addToBackStack(mPath).commit();
  137. }
  138. /**
  139. * Finish this Activity with a result code and URI of the selected file.
  140. *
  141. * @param file The file selected.
  142. */
  143. private void finishWithResult(File file) {
  144. if (file != null) {
  145. Uri uri = Uri.fromFile(file);
  146. setResult(RESULT_OK, new Intent().setData(uri));
  147. finish();
  148. } else {
  149. setResult(RESULT_CANCELED);
  150. finish();
  151. }
  152. }
  153. /**
  154. * Called when the user selects a File
  155. *
  156. * @param file The file that was selected
  157. */
  158. @Override
  159. public void onFileSelected(File file) {
  160. if (file != null) {
  161. if (file.isDirectory()) {
  162. replaceFragment(file);
  163. } else {
  164. finishWithResult(file);
  165. }
  166. } else {
  167. Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file,
  168. Toast.LENGTH_SHORT).show();
  169. }
  170. }
  171. /**
  172. * Register the external storage BroadcastReceiver.
  173. */
  174. private void registerStorageListener() {
  175. IntentFilter filter = new IntentFilter();
  176. filter.addAction(Intent.ACTION_MEDIA_REMOVED);
  177. registerReceiver(mStorageListener, filter);
  178. }
  179. /**
  180. * Unregister the external storage BroadcastReceiver.
  181. */
  182. private void unregisterStorageListener() {
  183. unregisterReceiver(mStorageListener);
  184. }
  185. }