6
6
import android .os .Build ;
7
7
import android .os .Bundle ;
8
8
import android .os .Environment ;
9
- import android .support .v4 .widget .SwipeRefreshLayout ;
10
- import android .support .v7 .app .AlertDialog ;
11
- import android .support .v7 .app .AppCompatActivity ;
12
- import android .support .v7 .widget .LinearLayoutManager ;
13
- import android .support .v7 .widget .RecyclerView ;
14
9
import android .view .Menu ;
15
10
import android .view .MenuItem ;
16
11
import android .view .View ;
17
12
13
+ import androidx .appcompat .app .AlertDialog ;
14
+ import androidx .appcompat .app .AppCompatActivity ;
15
+ import androidx .recyclerview .widget .LinearLayoutManager ;
16
+ import androidx .recyclerview .widget .RecyclerView ;
17
+ import androidx .swiperefreshlayout .widget .SwipeRefreshLayout ;
18
+
18
19
import com .blankj .utilcode .constant .PermissionConstants ;
19
20
import com .blankj .utilcode .util .PermissionUtils ;
20
21
import com .hzy .libmagic .MagicApi ;
24
25
import com .hzy .magic .app .bean .FileInfo ;
25
26
import com .hzy .magic .app .utils .FileUtils ;
26
27
27
- import java .io .IOException ;
28
- import java .io .InputStream ;
29
28
import java .util .List ;
29
+ import java .util .Stack ;
30
+ import java .util .concurrent .ExecutorService ;
31
+ import java .util .concurrent .Executors ;
30
32
31
33
import butterknife .BindView ;
32
34
import butterknife .ButterKnife ;
33
- import io .reactivex .Observable ;
34
- import io .reactivex .ObservableOnSubscribe ;
35
- import io .reactivex .android .schedulers .AndroidSchedulers ;
36
- import io .reactivex .functions .Consumer ;
37
- import io .reactivex .schedulers .Schedulers ;
38
35
39
36
public class MainActivity extends AppCompatActivity
40
- implements View .OnClickListener , SwipeRefreshLayout .OnRefreshListener ,
41
- Consumer <List <FileInfo >> {
37
+ implements View .OnClickListener , SwipeRefreshLayout .OnRefreshListener {
42
38
43
39
@ BindView (R .id .main_storage_path )
44
40
RecyclerView mPathList ;
@@ -51,16 +47,20 @@ public class MainActivity extends AppCompatActivity
51
47
52
48
private PathItemAdapter mPathAdapter ;
53
49
private FileItemAdapter mFileAdapter ;
54
- private String mCurPath ;
50
+ private String mCurrentPath ;
55
51
private ProgressDialog mProgressDialog ;
56
52
private AlertDialog mAboutDialog ;
53
+ private ExecutorService mThreadPool ;
54
+ private Stack <String > mHistoryStack ;
57
55
58
56
@ TargetApi (Build .VERSION_CODES .M )
59
57
@ Override
60
58
protected void onCreate (Bundle savedInstanceState ) {
61
59
super .onCreate (savedInstanceState );
62
60
setContentView (R .layout .activity_main );
63
61
ButterKnife .bind (this );
62
+ mThreadPool = Executors .newFixedThreadPool (8 );
63
+ mHistoryStack = new Stack <>();
64
64
initUI ();
65
65
PermissionUtils .permission (PermissionConstants .STORAGE )
66
66
.callback (new PermissionUtils .SimpleCallback () {
@@ -71,11 +71,11 @@ public void onGranted() {
71
71
72
72
@ Override
73
73
public void onDenied () {
74
-
75
74
}
76
75
}).request ();
77
76
}
78
77
78
+
79
79
private void initUI () {
80
80
mProgressDialog = new ProgressDialog (this );
81
81
mProgressDialog .setCancelable (false );
@@ -90,31 +90,31 @@ private void initUI() {
90
90
91
91
@ SuppressLint ("CheckResult" )
92
92
private void loadInitPath () {
93
- final String path = Environment .getExternalStorageDirectory ().getPath ();
94
- Observable .create ((ObservableOnSubscribe <List <FileInfo >>) e -> {
95
- if (initMagicFromAssets ()) {
96
- List <FileInfo > infoList = FileUtils .getInfoListFromPath (path );
97
- mCurPath = path ;
98
- e .onNext (infoList );
99
- }
100
- }).subscribeOn (Schedulers .io ()).observeOn (AndroidSchedulers .mainThread ())
101
- .subscribe (this );
93
+ mCurrentPath = Environment .getExternalStorageDirectory ().getPath ();
94
+ mHistoryStack .push (mCurrentPath );
95
+ loadPathInfo (mCurrentPath );
102
96
}
103
97
104
- @ SuppressLint ("CheckResult" )
105
- private void loadPathInfo (final String path ) {
106
- Observable .create ((ObservableOnSubscribe <List <FileInfo >>) e -> {
98
+ private void loadPathInfo (String path ) {
99
+ mThreadPool .submit (() -> {
107
100
List <FileInfo > infoList = FileUtils .getInfoListFromPath (path );
108
- mCurPath = path ;
109
- e .onNext (infoList );
110
- }).subscribeOn (Schedulers .io ()).observeOn (AndroidSchedulers .mainThread ())
111
- .subscribe (this );
101
+ runOnUiThread (() -> {
102
+ mCurrentPath = path ;
103
+ mFileAdapter .setDataList (infoList );
104
+ mPathAdapter .setPathView (mCurrentPath );
105
+ mPathList .scrollToPosition (mPathAdapter .getItemCount () - 1 );
106
+ mFileList .smoothScrollToPosition (0 );
107
+ mSwipeRefresh .setRefreshing (false );
108
+ mProgressDialog .dismiss ();
109
+ });
110
+ });
112
111
}
113
112
114
113
@ Override
115
114
protected void onDestroy () {
116
- super . onDestroy ();
115
+ mThreadPool . shutdownNow ();
117
116
MagicApi .close ();
117
+ super .onDestroy ();
118
118
}
119
119
120
120
@ Override
@@ -138,52 +138,44 @@ public boolean onOptionsItemSelected(MenuItem item) {
138
138
139
139
@ Override
140
140
public void onBackPressed () {
141
- super .onBackPressed ();
142
- }
143
-
144
- private boolean initMagicFromAssets () {
145
- try {
146
- InputStream inputStream = getAssets ().open ("magic.mgc" );
147
- int length = inputStream .available ();
148
- byte [] buffer = new byte [length ];
149
- if (inputStream .read (buffer ) > 0 ) {
150
- return MagicApi .loadFromBytes (buffer ) == 0 ;
141
+ if (mHistoryStack .size () > 1 ) {
142
+ mHistoryStack .pop ();
143
+ String backPath = mHistoryStack .peek ();
144
+ if (backPath != null ) {
145
+ loadPathInfo (backPath );
151
146
}
152
- } catch ( IOException e ) {
153
- e . printStackTrace ();
147
+ } else {
148
+ super . onBackPressed ();
154
149
}
155
- return false ;
156
150
}
157
151
158
152
@ Override
159
153
public void onClick (View v ) {
160
154
if (v .getTag () instanceof String ) {
161
155
mProgressDialog .show ();
162
- loadPathInfo ((String ) v .getTag ());
156
+ String path = (String ) v .getTag ();
157
+ if (path != null ) {
158
+ if (mHistoryStack .empty () || !mHistoryStack .peek ().equals (path )) {
159
+ mHistoryStack .push (path );
160
+ }
161
+ loadPathInfo (path );
162
+ }
163
163
} else {
164
164
FileInfo info = (FileInfo ) v .getTag ();
165
165
FileInfo .FileType type = info .getFileType ();
166
166
if (type == FileInfo .FileType .folderEmpty
167
167
|| type == FileInfo .FileType .folderFull ) {
168
168
mProgressDialog .show ();
169
- loadPathInfo (info .getFilePath ());
169
+ String path = info .getFilePath ();
170
+ mHistoryStack .push (path );
171
+ loadPathInfo (path );
170
172
}
171
173
}
172
174
}
173
175
174
176
@ Override
175
177
public void onRefresh () {
176
- loadPathInfo (mCurPath );
177
- }
178
-
179
- @ Override
180
- public void accept (List <FileInfo > fileInfos ) {
181
- mFileAdapter .setDataList (fileInfos );
182
- mPathAdapter .setPathView (mCurPath );
183
- mPathList .scrollToPosition (mPathAdapter .getItemCount () - 1 );
184
- mFileList .smoothScrollToPosition (0 );
185
- mSwipeRefresh .setRefreshing (false );
186
- mProgressDialog .dismiss ();
178
+ loadPathInfo (mCurrentPath );
187
179
}
188
180
189
181
private void showAboutDialog () {
0 commit comments