Skip to content

Commit 45f351a

Browse files
committed
Refactored Code. Added Indexed Fast Scroll
1 parent c6d7cc7 commit 45f351a

25 files changed

+627
-528
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

filebrowser/build.gradle

Lines changed: 4 additions & 3 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.3'
6+
PUBLISH_VERSION = '1.4'
77
}
88

99
android {
@@ -14,8 +14,8 @@ android {
1414
//applicationId "com.aditya.filebrowser"
1515
minSdkVersion 15
1616
targetSdkVersion 25
17-
versionCode 4
18-
versionName "1.3"
17+
versionCode 5
18+
versionName "1.4"
1919
}
2020
buildTypes {
2121
release {
@@ -32,6 +32,7 @@ dependencies {
3232
compile 'com.android.support:recyclerview-v7:25.3.0'
3333
compile 'com.beardedhen:androidbootstrap:2.3.1'
3434
compile 'com.roughike:bottom-bar:2.3.1'
35+
compile 'com.simplecityapps:recyclerview-fastscroll:1.0.14'
3536
compile files('libs/commons-io-2.5.jar')
3637
}
3738

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import android.Manifest;
44
import android.os.Environment;
55

6+
import com.aditya.filebrowser.fileoperations.GetRemovableDevice;
7+
68
import java.io.File;
79
import java.util.List;
810

@@ -48,7 +50,6 @@ public enum SELECTION_MODES {
4850
Manifest.permission.WRITE_EXTERNAL_STORAGE};
4951
public static final String INTERNALSTORAGE = "Internal Storage";
5052
public static final String EXTERNALSTORAGE = "External Storage";
51-
5253
public static File internalStorageRoot = Environment.getExternalStorageDirectory();
5354
public static File externalStorageRoot;
5455
static {
@@ -74,7 +75,7 @@ public enum SELECTION_MODES {
7475
externalStorageRoot = new File("/");
7576
}
7677
}
77-
//GetRemovableDevice.getDirectories().length>0?new File(GetRemovableDevice.getDirectories()[0]):Environment.getExternalStorageDirectory();
78+
7879
public static final String SHOW_FOLDER_SIZE = "false";
7980
public static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss";
8081
}

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

Lines changed: 101 additions & 115 deletions
Large diffs are not rendered by default.

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

Lines changed: 97 additions & 97 deletions
Large diffs are not rendered by default.

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

Lines changed: 102 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.content.Intent;
77
import android.net.Uri;
88
import android.os.Bundle;
9+
import android.os.Handler;
10+
import android.os.Looper;
911
import android.support.v7.app.AppCompatActivity;
1012
import android.support.v7.view.ActionMode;
1113
import android.support.v7.widget.LinearLayoutManager;
@@ -20,30 +22,36 @@
2022

2123
import com.aditya.filebrowser.adapters.CustomAdapter;
2224
import com.aditya.filebrowser.adapters.CustomAdapterItemClickListener;
23-
import com.aditya.filebrowser.interfaces.ContextSwitcher;
24-
import com.aditya.filebrowser.interfaces.OnChangeDirectoryListener;
25+
import com.aditya.filebrowser.fileoperations.FileIO;
26+
import com.aditya.filebrowser.fileoperations.Operations;
27+
import com.aditya.filebrowser.interfaces.IContextSwitcher;
28+
import com.aditya.filebrowser.listeners.OnFileChangedListener;
29+
import com.aditya.filebrowser.listeners.SearchViewListener;
2530
import com.aditya.filebrowser.listeners.TabChangeListener;
31+
import com.aditya.filebrowser.models.FileItem;
2632
import com.aditya.filebrowser.utils.AssortedUtils;
2733
import com.aditya.filebrowser.utils.Permissions;
28-
import com.aditya.filebrowser.utils.ToolbarActionMode;
2934
import com.roughike.bottombar.BottomBar;
35+
import com.simplecityapps.recyclerview_fastscroll.interfaces.OnFastScrollStateChangeListener;
36+
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView;
3037

3138
import org.apache.commons.io.FileUtils;
3239

3340
import java.io.File;
3441
import java.util.ArrayList;
42+
import java.util.List;
3543

3644
/**
3745
* Created by Aditya on 4/17/2017.
3846
*/
39-
public class FileChooser extends AppCompatActivity implements OnChangeDirectoryListener,ContextSwitcher,SearchView.OnQueryTextListener {
47+
public class FileChooser extends AppCompatActivity implements OnFileChangedListener,IContextSwitcher,SearchView.OnQueryTextListener {
4048

4149
private Context mContext;
4250
private Toolbar toolbar;
4351

4452
private CustomAdapter mAdapter;
45-
private RecyclerView.LayoutManager mLayoutManager;
46-
private RecyclerView mFilesList;
53+
private FastScrollRecyclerView.LayoutManager mLayoutManager;
54+
private FastScrollRecyclerView mFilesList;
4755

4856
private BottomBar mBottomView;
4957
private BottomBar mPathChange;
@@ -53,14 +61,19 @@ public class FileChooser extends AppCompatActivity implements OnChangeDirectoryL
5361
private NavigationHelper mNavigationHelper;
5462

5563
private FileIO io;
64+
private Operations op;
5665
private int mSelectionMode;
5766

5867
//Action Mode for filebrowser_toolbar
5968
private static ActionMode mActionMode;
6069
private static final int APP_PERMISSION_REQUEST = 0;
6170

62-
private SearchView searchView;
63-
private MenuItem searchMenuItem;
71+
private SearchView mSearchView;
72+
private MenuItem mSearchMenuItem;
73+
private SearchViewListener mSearchViewListener;
74+
private Handler mUIUpdateHandler;
75+
private List<FileItem> mFileList;
76+
6477
@Override
6578
protected void onCreate(Bundle savedInstanceState) {
6679
super.onCreate(savedInstanceState);
@@ -73,21 +86,11 @@ protected void onCreate(Bundle savedInstanceState) {
7386
startActivityForResult(in,APP_PERMISSION_REQUEST);
7487
mSelectionMode = getIntent().getIntExtra(Constants.SELECTION_MODE,Constants.SELECTION_MODES.SINGLE_SELECTION.ordinal());
7588
mNavigationHelper = new NavigationHelper(mContext);
76-
if (savedInstanceState != null) {
77-
mNavigationHelper.changeDirectory((File)savedInstanceState.getSerializable("current_node"));
78-
mNavigationHelper.setRootDirectory((File)savedInstanceState.getSerializable("root_node"));
79-
}
80-
io = new FileIO(this,this);
81-
mTabChangeListener = new TabChangeListener(this,mNavigationHelper,mAdapter,null,null,this,this);
82-
mTabChangeListener.setSelectionMode(Constants.SELECTION_MODES.values()[mSelectionMode]);
8389
mNavigationHelper.setmChangeDirectoryListener(this);
84-
}
85-
86-
@Override
87-
protected void onSaveInstanceState(Bundle outState) {
88-
outState.putSerializable("root_node", mNavigationHelper.getRootDirectory());
89-
outState.putSerializable("current_node", mNavigationHelper.getCurrentDirectory());
90-
super.onSaveInstanceState(outState);
90+
mUIUpdateHandler = new Handler(Looper.getMainLooper());
91+
io = new FileIO(mNavigationHelper,mUIUpdateHandler,mContext);
92+
op = Operations.getInstance(mContext);
93+
mFileList = mNavigationHelper.getFilesItemsInCurrentDirectory();
9194
}
9295

9396
@Override
@@ -120,12 +123,12 @@ public boolean onCreateOptionsMenu(Menu menu) {
120123

121124
// Get the SearchView and set the searchable configuration
122125
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
123-
searchMenuItem = menu.findItem(R.id.action_search);
124-
searchView = (SearchView)searchMenuItem.getActionView();
126+
mSearchMenuItem = menu.findItem(R.id.action_search);
127+
mSearchView = (SearchView)mSearchMenuItem.getActionView();
125128
// Assumes current activity is the searchable activity
126-
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
129+
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
127130
//searchView.setSubmitButtonEnabled(true);
128-
searchView.setOnQueryTextListener(this);
131+
mSearchView.setOnQueryTextListener(this);
129132
return true;
130133
}
131134

@@ -136,74 +139,90 @@ public boolean onOptionsItemSelected(MenuItem item) {
136139
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "false", mContext);
137140
else
138141
AssortedUtils.SavePrefs(Constants.SHOW_FOLDER_SIZE, "true", mContext);
139-
updateUI(null,false);
142+
onFileChanged(mNavigationHelper.getCurrentDirectory());
140143
}
141144
return false;
142145
}
143146

144147
private void loadUi() {
145148
setContentView(R.layout.filebrowser_activity_main);
146-
toolbar = (Toolbar) findViewById(R.id.filebrowser_tool_bar);
147-
setSupportActionBar(toolbar);
148-
149149
mCurrentPath = (TextView) findViewById(R.id.currentPath);
150150

151-
mFilesList = (RecyclerView) findViewById(R.id.recycler_view);
151+
mFilesList = (FastScrollRecyclerView) findViewById(R.id.recycler_view);
152152
mAdapter = new CustomAdapter(mNavigationHelper.getFilesItemsInCurrentDirectory(),mContext);
153153
mFilesList.setAdapter(mAdapter);
154154
mLayoutManager = new LinearLayoutManager(mContext);
155155
mFilesList.setLayoutManager(mLayoutManager);
156-
mFilesList.addOnItemTouchListener(
157-
new CustomAdapterItemClickListener(mContext, mFilesList, new CustomAdapterItemClickListener.OnItemClickListener() {
158-
@Override
159-
public void onItemClick(View view, int position) {
160-
// TODO Handle item click
161-
if (mAdapter.getChoiceMode()== Constants.CHOICE_MODE.SINGLE_CHOICE) {
162-
File f = mAdapter.getItemAt(position).getFile();
163-
if (f.isDirectory()) {
164-
closeSearchView();
165-
mNavigationHelper.changeDirectory(f);
166-
} else {
167-
if(mSelectionMode==Constants.SELECTION_MODES.SINGLE_SELECTION.ordinal()) {
168-
Uri fileUri = Uri.fromFile(f);
169-
Intent data = new Intent();
170-
data.setData(fileUri);
171-
setResult(RESULT_OK, data);
172-
finish();
173-
} else {
174-
ArrayList<Uri> chosenItems = new ArrayList<>();
175-
chosenItems.add(Uri.fromFile(f));
176-
Intent data = new Intent();
177-
data.putParcelableArrayListExtra(Constants.SELECTED_ITEMS, chosenItems);
178-
setResult(Activity.RESULT_OK, data);
179-
finish();
180-
}
181-
}
156+
final CustomAdapterItemClickListener onItemClickListener = new CustomAdapterItemClickListener(mContext, mFilesList, new CustomAdapterItemClickListener.OnItemClickListener() {
157+
@Override
158+
public void onItemClick(View view, int position) {
159+
// TODO Handle item click
160+
if (mAdapter.getChoiceMode()== Constants.CHOICE_MODE.SINGLE_CHOICE) {
161+
File f = mAdapter.getItemAt(position).getFile();
162+
if (f.isDirectory()) {
163+
closeSearchView();
164+
mNavigationHelper.changeDirectory(f);
165+
} else {
166+
if(mSelectionMode==Constants.SELECTION_MODES.SINGLE_SELECTION.ordinal()) {
167+
Uri fileUri = Uri.fromFile(f);
168+
Intent data = new Intent();
169+
data.setData(fileUri);
170+
setResult(RESULT_OK, data);
171+
finish();
172+
} else {
173+
ArrayList<Uri> chosenItems = new ArrayList<>();
174+
chosenItems.add(Uri.fromFile(f));
175+
Intent data = new Intent();
176+
data.putParcelableArrayListExtra(Constants.SELECTED_ITEMS, chosenItems);
177+
setResult(Activity.RESULT_OK, data);
178+
finish();
182179
}
183180
}
181+
}
182+
}
184183

185-
@Override
186-
public void onItemLongClick(View view, int position) {
187-
switchMode(Constants.CHOICE_MODE.MULTI_CHOICE);
188-
mAdapter.selectItem(position);
189-
mFilesList.scrollToPosition(position);
190-
}
191-
}));
184+
@Override
185+
public void onItemLongClick(View view, int position) {
186+
switchMode(Constants.CHOICE_MODE.MULTI_CHOICE);
187+
mAdapter.selectItem(position);
188+
mFilesList.scrollToPosition(position);
189+
}
190+
});
191+
mFilesList.addOnItemTouchListener(onItemClickListener);
192+
193+
mFilesList.setStateChangeListener(new OnFastScrollStateChangeListener() {
194+
@Override
195+
public void onFastScrollStart() {
196+
onItemClickListener.setmFastScrolling(true);
197+
}
198+
199+
@Override
200+
public void onFastScrollStop() {
201+
onItemClickListener.setmFastScrolling(false);
202+
}
203+
});
204+
205+
mSearchViewListener = new SearchViewListener(mAdapter);
206+
207+
toolbar = (Toolbar) findViewById(R.id.filebrowser_tool_bar);
208+
setSupportActionBar(toolbar);
192209

193210
mBottomView = (BottomBar) findViewById(R.id.bottom_navigation);
194211
mPathChange = (BottomBar) findViewById(R.id.currPath_Nav);
195212

213+
mTabChangeListener = new TabChangeListener(this,mNavigationHelper,mAdapter,io,this);
214+
mTabChangeListener.setSelectionMode(Constants.SELECTION_MODES.values()[mSelectionMode]);
215+
mTabChangeListener.setmRecyclerView(mFilesList);
216+
196217
mBottomView.setOnTabSelectListener(mTabChangeListener);
197218
mBottomView.setOnTabReselectListener(mTabChangeListener);
198-
mPathChange.getTabWithId(R.id.menu_internal_storage).setTitle( FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getTotalSpace()) );
199-
if(Constants.externalStorageRoot!=null)
200-
mPathChange.getTabWithId(R.id.menu_external_storage).setTitle( FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getTotalSpace()));
219+
201220
mPathChange.setOnTabSelectListener(mTabChangeListener);
202221
mPathChange.setOnTabReselectListener(mTabChangeListener);
203222

204223
mBottomView.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
205224
mPathChange.getTabWithId(R.id.menu_none).setVisibility(View.GONE);
206-
updateUI(null,true);
225+
onFileChanged(mNavigationHelper.getCurrentDirectory());
207226
}
208227

209228
@Override
@@ -214,7 +233,8 @@ public void switchMode(Constants.CHOICE_MODE mode) {
214233
} else {
215234
if(mActionMode==null) {
216235
closeSearchView();
217-
mActionMode = startSupportActionMode(new ToolbarActionMode(mContext,this,mAdapter,Constants.APP_MODE.FILE_CHOOSER,io));
236+
ToolbarActionMode newToolBar = new ToolbarActionMode(this,this,mAdapter,Constants.APP_MODE.FILE_CHOOSER,io);
237+
mActionMode = startSupportActionMode(newToolBar);
218238
mActionMode.setTitle("Select Multiple Files");
219239
}
220240
}
@@ -252,17 +272,15 @@ public void reDrawFileList() {
252272
}
253273

254274
@Override
255-
public void updateUI(File updatedDirectory,boolean shouldRePopulateDirectory) {
256-
if(updatedDirectory==null)
257-
updatedDirectory = mNavigationHelper.getCurrentDirectory();
258-
mCurrentPath.setText(updatedDirectory.getAbsolutePath());
259-
if(shouldRePopulateDirectory)
260-
mNavigationHelper.getFilesItemsInCurrentDirectory();
261-
mAdapter.notifyDataSetChanged();
262-
mPathChange.getTabWithId(R.id.menu_internal_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getTotalSpace()) );
263-
if(Constants.externalStorageRoot!=null)
264-
mPathChange.getTabWithId(R.id.menu_external_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getTotalSpace()));
265-
275+
public void onFileChanged(File updatedDirectory) {
276+
if(updatedDirectory!=null && updatedDirectory.exists() && updatedDirectory.isDirectory()) {
277+
mFileList = mNavigationHelper.getFilesItemsInCurrentDirectory();
278+
mCurrentPath.setText(updatedDirectory.getAbsolutePath());
279+
mAdapter.notifyDataSetChanged();
280+
mPathChange.getTabWithId(R.id.menu_internal_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.internalStorageRoot.getTotalSpace()));
281+
if (Constants.externalStorageRoot != null)
282+
mPathChange.getTabWithId(R.id.menu_external_storage).setTitle(FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getUsableSpace()) + "/" + FileUtils.byteCountToDisplaySize(Constants.externalStorageRoot.getTotalSpace()));
283+
}
266284
}
267285

268286
@Override
@@ -277,10 +295,10 @@ public boolean onQueryTextChange(String newText) {
277295
}
278296

279297
private void closeSearchView() {
280-
if (searchView.isShown()) {
281-
searchView.setQuery("", false);
282-
searchMenuItem.collapseActionView();
283-
searchView.setIconified(true);
298+
if (mSearchView.isShown()) {
299+
mSearchView.setQuery("", false);
300+
mSearchMenuItem.collapseActionView();
301+
mSearchView.setIconified(true);
284302
}
285303
}
286304
}

0 commit comments

Comments
 (0)