Back to project page filemanager.
The source code is released under:
MIT License
If you think the Android project filemanager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.binkery.app.filemanager.fragments; /*from w ww . j a v a 2 s. co m*/ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.content.LocalBroadcastManager; import android.util.SparseBooleanArray; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.webkit.MimeTypeMap; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.binkery.app.filemanager.BaseFragment; import com.binkery.app.filemanager.FragmentController; import com.binkery.app.filemanager.R; import com.binkery.app.filemanager.commons.Constants; import com.binkery.app.filemanager.settings.FileSettings; import com.binkery.app.filemanager.utils.FileSortUtils; import com.binkery.app.filemanager.utils.Logs; import com.binkery.app.filemanager.utils.OptionMenuUtils; import com.binkery.app.filemanager.utils.StringUtils; import com.binkery.app.filemanager.utils.ToastUtils; public class FileListFragment extends BaseFragment implements OnItemClickListener { private static final String TAG = FileListFragment.class.getSimpleName(); private static MimeTypeMap mMimeTypeMap = null; private FileAdapter mAdapter = null; private ListView mListView = null; private File mCurFile = null; private List<File> mFileList = new ArrayList<File>(); public static final int CLIP_TYPE_NONE = 0; public static final int CLIP_TYPE_COPY = 1; public static final int CLIP_TYPE_MOVE = 2; private static File mClipAreaFile = null; private static int mClipType = CLIP_TYPE_NONE; private static final int OPTION_MENU_SELECTED = 0; private static final int OPTION_MENU_SELECTED_CANCEL = 1; private static final int OPTION_MENU_MOVE_TO_HERE = 2; private static final int OPTION_MENU_COPY_TO_HERE = 3; private static final int CONTEXT_MENU_DELETE = 0; private static final int CONTEXT_MENU_DETAIL = 1; private static final int CONTEXT_MENU_COPY = 2; private static final int CONTEXT_MENU_MOVE = 3; private static final int CONTEXT_MENU_RENAME = 4; private enum OptionMenu { SELECTED("??") { @Override void onSelected(FileListFragment fragment) { fragment.mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); fragment.mAdapter.notifyDataSetChanged(); fragment.mListView.clearChoices(); fragment.getActivity().getWindow() .invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); } }, SELECTED_CANCEL("??????") { @Override void onSelected(FileListFragment fragment) { fragment.mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); fragment.mAdapter.notifyDataSetChanged(); fragment.getActivity().getWindow() .invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); } }, SHOW_HIDDEN("????????") { @Override void onSelected(FileListFragment fragment) { FileSettings.showHidden(); } }, HIDE_HIDDEN("??????????") { @Override void onSelected(FileListFragment fragment) { FileSettings.hideHidden(); } }, PASTE("??") { @Override void onSelected(FileListFragment fragment) { } }, MOVE("??") { @Override void onSelected(FileListFragment fragment) { } } , COPY("???") { @Override void onSelected(FileListFragment fragment) { } },DELETE("??"){ @Override void onSelected(FileListFragment fragment) { long[] ids = fragment.mListView.getCheckedItemIds(); if (ids == null || ids.length == 0) { return; } String[] paths = new String[ids.length]; for (int i = 0, len = ids.length; i < len; i++) { paths[i] = fragment.mFileList.get((int) ids[i]).getPath(); } FragmentController.showFilesDeleteFragment(fragment.getActivity(), paths); } },DETAIL("??"){ @Override void onSelected(FileListFragment fragment) { long[] ids = fragment.mListView.getCheckedItemIds(); if (ids == null || ids.length == 0) { return; } String[] paths = new String[ids.length]; for (int i = 0, len = ids.length; i < len; i++) { paths[i] = fragment.mFileList.get((int) ids[i]).getPath(); } FragmentController.showFileDetailFragment(fragment.getActivity(), paths); } },RENAME("??????"){ @Override void onSelected(FileListFragment fragment) { long[] ids = fragment.mListView.getCheckedItemIds(); if (ids == null || ids.length != 1) { return; } String[] paths = new String[1]; paths[0]= fragment.mFileList.get((int) ids[0]).getPath(); FragmentController.showFileRenameFragment(fragment.getActivity(), paths); } }; private String resid; private OptionMenu(String resid) { this.resid = resid; } private void appendToMenu(Menu menu) { menu.add(0, ordinal(), 0, resid).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);; } abstract void onSelected(FileListFragment fragment); } public static FileListFragment newInstance(String path) { FileListFragment fragment = new FileListFragment(); Bundle b = new Bundle(); b.putString("path", path); fragment.setArguments(b); return fragment; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { Logs.i(TAG, "onCreateOptionsMenu" + this); super.onCreateOptionsMenu(menu, inflater); } @Override public void onPrepareOptionsMenu(Menu menu) { Logs.i(TAG, "onPrepareOptionsMenu"); super.onPrepareOptionsMenu(menu); menu.clear(); if (mFileList == null || mFileList.size() == 0) { } else if (mListView.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) { OptionMenu.SELECTED.appendToMenu(menu); } else { OptionMenu.SELECTED_CANCEL.appendToMenu(menu); } if(FileSettings.isShowHidden()){ OptionMenu.HIDE_HIDDEN.appendToMenu(menu); }else{ OptionMenu.SHOW_HIDDEN.appendToMenu(menu); } if(mListView.getChoiceMode() == AbsListView.CHOICE_MODE_MULTIPLE && mListView.getCheckedItemCount() > 0){ OptionMenu.DETAIL.appendToMenu(menu); OptionMenu.DELETE.appendToMenu(menu); if(mListView.getCheckedItemCount() == 1){ OptionMenu.RENAME.appendToMenu(menu); } } // if (mFileList == null || mFileList.size() == 0) { // OptionMenuUtils.delete(menu, OPTION_MENU_SELECTED_CANCEL); // OptionMenuUtils.delete(menu, OPTION_MENU_SELECTED); // } else if (mListView.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) { // OptionMenuUtils.delete(menu, OPTION_MENU_SELECTED_CANCEL); // OptionMenuUtils.add(menu, OPTION_MENU_SELECTED, "??"); // } else { // OptionMenuUtils.delete(menu, OPTION_MENU_SELECTED); // OptionMenuUtils.add(menu, OPTION_MENU_SELECTED_CANCEL, "??????"); // } // // OptionMenuUtils.delete(menu, OPTION_MENU_MOVE_TO_HERE); // OptionMenuUtils.delete(menu, OPTION_MENU_COPY_TO_HERE); // if(mClipType == CLIP_TYPE_MOVE){ // OptionMenuUtils.add(menu, OPTION_MENU_MOVE_TO_HERE, "????"); // }else if(mClipType == CLIP_TYPE_COPY){ // OptionMenuUtils.add(menu, OPTION_MENU_COPY_TO_HERE, "?????"); // } } @Override public boolean onOptionsItemSelected(MenuItem item) { if(super.onOptionsItemSelected(item)){ return true; } Logs.i(TAG, "onOptionsItemSelected"); int id = item.getItemId(); OptionMenu.values()[id].onSelected(this); // OptionMenu.selected(id,this); // switch (id) { // case OPTION_MENU_SELECTED: // mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); // mAdapter.notifyDataSetChanged(); // break; // case OPTION_MENU_SELECTED_CANCEL: // mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); // mAdapter.notifyDataSetChanged(); // break; // case OPTION_MENU_MOVE_TO_HERE: // moveFile(); // break; // case OPTION_MENU_COPY_TO_HERE: //// copy(); // new CopyTask(this).execute(mClipAreaFile,mCurFile); // break; // } return true; } public void onCopyCompleted(Integer result){ mClipType = CLIP_TYPE_NONE; switch(result){ case CopyTask.OK: mFileList = FileSortUtils.list(mCurFile); mAdapter.notifyDataSetChanged(); break; case CopyTask.FAILED_FILE_NO_EXIST: break; case CopyTask.FAILED_NO_SPACE: AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setTitle("Notification").setMessage("No enough space").setNegativeButton(R.string.ok, null); builder.create().show(); break; } } public void freshFileList() { mFileList = FileSortUtils.list(mCurFile); mAdapter.notifyDataSetChanged(); } private void moveFile() { // If file is null or file not exist if (mClipAreaFile == null || !mClipAreaFile.exists()) { mClipType = CLIP_TYPE_NONE; return; } File newFile = new File(mCurFile.getAbsoluteFile() + File.separator + mClipAreaFile.getName()); if (mClipAreaFile.renameTo(newFile)) { mFileList = FileSortUtils.list(mCurFile); mAdapter.notifyDataSetChanged(); mClipType = CLIP_TYPE_NONE; return; } ToastUtils.show(getActivity(), "Move file failed ."); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); File file = mFileList.get(info.position); String path = file.getAbsolutePath(); int id = item.getItemId(); switch (id) { case CONTEXT_MENU_DELETE: // BaseDialogFragment.show(DeleteDialogFragment.class, path,getFragmentManager()); break; case CONTEXT_MENU_DETAIL: // BaseDialogFragment.show(DetailDialogFragment.class, path,getFragmentManager()); break; case CONTEXT_MENU_COPY: mClipAreaFile = file; mClipType = CLIP_TYPE_COPY; break; case CONTEXT_MENU_MOVE: mClipAreaFile = file; mClipType = CLIP_TYPE_MOVE; break; case CONTEXT_MENU_RENAME: // BaseDialogFragment.show(RenameDialogFragment.class, path,getFragmentManager()); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { Logs.i(TAG, "onCreateContextMenu"); super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, CONTEXT_MENU_DELETE, 0, R.string.delete); menu.add(0, CONTEXT_MENU_DETAIL, 0, R.string.detail); menu.add(0, CONTEXT_MENU_COPY, 0, R.string.copy); menu.add(0, CONTEXT_MENU_MOVE, 0, R.string.move); menu.add(0, CONTEXT_MENU_RENAME, 0, R.string.rename); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Logs.i(TAG, "onCreate" + this); Bundle b = getArguments(); String path = b.getString("path"); mCurFile = new File(path); setHasOptionsMenu(true); if (mMimeTypeMap == null) { mMimeTypeMap = MimeTypeMap.getSingleton(); } // print(); } @Override public void onDetach() { Logs.i(TAG, "onDetach"); super.onDetach(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Logs.i(TAG, "onCreateView mPath = " + mCurFile.getAbsolutePath()); View view = inflater.inflate(R.layout.frag_file_list, null); mListView = (ListView) view.findViewById(R.id.listview); mAdapter = new FileAdapter(); mListView.setAdapter(mAdapter); // File file = new File(mPath); mFileList = FileSortUtils.list(mCurFile); mListView.setOnItemClickListener(this); // mListView.setOnCreateContextMenuListener(this); getActivity().getActionBar().setTitle(mCurFile.getName()); // print(); return view; } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mFileList = FileSortUtils.list(mCurFile); mAdapter.notifyDataSetChanged(); } }; @Override public void onResume() { Logs.i(TAG, "onResume"); IntentFilter filter = new IntentFilter(); filter.addAction(Constants.INTENT_FILE_DELETED); filter.addAction(Constants.INTENT_FILE_RENAME); LocalBroadcastManager.getInstance(getActivity()).registerReceiver( mReceiver, filter); super.onResume(); } @Override public void onPause() { Logs.i(TAG, "onPause"); LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver( mReceiver); super.onPause(); } @Override public void onStop() { Logs.i(TAG, "onStop"); super.onStop(); } @Override public void onDestroyView() { Logs.i(TAG, "onDestroyView"); mListView.setOnCreateContextMenuListener(null); super.onDestroyView(); } @Override public void onDestroy() { Logs.i(TAG, "onDestroy"); super.onDestroy(); } private class FileAdapter extends BaseAdapter { @Override public int getCount() { return mFileList == null ? 0 : mFileList.size(); } @Override public File getItem(int position) { return mFileList == null ? null : mFileList.get(position); } @Override public long getItemId(int position) { return position; } @Override public boolean hasStableIds() { return true; } @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = View.inflate(getActivity(), R.layout.view_file_list_item, null); } File file = getItem(position); ImageView icon = (ImageView) view.findViewById(R.id.icon); CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox); if (mListView.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) { checkbox.setChecked(false); checkbox.setVisibility(View.GONE); } else if (mListView.getChoiceMode() == AbsListView.CHOICE_MODE_MULTIPLE) { checkbox.setVisibility(View.VISIBLE); boolean isSelected = mListView.getCheckedItemPositions().get( position); checkbox.setChecked(isSelected); } TextView tv = (TextView) view.findViewById(R.id.title); TextView fz = (TextView) view.findViewById(R.id.filesize); if (file.isDirectory()) { tv.setText(file.getName() + "/"); int[] children = getFileAndFolderCount(file); String text = getResources().getString(R.string.folder_static); text = String.format(text, children[0], children[1]); fz.setText(text); icon.setImageResource(R.drawable.ic_folder); } else { tv.setText(file.getName()); fz.setText(StringUtils.formatFileSize(file.length())); icon.setImageResource(R.drawable.ic_file); // if (MimeTypeUtils.isImage(file.getName()) // || MimeTypeUtils.isVideo(file.getName())) { // icon.setTag(file.getAbsolutePath()); // Bitmap bitmap = ThumbnailLoader.getInstance(getActivity()) // .loadMicroThumbnail(file.getAbsolutePath(), icon); // if (bitmap != null) { // icon.setImageBitmap(bitmap); // } else { // icon.setImageResource(R.drawable.app_icon); // } // } } return view; } } private Bitmap getImageThumbnail(String imagePath) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // ???????????????????bitmap?null bitmap = BitmapFactory.decodeFile(imagePath, options); options.inJustDecodeBounds = false; // ?? false // ????? int h = options.outHeight; int w = options.outWidth; int beWidth = w / 96; int beHeight = h / 96; int be = 1; if (beWidth < beHeight) { be = beWidth; } else { be = beHeight; } if (be <= 0) { be = 1; } options.inSampleSize = be; // ??????????????????bitmap?????????options.inJustDecodeBounds ?? false bitmap = BitmapFactory.decodeFile(imagePath, options); // ??ThumbnailUtils?????????????????????Bitmap?? bitmap = ThumbnailUtils.extractThumbnail(bitmap, 96, 96, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } private void print(){ Cursor cursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA}, null, null, null); if(cursor == null){ Logs.i(TAG, "cursor is null 0"); return; } if(!cursor.moveToFirst()){ cursor.close(); Logs.i(TAG, "cursor is empty 0"); return; } do{ String path = cursor.getString(0); Logs.i(TAG, "path = " + path); // /storage/emulated/0/tencent/MobileQQ/head/_hd/AACA98351DEB567BCB3B0823A41763B7.png }while(cursor.moveToNext()); cursor.close(); } private int[] getFileAndFolderCount(File file) { final int[] EMPTY = new int[] { 0, 0 }; if (file == null || !file.isDirectory()) { return EMPTY; } File[] files = file.listFiles(); if (files == null || files.length == 0) { return EMPTY; } int fileCount = 0; int folderCount = 0; for (int i = 0, len = files.length; i < len; i++) { if (files[i].isFile()) { fileCount++; } else if (files[i].isDirectory()) { folderCount++; } } return new int[] { fileCount, folderCount }; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Logs.i(TAG, "onItemClick"); File file = mFileList.get(position); int mode = mListView.getChoiceMode(); if (mode == AbsListView.CHOICE_MODE_MULTIPLE) { boolean isSelected = mListView.isItemChecked(position); ((CheckBox) view.findViewById(R.id.checkbox)) .setChecked(isSelected); getActivity().getWindow() .invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); return; } if (file.isDirectory()) { FileListFragment fragment = FileListFragment.newInstance(file .getAbsolutePath()); addFragment(fragment); return; } Intent intent = new Intent(Intent.ACTION_VIEW); String absolutionPath = file.getAbsolutePath(); Logs.i(TAG, "absolution path = " + absolutionPath); // Uri data = Uri.parse("file://" + file.getAbsolutePath()); // // Uri uri = Uri.fromFile(file); // String extension = FileUtils.getExtensionName(file.getName()); // Logs.i(TAG, "extension = " + extension); // String mimeType = null; // if(extension != null && !extension.equals("")){ // mimeType = mMimeTypeMap.getMimeTypeFromExtension(extension.toLowerCase()); // Logs.i(TAG, "mimeType = " + mimeType); // } // if(mimeType != null){ // intent.setDataAndType(uri, mimeType); // }else{ intent.setData(uri); // } try { getActivity().startActivity(intent); } catch (ActivityNotFoundException e) { ToastUtils.show(getActivity(), "Can not open this file ."); } } @Override protected boolean onKeyBack() { if (mListView.getChoiceMode() == AbsListView.CHOICE_MODE_NONE) { return false; } mListView.setChoiceMode(AbsListView.CHOICE_MODE_NONE); mListView.clearChoices(); mAdapter.notifyDataSetChanged(); getActivity().getWindow() .invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); return true; } }