Skip to content

Commit fba6d28

Browse files
committed
CleanedUp Code. Added Folder Selection feature
1 parent 9a0e038 commit fba6d28

37 files changed

+851
-478
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
google()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.3.0'
9+
classpath 'com.android.tools.build:gradle:3.4.1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

filebrowser/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
33
ext {
44
PUBLISH_GROUP_ID = 'com.adityak'
55
PUBLISH_ARTIFACT_ID = 'browsemyfiles'
6-
PUBLISH_VERSION = '1.8'
6+
PUBLISH_VERSION = '1.9'
77
}
88

99
android {
@@ -12,10 +12,10 @@ android {
1212

1313
defaultConfig {
1414
//applicationId "com.aditya.filebrowser"
15-
minSdkVersion 15
15+
minSdkVersion 17
1616
targetSdkVersion 27
17-
versionCode 10
18-
versionName "1.8"
17+
versionCode 11
18+
versionName "1.9"
1919
}
2020
buildTypes {
2121
release {
Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
<manifest
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
package="com.aditya.filebrowser">
45

56
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6-
77
<application
88
android:allowBackup="true"
99
android:label="@string/app_name"
1010
android:supportsRtl="true">
11-
<activity android:name=".FileBrowser"
11+
12+
<activity
13+
android:name=".FileBrowser"
14+
android:theme="@style/FileBrowserTheme">
15+
<meta-data
16+
android:name="android.app.searchable"
17+
android:resource="@xml/searchable"
18+
/>
19+
</activity>
20+
21+
<activity
22+
android:name=".FolderChooser"
1223
android:theme="@style/FileBrowserTheme">
1324
<meta-data
1425
android:name="android.app.searchable"
15-
android:resource="@xml/searchable">
16-
</meta-data>
26+
android:resource="@xml/searchable"
27+
/>
1728
</activity>
18-
<activity android:name=".FileChooser"
29+
30+
<activity
31+
android:name=".FileChooser"
1932
android:theme="@style/FileBrowserTheme">
2033
<meta-data
2134
android:name="android.app.searchable"
22-
android:resource="@xml/searchable">
23-
</meta-data>
35+
android:resource="@xml/searchable" />
2436
</activity>
25-
<activity android:name=".FileBrowserWithCustomHandler"
37+
38+
<activity
39+
android:name=".FileBrowserWithCustomHandler"
2640
android:theme="@style/FileBrowserTheme">
2741
<meta-data
2842
android:name="android.app.searchable"
29-
android:resource="@xml/searchable">
30-
</meta-data>
43+
android:resource="@xml/searchable" />
3144
</activity>
45+
3246
<activity android:name=".utils.Permissions" android:theme="@style/Theme.AppCompat.Translucent" />
47+
3348
<provider
3449
android:name="android.support.v4.content.FileProvider"
35-
android:authorities="@string/aditya_filebrowser_provider"
50+
android:authorities="@string/filebrowser_provider"
3651
android:exported="false"
3752
android:grantUriPermissions="true">
3853
<meta-data
3954
android:name="android.support.FILE_PROVIDER_PATHS"
4055
android:resource="@xml/provider_paths"/>
4156
</provider>
57+
4258
</application>
4359

4460
</manifest>

filebrowser/src/main/java/com/aditya/filebrowser/Constants.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public enum SORT_OPTIONS {
2727

2828
public enum APP_MODE {
2929
FILE_BROWSER,
30-
FILE_CHOOSER
30+
FILE_CHOOSER,
31+
FOLDER_CHOOSER,
3132
}
3233

3334
public enum CHOICE_MODE {
@@ -61,18 +62,18 @@ public enum SELECTION_MODES {
6162
try {
6263
List<GetRemovableDevice.StorageInfo> infos = GetRemovableDevice.getStorageList();
6364
boolean isExternalDirectoryInitialized = false;
64-
for(int i=0;i<infos.size();i++) {
65-
if(infos.get(i).getDisplayName().contains(Constants.EXTERNALSTORAGE)) {
65+
for(int i=0; i<infos.size(); i++) {
66+
if (infos.get(i).getDisplayName().contains(Constants.EXTERNALSTORAGE)) {
6667
File detectedDirectory = new File(infos.get(i).path).getCanonicalFile();
67-
if(detectedDirectory!=null && detectedDirectory.exists() && detectedDirectory.isDirectory() && detectedDirectory.getTotalSpace()>0)
68+
if (detectedDirectory!=null && detectedDirectory.exists() && detectedDirectory.isDirectory() && detectedDirectory.getTotalSpace()>0)
6869
externalStorageRoot = detectedDirectory;
6970
else
7071
externalStorageRoot = new File("/");
7172
isExternalDirectoryInitialized = true;
7273
break;
7374
}
7475
}
75-
if(!isExternalDirectoryInitialized)
76+
if (!isExternalDirectoryInitialized)
7677
externalStorageRoot = new File("/");
7778
} catch (Exception e) {
7879
e.printStackTrace();

filebrowser/src/main/java/com/aditya/filebrowser/FileBrowser.java

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@
5757
public class FileBrowser extends AppCompatActivity implements OnFileChangedListener,IContextSwitcher {
5858

5959
private Context mContext;
60-
private Toolbar toolbar;
6160

6261
private CustomAdapter mAdapter;
6362
private FastScrollRecyclerView.LayoutManager mLayoutManager;
6463
private FastScrollRecyclerView mFilesListView;
6564

6665
private BottomBar mBottomView;
67-
private BottomBar mPathChange;
66+
private BottomBar mTopStorageView;
6867
private TabChangeListener mTabChangeListener;
6968

7069
private TextView mCurrentPath;
@@ -79,34 +78,32 @@ public class FileBrowser extends AppCompatActivity implements OnFileChangedListe
7978
private SearchView mSearchView;
8079
private MenuItem mSearchMenuItem;
8180
private SearchViewListener mSearchViewListener;
82-
private Handler mUIUpdateHandler;
8381
private List<FileItem> mFileList = new ArrayList<>();
8482

85-
private String mInitialDirectory;
86-
private String mFilterFilesWithExtension;
87-
8883
@Override
8984
protected void onCreate(Bundle savedInstanceState) {
9085
super.onCreate(savedInstanceState);
9186

9287
mContext = this;
88+
89+
// Get File Storage Permission
9390
Intent in = new Intent(this, Permissions.class);
94-
Bundle b=new Bundle();
95-
b.putStringArray(Constants.APP_PREMISSION_KEY, Constants.APP_PREMISSIONS);
96-
in.putExtras(b);
97-
startActivityForResult(in,APP_PERMISSION_REQUEST);
91+
Bundle bundle = new Bundle();
92+
bundle.putStringArray(Constants.APP_PREMISSION_KEY, Constants.APP_PREMISSIONS);
93+
in.putExtras(bundle);
94+
startActivityForResult(in, APP_PERMISSION_REQUEST);
95+
96+
// Initialize Stuff
9897
mNavigationHelper = new NavigationHelper(mContext);
9998
mNavigationHelper.setmChangeDirectoryListener(this);
100-
mUIUpdateHandler = new Handler(Looper.getMainLooper());
101-
io = new FileIO(mNavigationHelper,mUIUpdateHandler,mContext);
99+
io = new FileIO(mNavigationHelper, new Handler(Looper.getMainLooper()), mContext);
102100
op = Operations.getInstance(mContext);
103101

104102
//set file filter (i.e display files with the given extension)
105-
mFilterFilesWithExtension = getIntent().getStringExtra(Constants.ALLOWED_FILE_EXTENSIONS);
106-
if(mFilterFilesWithExtension!=null && !mFilterFilesWithExtension.isEmpty()) {
107-
String allowedFileExtension[] = mFilterFilesWithExtension.split(";");
108-
Set<String> allowedFilesFilter = new HashSet<String>(Arrays.asList(allowedFileExtension));
109-
mNavigationHelper.setAllowedFileExtensionFilter(allowedFilesFilter);
103+
String filterFilesWithExtension = getIntent().getStringExtra(Constants.ALLOWED_FILE_EXTENSIONS);
104+
if (filterFilesWithExtension != null && !filterFilesWithExtension.isEmpty()) {
105+
Set<String> allowedFileExtensions = new HashSet<String>(Arrays.asList(filterFilesWithExtension.split(";")));
106+
mNavigationHelper.setAllowedFileExtensionFilter(allowedFileExtensions);
110107
}
111108

112109
mFileList = mNavigationHelper.getFilesItemsInCurrentDirectory();
@@ -115,22 +112,22 @@ protected void onCreate(Bundle savedInstanceState) {
115112
@Override
116113
public void onBackPressed() {
117114

118-
if(mAdapter.getChoiceMode()==Constants.CHOICE_MODE.MULTI_CHOICE) {
115+
if (mAdapter.getChoiceMode() == Constants.CHOICE_MODE.MULTI_CHOICE) {
119116
switchMode(Constants.CHOICE_MODE.SINGLE_CHOICE);
120117
return;
121118
}
122119

123-
if(!mNavigationHelper.navigateBack()) {
120+
if (!mNavigationHelper.navigateBack()) {
124121
super.onBackPressed();
125122
}
126123
}
127124

128125
@Override
129126
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
130127
super.onActivityResult(requestCode, resultCode, data);
131-
if(requestCode==APP_PERMISSION_REQUEST ) {
132-
if(resultCode != Activity.RESULT_OK)
133-
Toast.makeText(mContext,mContext.getString(R.string.error_no_permissions),Toast.LENGTH_LONG).show();
128+
if (requestCode == APP_PERMISSION_REQUEST) {
129+
if (resultCode != Activity.RESULT_OK)
130+
Toast.makeText(mContext, mContext.getString(R.string.error_no_permissions), Toast.LENGTH_LONG).show();
134131
loadUi();
135132
}
136133
}
@@ -153,52 +150,53 @@ public boolean onCreateOptionsMenu(Menu menu) {
153150
@Override
154151
public boolean onOptionsItemSelected(MenuItem item) {
155152

156-
if(item.getItemId()== R.id.action_showfoldersizes) {
157-
if (AssortedUtils.GetPrefs(Constants.SHOW_FOLDER_SIZE, mContext).equalsIgnoreCase("true"))
158-
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "false", mContext);
159-
else
160-
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "true", mContext);
161-
onFileChanged(mNavigationHelper.getCurrentDirectory());
162-
}
163-
else if (item.getItemId()== R.id.action_newfolder) {
164-
UIUtils.showEditTextDialog(this, getString(R.string.new_folder), "" , new IFuncPtr(){
165-
@Override
166-
public void execute(final String val) {
167-
io.createDirectory(new File(mNavigationHelper.getCurrentDirectory(),val.trim()));
168-
}
169-
});
170-
}
171-
else if(item.getItemId()== R.id.action_paste) {
172-
if (op.getOperation() == Operations.FILE_OPERATIONS.NONE) {
173-
UIUtils.ShowToast(mContext.getString(R.string.no_operation_error), mContext);
174-
return false;
175-
}
176-
if(op.getSelectedFiles()==null) {
177-
UIUtils.ShowToast(mContext.getString(R.string.no_files_paste), mContext);
178-
return false;
153+
if (item.getItemId() == R.id.action_showfoldersizes) {
154+
155+
if (AssortedUtils.GetPrefs(Constants.SHOW_FOLDER_SIZE, mContext).equalsIgnoreCase("true"))
156+
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "false", mContext);
157+
else
158+
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "true", mContext);
159+
160+
onFileChanged(mNavigationHelper.getCurrentDirectory());
161+
}
162+
else if (item.getItemId() == R.id.action_newfolder) {
163+
UIUtils.showEditTextDialog(this, getString(R.string.new_folder), "", new IFuncPtr(){
164+
@Override
165+
public void execute(final String val) {
166+
io.createDirectory(new File(mNavigationHelper.getCurrentDirectory(),val.trim()));
179167
}
180-
io.pasteFiles(mNavigationHelper.getCurrentDirectory());
168+
});
169+
}
170+
else if (item.getItemId() == R.id.action_paste) {
171+
if (op.getOperation() == Operations.FILE_OPERATIONS.NONE) {
172+
UIUtils.ShowToast(mContext.getString(R.string.no_operation_error), mContext);
181173
}
174+
if (op.getSelectedFiles() == null) {
175+
UIUtils.ShowToast(mContext.getString(R.string.no_files_paste), mContext);
176+
}
177+
io.pasteFiles(mNavigationHelper.getCurrentDirectory());
178+
}
179+
182180
return false;
183181
}
184182

185183
@Override
186184
public void onFileChanged(File updatedDirectory) {
187-
if(updatedDirectory!=null && updatedDirectory.exists() && updatedDirectory.isDirectory()) {
185+
if (updatedDirectory != null && updatedDirectory.exists() && updatedDirectory.isDirectory()) {
188186
mFileList = mNavigationHelper.getFilesItemsInCurrentDirectory();
189187
mCurrentPath.setText(updatedDirectory.getAbsolutePath());
190188
mAdapter.notifyDataSetChanged();
191-
mPathChange.getTabWithId(R.id.menu_internal_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getTotalSpace()));
189+
mTopStorageView.getTabWithId(R.id.menu_internal_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getTotalSpace()));
192190
if (Constants.externalStorageRoot != null)
193-
mPathChange.getTabWithId(R.id.menu_external_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getTotalSpace()));
191+
mTopStorageView.getTabWithId(R.id.menu_external_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getTotalSpace()));
194192
}
195193
}
196194

197195
private void loadUi() {
198196
setContentView(R.layout.filebrowser_activity_main);
199197

200-
mCurrentPath = (TextView) findViewById(R.id.currentPath);
201-
mFilesListView = (FastScrollRecyclerView) findViewById(R.id.recycler_view);
198+
mCurrentPath = findViewById(R.id.currentPath);
199+
mFilesListView = findViewById(R.id.recycler_view);
202200
mAdapter = new CustomAdapter(mFileList,mContext);
203201
mFilesListView.setAdapter(mAdapter);
204202
mLayoutManager = new LinearLayoutManager(mContext);
@@ -216,7 +214,7 @@ public void onItemClick(View view, int position) {
216214
MimeTypeMap mimeMap = MimeTypeMap.getSingleton();
217215
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
218216
String mimeType = mimeMap.getMimeTypeFromExtension(FilenameUtils.getExtension(f.getName()));
219-
Uri uri = FileProvider.getUriForFile(mContext, mContext.getString(R.string.aditya_filebrowser_provider), f);
217+
Uri uri = FileProvider.getUriForFile(mContext, mContext.getString(R.string.filebrowser_provider), f);
220218
openFileIntent.setDataAndType(uri,mimeType);
221219
openFileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
222220
openFileIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@@ -252,57 +250,59 @@ public void onFastScrollStop() {
252250

253251
mSearchViewListener = new SearchViewListener(mAdapter);
254252

255-
toolbar = (Toolbar) findViewById(R.id.filebrowser_tool_bar);
253+
Toolbar toolbar = findViewById(R.id.filebrowser_tool_bar);
256254
setSupportActionBar(toolbar);
257255

258-
mBottomView = (BottomBar) findViewById(R.id.bottom_navigation);
259-
mPathChange = (BottomBar) findViewById(R.id.currPath_Nav);
256+
mBottomView = findViewById(R.id.bottom_navigation);
257+
mTopStorageView = findViewById(R.id.currPath_Nav);
260258

261-
mTabChangeListener = new TabChangeListener(this,mNavigationHelper,mAdapter,io,this);
262-
mTabChangeListener.setmRecyclerView(mFilesListView);
259+
mTabChangeListener = new TabChangeListener(this, mNavigationHelper, mAdapter, io,this);
263260

264261
mBottomView.setOnTabSelectListener(mTabChangeListener);
265262
mBottomView.setOnTabReselectListener(mTabChangeListener);
266263

267-
mPathChange.setOnTabSelectListener(mTabChangeListener);
268-
mPathChange.setOnTabReselectListener(mTabChangeListener);
264+
mTopStorageView.setOnTabSelectListener(mTabChangeListener);
265+
mTopStorageView.setOnTabReselectListener(mTabChangeListener);
269266

270267
mBottomView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
271-
mPathChange.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
268+
mTopStorageView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
269+
272270
onFileChanged(mNavigationHelper.getCurrentDirectory());
273271

274272
//switch to initial directory if given
275-
mInitialDirectory = getIntent().getStringExtra(Constants.INITIAL_DIRECTORY);
276-
if(mInitialDirectory != null && !mInitialDirectory.isEmpty() ) {
277-
File initDir = new File(mInitialDirectory);
273+
String initialDirectory = getIntent().getStringExtra(Constants.INITIAL_DIRECTORY);
274+
if (initialDirectory != null && !initialDirectory.isEmpty() ) {
275+
File initDir = new File(initialDirectory);
278276
if (initDir.exists())
279277
mNavigationHelper.changeDirectory(initDir);
280278
}
281279
}
282280

281+
@Override
283282
public void switchMode(Constants.CHOICE_MODE mode) {
284-
if(mode== Constants.CHOICE_MODE.SINGLE_CHOICE) {
285-
if(mActionMode!=null)
283+
if (mode == Constants.CHOICE_MODE.SINGLE_CHOICE) {
284+
if (mActionMode != null)
286285
mActionMode.finish();
287286
} else {
288-
if(mActionMode==null) {
287+
if(mActionMode == null) {
289288
closeSearchView();
290-
ToolbarActionMode newToolBar = new ToolbarActionMode(this,this,mAdapter,Constants.APP_MODE.FILE_BROWSER,io);
289+
ToolbarActionMode newToolBar = new ToolbarActionMode(this,this, mAdapter, Constants.APP_MODE.FILE_BROWSER, io);
291290
mActionMode = startSupportActionMode(newToolBar);
292291
mActionMode.setTitle(mContext.getString(R.string.select_multiple));
293292
}
294293
}
295294
}
296295

296+
@Override
297297
public void changeBottomNavMenu(Constants.CHOICE_MODE multiChoice) {
298-
if(multiChoice== Constants.CHOICE_MODE.SINGLE_CHOICE) {
298+
if (multiChoice == Constants.CHOICE_MODE.SINGLE_CHOICE) {
299299
mBottomView.setItems(R.xml.bottom_nav_items);
300300
mBottomView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
301-
mPathChange.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
301+
mTopStorageView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
302302
} else {
303303
mBottomView.setItems(R.xml.bottom_nav_items_multiselect);
304304
mBottomView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
305-
mPathChange.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
305+
mTopStorageView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
306306
}
307307
}
308308

0 commit comments

Comments
 (0)