66import android .content .Intent ;
77import android .net .Uri ;
88import android .os .Bundle ;
9+ import android .os .Handler ;
10+ import android .os .Looper ;
911import android .support .v7 .app .AppCompatActivity ;
1012import android .support .v7 .view .ActionMode ;
1113import android .support .v7 .widget .LinearLayoutManager ;
2022
2123import com .aditya .filebrowser .adapters .CustomAdapter ;
2224import 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 ;
2530import com .aditya .filebrowser .listeners .TabChangeListener ;
31+ import com .aditya .filebrowser .models .FileItem ;
2632import com .aditya .filebrowser .utils .AssortedUtils ;
2733import com .aditya .filebrowser .utils .Permissions ;
28- import com .aditya .filebrowser .utils .ToolbarActionMode ;
2934import com .roughike .bottombar .BottomBar ;
35+ import com .simplecityapps .recyclerview_fastscroll .interfaces .OnFastScrollStateChangeListener ;
36+ import com .simplecityapps .recyclerview_fastscroll .views .FastScrollRecyclerView ;
3037
3138import org .apache .commons .io .FileUtils ;
3239
3340import java .io .File ;
3441import 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