55import android .net .Uri ;
66import android .os .Bundle ;
77import android .util .Log ;
8- import android .util .SparseBooleanArray ;
98import android .view .MenuInflater ;
109import android .view .MenuItem ;
1110import android .view .View ;
@@ -101,6 +100,11 @@ public void onClick(View view) {
101100 MenuInflater inflater = popup .getMenuInflater ();
102101 inflater .inflate (R .menu .browser_menu , popup .getMenu ());
103102
103+ // Show/hide menu items based on selection state
104+ boolean hasSelection = fileAdapter != null && fileAdapter .getSelectedPosition () != -1 ;
105+ popup .getMenu ().findItem (R .id .action_rename ).setVisible (hasSelection );
106+ popup .getMenu ().findItem (R .id .action_delete ).setVisible (hasSelection );
107+
104108 // Force PopupMenu to show icons
105109 try {
106110 java .lang .reflect .Field mFieldPopup = popup .getClass ().getDeclaredField ("mPopup" );
@@ -125,12 +129,12 @@ public boolean onMenuItemClick(MenuItem item) {
125129 }
126130 else if (id == R .id .action_delete )
127131 {
128- deleteSelectedFiles ();
132+ deleteSelectedFileOrFolder ();
129133 return true ;
130134 }
131135 else if (id == R .id .action_rename )
132136 {
133- renameSelectedFile ();
137+ renameSelectedFileOrFolder ();
134138 return true ;
135139 }
136140 return false ;
@@ -200,8 +204,8 @@ public void onClick(View view) {
200204 }
201205 });
202206
203- btnSelectOneFile . setVisibility ( View . VISIBLE );
204- btnShare . setVisibility ( View . VISIBLE );
207+ // Initially hide buttons until a file is selected
208+ updateButtonVisibility ( false );
205209 }
206210
207211 @ Override
@@ -252,7 +256,7 @@ private void finishWithFileSelectArguments()
252256 }
253257 else
254258 {
255- Toast .makeText (this , getString (R .string .please_select_file ), Toast .LENGTH_LONG ).show ();
259+ Toast .makeText (this , getString (R .string .please_select_file_or_folder ), Toast .LENGTH_LONG ).show ();
256260 }
257261 }
258262
@@ -308,6 +312,13 @@ public void onFolderClick(File folder) {
308312 navigateToFolder (folder );
309313 }
310314 });
315+ // Set up the selection change listener for button visibility
316+ fileAdapter .setOnSelectionChangeListener (new FileAdapter .OnSelectionChangeListener () {
317+ @ Override
318+ public void onSelectionChanged (boolean hasFileSelected ) {
319+ updateButtonVisibility (hasFileSelected );
320+ }
321+ });
311322 listView .setAdapter (fileAdapter );
312323 }
313324 // Clear selection when file list changes
@@ -326,62 +337,122 @@ public void onFolderClick(File folder) {
326337 }
327338 }
328339
329- private void renameSelectedFile ()
340+ private void renameSelectedFileOrFolder ()
330341 {
331342 if (fileAdapter != null && fileAdapter .getSelectedPosition () != -1 ) {
332343 final File fileName = fileAdapter .getSelectedFile ();
333- if (fileName == null || fileName .getName ().equals (".." ) || fileName . isDirectory () ) {
334- Toast .makeText (this , getString (R .string .cannot_rename_folders_or_parent ), Toast .LENGTH_LONG ).show ();
344+ if (fileName == null || fileName .getName ().equals (".." )) {
345+ Toast .makeText (this , getString (R .string .cannot_rename_parent_folder ), Toast .LENGTH_LONG ).show ();
335346 return ;
336347 }
337- final int fileNamePosition = fileAdapter .getSelectedPosition ();
338- ((TextView )findViewById (R .id .txtTitle )).setText (getString (R .string .rename ));
339- ((TextView )findViewById (R .id .txtMessage )).setText (getString (R .string .rename_file ));
340- ((EditText )findViewById (R .id .etData )).setText (FileUtil .getFileNameWithoutExtension (fileName ));
341- ((TextView )findViewById (R .id .txtDataLabelRight )).setVisibility (View .VISIBLE );
342- ((TextView )findViewById (R .id .txtDataLabelRight )).setText (fileExtension );
343- btPopupYes .setOnClickListener (new View .OnClickListener () {
344- @ Override
345- public void onClick (View view ) {
346- String newFileName = ((EditText ) findViewById (R .id .etData )).getText ().toString ();
347- if (newFileName .isEmpty () == false ) {
348- File newFile = new File (currentFolder , newFileName + fileExtension );
349- if (newFile .exists ())
350- {
351- Toast .makeText (BrowserActivity .this , getString (R .string .file_already_exists ), Toast .LENGTH_LONG ).show ();
348+ if (fileName .isFile ())
349+ {
350+ renameFile (fileName );
351+ }
352+ else
353+ {
354+ renameFolder (fileName );
355+ }
356+ }
357+ else
358+ {
359+ Toast .makeText (this , getString (R .string .please_select_file_or_folder ), Toast .LENGTH_LONG ).show ();
360+ }
361+ }
362+
363+ private void renameFile (File fileName ) {
364+ final int fileNamePosition = fileAdapter .getSelectedPosition ();
365+ ((TextView )findViewById (R .id .txtTitle )).setText (getString (R .string .rename ));
366+ ((TextView )findViewById (R .id .txtMessage )).setText (getString (R .string .rename_file ));
367+ ((EditText )findViewById (R .id .etData )).setText (FileUtil .getFileNameWithoutExtension (fileName ));
368+ ((TextView )findViewById (R .id .txtDataLabelRight )).setVisibility (View .VISIBLE );
369+ ((TextView )findViewById (R .id .txtDataLabelRight )).setText (fileExtension );
370+ btPopupYes .setOnClickListener (new View .OnClickListener () {
371+ @ Override
372+ public void onClick (View view ) {
373+ String newFileName = ((EditText ) findViewById (R .id .etData )).getText ().toString ();
374+ if (newFileName .isEmpty () == false ) {
375+ File newFile = new File (currentFolder , newFileName + fileExtension );
376+ if (newFile .exists ())
377+ {
378+ Toast .makeText (BrowserActivity .this , getString (R .string .file_already_exists ), Toast .LENGTH_LONG ).show ();
379+ viewOverlay .setVisibility (View .GONE );
380+ clPopup .setVisibility (View .GONE );
381+ fileAdapter .clearSelection ();
382+ return ;
383+ }
384+ try {
385+ filesList .remove (fileNamePosition );
386+ fileName .renameTo (newFile );
387+ filesList .add (newFile );
388+ fileAdapter .notifyDataSetChanged ();
352389 viewOverlay .setVisibility (View .GONE );
353390 clPopup .setVisibility (View .GONE );
354- return ;
355- }
356- try {
357- filesList .remove (fileNamePosition );
358- fileName .renameTo (newFile );
359- filesList .add (newFile );
360- fileAdapter .notifyDataSetChanged ();
361- viewOverlay .setVisibility (View .GONE );
362- clPopup .setVisibility (View .GONE );
363- } catch (Exception e ) {
364- e .printStackTrace ();
365- Toast .makeText (BrowserActivity .this , getString (R .string .error_renaming_file ), Toast .LENGTH_SHORT ).show ();
366- }
391+ fileAdapter .clearSelection ();
392+ } catch (Exception e ) {
393+ e .printStackTrace ();
394+ Toast .makeText (BrowserActivity .this , getString (R .string .error_renaming_file ), Toast .LENGTH_SHORT ).show ();
367395 }
368396 }
369- });
370- btPopupNo .setOnClickListener (new View .OnClickListener () {
371- @ Override
372- public void onClick (View view ) {
373- viewOverlay .setVisibility (View .GONE );
374- clPopup .setVisibility (View .GONE );
397+ }
398+ });
399+ btPopupNo .setOnClickListener (new View .OnClickListener () {
400+ @ Override
401+ public void onClick (View view ) {
402+ viewOverlay .setVisibility (View .GONE );
403+ clPopup .setVisibility (View .GONE );
404+ }
405+ });
406+
407+ viewOverlay .setVisibility (View .VISIBLE );
408+ clPopup .setVisibility (View .VISIBLE );
409+ }
410+
411+ private void renameFolder (File folderName ) {
412+ final int fileNamePosition = fileAdapter .getSelectedPosition ();
413+ ((TextView )findViewById (R .id .txtTitle )).setText (getString (R .string .rename ));
414+ ((TextView )findViewById (R .id .txtMessage )).setText (getString (R .string .rename_folder ));
415+ ((EditText )findViewById (R .id .etData )).setText (folderName .getName ());
416+ ((TextView )findViewById (R .id .txtDataLabelRight )).setVisibility (View .GONE );
417+ btPopupYes .setOnClickListener (new View .OnClickListener () {
418+ @ Override
419+ public void onClick (View view ) {
420+ String newFolderName = ((EditText ) findViewById (R .id .etData )).getText ().toString ();
421+ if (newFolderName .isEmpty () == false ) {
422+ File newFile = new File (currentFolder , newFolderName );
423+ if (newFile .exists ())
424+ {
425+ Toast .makeText (BrowserActivity .this , getString (R .string .folder_already_exists ), Toast .LENGTH_LONG ).show ();
426+ viewOverlay .setVisibility (View .GONE );
427+ clPopup .setVisibility (View .GONE );
428+ fileAdapter .clearSelection ();
429+ return ;
430+ }
431+ try {
432+ filesList .remove (fileNamePosition );
433+ folderName .renameTo (newFile );
434+ filesList .add (newFile );
435+ fileAdapter .notifyDataSetChanged ();
436+ viewOverlay .setVisibility (View .GONE );
437+ clPopup .setVisibility (View .GONE );
438+ fileAdapter .clearSelection ();
439+ } catch (Exception e ) {
440+ e .printStackTrace ();
441+ Toast .makeText (BrowserActivity .this , getString (R .string .error_renaming_file ), Toast .LENGTH_SHORT ).show ();
442+ }
375443 }
376- });
444+ }
445+ });
446+ btPopupNo .setOnClickListener (new View .OnClickListener () {
447+ @ Override
448+ public void onClick (View view ) {
449+ viewOverlay .setVisibility (View .GONE );
450+ clPopup .setVisibility (View .GONE );
451+ }
452+ });
377453
378- viewOverlay .setVisibility (View .VISIBLE );
379- clPopup .setVisibility (View .VISIBLE );
380- }
381- else
382- {
383- Toast .makeText (this , getString (R .string .please_select_file ), Toast .LENGTH_LONG ).show ();
384- }
454+ viewOverlay .setVisibility (View .VISIBLE );
455+ clPopup .setVisibility (View .VISIBLE );
385456 }
386457
387458 private void createNewFolder ()
@@ -484,7 +555,7 @@ public void onClick(View view) {
484555 clPopup .setVisibility (View .VISIBLE );
485556 }
486557
487- private void deleteSelectedFiles () {
558+ private void deleteSelectedFileOrFolder () {
488559 if (fileAdapter != null && fileAdapter .getSelectedPosition () != -1 ) {
489560 File selectedFile = fileAdapter .getSelectedFile ();
490561 if (selectedFile != null ) {
@@ -499,7 +570,7 @@ private void deleteSelectedFiles() {
499570 }
500571 }
501572 } else {
502- Toast .makeText (this , getString (R .string .please_select_file ), Toast .LENGTH_LONG ).show ();
573+ Toast .makeText (this , getString (R .string .please_select_file_or_folder ), Toast .LENGTH_LONG ).show ();
503574 }
504575 }
505576
@@ -557,6 +628,7 @@ public void onClick(View view) {
557628 }
558629 viewOverlay .setVisibility (View .GONE );
559630 clPopup .setVisibility (View .GONE );
631+ fileAdapter .clearSelection ();
560632 }
561633 });
562634 btPopupNo .setOnClickListener (new View .OnClickListener () {
@@ -568,6 +640,7 @@ public void onClick(View view) {
568640 }
569641 viewOverlay .setVisibility (View .GONE );
570642 clPopup .setVisibility (View .GONE );
643+ fileAdapter .clearSelection ();
571644 }
572645 });
573646
@@ -619,10 +692,16 @@ private void shareSelectedFile()
619692 }
620693 else
621694 {
622- Toast .makeText (this , getString (R .string .please_select_file ), Toast .LENGTH_LONG ).show ();
695+ Toast .makeText (this , getString (R .string .please_select_file_or_folder ), Toast .LENGTH_LONG ).show ();
623696 }
624697 }
625698
699+ private void updateButtonVisibility (boolean hasFileSelected ) {
700+ int visibility = hasFileSelected ? View .VISIBLE : View .GONE ;
701+ btnSelectOneFile .setVisibility (visibility );
702+ btnShare .setVisibility (visibility );
703+ }
704+
626705 @ Override
627706 protected void attachBaseContext (Context newBase ) {
628707 String languageCode = LocaleHelper .getCurrentLanguageCode (newBase );
0 commit comments