5
5
import android .content .Intent ;
6
6
import android .content .pm .PackageManager ;
7
7
import android .os .Environment ;
8
- import android .support .v4 .app .Fragment ;
9
8
import android .support .v4 .content .ContextCompat ;
10
9
import android .support .v7 .app .AppCompatActivity ;
11
10
import android .os .Bundle ;
12
11
import android .view .View ;
13
12
import android .widget .Button ;
14
13
import android .widget .ImageView ;
15
- import android .widget .Toast ;
16
-
17
- import java .util .List ;
18
14
19
15
20
16
public class SimpleFileExplorerActivity extends AppCompatActivity implements ActivityListener {
21
17
22
18
23
19
private String STACK_KEY = "FRAGMENT_STACK_KEY" ;
20
+ public static String ENABLE_DIRECTORY_SELECT_KEY = "DIRECTORY_SELECT_KEY" ;
24
21
public static String ON_ACTIVITY_RESULT_KEY = "ABSOLUTE_PATH_KEY" ;
25
22
26
23
27
24
private Button selectButton ;
28
25
private ImageView fileTypeImageView ;
29
26
private String selectedAbsolutePath ;
30
-
27
+ private boolean isDirectorySelectEnabled ;
31
28
32
29
@ Override
33
30
protected void onCreate (Bundle savedInstanceState ) {
@@ -36,9 +33,10 @@ protected void onCreate(Bundle savedInstanceState) {
36
33
checkPermission ();
37
34
this .initViews ();
38
35
this .setSelectButtonClickListener ();
36
+ this .configureButtonFromUserPreferences ();
39
37
SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment ();
40
38
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 ();
42
40
this .selectedAbsolutePath = Environment .getExternalStorageDirectory ().getAbsolutePath ();
43
41
}
44
42
@@ -71,6 +69,7 @@ public void onClick(View v) {
71
69
@ Override
72
70
public void onDirectoryChanged (String absolutePath ) {
73
71
this .selectedAbsolutePath = absolutePath ;
72
+ this .updateButtonSelectStateOnDirectory ();
74
73
SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment ();
75
74
fragment .setListeners (this );
76
75
fragment .setDirectory (absolutePath );
@@ -82,10 +81,12 @@ public void onFileSelect(FileModel fileModel) {
82
81
if (fileModel .isSelected ()){
83
82
this .selectedAbsolutePath = fileModel .getAbsolutePath ();
84
83
this .fileTypeImageView .setImageResource (R .drawable .ic_file );
84
+ this .selectButton .setEnabled (true );
85
85
}
86
86
else {
87
87
this .selectedAbsolutePath = fileModel .getDirectoryPath ();
88
88
this .fileTypeImageView .setImageResource (R .drawable .ic_folder );
89
+ this .updateButtonSelectStateOnDirectory ();
89
90
}
90
91
91
92
}
@@ -103,5 +104,23 @@ private void checkPermission(){
103
104
}
104
105
}
105
106
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
+ }
106
117
118
+ private void updateButtonSelectStateOnDirectory (){
119
+ if (this .isDirectorySelectEnabled ){
120
+ this .selectButton .setEnabled (true );
121
+ }
122
+ else {
123
+ this .selectButton .setEnabled (false );
124
+ }
125
+ }
107
126
}
0 commit comments