Advertisement
Guest User

Aaron - FileBrowser.java

a guest
Feb 17th, 2011
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. package org.IRE.toolbox;
  2. import java.io.DataOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URISyntaxException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import android.app.AlertDialog;
  10. import android.app.ListActivity;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.content.DialogInterface.OnClickListener;
  14.  
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.util.Log;
  18. import android.view.Menu;
  19. import android.view.MenuInflater;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.widget.ArrayAdapter;
  23. import android.widget.ListView;
  24.  
  25. public class FileBrowser extends ListActivity {
  26.     private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }
  27.    
  28.     private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
  29.     private List<String> directoryEntries = new ArrayList<String>();
  30.     private File currentDirectory = new File("/");
  31.    
  32.    
  33.    
  34.    
  35.     public boolean onCreateOptionsMenu(Menu menu) {
  36.         MenuInflater inflater = getMenuInflater();
  37.         inflater.inflate(R.menu.menu_button_infile, menu);
  38.         return true;
  39.     }
  40.     public boolean onOptionsItemSelected(MenuItem item) {
  41.         // Handle item selection
  42.         switch (item.getItemId()){
  43.         case R.id.Main:
  44.             Intent myIntent = new Intent(getApplicationContext(), Controller.class);
  45.             startActivityForResult(myIntent, 0);
  46.             return true;
  47.         default:
  48.             return super.onOptionsItemSelected(item);
  49.         }
  50.     }
  51.        
  52.    
  53.    
  54.     @Override
  55.     public void onCreate(Bundle savedInstanceState){
  56.         super.onCreate(savedInstanceState);
  57.     browseToIreFoler();
  58.    
  59.     }
  60.     private void browseToIreFoler() {
  61.         File sdCard = Environment.getExternalStorageDirectory();   
  62.          File dir = new File(sdCard.getAbsolutePath() +"/IRE/Downloads");
  63.         browseTo(dir);
  64. }
  65.       private void browseTo(final File aDirectory){
  66.           if (aDirectory.isDirectory()){
  67.                   this.currentDirectory = aDirectory;
  68.                   fill(aDirectory.listFiles());
  69.           }
  70.       }
  71.       private void fill(File[] files) {
  72.           this.directoryEntries.clear();
  73.          
  74.           // Add the "." == "current directory"
  75.           // and the ".." == 'Up one level'
  76.           this.directoryEntries.add(getString(R.string.current_dir));            
  77.           if(this.currentDirectory.getParent() != null)
  78.                   this.directoryEntries.add(getString(R.string.up_one_level));
  79.          
  80.           switch(this.displayMode){
  81.                   case ABSOLUTE:
  82.                           for (File file : files){
  83.                                   this.directoryEntries.add(file.getPath());
  84.                           }
  85.                           break;
  86.                   case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
  87.                           int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
  88.                           for (File file : files){
  89.                                   this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
  90.                           }
  91.                           break;
  92.           }
  93.           ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
  94.                   R.layout.file_row, this.directoryEntries);
  95.  
  96.   this.setListAdapter(directoryList);
  97.    
  98. }
  99.       private void upOneLevel(){
  100.           if(this.currentDirectory.getParent() != null)
  101.                   this.browseTo(this.currentDirectory.getParentFile());
  102.   }
  103.      @Override
  104.       protected void onListItemClick(ListView l, View v, int position, long id) {
  105.               int selectionRowID = (int) this.getSelectedItemId();
  106.              
  107.               String selectedFileString = this.directoryEntries.get(selectionRowID);
  108.               if (selectedFileString.equals(".")) {
  109.                       // Refresh
  110.                       this.browseTo(this.currentDirectory);
  111.               } else if(selectedFileString.equals("..")){
  112.                       this.upOneLevel();
  113.               } else {
  114.                       File clickedFile = null;
  115.                       switch(this.displayMode){
  116.                               case RELATIVE:
  117.                                       clickedFile = new File(this.currentDirectory.getAbsolutePath()
  118.                                                                                               + this.directoryEntries.get(selectionRowID));
  119.                                       break;
  120.                               case ABSOLUTE:
  121.                                       clickedFile = new File(this.directoryEntries.get(selectionRowID));
  122.                                       break;
  123.                       }
  124.                       if(clickedFile != null){
  125.                        
  126.                           String end = clickedFile.getName().substring(clickedFile.getName().lastIndexOf(".")+1, clickedFile.getName().length()).toLowerCase();
  127.                              // this.browseTo(clickedFile)
  128.                       Log.d("CLICKED_FILE", clickedFile.toString());
  129.                       Log.d("CLICKED_FILE", end);
  130.                       if(end.equalsIgnoreCase("zip")){
  131.                           Runtime run = Runtime.getRuntime();
  132.                           Process p = null;
  133.                           DataOutputStream out = null;
  134.                           try{
  135.                               p = run.exec("su");
  136.                               out = new DataOutputStream(p.getOutputStream());
  137.                               out.writeBytes("echo install_zip SDCARD:" +clickedFile.toString() +" > /cache/recovery/extendedcommand\n");
  138.                               out.writeBytes("reboot recovery\n"); // testing
  139.                               out.flush();
  140.  
  141.                           }catch(Exception e){
  142.                               Log.e("FLASH", "Unable to reboot into recovery mode:", e);
  143.                               e.printStackTrace();
  144.  
  145.                           }
  146.                       }
  147.  
  148.               }
  149.              
  150.              
  151.              
  152.              
  153.       }
  154.      }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement