Skip to content

Commit e1e749f

Browse files
author
Royce Whetstine
authored
Merge pull request #123 from ModdingFox/Issue_121
Fixed avatar upload bug and now uses statndard android filePicker
2 parents 3f195cf + f213fc6 commit e1e749f

11 files changed

Lines changed: 242 additions & 145 deletions

File tree

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "open.furaffinity.client"
99
minSdkVersion 26
1010
targetSdkVersion 29
11-
versionCode 31
12-
versionName "2021.08.22"
11+
versionCode 32
12+
versionName "2021.08.24"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
}
@@ -67,6 +67,4 @@ dependencies {
6767
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
6868

6969
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
70-
71-
implementation 'com.nbsp:materialfilepicker:1.9.1'
7270
}

app/src/main/java/open/furaffinity/client/dialogs/uploadAvatarDialog.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
package open.furaffinity.client.dialogs;
22

33
import android.Manifest;
4+
import android.app.Activity;
45
import android.app.AlertDialog;
56
import android.app.Dialog;
67
import android.content.Intent;
78
import android.content.pm.PackageManager;
9+
import android.database.Cursor;
10+
import android.net.Uri;
811
import android.os.Bundle;
9-
import android.os.Environment;
12+
import android.provider.OpenableColumns;
1013
import android.view.LayoutInflater;
1114
import android.view.View;
1215
import android.widget.Button;
1316
import android.widget.TextView;
17+
import android.widget.Toast;
1418

1519
import androidx.annotation.NonNull;
1620
import androidx.annotation.Nullable;
1721
import androidx.core.content.ContextCompat;
1822
import androidx.fragment.app.DialogFragment;
1923
import androidx.fragment.app.Fragment;
2024

21-
import com.nbsp.materialfilepicker.MaterialFilePicker;
22-
import com.nbsp.materialfilepicker.ui.FilePickerActivity;
25+
import java.util.Arrays;
26+
import java.util.List;
27+
import java.util.stream.Collectors;
2328

2429
import open.furaffinity.client.R;
2530

2631
public class uploadAvatarDialog extends DialogFragment {
32+
private static final List<String> imageMimeTypes = Arrays.asList(new String [] { "image/jpeg", "image/png", "image/gif" });
33+
2734
private static final int submissionFileRequestCode = 132;
2835

2936
private Button selectSourceFile;
30-
private TextView sourceFilePath;
37+
private TextView sourceFileName;
38+
private String sourceFilePath;
3139
private uploadAvatarDialogListener listener;
3240

3341
public void setListener(uploadAvatarDialogListener uploadAvatarDialogListener) {
@@ -36,7 +44,7 @@ public void setListener(uploadAvatarDialogListener uploadAvatarDialogListener) {
3644

3745
private void getElements(View rootView) {
3846
selectSourceFile = rootView.findViewById(R.id.selectSourceFile);
39-
sourceFilePath = rootView.findViewById(R.id.sourceFilePath);
47+
sourceFileName = rootView.findViewById(R.id.sourceFileName);
4048
}
4149

4250
private void initClientAndPage() {
@@ -46,12 +54,11 @@ private void updateUIElementListeners() {
4654
Fragment uploadFrag = this;
4755
selectSourceFile.setOnClickListener(v -> {
4856
if(ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
49-
new MaterialFilePicker()
50-
.withSupportFragment(uploadFrag)
51-
.withPath(Environment.getRootDirectory().getPath())
52-
.withFilterDirectories(false)
53-
.withRequestCode(submissionFileRequestCode)
54-
.start();
57+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
58+
intent.addCategory(Intent.CATEGORY_OPENABLE);
59+
intent.setType(imageMimeTypes.stream().collect(Collectors.joining(",")));
60+
intent.putExtra(Intent.EXTRA_MIME_TYPES, imageMimeTypes.toArray());
61+
startActivityForResult(intent, submissionFileRequestCode);
5562
} else {
5663
String [] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
5764
requestPermissions(permissions, 0);
@@ -72,7 +79,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
7279

7380
builder.setView(rootView);
7481
builder.setPositiveButton(R.string.acceptButton, (dialog, which) -> {
75-
listener.onDialogPositiveClick(sourceFilePath.getText().toString());
82+
listener.onDialogPositiveClick(sourceFilePath);
7683
dismiss();
7784
});
7885
builder.setNegativeButton(R.string.cancelButton, (dialog, which) -> dismiss());
@@ -86,10 +93,22 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
8693

8794
switch (requestCode) {
8895
case submissionFileRequestCode:
89-
if (resultCode == FilePickerActivity.RESULT_OK) {
90-
sourceFilePath.setText(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));
96+
if (resultCode == Activity.RESULT_OK) {
97+
Uri selectedFile = data.getData();
98+
Cursor sourceFileCursor = requireContext().getContentResolver().query(selectedFile, null, null, null);
99+
if(sourceFileCursor.moveToFirst()) {
100+
sourceFilePath = selectedFile.toString();
101+
int displayNameColumnIndex = sourceFileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
102+
String displayNameString = sourceFileCursor.getString(displayNameColumnIndex);
103+
sourceFileName.setText(displayNameString);
104+
} else {
105+
Toast.makeText(requireContext(), "Failed to find file info", Toast.LENGTH_SHORT).show();
106+
sourceFilePath = null;
107+
sourceFileName.setText("");
108+
}
91109
} else {
92-
sourceFilePath.setText("");
110+
sourceFilePath = null;
111+
sourceFileName.setText("");
93112
}
94113
break;
95114
}

app/src/main/java/open/furaffinity/client/dialogs/uploadDialog.java

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package open.furaffinity.client.dialogs;
22

33
import android.Manifest;
4+
import android.app.Activity;
45
import android.app.AlertDialog;
56
import android.app.Dialog;
67
import android.content.Context;
78
import android.content.Intent;
89
import android.content.pm.PackageManager;
10+
import android.database.Cursor;
11+
import android.net.Uri;
912
import android.os.Bundle;
10-
import android.os.Environment;
13+
import android.provider.OpenableColumns;
1114
import android.view.LayoutInflater;
1215
import android.view.View;
1316
import android.widget.AdapterView;
@@ -20,11 +23,12 @@
2023
import androidx.annotation.Nullable;
2124
import androidx.core.content.ContextCompat;
2225
import androidx.fragment.app.DialogFragment;
23-
import androidx.fragment.app.Fragment;
2426
import androidx.fragment.app.FragmentManager;
2527

26-
import com.nbsp.materialfilepicker.MaterialFilePicker;
27-
import com.nbsp.materialfilepicker.ui.FilePickerActivity;
28+
import java.util.Arrays;
29+
import java.util.List;
30+
import java.util.stream.Collectors;
31+
import java.util.stream.Stream;
2832

2933
import open.furaffinity.client.R;
3034
import open.furaffinity.client.abstractClasses.abstractPage;
@@ -34,23 +38,30 @@
3438
import open.furaffinity.client.utilities.uiControls;
3539

3640
public class uploadDialog extends DialogFragment {
41+
private static final List<String> imageMimeTypes = Arrays.asList(new String [] { "image/jpeg", "image/png", "image/gif" });
42+
private static final List<String> textMimeTypes = Arrays.asList(new String [] { "text/plain", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.oasis.opendocument.text", "application/rtf", "application/pdf" });
43+
private static final List<String> audioMimeTypes = Arrays.asList(new String [] { "audio/mpeg", "audio/x-wav", "audio/midi" });
44+
private static final List<String> allMimeTypes = Stream.concat(Stream.concat(imageMimeTypes.stream(), textMimeTypes.stream()), audioMimeTypes.stream()).collect(Collectors.toList());
45+
3746
private static final int submissionFileRequestCode = 132;
3847
private static final int thumbnailFileRequestCode = 133;
3948

4049
private Spinner submissionType;
4150
private Button selectSourceFile;
42-
private TextView sourceFilePath;
51+
private TextView sourceFileName;
52+
private String sourceFilePath;
4353
private Button selectThumbnailFile;
44-
private TextView thumbnailFilePath;
54+
private TextView thumbnailFileName;
55+
private String thumbnailFilePath;
4556

4657
private submitSubmissionPart1 page;
4758

4859
private void getElements(View rootView) {
4960
submissionType = rootView.findViewById(R.id.submissionType);
5061
selectSourceFile = rootView.findViewById(R.id.selectSourceFile);
51-
sourceFilePath = rootView.findViewById(R.id.sourceFilePath);
62+
sourceFileName = rootView.findViewById(R.id.sourceFileName);
5263
selectThumbnailFile = rootView.findViewById(R.id.selectThumbnailFile);
53-
thumbnailFilePath = rootView.findViewById(R.id.thumbnailFilePath);
64+
thumbnailFileName = rootView.findViewById(R.id.thumbnailFileName);
5465
}
5566

5667
private void updateUIElements() {
@@ -77,7 +88,6 @@ private void fetchPageData() {
7788
page.execute();
7889
}
7990

80-
8191
private void updateUIElementListeners() {
8292
submissionType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
8393
@Override
@@ -91,15 +101,32 @@ public void onNothingSelected(AdapterView<?> parent) {
91101
}
92102
});
93103

94-
Fragment uploadFrag = this;
95104
selectSourceFile.setOnClickListener(v -> {
96105
if(ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
97-
new MaterialFilePicker()
98-
.withSupportFragment(uploadFrag)
99-
.withPath(Environment.getRootDirectory().getPath())
100-
.withFilterDirectories(false)
101-
.withRequestCode(submissionFileRequestCode)
102-
.start();
106+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
107+
intent.addCategory(Intent.CATEGORY_OPENABLE);
108+
109+
switch(((kvPair) submissionType.getSelectedItem()).getKey()) {
110+
case "submission":
111+
intent.setType(imageMimeTypes.stream().collect(Collectors.joining(",")));
112+
intent.putExtra(Intent.EXTRA_MIME_TYPES, imageMimeTypes.toArray());
113+
break;
114+
case "story":
115+
case "poetry":
116+
intent.setType(textMimeTypes.stream().collect(Collectors.joining(",")));
117+
intent.putExtra(Intent.EXTRA_MIME_TYPES, textMimeTypes.toArray());
118+
break;
119+
case "music":
120+
intent.setType(audioMimeTypes.stream().collect(Collectors.joining(",")));
121+
intent.putExtra(Intent.EXTRA_MIME_TYPES, audioMimeTypes.toArray());
122+
break;
123+
default:
124+
intent.setType(allMimeTypes.stream().collect(Collectors.joining(",")));
125+
intent.putExtra(Intent.EXTRA_MIME_TYPES, allMimeTypes.toArray());
126+
break;
127+
}
128+
129+
startActivityForResult(intent, submissionFileRequestCode);
103130
} else {
104131
String [] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
105132
requestPermissions(permissions, 0);
@@ -108,12 +135,11 @@ public void onNothingSelected(AdapterView<?> parent) {
108135

109136
selectThumbnailFile.setOnClickListener(v -> {
110137
if(ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
111-
new MaterialFilePicker()
112-
.withSupportFragment(uploadFrag)
113-
.withPath(Environment.getRootDirectory().getPath())
114-
.withFilterDirectories(false)
115-
.withRequestCode(thumbnailFileRequestCode)
116-
.start();
138+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
139+
intent.addCategory(Intent.CATEGORY_OPENABLE);
140+
intent.setType(imageMimeTypes.stream().collect(Collectors.joining(",")));
141+
intent.putExtra(Intent.EXTRA_MIME_TYPES, imageMimeTypes.toArray());
142+
startActivityForResult(intent, thumbnailFileRequestCode);
117143
} else {
118144
String [] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
119145
requestPermissions(permissions, 0);
@@ -151,7 +177,7 @@ public void requestFailed(abstractPage abstractPage) {
151177
Toast.makeText(context, "Failed to upload submission step 2", Toast.LENGTH_SHORT).show();
152178
uploadDialog.this.dismiss();
153179
}
154-
}, page, sourceFilePath.getText().toString(), thumbnailFilePath.getText().toString()).execute());
180+
}, page, sourceFilePath, thumbnailFilePath).execute());
155181
builder.setNegativeButton(R.string.cancelButton, (dialog, which) -> {
156182

157183
});
@@ -165,17 +191,41 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
165191

166192
switch (requestCode) {
167193
case submissionFileRequestCode:
168-
if (resultCode == FilePickerActivity.RESULT_OK) {
169-
sourceFilePath.setText(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));
194+
if (resultCode == Activity.RESULT_OK) {
195+
Uri selectedFile = data.getData();
196+
Cursor sourceFileCursor = requireContext().getContentResolver().query(selectedFile, null, null, null);
197+
if(sourceFileCursor.moveToFirst()) {
198+
sourceFilePath = selectedFile.toString();
199+
int displayNameColumnIndex = sourceFileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
200+
String displayNameString = sourceFileCursor.getString(displayNameColumnIndex);
201+
sourceFileName.setText(displayNameString);
202+
} else {
203+
Toast.makeText(requireContext(), "Failed to find file info", Toast.LENGTH_SHORT).show();
204+
sourceFilePath = null;
205+
sourceFileName.setText("");
206+
}
170207
} else {
171-
sourceFilePath.setText("");
208+
sourceFilePath = null;
209+
sourceFileName.setText("");
172210
}
173211
break;
174212
case thumbnailFileRequestCode:
175-
if (resultCode == FilePickerActivity.RESULT_OK) {
176-
thumbnailFilePath.setText(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH));
213+
if (resultCode == Activity.RESULT_OK) {
214+
Uri selectedFile = data.getData();
215+
Cursor sourceFileCursor = requireContext().getContentResolver().query(selectedFile, null, null, null);
216+
if(sourceFileCursor.moveToFirst()) {
217+
thumbnailFilePath = selectedFile.toString();
218+
int displayNameColumnIndex = sourceFileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
219+
String displayNameString = sourceFileCursor.getString(displayNameColumnIndex);
220+
thumbnailFileName.setText(displayNameString);
221+
} else {
222+
Toast.makeText(requireContext(), "Failed to find file info", Toast.LENGTH_SHORT).show();
223+
thumbnailFilePath = null;
224+
thumbnailFileName.setText("");
225+
}
177226
} else {
178-
thumbnailFilePath.setText("");
227+
thumbnailFilePath = null;
228+
thumbnailFileName.setText("");
179229
}
180230
break;
181231
}

app/src/main/java/open/furaffinity/client/fragmentTabs/manageAvatar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void requestSucceeded(abstractPage abstractPage) {
7575
mDataSet.clear();
7676
mDataSet.addAll(((controlsAvatar) abstractPage).getPageResults());
7777
mAdapter.notifyDataSetChanged();
78-
//fab.setVisibility(View.VISIBLE);
78+
fab.setVisibility(View.VISIBLE);
7979
isLoading = false;
8080
swipeRefreshLayout.setRefreshing(false);
8181
}

app/src/main/java/open/furaffinity/client/submitPages/submitNewAvatar.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public submitNewAvatar(Context context, abstractPage.pageListener pageListener,
1919

2020
@Override
2121
protected Boolean processPageData(String html) {
22-
//may need to come back to this to do validation. If i recall correctly this page will go back to the home page if it fails...
2322
return true;
2423
}
2524

@@ -37,7 +36,7 @@ protected Boolean doInBackground(Void... voids) {
3736
newParam.put("filePath", filePath);
3837
params.add(newParam);
3938

40-
String html = webClient.sendPostRequest(open.furaffinity.client.utilities.webClient.getBaseUrl() + controlsAvatar.getPagePath(), params);
39+
String html = webClient.sendFormPostRequest(open.furaffinity.client.utilities.webClient.getBaseUrl() + controlsAvatar.getPagePath(), params);
4140
if (webClient.getLastPageLoaded() && html != null) {
4241
return processPageData(html);
4342
}

app/src/main/java/open/furaffinity/client/submitPages/submitSubmissionPart2.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,17 @@ protected Boolean doInBackground(Void... voids) {
9696
postParams.add(newParam);
9797
}
9898

99-
File sourceFile = new File(sourceFilePath);
100-
if (sourceFile.exists()) {
99+
if (sourceFilePath != null && !sourceFilePath.isEmpty()) {
101100
HashMap<String, String> newParam = new HashMap<>();
102101
newParam.put("name", "submission");
103-
newParam.put("filePath", sourceFile.getPath());
102+
newParam.put("filePath", sourceFilePath);
104103
postParams.add(newParam);
105104
}
106105

107-
File thumbnailFile = new File(thumbnailFilePath);
108-
if (thumbnailFile.exists()) {
106+
if (thumbnailFilePath != null && !thumbnailFilePath.isEmpty()) {
109107
HashMap<String, String> newParam = new HashMap<>();
110108
newParam.put("name", "thumbnail");
111-
newParam.put("filePath", thumbnailFile.getPath());
109+
newParam.put("filePath", thumbnailFilePath);
112110
postParams.add(newParam);
113111
} else {
114112
HashMap<String, String> newParam = new HashMap<>();
@@ -117,7 +115,7 @@ protected Boolean doInBackground(Void... voids) {
117115
postParams.add(newParam);
118116
}
119117

120-
String html = webClient.sendPostRequest(open.furaffinity.client.utilities.webClient.getBaseUrl() + pagePath, postParams);
118+
String html = webClient.sendFormPostRequest(open.furaffinity.client.utilities.webClient.getBaseUrl() + pagePath, postParams);
121119
if (webClient.getLastPageLoaded() && html != null) {
122120
return processPageData(html);
123121
}

0 commit comments

Comments
 (0)