@@ -214,14 +214,84 @@ public void onEnd(int exitCode) {
214214 }
215215 }
216216
217+ private boolean isDirectoryEmptyOrWithVersion (File folder ) {
218+ String [] files = folder .list ();
219+
220+ if (files .length > 0 ) {
221+ for (String fname : files ) {
222+ if ("VERSION" .equals (fname )) {
223+ return true ;
224+ }
225+ }
226+ return false ;
227+ }
228+
229+ return true ;
230+ }
231+
232+ private ExecChecker getCheckerForKey (String key ) {
233+ switch (key ) {
234+ case "RUBY_DIR" :
235+ return ExecChecker .ruby ();
236+ case "MSF_DIR" :
237+ return ExecChecker .msf ();
238+ }
239+ return null ;
240+ }
241+
242+ private String getCurrentPathForKey (String key ) {
243+ switch (key ) {
244+ case "RUBY_DIR" :
245+ return System .getRubyPath ();
246+ case "MSF_DIR" :
247+ return System .getMsfPath ();
248+ }
249+ return null ;
250+ }
251+
252+ private boolean shallAskForDelete (String key ) {
253+ return key .equals ("RUBY_DIR" ) || key .equals ("MSF_DIR" );
254+ }
255+
256+ /**
257+ * check if selected directory is valid for the given key.
258+ * @param key to be updated
259+ * @param path of the chosen directory
260+ * @return true if {@code path} is valid, false otherwise
261+ */
262+ private boolean canChangeDirectoryTo (String key , String path ) {
263+ File folder = new File (path );
264+ ExecChecker checker = getCheckerForKey (key );
265+ String oldPath = getCurrentPathForKey (key );
266+ String toastMessage = null ;
267+ boolean valid = false ;
268+ boolean checkEmptyOrVersion = shallAskForDelete (key );
269+
270+ if (!folder .exists ()) {
271+ toastMessage = getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_exists );
272+ } else if (!folder .canWrite ()) {
273+ toastMessage = getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_writable );
274+ } else if (checker != null && !checker .canExecuteInDir (path )) {
275+ toastMessage = getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_executable );
276+ } else if (checkEmptyOrVersion && !isDirectoryEmptyOrWithVersion (folder )) {
277+ toastMessage = getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_empty_or_old );
278+ } else if (oldPath == null || !oldPath .equals (path )) {
279+ valid = true ;
280+ }
281+
282+ if (toastMessage != null ) {
283+ Toast .makeText (getContext (), toastMessage , Toast .LENGTH_LONG ).show ();
284+ }
285+
286+ return valid ;
287+ }
288+
217289 @ Override
218290 public void onActivityResult (int requestCode , int resultCode , Intent intent ) {
219291 if (requestCode == DirectoryPicker .PICK_DIRECTORY && resultCode != RESULT_CANCELED ) {
220292 Bundle extras = intent .getExtras ();
221293 String path ;
222294 String key ;
223- File folder ;
224- String oldPath = null ;
225295
226296 if (extras == null ) {
227297 Logger .debug ("null extra: " + intent );
@@ -236,35 +306,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
236306 return ;
237307 }
238308
239- folder = new File (path );
240- ExecChecker checker = null ;
241-
242-
243- if (key .equals ("RUBY_DIR" )) {
244- oldPath = System .getRubyPath ();
245- checker = ExecChecker .ruby ();
246- } else if (key .equals ("MSF_DIR" )) {
247- oldPath = System .getMsfPath ();
248- checker = ExecChecker .msf ();
249- }
250-
251- if (!folder .exists ())
252- Toast .makeText (getActivity (), getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_exists ), Toast .LENGTH_SHORT ).show ();
253-
254- else if (!folder .canWrite ())
255- Toast .makeText (getActivity (), getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_writable ), Toast .LENGTH_SHORT ).show ();
309+ if (canChangeDirectoryTo (key , path )) {
256310
257- else if (checker != null && !checker .canExecuteInDir (path ))
258- Toast .makeText (getActivity (), getString (R .string .pref_folder ) + " " + path + " " + getString (R .string .pref_err_executable ), Toast .LENGTH_LONG ).show ();
259311
260- else {
261- //noinspection ConstantConditions
262312 getPreferenceManager ().getSharedPreferences ().edit ().putString (key , path ).commit ();
263- if (oldPath != null && !oldPath .equals (path )) {
264- File current = new File (oldPath );
265313
266- if (current .exists () && current .isDirectory () && current .listFiles ().length > 2 ) {
267- wipe_prompt_older (current );
314+ if (shallAskForDelete (key )) {
315+ String oldPath = getCurrentPathForKey (key );
316+ if (oldPath != null ) {
317+ File current = new File (oldPath );
318+ if (current .exists () && current .isDirectory () && current .list ().length > 0 ) {
319+ wipe_prompt_older (current );
320+ }
268321 }
269322 }
270323 }
0 commit comments