diff --git a/res/layout/main.xml b/res/layout/main.xml index 2b1a95e2..842378f6 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -7,9 +7,9 @@ - + android:text="@string/hello" + /> + + + + + + + + diff --git a/res/layout/ua_com_vassiliev_filebrowser_layout.xml b/res/layout/ua_com_vassiliev_filebrowser_layout.xml index 4c755c61..a568ecf8 100644 --- a/res/layout/ua_com_vassiliev_filebrowser_layout.xml +++ b/res/layout/ua_com_vassiliev_filebrowser_layout.xml @@ -10,20 +10,31 @@ android:layout_height="wrap_content" android:orientation="horizontal" > - + android:layout_weight="0.5" + android:src="@drawable/dir_up" + android:text="" + android:textAppearance="?android:attr/textAppearanceLarge" /> + + + android:text="Select" + android:textAppearance="?android:attr/textAppearanceLarge" /> AndroidFileBrowserExampleActivity Android File Browser Example + Choose a directory name + OK + Cancel + Unnamed - \ No newline at end of file + diff --git a/src/ua/com/vassiliev/androidfilebrowser/FileBrowserActivity.java b/src/ua/com/vassiliev/androidfilebrowser/FileBrowserActivity.java index 316e5e18..3f27ac7e 100644 --- a/src/ua/com/vassiliev/androidfilebrowser/FileBrowserActivity.java +++ b/src/ua/com/vassiliev/androidfilebrowser/FileBrowserActivity.java @@ -1,4 +1,5 @@ package ua.com.vassiliev.androidfilebrowser; + //Heavily based on code from //https://github.com/mburman/Android-File-Explore // Version of Aug 13, 2011 @@ -10,108 +11,101 @@ //General Java imports import java.io.File; import java.io.FilenameFilter; +import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; -import java.util.Collections; -//Android imports import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; -//import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; import android.util.Log; +import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.view.View.OnClickListener; +import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; - - +//Android imports +//import android.content.res.ColorStateList; //Import of resources file for file browser -import ua.com.vassiliev.androidfilebrowser.R; +public class FileBrowserActivity extends Activity { + // Intent Action Constants + public static final String INTENT_ACTION_SELECT_DIR = "ua.com.vassiliev.androidfilebrowser.SELECT_DIRECTORY_ACTION"; + public static final String INTENT_ACTION_SELECT_FILE = "ua.com.vassiliev.androidfilebrowser.SELECT_FILE_ACTION"; + // Intent parameters names constants + public static final String startDirectoryParameter = "ua.com.vassiliev.androidfilebrowser.directoryPath"; + public static final String returnDirectoryParameter = "ua.com.vassiliev.androidfilebrowser.directoryPathRet"; + public static final String returnFileParameter = "ua.com.vassiliev.androidfilebrowser.filePathRet"; + public static final String showCannotReadParameter = "ua.com.vassiliev.androidfilebrowser.showCannotRead"; -public class FileBrowserActivity extends Activity { - //Intent Action Constants - public static final String INTENT_ACTION_SELECT_DIR = - "ua.com.vassiliev.androidfilebrowser.SELECT_DIRECTORY_ACTION"; - public static final String INTENT_ACTION_SELECT_FILE = - "ua.com.vassiliev.androidfilebrowser.SELECT_FILE_ACTION"; - - //Intent parameters names constants - public static final String startDirectoryParameter = - "ua.com.vassiliev.androidfilebrowser.directoryPath"; - public static final String returnDirectoryParameter = - "ua.com.vassiliev.androidfilebrowser.directoryPathRet"; - public static final String returnFileParameter = - "ua.com.vassiliev.androidfilebrowser.filePathRet"; - public static final String showCannotReadParameter = - "ua.com.vassiliev.androidfilebrowser.showCannotRead"; - - - // Stores names of traversed directories ArrayList pathDirsList = new ArrayList(); // Check if the first level of the directory structure is the one showing - //private Boolean firstLvl = true; + // private Boolean firstLvl = true; private static final String LOGTAG = "F_PATH"; private List fileList = new ArrayList(); private File path = null; private String chosenFile; - //private static final int DIALOG_LOAD_FILE = 1000; + + // private static final int DIALOG_LOAD_FILE = 1000; ArrayAdapter adapter; - + private boolean showHiddenFilesAndDirs = true; - + private boolean directoryShownIsEmpty = false; - - //Action constants + + // Action constants private static int currentAction = -1; private static final int SELECT_DIRECTORY = 1; private static final int SELECT_FILE = 2; - - + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - //In case of ua.com.vassiliev.androidfilebrowser.SELECT_DIRECTORY_ACTION - //Expects com.mburman.fileexplore.directoryPath parameter to + // In case of + // ua.com.vassiliev.androidfilebrowser.SELECT_DIRECTORY_ACTION + // Expects com.mburman.fileexplore.directoryPath parameter to // point to the start folder. // If empty or null, will start from SDcard root. setContentView(R.layout.ua_com_vassiliev_filebrowser_layout); - - //Set action for this activity + + // Set action for this activity Intent thisInt = this.getIntent(); - currentAction = SELECT_DIRECTORY;//This would be a default action in case not set by intent -// if(thisInt.getAction().equalsIgnoreCase(INTENT_ACTION_SELECT_DIR)) { -// currentAction = SELECT_DIRECTORY; -// } - if(thisInt.getAction().equalsIgnoreCase(INTENT_ACTION_SELECT_FILE)) { + currentAction = SELECT_DIRECTORY;// This would be a default action in + // case not set by intent + + if (thisInt.getAction().equalsIgnoreCase(INTENT_ACTION_SELECT_FILE)) { Log.d(LOGTAG, "SELECT ACTION - SELECT FILE"); currentAction = SELECT_FILE; } - - showHiddenFilesAndDirs = - thisInt.getBooleanExtra(showCannotReadParameter, true); - + + showHiddenFilesAndDirs = thisInt.getBooleanExtra( + showCannotReadParameter, true); + setInitialDirectory(); - - parceDirectoryPath(); + + parseDirectoryPath(); loadFileList(); this.createFileListAdapter(); this.initializeButtons(); @@ -119,187 +113,241 @@ public void onCreate(Bundle savedInstanceState) { updateCurrentDirectoryTextView(); Log.d(LOGTAG, path.getAbsolutePath()); } - - + private void setInitialDirectory() { - //this.path = Environment.getExternalStorageDirectory(); + Intent thisInt = this.getIntent(); - String requestedStartDir = - thisInt.getStringExtra(startDirectoryParameter); - - if(requestedStartDir!=null - && - requestedStartDir.length()>0 - ) {//if(requestedStartDir!=null + String requestedStartDir = thisInt + .getStringExtra(startDirectoryParameter); + + if (requestedStartDir != null && requestedStartDir.length() > 0) { File tempFile = new File(requestedStartDir); - if(tempFile.isDirectory()) + if (tempFile.isDirectory()) this.path = tempFile; - }//if(requestedStartDir!=null - - if(this.path==null) {//No or invalid directory supplied in intent parameter - if(Environment.getExternalStorageDirectory().isDirectory() - && - Environment.getExternalStorageDirectory().canRead()) + } + + if (this.path == null) {// No or invalid directory supplied in intent + // parameter + if (Environment.getExternalStorageDirectory().isDirectory() + && Environment.getExternalStorageDirectory().canRead()) path = Environment.getExternalStorageDirectory(); else path = new File("/"); - }//if(this.path==null) {//No or invalid directory supplied in intent parameter - }//private void setInitialDirectory() { - - - private void parceDirectoryPath() { + } + } + + private void parseDirectoryPath() { pathDirsList.clear(); String pathString = path.getAbsolutePath(); String[] parts = pathString.split("/"); - int i=0; - while(i0) -// firstLvl = false; -// else firstLvl = true; } - + private void initializeButtons() { - Button upDirButton = (Button)this.findViewById(R.id.upDirectoryButton); - upDirButton.setOnClickListener( - new OnClickListener() { + ImageButton upDirButton = (ImageButton) this + .findViewById(R.id.upDirectoryButton); + upDirButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + Log.d(LOGTAG, "onclick for upDirButton"); + loadDirectoryUp(); + loadFileList(); + adapter.notifyDataSetChanged(); + updateCurrentDirectoryTextView(); + } + }); + + Button selectFolderButton = (Button) this + .findViewById(R.id.selectCurrentDirectoryButton); + if (currentAction == FileBrowserActivity.SELECT_DIRECTORY) { + selectFolderButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { - Log.d(LOGTAG, "onclick for upDirButton"); - loadDirectoryUp(); - loadFileList(); - adapter.notifyDataSetChanged(); - updateCurrentDirectoryTextView(); + Log.d(LOGTAG, "onclick for selectFolderButton"); + returnDirectoryFinishActivity(); } - });//upDirButton.setOnClickListener( - - Button selectFolderButton = (Button)this.findViewById(R.id.selectCurrentDirectoryButton); - if(currentAction == this.SELECT_DIRECTORY) { - selectFolderButton.setOnClickListener( - new OnClickListener() { - public void onClick(View v) { - Log.d(LOGTAG, "onclick for selectFolderButton"); - returnDirectoryFinishActivity(); - } - });//upDirButton.setOnClickListener( - } else {//if(currentAction == this.SELECT_DIRECTORY) { + }); + } else { selectFolderButton.setVisibility(View.GONE); -// ((TextView)this.findViewById( -// R.id.currentDirectoryTextView)).setVisibility(View.GONE); - }//} else {//if(currentAction == this.SELECT_DIRECTORY) { - }//private void initializeButtons() { - + + } + + // new Folder button + Button newDirButton = (Button) this + .findViewById(R.id.newDirectoryButton); + newDirButton.setOnClickListener(new OnClickListener() { + + public void onClick(View v) { + createDirectoryDialog(); + } + }); + + } + private void loadDirectoryUp() { // present directory removed from list String s = pathDirsList.remove(pathDirsList.size() - 1); // path modified to exclude present directory path = new File(path.toString().substring(0, path.toString().lastIndexOf(s))); - //fileList = null; + fileList.clear(); // if there are no more directories in the list, then // its the first level -// if (pathDirsList.isEmpty()) { -// firstLvl = true; -// } + + } + + private void createDirectoryDialog() { + + // inflate layout of AlertDialog content (with edit text) + LayoutInflater li = LayoutInflater.from(this); + View promptsView = li.inflate(R.layout.prompts_holo, null); + final EditText etNewName = (EditText) promptsView + .findViewById(R.id.editTextDialogUserInput); + etNewName.setText(getString(R.string.default_directoryname)); // set + // directory + // name + // to + // some + // default + // value + etNewName.setSelectAllOnFocus(true); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setView(promptsView); + builder.setCancelable(true) + .setMessage(R.string.alert_chooseName) + .setPositiveButton(R.string.ok, + new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, + int which) { + String tempText = etNewName.getText() + .toString(); + if (tempText.equals("")) { + createDirectoryDialog(); + dialog.cancel(); + } else { + // create directory and load new views + createNewDirectory(tempText); + } + } + }) + .setNegativeButton(R.string.cancel, + new DialogInterface.OnClickListener() { + + public void onClick(DialogInterface dialog, + int which) { + dialog.cancel(); + + } + }); + builder.create().show(); + + } + + private void createNewDirectory(String name) { + try { + String newPath = path.getAbsolutePath() + "/" + name; + File directory = new File(newPath); + directory.mkdir(); + + path = directory; + parseDirectoryPath(); + loadFileList(); + adapter.notifyDataSetChanged(); + updateCurrentDirectoryTextView(); + // fileList.clear(); + + } catch (NullPointerException e) { + e.printStackTrace(); + } + } - + private void updateCurrentDirectoryTextView() { - int i=0; + int i = 0; String curDirString = ""; - while(i(this, android.R.layout.select_dialog_item, android.R.id.text1, - fileList) - { + fileList) { @Override public View getView(int position, View convertView, ViewGroup parent) { // creates view - //Log.d(TAG, "getView start"); + // Log.d(TAG, "getView start"); View view = super.getView(position, convertView, parent); TextView textView = (TextView) view .findViewById(android.R.id.text1); // put the image on the text view int drawableID = 0; - if(fileList.get(position).icon != -1) { - //Log.d(TAG, "Putting image"); - //If icon == -1, then directory is empty + if (fileList.get(position).icon != -1) { + // Log.d(TAG, "Putting image"); + // If icon == -1, then directory is empty drawableID = fileList.get(position).icon; } - textView.setCompoundDrawablesWithIntrinsicBounds( - drawableID, 0, 0, 0); - + textView.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0, + 0, 0); + textView.setEllipsize(null); // add margin between image and text (support various screen // densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); int dp3 = (int) (3 * getResources().getDisplayMetrics().density + 0.5f); - //TODO: change next line for empty directory, so text will be centered + // TODO: change next line for empty directory, so text will be + // centered textView.setCompoundDrawablePadding(dp3); textView.setBackgroundColor(Color.LTGRAY); return view; - }//public View getView(int position, View convertView, ViewGroup parent) { - };//adapter = new ArrayAdapter(this, - //adapter.setNotifyOnChange(true); - - - - }//private createFileListAdapter(){ + } + }; + + } private class Item { public String file; @@ -434,14 +467,13 @@ public String toString() { return file; } } - + private class ItemFileNameComparator implements Comparator { public int compare(Item lhs, Item rhs) { return lhs.file.toLowerCase().compareTo(rhs.file.toLowerCase()); } - - }//private class ItemComparator implements Comparator { - + } + public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); @@ -452,7 +484,7 @@ public void onConfigurationChanged(Configuration newConfig) { } // Layout apparently changes itself, only have to provide good onMeasure // in custom components - //TODO: check with keyboard + // TODO: check with keyboard // if(newConfig.keyboard == Configuration.KEYBOARDHIDDEN_YES) }