55import android .content .Intent ;
66import android .content .pm .PackageManager ;
77import android .os .Environment ;
8- import android .support .v4 .app .Fragment ;
98import android .support .v4 .content .ContextCompat ;
109import android .support .v7 .app .AppCompatActivity ;
1110import android .os .Bundle ;
1211import android .view .View ;
1312import android .widget .Button ;
1413import android .widget .ImageView ;
15- import android .widget .Toast ;
16-
17- import java .util .List ;
1814
1915
2016public class SimpleFileExplorerActivity extends AppCompatActivity implements ActivityListener {
2117
2218
2319 private String STACK_KEY = "FRAGMENT_STACK_KEY" ;
20+ public static String ENABLE_DIRECTORY_SELECT_KEY = "DIRECTORY_SELECT_KEY" ;
2421 public static String ON_ACTIVITY_RESULT_KEY = "ABSOLUTE_PATH_KEY" ;
2522
2623
2724 private Button selectButton ;
2825 private ImageView fileTypeImageView ;
2926 private String selectedAbsolutePath ;
30-
27+ private boolean isDirectorySelectEnabled ;
3128
3229 @ Override
3330 protected void onCreate (Bundle savedInstanceState ) {
@@ -36,9 +33,10 @@ protected void onCreate(Bundle savedInstanceState) {
3633 checkPermission ();
3734 this .initViews ();
3835 this .setSelectButtonClickListener ();
36+ this .configureButtonFromUserPreferences ();
3937 SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment ();
4038 fragment .setListeners (this );
41- getSupportFragmentManager ().beginTransaction ().add (R .id .frame_layout , fragment ).addToBackStack ( STACK_KEY ). commit ();
39+ getSupportFragmentManager ().beginTransaction ().add (R .id .frame_layout , fragment ).commit ();
4240 this .selectedAbsolutePath = Environment .getExternalStorageDirectory ().getAbsolutePath ();
4341 }
4442
@@ -71,6 +69,7 @@ public void onClick(View v) {
7169 @ Override
7270 public void onDirectoryChanged (String absolutePath ) {
7371 this .selectedAbsolutePath = absolutePath ;
72+ this .updateButtonSelectStateOnDirectory ();
7473 SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment ();
7574 fragment .setListeners (this );
7675 fragment .setDirectory (absolutePath );
@@ -82,10 +81,12 @@ public void onFileSelect(FileModel fileModel) {
8281 if (fileModel .isSelected ()){
8382 this .selectedAbsolutePath = fileModel .getAbsolutePath ();
8483 this .fileTypeImageView .setImageResource (R .drawable .ic_file );
84+ this .selectButton .setEnabled (true );
8585 }
8686 else {
8787 this .selectedAbsolutePath = fileModel .getDirectoryPath ();
8888 this .fileTypeImageView .setImageResource (R .drawable .ic_folder );
89+ this .updateButtonSelectStateOnDirectory ();
8990 }
9091
9192 }
@@ -103,5 +104,23 @@ private void checkPermission(){
103104 }
104105 }
105106
107+ private void configureButtonFromUserPreferences (){
108+ Intent intent = getIntent ();
109+ if (intent .hasExtra (ENABLE_DIRECTORY_SELECT_KEY )){
110+ this .isDirectorySelectEnabled = intent .getBooleanExtra (ENABLE_DIRECTORY_SELECT_KEY , true );
111+ }
112+ else {
113+ this .isDirectorySelectEnabled = true ;
114+ }
115+ this .selectButton .setEnabled (this .isDirectorySelectEnabled );
116+ }
106117
118+ private void updateButtonSelectStateOnDirectory (){
119+ if (this .isDirectorySelectEnabled ){
120+ this .selectButton .setEnabled (true );
121+ }
122+ else {
123+ this .selectButton .setEnabled (false );
124+ }
125+ }
107126}
0 commit comments