1 /* 2 * <Copyright 2013 Jose F. Maldonado> 3 * 4 * This file is part of aFileDialog. 5 * 6 * aFileDialog is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published 8 * by the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * aFileDialog is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with aFileDialog. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package ar.com.daidalos.afiledialog; 21 22 import java.io.File; 23 24 import android.app.Activity; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.Bundle; 28 import android.view.View; 29 import android.widget.LinearLayout; 30 import ar.com.daidalos.afiledialog.R; 31 import android.widget.TextView; 32 33 /** 34 * A file chooser implemented in an Activity. 35 */ 36 public class FileChooserActivity extends Activity implements FileChooser { 37 38 // ----- Fields ----- // 39 40 /** 41 * The folder that the class opened by default. 42 */ 43 private File startFolder; 44 45 /** 46 * The core of the file chooser. 47 */ 48 private FileChooserCore core; 49 50 /** 51 * A boolean indicating if the 'back' button must be used to navigate to parent folders. 52 */ 53 private boolean useBackButton; 54 55 // ----- Constants ----- // 56 57 /** 58 * Constant used for represent the key of the bundle object (inside the start's intent) which contains the 59 * path of the folder which files are going to be listed. 60 */ 61 public static final String INPUT_START_FOLDER = "input_start_folder"; 62 63 /** 64 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 65 * a boolean that indicates if the user is going to select folders instead of select files. 66 */ 67 public static final String INPUT_FOLDER_MODE = "input_folder_mode"; 68 69 /** 70 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 71 * a boolean that indicates if the user can create files. 72 */ 73 public static final String INPUT_CAN_CREATE_FILES = "input_can_create_files"; 74 75 /** 76 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 77 * a regular expression which is going to be used as a filter to determine which files can be selected. 78 */ 79 public static final String INPUT_REGEX_FILTER = "input_regex_filter"; 80 81 /** 82 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 83 * a boolean that indicates if only the files that can be selected must be displayed. 84 */ 85 public static final String INPUT_SHOW_ONLY_SELECTABLE = "input_show_only_selectable"; 86 87 /** 88 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 89 * an instance of the class FileChooserLabels that allows to override the default value of the labels. 90 */ 91 public static final String INPUT_LABELS = "input_labels"; 92 93 /** 94 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 95 * a boolean that indicates if a confirmation dialog must be displayed when creating a file. 96 */ 97 public static final String INPUT_SHOW_CONFIRMATION_ON_CREATE = "input_show_confirmation_on_create"; 98 99 /** 100 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 101 * a boolean that indicates if a confirmation dialog must be displayed when selecting a file. 102 */ 103 public static final String INPUT_SHOW_CONFIRMATION_ON_SELECT = "input_show_confirmation_on_select"; 104 105 /** 106 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 107 * a boolean that indicates if the title must show the full path of the current's folder (true) or only 108 * the folder's name (false). 109 */ 110 public static final String INPUT_SHOW_FULL_PATH_IN_TITLE = "input_show_full_path_in_title"; 111 112 /** 113 * Constant used for represent the key of the bundle object (inside the start's intent) which contains 114 * a boolean that indicates if the 'Back' button must be used to navigate to the parents folder (true) or 115 * if must follow the default behavior (and close the activity when the button is pressed). 116 */ 117 public static final String INPUT_USE_BACK_BUTTON_TO_NAVIGATE = "input_use_back_button_to_navigate"; 118 119 /** 120 * Constant used for represent the key of the bundle object (inside the result's intent) which contains the 121 * File object, that represents the file selected by the user or the folder in which the user wants to create 122 * a file. 123 */ 124 public static final String OUTPUT_FILE_OBJECT = "output_file_object"; 125 126 /** 127 * Constant used for represent the key of the bundle object (inside the result's intent) which contains the 128 * name of the file that the user wants to create. 129 */ 130 public static final String OUTPUT_NEW_FILE_NAME = "output_new_file_name"; 131 132 public static final String INPUT_TITLE_STRING = "Title Message"; 133 134 // ---- Activity methods ----- // 135 136 /** Called when the activity is first created. */ 137 @Override onCreate(Bundle savedInstanceState)138 public void onCreate(Bundle savedInstanceState) { 139 // Call superclass creator. 140 super.onCreate(savedInstanceState); 141 142 // Set layout. 143 this.setContentView(R.layout.daidalos_file_chooser); 144 145 // Set the background color. 146 LinearLayout layout = (LinearLayout) this.findViewById(R.id.rootLayout); 147 layout.setBackgroundColor(getResources().getColor(R.color.daidalos_backgroud)); 148 149 // Initialize fields. 150 this.useBackButton = false; 151 152 // Create the core of the file chooser. 153 this.core = new FileChooserCore(this); 154 155 // Verify if the optional parameters has been defined. 156 String folderPath = null; 157 Bundle extras = this.getIntent().getExtras(); 158 if(extras != null) { 159 if(extras.containsKey(INPUT_START_FOLDER)) folderPath = extras.getString(INPUT_START_FOLDER); 160 if(extras.containsKey(INPUT_TITLE_STRING)) this.setTitle(extras.getString(INPUT_TITLE_STRING)); 161 if(extras.containsKey(INPUT_REGEX_FILTER)) core.setFilter(extras.getString(INPUT_REGEX_FILTER)); 162 if(extras.containsKey(INPUT_SHOW_ONLY_SELECTABLE)) core.setShowOnlySelectable(extras.getBoolean(INPUT_SHOW_ONLY_SELECTABLE)); 163 if(extras.containsKey(INPUT_FOLDER_MODE)) core.setFolderMode(extras.getBoolean(INPUT_FOLDER_MODE)); 164 if(extras.containsKey(INPUT_CAN_CREATE_FILES)) core.setCanCreateFiles(extras.getBoolean(INPUT_CAN_CREATE_FILES)); 165 if(extras.containsKey(INPUT_LABELS)) core.setLabels((FileChooserLabels) extras.get(INPUT_LABELS)); 166 if(extras.containsKey(INPUT_SHOW_CONFIRMATION_ON_CREATE)) core.setShowConfirmationOnCreate(extras.getBoolean(INPUT_SHOW_CONFIRMATION_ON_CREATE)); 167 if(extras.containsKey(INPUT_SHOW_CONFIRMATION_ON_SELECT)) core.setShowConfirmationOnSelect(extras.getBoolean(INPUT_SHOW_CONFIRMATION_ON_SELECT)); 168 if(extras.containsKey(INPUT_SHOW_FULL_PATH_IN_TITLE)) core.setShowFullPathInTitle(extras.getBoolean(INPUT_SHOW_FULL_PATH_IN_TITLE)); 169 if(extras.containsKey(INPUT_USE_BACK_BUTTON_TO_NAVIGATE)) this.useBackButton = extras.getBoolean(INPUT_USE_BACK_BUTTON_TO_NAVIGATE); 170 } 171 172 // Load the files of a folder. 173 core.loadFolder(folderPath); 174 this.startFolder = this.core.getCurrentFolder(); 175 176 // Add a listener for when a file is selected. 177 core.addListener(new FileChooserCore.OnFileSelectedListener() { 178 public void onFileSelected(File folder, String name) { 179 // Pass the data through an intent. 180 Intent intent = new Intent(); 181 Bundle bundle = new Bundle(); 182 bundle.putSerializable(OUTPUT_FILE_OBJECT, folder); 183 bundle.putString(OUTPUT_NEW_FILE_NAME, name); 184 intent.putExtras(bundle); 185 186 setResult(RESULT_OK, intent); 187 finish(); 188 } 189 public void onFileSelected(File file) { 190 // Pass the data through an intent. 191 Intent intent = new Intent(); 192 Bundle bundle = new Bundle(); 193 bundle.putSerializable(OUTPUT_FILE_OBJECT, file); 194 intent.putExtras(bundle); 195 196 setResult(RESULT_OK, intent); 197 finish(); 198 } 199 }); 200 } 201 202 /** Called when the user push the 'back' button. */ 203 @Override onBackPressed()204 public void onBackPressed() { 205 // Verify if the activity must be finished or if the parent folder must be opened. 206 File current = this.core.getCurrentFolder(); 207 if(!this.useBackButton || current == null || current.getParent() == null || current.getPath().compareTo(this.startFolder.getPath()) == 0) { 208 // Close activity. 209 super.onBackPressed(); 210 }else{ 211 // Open parent. 212 this.core.loadFolder(current.getParent()); 213 } 214 } 215 216 // ----- FileChooser methods ----- // 217 getRootLayout()218 public LinearLayout getRootLayout() { 219 View root = this.findViewById(R.id.rootLayout); 220 return (root instanceof LinearLayout)? (LinearLayout)root : null; 221 } 222 getContext()223 public Context getContext() { 224 //return this.getBaseContext(); 225 return this; 226 } 227 setCurrentFolderName(String name)228 public void setCurrentFolderName(String name) { 229 //this.setTitle(name); 230 231 TextView selected = (TextView)this.findViewById(R.id.afdLabelFile); 232 if(null != selected){ 233 selected.setText( name ); 234 } 235 236 237 } 238 } 239