//這種方式只能瀏覽普通使用者有許可權的檔案。
package talent.fm;import java.io.File;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;public class FileView extends ListView{private FileAdapter browser;public interface OnPathChanged{abstract boolean onChanged(String old,String path);}public void SetOnPathChangedListener(OnPathChanged listener){if(browser==null) return;browser.onChanged = listener;}public FileView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public FileView(Context context, AttributeSet attrs) {super(context, attrs);init(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FileView); String path = a.getString(R.styleable.FileView_path); a.recycle(); if(path!=null) browser.SetPath(path); }public FileView(Context context) {super(context);init();}private void init(){browser = new FileAdapter();setAdapter(browser); setOnItemClickListener(onItemClick);} private OnItemClickListener onItemClick = new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {//com.Logi("item:"+arg2);if(!browser.OpenChild(arg2)){com.Toast(arg1.getContext(),"folder is empty or can't open");return;}} }; /**goto parent folder,return false if no parent folder.*/ public boolean Up(){ return browser.Up(); } public boolean SetPath(String path){ return browser.SetPath(path); } public String GetPath(){ return browser.GetPath(); }}class FileAdapter extends BaseAdapter{private File folder = File.listRoots()[0];private List<File> contents = new ArrayList<File>();protectedFileView.OnPathChanged onChanged;com.SU su = new com.SU();public FileAdapter(){su.Open();initContents(null);}@Overridepublic int getCount() {return contents.size();}/**return a File Object*/@Overridepublic Object getItem(int position) {if(position>=contents.size()) return null;return contents.get(position);}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); convertView = inflater.inflate(R.layout.item_explorer,null);} ImageView iv = (ImageView)convertView.findViewById(R.id.explorer_iv_image); int icon; File file = contents.get(position); if(file.isFile()) icon = R.drawable.icon_file; else if(file.isDirectory()) icon = R.drawable.icon_folder; else icon = R.drawable.icon_locked;iv.setImageResource(icon);TextView tv = (TextView)convertView.findViewById(R.id.explorer_iv_text);tv.setText(file.getName());;return convertView;}public boolean SetPath(String path){File file = new File(path);return initContents(file);}public String GetPath(){return folder.getPath();}public boolean OpenChild(int index){if(index>=contents.size()) return false;File sub = contents.get(index);if(!sub.isDirectory()) return false;return initContents(sub);}public boolean Up(){if(folder==null) return false;File root = folder.getParentFile();if(root==null) return false;return initContents(root);}private boolean initContents(File parent){if(parent==null){//this function return a File list that only one element of root folder.parent = File.listRoots()[0];}if(onChanged!=null){String old = "",cur = "";if(folder!=null)old = folder.getPath();cur = parent.getPath();if(onChanged.onChanged(old,cur)) return false;}//get parent contents.try {writeChmod(parent.getPath());File[] fs = parent.listFiles();contents.clear();folder = parent;resortByName(fs);} catch (SecurityException e){e.printStackTrace();return false;}notifyDataSetChanged();return true;}private void writeChmod(String file){String cmd = "chmod 777 " + file;su.Run(cmd);}private void resortByName(File[] ct){if(ct==null) return;Map<String,File> mapD = new HashMap<String,File>();//directoryMap<String,File> mapF = new HashMap<String,File>();//fileMap<String,File> mapU = new HashMap<String,File>();//unkownfor(int i=0;i<ct.length;i++){File file = ct[i];String name = file.getName();if(file.isDirectory()) mapD.put(name,file);else if(file.isFile()) mapF.put(name,file);else mapU.put(name,file);}sortName(mapD);//sort folder;sortName(mapF);//sort file;sortName(mapU);//sort unkown;}private void sortName(Map<String,File> map){String[] ns = new String[map.size()];Set<String> set = map.keySet();ns = set.toArray(ns);Arrays.sort(ns);for(int i=0;i<ns.length;i++){contents.add(map.get(ns[i]));}}}
//this is second edition,code is lesspackage talent.fm;import java.io.File;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;public class FileBrowser extends ListView{private BrowserAdapter browser;public interface OnPathChanged{abstract boolean onChanged(String old,String path);}public void SetOnPathChangedListener(OnPathChanged listener){if(browser==null) return;browser.onChanged = listener;}public FileBrowser(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public FileBrowser(Context context, AttributeSet attrs) {super(context, attrs);init(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FileView); String path = a.getString(R.styleable.FileView_path); a.recycle(); if(path!=null) browser.SetPath(path); }public FileBrowser(Context context) {super(context);init();}private void init(){browser = new BrowserAdapter();setAdapter(browser); setOnItemClickListener(onItemClick);} private OnItemClickListener onItemClick = new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {//com.Logi("item:"+arg2);if(!browser.OpenChild(arg2)){com.Toast(arg1.getContext(),"folder is empty or can't open");return;}} }; /**goto parent folder,return false if no parent folder.*/ public boolean Up(){ return browser.Up(); } public void SetPath(String path){ browser.SetPath(path); } public String GetPath(){ return browser.GetPath(); }}class BrowserAdapter extends BaseAdapter{private String path = "/";private List<FileInfo> contents = new ArrayList<FileInfo>();protectedFileBrowser.OnPathChanged onChanged; Console cs = new Console(); private class FileInfo{ StringName; intIconId; FileInfo(String name,int id){ Name = name; IconId = id; } boolean IsFolder(){ return R.drawable.icon_folder==IconId; } }public BrowserAdapter(){}@Overridepublic int getCount() {return contents.size();}/**return a File Object*/@Overridepublic Object getItem(int position) {if(position>=contents.size()) return null;return contents.get(position);}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); convertView = inflater.inflate(R.layout.item_explorer,null);} FileInfo fi = contents.get(position);ImageView iv = (ImageView)convertView.findViewById(R.id.explorer_iv_image);iv.setImageResource(fi.IconId);TextView tv = (TextView)convertView.findViewById(R.id.explorer_iv_text);tv.setText(fi.Name);return convertView;}public boolean SetPath(String path){if(path==null||path.length()<1)path = "/";File file = new File(path);if(!file.isDirectory()){com.Logw("FileBrowser.SetPath: path is not a directory");return false;}contents.clear();if(onChanged!=null) onChanged.onChanged(this.path,path);this.path = path;File[] fl = file.listFiles();if(fl==null){notifyDataSetChanged();return true;}Arrays.sort(fl);folderInsertPos = 0;for(int i=0;i<fl.length;i++){insertByFile(fl[i]);}notifyDataSetChanged();return true;}public String GetPath(){return path;}public boolean OpenChild(int index){if(index<0||index>=contents.size()) return false;FileInfo fi = contents.get(index);if(!fi.IsFolder()) return false;if(path.equals("/")) path = "";return SetPath(path+"/"+fi.Name);}public boolean Up(){File file = new File(path);File root = file.getParentFile();if(root==null){com.Logw("FileBrowser.Up: no parent path");return false;}SetPath(root.getPath());return true;}private int folderInsertPos = 0;/**insert FileInfo object in contents set,and position by name*/private void insertByFile(File file){int icon;if(file.isDirectory()){icon = R.drawable.icon_folder;contents.add(folderInsertPos,new FileInfo(file.getName(),icon));folderInsertPos++;}else{ if(file.isFile()){ icon = R.drawable.icon_file; } else{ icon = R.drawable.icon_script; }contents.add(new FileInfo(file.getName(),icon));}}}