package training.filesystem; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.R.integer; import android.R.layout; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.Display; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.LinearLayout.LayoutParams; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class FilesystemActivity extends ListActivity implements OnClickListener, OnItemSelectedListener { private TextView hint; private ListView list; private TextView filename; private Spinner filetype; private Button oKButton,cancelButton; private String root = "/"; private String root_sdcard = "/mnt/sdcard"; private ArrayList<String> path; private ArrayList<Map<String, Object>> item; private ArrayList<Version> aboutVersionList = new ArrayList<Version>(); private String pathString; private String PATH_FILE_NAME; private AlertDialog warnAlertDialog; private AlertDialog errorAlertDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); hint = (TextView) findViewById(R.id.path); filename =(TextView)findViewById(R.id.name); oKButton = (Button)findViewById(R.id.ok); cancelButton = (Button)findViewById(R.id.cancel); filetype = (Spinner)findViewById(R.id.spinner); filetype.setOnItemSelectedListener(this); list = this.getListView(); oKButton.setOnClickListener(this); cancelButton.setOnClickListener(this); getfiledir(root_sdcard,0); WindowManager manager = getWindowManager(); Display display = manager.getDefaultDisplay(); android.view.WindowManager.LayoutParams params = getWindow().getAttributes(); params.height = (int)(display.getHeight() * 0.5); params.width = (int)(display.getWidth() * 0.5); params.dimAmount = 0.0f; getWindow().setAttributes(params); getWindow().setGravity(Gravity.CENTER); } protected void getfiledir(String filepath,int type) { final int abcd = type; item = new ArrayList<Map<String, Object>>(); path = new ArrayList<String>(); hint.setText(filepath); pathString = filepath; File f = new File(filepath); File[] files = f.listFiles(); if (!filepath.equals(root)) { Map<String, Object> map = new HashMap<String, Object>(); map.put("icon", R.drawable.up_folder); map.put("name", "back to /"); item.add(map); path.add("/mnt/sdcard/"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("icon", R.drawable.up_folder); map2.put("name", "back to ../"); item.add(map2); path.add(f.getParent()); } if (files != null) { for (int i = 0; i < files.length; i++) { if(type == 0){ Map<String, Object> map = new HashMap<String, Object>(); if (files[i].isDirectory()) { map.put("icon", R.drawable.folder); }else { map.put("icon", R.drawable.file); } map.put("name", files[i].getName()); item.add(map); path.add(files[i].getPath()); }else if (type == 1) { if (files[i].isDirectory()) { Map<String , Object> map = new HashMap<String, Object>(); map.put("icon", R.drawable.folder); map.put("name", files[i].getName()); item.add(map); path.add(files[i].getPath()); } }else if (type == 2) { if (files[i].isFile()) { Map<String, Object> map = new HashMap<String, Object>(); map.put("icon", R.drawable.file); map.put("name", files[i].getName()); item.add(map); path.add(files[i].getPath()); } } } } SimpleAdapter simpleAdapter = new SimpleAdapter(FilesystemActivity.this, item, R.layout.line, new String[]{"icon","name"}, new int[]{R.id.icon,R.id.name}); list.setAdapter(simpleAdapter); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub File file = new File(path.get(arg2)); if (file.isDirectory()) { getfiledir(path.get(arg2),abcd); } else { Toast.makeText(FilesystemActivity.this, "这是一个文件!", Toast.LENGTH_SHORT).show(); filename.setText(file.getName()); PATH_FILE_NAME = file.getName(); } } }); } public boolean GetVersionInfo(){ String tempString = null; boolean isHaveVersion = false; try { File file = new File(pathString + "/" + PATH_FILE_NAME); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); while ((tempString = bufferedReader.readLine()) != null) { if (tempString.startsWith("[")) { if (tempString.substring(tempString.indexOf("[")+1,tempString.indexOf("]")).equals("Version")) { tempString = bufferedReader.readLine(); Version version = new Version(); String[] splitString = tempString.split(","); if (splitString.length != 3) { break; } version.VERSION_ID = splitString[0]; version.VERSION_DEVICE = splitString[1]; version.VERSION_PLATFORM = splitString[2]; aboutVersionList.add(version); isHaveVersion = true; break; } } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return false; } if (isHaveVersion) { return true; }else { return false; } } public class Version{ String VERSION_ID; String VERSION_PLATFORM; String VERSION_DEVICE; } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == oKButton) { if(GetVersionInfo()){ Version version = aboutVersionList.get(0); warnAlertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.warning).setTitle(getText(R.string.dialog_title)) .setMessage(getText(R.string.dialog_content_id) + version.VERSION_ID +"\n" + getText(R.string.dialog_content_platform) + version.VERSION_PLATFORM + "\n" + getText(R.string.dialog_content_device) + version.VERSION_DEVICE + "\n" + getText(R.string.dialog_content)).setPositiveButton(getText(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).setNegativeButton(getText(R.string.dialog_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); }else { errorAlertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.warning).setTitle(getText(R.string.dialog_title)) .setMessage(getText(R.string.dialog_error)) .setPositiveButton(getText(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); } } } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub String aString = filetype.getSelectedItem().toString(); if (aString.equals("所有档案")) { getfiledir(pathString,0); }else if (aString.equals("文件夹")) { getfiledir(pathString, 1); }else if (aString.equals("文件")) { getfiledir(pathString, 2); } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }