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; /* w w w . ja v a2 s. co m*/ import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.text.format.DateFormat; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.binkery.app.filemanager.R; import com.binkery.app.filemanager.utils.Logs; import com.binkery.app.filemanager.utils.StringUtils; public class DetailDialogFragment extends BaseDialogFragment { private static final String TAG = DetailDialogFragment.class .getSimpleName(); private static final int TYPE_FILE = 1; private static final int TYPE_FOLDER = 2; private static final int TYPE_SIZE = 3; private FileStaticAsyncTask mTask = null; private List<Item> mData = new ArrayList<Item>(); public static DetailDialogFragment newInstance(String[] paths){ DetailDialogFragment fragment = new DetailDialogFragment(); Bundle b = new Bundle(); b.putStringArray("paths", paths); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } private BaseAdapter mAdapter = new BaseAdapter() { @Override public View getView(int position, View view, ViewGroup parent) { if (view == null) { view = View.inflate(getActivity(), R.layout.view_detail_list_item, null); } TextView title = (TextView) view.findViewById(R.id.title); title.setText(getItem(position).key); TextView value = (TextView) view.findViewById(R.id.value); value.setText(getItem(position).value); return view; } @Override public long getItemId(int position) { return position; } @Override public Item getItem(int position) { return mData != null ? mData.get(position) : null; } @Override public int getCount() { return mData != null ? mData.size() : 0; } }; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Logs.i(TAG, "onCreateDialog"); Dialog dialog = new AlertDialog.Builder(getActivity()) .setAdapter(mAdapter, null) .setTitle(R.string.detail) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (mTask != null) { mTask.cancel(true); } } }).create(); return dialog; } @Override public void onActivityCreated(Bundle savedInstanceState) { Logs.i(TAG, "onActivityCreated"); super.onActivityCreated(savedInstanceState); mData.add(new Item("Count",mPaths.length + "")); // mData.add(new Item(R.string.detail_key_name, mFile.getName())); // String permission = ""; // permission += mFile.canRead() ? "R" : ""; // permission += mFile.canWrite() ? "W" : ""; // permission += mFile.canExecute() ? "X" : ""; // mData.add(new Item(R.string.detail_key_permission, permission)); // mData.add(new Item(R.string.detail_key_modified, formatDate(mFile.lastModified()) // .toString())); // if (mFile.isFile()) { // mData.add(new Item(R.string.detail_key_size, StringUtils.formatFileSize(mFile // .length()))); // } else { // mData.add(new Item(R.string.detail_key_size, "Loading...", TYPE_SIZE)); // mData.add(new Item(R.string.detail_key_file_count, "Loading...", TYPE_FILE)); // mData.add(new Item(R.string.detail_key_folder_count, "Loading...", TYPE_FOLDER)); // } // mData.add(new Item(R.string.detail_key_location, mFile.getAbsolutePath())); // if (mFile.isDirectory()) { // mTask = new FileStaticAsyncTask(); // mTask.execute(mFile); // } mAdapter.notifyDataSetChanged(); } public static CharSequence formatDate(long time) { return DateFormat.format("yyyy-MM-dd hh:mm:ss", time); } private class Item { public int type = 0; public String key = ""; public String value = ""; public Item(String k, String v) { key = k; value = v; } // public Item(int k, String v, int t) { // this(k, v); // type = t; // } } private class FileStaticAsyncTask extends AsyncTask<File, String, String> { private long mTotalSize = 0; private int mFileCount = 0; private int mFolderCount = 0; @Override protected String doInBackground(File... params) { if (params[0].isFile()) { mTotalSize = params[0].length(); return null; } calculateFileCount(params[0]); return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Logs.i(TAG, "onPostExecute"); for(int i = 0 , len = mData.size();i<len;i++){ Item item = mData.get(i); switch(item.type){ case TYPE_SIZE: item.value = StringUtils.formatFileSize(mTotalSize); break; case TYPE_FOLDER: item.value = String.valueOf(mFolderCount); break; case TYPE_FILE: item.value = String.valueOf(mFileCount); break; } } mAdapter.notifyDataSetChanged(); } private void calculateFileCount(File file) { Logs.i(TAG, "count " + file.getAbsolutePath()); if (isCancelled()) { return; } if (!file.isDirectory()) { mFileCount++; mTotalSize += file.length(); return; } if (file.list() == null) { return; } for (String fileName : file.list()) { File f = new File(file.getAbsolutePath() + File.separator + fileName); mFolderCount++; calculateFileCount(f); } } } }