FileChooserActivity.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 com.xzjmyk.pm.activity.R;
  35. import java.io.File;
  36. /**
  37. *
  38. * @项目名称: SkWeiChat-Baidu
  39. * @包名: com.ipaulpro.afilechooser
  40. * @作者:王阳
  41. * @创建时间: 2015年10月12日 上午9:25:39
  42. * @描述:
  43. * @SVN版本号: $Rev: 2143 $
  44. * @修改人: $Author: luorc $
  45. * @修改时间: $Date: 2015-10-23 09:31:46 +0800 (Fri, 23 Oct 2015) $
  46. * @修改的内容: 主activity 文件选择
  47. */
  48. public class FileChooserActivity extends FragmentActivity implements
  49. OnBackStackChangedListener, FileListFragment.Callbacks {
  50. public static final String PATH = "path";
  51. public static final String EXTERNAL_BASE_PATH = Environment
  52. .getExternalStorageDirectory().getAbsolutePath();
  53. private static final boolean HAS_ACTIONBAR = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
  54. private FragmentManager mFragmentManager;
  55. private BroadcastReceiver mStorageListener = new BroadcastReceiver() {
  56. @Override
  57. public void onReceive(Context context, Intent intent) {
  58. Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show();
  59. finishWithResult(null);
  60. }
  61. };
  62. private String mPath;
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. mFragmentManager = getSupportFragmentManager();
  67. mFragmentManager.addOnBackStackChangedListener(this);
  68. if (savedInstanceState == null) {
  69. mPath = EXTERNAL_BASE_PATH;
  70. addFragment();
  71. } else {
  72. mPath = savedInstanceState.getString(PATH);
  73. }
  74. setTitle(mPath);
  75. }
  76. @Override
  77. protected void onPause() {
  78. super.onPause();
  79. unregisterStorageListener();
  80. }
  81. @Override
  82. protected void onResume() {
  83. super.onResume();
  84. registerStorageListener();
  85. }
  86. @Override
  87. protected void onSaveInstanceState(Bundle outState) {
  88. super.onSaveInstanceState(outState);
  89. outState.putString(PATH, mPath);
  90. }
  91. @Override
  92. public void onBackStackChanged() {
  93. int count = mFragmentManager.getBackStackEntryCount();
  94. if (count > 0) {
  95. BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1);
  96. mPath = fragment.getName();
  97. } else {
  98. mPath = EXTERNAL_BASE_PATH;
  99. }
  100. setTitle(mPath);
  101. if (HAS_ACTIONBAR)
  102. invalidateOptionsMenu();
  103. }
  104. @Override
  105. public boolean onCreateOptionsMenu(Menu menu) {
  106. if (HAS_ACTIONBAR) {
  107. boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;
  108. ActionBar actionBar = getActionBar();
  109. actionBar.setDisplayHomeAsUpEnabled(hasBackStack);
  110. actionBar.setHomeButtonEnabled(hasBackStack);
  111. }
  112. return true;
  113. }
  114. @Override
  115. public boolean onOptionsItemSelected(MenuItem item) {
  116. switch (item.getItemId()) {
  117. case android.R.id.home:
  118. mFragmentManager.popBackStack();
  119. return true;
  120. }
  121. return super.onOptionsItemSelected(item);
  122. }
  123. /**
  124. * Add the initial Fragment with given path.
  125. */
  126. private void addFragment() {
  127. FileListFragment fragment = FileListFragment.newInstance(mPath);
  128. mFragmentManager.beginTransaction()
  129. .add(android.R.id.content, fragment).commitAllowingStateLoss();
  130. }
  131. /**
  132. * "Replace" the existing Fragment with a new one using given path. We're
  133. * really adding a Fragment to the back stack.
  134. *
  135. * @param file The file (directory) to display.
  136. */
  137. private void replaceFragment(File file) {
  138. mPath = file.getAbsolutePath();
  139. FileListFragment fragment = FileListFragment.newInstance(mPath);
  140. mFragmentManager.beginTransaction()
  141. .replace(android.R.id.content, fragment)
  142. .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
  143. .addToBackStack(mPath).commit();
  144. }
  145. /**
  146. * Finish this Activity with a result code and URI of the selected file.
  147. *
  148. * @param file The file selected.
  149. */
  150. private void finishWithResult(File file) {
  151. if (file != null) {
  152. Uri uri = Uri.fromFile(file);
  153. setResult(RESULT_OK, new Intent().setData(uri));
  154. finish();
  155. } else {
  156. setResult(RESULT_CANCELED);
  157. finish();
  158. }
  159. }
  160. /**
  161. * Called when the user selects a File
  162. *
  163. * @param file The file that was selected
  164. */
  165. @Override
  166. public void onFileSelected(File file) {
  167. if (file != null) {
  168. if (file.isDirectory()) {
  169. replaceFragment(file);
  170. } else {
  171. finishWithResult(file);
  172. }
  173. } else {
  174. Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file,
  175. Toast.LENGTH_SHORT).show();
  176. }
  177. }
  178. /**
  179. * Register the external storage BroadcastReceiver.
  180. */
  181. private void registerStorageListener() {
  182. IntentFilter filter = new IntentFilter();
  183. filter.addAction(Intent.ACTION_MEDIA_REMOVED);
  184. registerReceiver(mStorageListener, filter);
  185. }
  186. /**
  187. * Unregister the external storage BroadcastReceiver.
  188. */
  189. private void unregisterStorageListener() {
  190. unregisterReceiver(mStorageListener);
  191. }
  192. }