mirror of
				https://github.com/SimpleMobileTools/Simple-File-Manager.git
				synced 2025-06-05 22:09:15 +02:00 
			
		
		
		
	allow opening subdirs
This commit is contained in:
		| @@ -5,6 +5,7 @@ import android.content.pm.PackageManager; | ||||
| import android.os.Bundle; | ||||
| import android.os.Environment; | ||||
| import android.support.v4.app.ActivityCompat; | ||||
| import android.support.v4.app.FragmentManager; | ||||
| import android.support.v7.app.AppCompatActivity; | ||||
|  | ||||
| import com.simplemobiletools.filemanager.Constants; | ||||
| @@ -14,7 +15,7 @@ import com.simplemobiletools.filemanager.fragments.ItemsFragment; | ||||
|  | ||||
| import butterknife.ButterKnife; | ||||
|  | ||||
| public class MainActivity extends AppCompatActivity { | ||||
| public class MainActivity extends AppCompatActivity implements ItemsFragment.ItemInteractionListener { | ||||
|     private static final int STORAGE_PERMISSION = 1; | ||||
|  | ||||
|     @Override | ||||
| @@ -32,20 +33,35 @@ public class MainActivity extends AppCompatActivity { | ||||
|  | ||||
|     private void tryInitFileManager() { | ||||
|         if (Utils.hasStoragePermission(getApplicationContext())) { | ||||
|             initializeFileManager(); | ||||
|             initRootFileManager(); | ||||
|         } else { | ||||
|             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void initializeFileManager() { | ||||
|         final Bundle bundle = new Bundle(); | ||||
|     private void initRootFileManager() { | ||||
|         final String path = Environment.getExternalStorageDirectory().toString(); | ||||
|         openPath(path); | ||||
|     } | ||||
|  | ||||
|     private void openPath(String path) { | ||||
|         final Bundle bundle = new Bundle(); | ||||
|         bundle.putString(Constants.PATH, path); | ||||
|  | ||||
|         final ItemsFragment fragment = new ItemsFragment(); | ||||
|         fragment.setArguments(bundle); | ||||
|         getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).commit(); | ||||
|         fragment.setListener(this); | ||||
|         getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, fragment).addToBackStack("").commit(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onBackPressed() { | ||||
|         final FragmentManager manager = getSupportFragmentManager(); | ||||
|         if (manager.getBackStackEntryCount() == 1) | ||||
|             finish(); | ||||
|         else { | ||||
|             super.onBackPressed(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -54,11 +70,16 @@ public class MainActivity extends AppCompatActivity { | ||||
|  | ||||
|         if (requestCode == STORAGE_PERMISSION) { | ||||
|             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||||
|                 initializeFileManager(); | ||||
|                 initRootFileManager(); | ||||
|             } else { | ||||
|                 Utils.showToast(getApplicationContext(), R.string.no_permissions); | ||||
|                 finish(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void itemClicked(String path) { | ||||
|         openPath(path); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -25,6 +25,9 @@ import butterknife.ButterKnife; | ||||
| public class ItemsFragment extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener { | ||||
|     @BindView(R.id.items_list) ListView mListView; | ||||
|  | ||||
|     private List<FileDirItem> mItems; | ||||
|     private ItemInteractionListener mListener; | ||||
|  | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||||
| @@ -38,18 +41,22 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad | ||||
|         super.onViewCreated(view, savedInstanceState); | ||||
|  | ||||
|         final String path = getArguments().getString(Constants.PATH); | ||||
|         List<FileDirItem> items = getItems(path); | ||||
|         Collections.sort(items); | ||||
|         mItems = getItems(path); | ||||
|         Collections.sort(mItems); | ||||
|  | ||||
|         final ItemsAdapter adapter = new ItemsAdapter(getContext(), items); | ||||
|         final ItemsAdapter adapter = new ItemsAdapter(getContext(), mItems); | ||||
|         mListView.setAdapter(adapter); | ||||
|         mListView.setOnItemClickListener(this); | ||||
|     } | ||||
|  | ||||
|     public void setListener(ItemInteractionListener listener) { | ||||
|         mListener = listener; | ||||
|     } | ||||
|  | ||||
|     private List<FileDirItem> getItems(String path) { | ||||
|         final List<FileDirItem> items = new ArrayList<>(); | ||||
|         final File root = new File(path); | ||||
|         File[] files = root.listFiles(); | ||||
|         final File base = new File(path); | ||||
|         File[] files = base.listFiles(); | ||||
|         for (File file : files) { | ||||
|             final String curPath = file.getAbsolutePath(); | ||||
|             final String curName = Utils.getFilename(curPath); | ||||
| @@ -60,6 +67,13 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad | ||||
|  | ||||
|     @Override | ||||
|     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||||
|         final FileDirItem item = mItems.get(position); | ||||
|         if (item.getIsDirectory()) { | ||||
|             mListener.itemClicked(item.getPath()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public interface ItemInteractionListener { | ||||
|         void itemClicked(String path); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user