Skip to content
This repository was archived by the owner on Jul 22, 2021. It is now read-only.

Commit 1c0429c

Browse files
Improved in-built file picker
Signed-off-by: sunilpaulmathew <sunil.kde@gmail.com>
1 parent 9a4be17 commit 1c0429c

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

app/src/main/java/com/smartpack/scriptmanager/activities/FilePickerActivity.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8686
});
8787

8888
mRecycleViewAdapter.setOnItemClickListener((position, v) -> {
89-
if (isDirectory(mData.get(position))) {
89+
if (new File(mData.get(position)).isDirectory()) {
9090
mPath = mData.get(position);
9191
reload();
9292
} else {
@@ -120,8 +120,8 @@ private int getSpanCount(Activity activity) {
120120
}
121121

122122
private File[] getFilesList() {
123-
if (!mPath.endsWith("/")) {
124-
mPath = mPath + "/";
123+
if (!mPath.endsWith(File.separator)) {
124+
mPath = mPath + File.separator;
125125
}
126126
if (!new File(mPath).exists()) {
127127
mPath = Environment.getExternalStorageDirectory().toString();
@@ -131,32 +131,25 @@ private File[] getFilesList() {
131131

132132
private List<String> getData() {
133133
try {
134+
mData.clear();
134135
// Add directories
135136
for (File mFile : getFilesList()) {
136-
if (isDirectory(mPath + mFile.getName())) {
137-
mData.add(mPath + mFile.getName());
137+
if (mFile.isDirectory()) {
138+
mData.add(mFile.getAbsolutePath());
138139
}
139140
}
140141
// Add files
141142
for (File mFile : getFilesList()) {
142-
if (isFile(mPath + mFile.getName())) {
143-
mData.add(mPath + mFile.getName());
143+
if (mFile.isFile()) {
144+
mData.add(mFile.getAbsolutePath());
144145
}
145146
}
146-
} catch (RuntimeException e) {
147-
Utils.snackbar(findViewById(android.R.id.content), e.getMessage());
147+
} catch (NullPointerException ignored) {
148+
Utils.snackbar(findViewById(android.R.id.content), getString(R.string.file_picker_failed_message));
148149
}
149150
return mData;
150151
}
151152

152-
private static boolean isDirectory(String path) {
153-
return new File(path).isDirectory();
154-
}
155-
156-
private static boolean isFile(String path) {
157-
return new File(path).isFile();
158-
}
159-
160153
private static String getSize(File file) {
161154
long size = file.length() / 1024;
162155
String timeCreated = DateFormat.getDateTimeInstance().format(System.currentTimeMillis());
@@ -240,9 +233,10 @@ public RecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int vi
240233
@SuppressLint("UseCompatLoadingForDrawables")
241234
@Override
242235
public void onBindViewHolder(@NonNull RecycleViewAdapter.ViewHolder holder, int position) {
243-
if (isDirectory(this.data.get(position))) {
236+
if (new File(this.data.get(position)).isDirectory()) {
244237
holder.mIcon.setImageDrawable(holder.mTitle.getContext().getResources().getDrawable(R.drawable.ic_folder));
245238
holder.mIcon.setColorFilter(Utils.getThemeAccentColor(holder.mTitle.getContext()));
239+
holder.mDescription.setVisibility(View.GONE);
246240
} else {
247241
if (Utils.getExtension(this.data.get(position)).equals("sh")) {
248242
holder.mDescription.setVisibility(View.VISIBLE);

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<string name="translations_summary">Help me to translate this app! Click here to get the original language strings in english</string>
8585
<string name="file_picker">File Picker</string>
8686
<string name="file_picker_external">External</string>
87+
<string name="file_picker_failed_message">Permission denied to read this folder!</string>
8788
<string name="file_picker_inbuilt">In-built</string>
8889
<string name="use_file_picker_message">Got some issues? Please disable \'Use In-built File Picker\' in \'Settings\' Menu!</string>
8990
<string name="wrong_extension">Please select a file with %s extension.</string>

0 commit comments

Comments
 (0)