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.Bundle; | ||||||
| import android.os.Environment; | import android.os.Environment; | ||||||
| import android.support.v4.app.ActivityCompat; | import android.support.v4.app.ActivityCompat; | ||||||
|  | import android.support.v4.app.FragmentManager; | ||||||
| import android.support.v7.app.AppCompatActivity; | import android.support.v7.app.AppCompatActivity; | ||||||
|  |  | ||||||
| import com.simplemobiletools.filemanager.Constants; | import com.simplemobiletools.filemanager.Constants; | ||||||
| @@ -14,7 +15,7 @@ import com.simplemobiletools.filemanager.fragments.ItemsFragment; | |||||||
|  |  | ||||||
| import butterknife.ButterKnife; | import butterknife.ButterKnife; | ||||||
|  |  | ||||||
| public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity implements ItemsFragment.ItemInteractionListener { | ||||||
|     private static final int STORAGE_PERMISSION = 1; |     private static final int STORAGE_PERMISSION = 1; | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -32,20 +33,35 @@ public class MainActivity extends AppCompatActivity { | |||||||
|  |  | ||||||
|     private void tryInitFileManager() { |     private void tryInitFileManager() { | ||||||
|         if (Utils.hasStoragePermission(getApplicationContext())) { |         if (Utils.hasStoragePermission(getApplicationContext())) { | ||||||
|             initializeFileManager(); |             initRootFileManager(); | ||||||
|         } else { |         } else { | ||||||
|             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); |             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void initializeFileManager() { |     private void initRootFileManager() { | ||||||
|         final Bundle bundle = new Bundle(); |  | ||||||
|         final String path = Environment.getExternalStorageDirectory().toString(); |         final String path = Environment.getExternalStorageDirectory().toString(); | ||||||
|  |         openPath(path); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void openPath(String path) { | ||||||
|  |         final Bundle bundle = new Bundle(); | ||||||
|         bundle.putString(Constants.PATH, path); |         bundle.putString(Constants.PATH, path); | ||||||
|  |  | ||||||
|         final ItemsFragment fragment = new ItemsFragment(); |         final ItemsFragment fragment = new ItemsFragment(); | ||||||
|         fragment.setArguments(bundle); |         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 |     @Override | ||||||
| @@ -54,11 +70,16 @@ public class MainActivity extends AppCompatActivity { | |||||||
|  |  | ||||||
|         if (requestCode == STORAGE_PERMISSION) { |         if (requestCode == STORAGE_PERMISSION) { | ||||||
|             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||||||
|                 initializeFileManager(); |                 initRootFileManager(); | ||||||
|             } else { |             } else { | ||||||
|                 Utils.showToast(getApplicationContext(), R.string.no_permissions); |                 Utils.showToast(getApplicationContext(), R.string.no_permissions); | ||||||
|                 finish(); |                 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 { | public class ItemsFragment extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener { | ||||||
|     @BindView(R.id.items_list) ListView mListView; |     @BindView(R.id.items_list) ListView mListView; | ||||||
|  |  | ||||||
|  |     private List<FileDirItem> mItems; | ||||||
|  |     private ItemInteractionListener mListener; | ||||||
|  |  | ||||||
|     @Nullable |     @Nullable | ||||||
|     @Override |     @Override | ||||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |     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); |         super.onViewCreated(view, savedInstanceState); | ||||||
|  |  | ||||||
|         final String path = getArguments().getString(Constants.PATH); |         final String path = getArguments().getString(Constants.PATH); | ||||||
|         List<FileDirItem> items = getItems(path); |         mItems = getItems(path); | ||||||
|         Collections.sort(items); |         Collections.sort(mItems); | ||||||
|  |  | ||||||
|         final ItemsAdapter adapter = new ItemsAdapter(getContext(), items); |         final ItemsAdapter adapter = new ItemsAdapter(getContext(), mItems); | ||||||
|         mListView.setAdapter(adapter); |         mListView.setAdapter(adapter); | ||||||
|         mListView.setOnItemClickListener(this); |         mListView.setOnItemClickListener(this); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public void setListener(ItemInteractionListener listener) { | ||||||
|  |         mListener = listener; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private List<FileDirItem> getItems(String path) { |     private List<FileDirItem> getItems(String path) { | ||||||
|         final List<FileDirItem> items = new ArrayList<>(); |         final List<FileDirItem> items = new ArrayList<>(); | ||||||
|         final File root = new File(path); |         final File base = new File(path); | ||||||
|         File[] files = root.listFiles(); |         File[] files = base.listFiles(); | ||||||
|         for (File file : files) { |         for (File file : files) { | ||||||
|             final String curPath = file.getAbsolutePath(); |             final String curPath = file.getAbsolutePath(); | ||||||
|             final String curName = Utils.getFilename(curPath); |             final String curName = Utils.getFilename(curPath); | ||||||
| @@ -60,6 +67,13 @@ public class ItemsFragment extends android.support.v4.app.Fragment implements Ad | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |     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); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -37,7 +37,7 @@ public class FileDirItem implements Comparable { | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public String toString() { |     public String toString() { | ||||||
|         return "FileDirItem {" + |         return "FileDirItem{" + | ||||||
|                 "name=" + getName() + |                 "name=" + getName() + | ||||||
|                 ", isDirectory=" + getIsDirectory() + |                 ", isDirectory=" + getIsDirectory() + | ||||||
|                 ", path=" + getPath() + "}"; |                 ", path=" + getPath() + "}"; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user