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

Commit 038e47e

Browse files
Sam1301kunall17
authored andcommitted
Share all types of files.
1 parent 243c134 commit 038e47e

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@
5555
<intent-filter>
5656
<action android:name="android.intent.action.SEND" />
5757
<category android:name="android.intent.category.DEFAULT" />
58-
<data android:mimeType="text/plain" />
59-
</intent-filter>
60-
<intent-filter>
61-
<action android:name="android.intent.action.SEND" />
62-
<category android:name="android.intent.category.DEFAULT" />
63-
<data android:mimeType="image/*" />
58+
<data android:mimeType="*/*" />
6459
</intent-filter>
6560
</activity>
6661
<activity

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public class ZulipActivity extends BaseActivity implements
148148
private static final int MAX_THRESOLD_EMOJI_HINT = 5;
149149
//At these many letters the emoji/person hint starts to show up
150150
private static final int MIN_THRESOLD_EMOJI_HINT = 1;
151-
private static final int PERMISSION_REQUEST_READ_CONTACTS = 1;
151+
private static final int PERMISSION_REQUEST_READ_STORAGE = 1;
152152
private static final int REQUEST_TAKE_PHOTO = 2;
153153
private static final Interpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new FastOutSlowInInterpolator();
154154
private static final int HIDE_FAB_AFTER_SEC = 5;
@@ -201,7 +201,7 @@ public void onReceive(Context contenxt, Intent intent) {
201201
private ExpandableStreamDrawerAdapter streamsDrawerAdapter;
202202
private List<PeopleDrawerList> recentPeopleDrawerList;
203203
private List<PeopleDrawerList> filteredRecentPeopleDrawerList;
204-
private Uri mImageUri;
204+
private Uri mFileUri;
205205
private ImageView cameraBtn;
206206
private String mCurrentPhotoPath;
207207
private Uri mPhotoURI;
@@ -536,12 +536,12 @@ public Cursor runQuery(CharSequence charSequence) {
536536
String type = intent.getType();
537537

538538
if (Intent.ACTION_SEND.equals(action) && type != null) {
539-
if (type.startsWith("image/")) {
540-
// Handle single image being sent
541-
handleSentImage(intent);
542-
} else if ("text/plain".equals(type)) {
539+
if ("text/plain".equals(type)) {
543540
// Handle text being sent
544541
handleSentText(intent);
542+
} else {
543+
// Handle single file being sent
544+
handleSentFile(intent);
545545
}
546546
}
547547
// if device doesn't have camera, disable camera button
@@ -868,7 +868,7 @@ protected void onNewIntent(Intent intent) {
868868
if (Intent.ACTION_SEND.equals(action) && type != null) {
869869
if (type.startsWith("image/")) {
870870
// Handle single image being sent
871-
handleSentImage(intent);
871+
handleSentFile(intent);
872872
} else if ("text/plain".equals(type)) {
873873
// Handle text being sent
874874
handleSentText(intent);
@@ -930,9 +930,9 @@ private void handleSentText(Intent intent) {
930930
* @param intent passed to the activity with action SEND
931931
*/
932932
@SuppressLint("InlinedApi")
933-
private void handleSentImage(Intent intent) {
934-
mImageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
935-
if (mImageUri != null) {
933+
private void handleSentFile(Intent intent) {
934+
mFileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
935+
if (mFileUri != null) {
936936
// check if user has granted read external storage permission
937937
// for Android 6.0 or higher
938938
if (ContextCompat.checkSelfPermission(this,
@@ -941,14 +941,14 @@ private void handleSentImage(Intent intent) {
941941
// we need to request the permission.
942942
ActivityCompat.requestPermissions(this,
943943
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
944-
PERMISSION_REQUEST_READ_CONTACTS);
944+
PERMISSION_REQUEST_READ_STORAGE);
945945
} else {
946946
// permission already granted
947947
// start with file upload
948948
startFileUpload();
949949
}
950950
} else {
951-
Toast.makeText(this, R.string.cannot_find_image, Toast.LENGTH_SHORT).show();
951+
Toast.makeText(this, R.string.cannot_find_file, Toast.LENGTH_SHORT).show();
952952
}
953953
}
954954

@@ -957,7 +957,7 @@ public void onRequestPermissionsResult(int requestCode,
957957
String permissions[], int[] grantResults) {
958958

959959
switch (requestCode) {
960-
case PERMISSION_REQUEST_READ_CONTACTS: {
960+
case PERMISSION_REQUEST_READ_STORAGE: {
961961
// If request is cancelled, the result arrays are empty.
962962
if (grantResults.length > 0
963963
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
@@ -966,7 +966,7 @@ public void onRequestPermissionsResult(int requestCode,
966966
startFileUpload();
967967
} else {
968968
// permission denied
969-
Toast.makeText(this, R.string.cannot_upload_image, Toast.LENGTH_SHORT).show();
969+
Toast.makeText(this, R.string.cannot_upload_file, Toast.LENGTH_SHORT).show();
970970
}
971971
}
972972
break;
@@ -1036,32 +1036,25 @@ private File createPhotoFile() throws IOException {
10361036
}
10371037

10381038
/**
1039-
* Helper function to update UI to indicate image is being uploaded and call
1040-
* {@link ZulipActivity#uploadFile(File)} to upload the image.
1039+
* Helper function to update UI to indicate file is being uploaded and call
1040+
* {@link #uploadFile(File)} to upload the image.
10411041
*/
10421042
private void startFileUpload() {
1043-
// Update UI to indicate image is being loaded
1044-
// hide fab and display chatbox
1045-
displayFAB(false);
1046-
displayChatBox(true);
1047-
String loadingMsg = getResources().getString(R.string.uploading_message);
1048-
sendingMessage(true, loadingMsg);
1049-
10501043
File file = null;
1051-
if (FilePathHelper.isLegacy(mImageUri)) {
1052-
file = FilePathHelper.getTempFileFromContentUri(this, mImageUri);
1044+
if (FilePathHelper.isLegacy(mFileUri)) {
1045+
file = FilePathHelper.getTempFileFromContentUri(this, mFileUri);
10531046
} else {
10541047
// get actual file path
1055-
String imageFilePath = FilePathHelper.getPath(this, mImageUri);
1056-
if (imageFilePath != null) {
1057-
file = new File(imageFilePath);
1058-
} else if ("content".equalsIgnoreCase(mImageUri.getScheme())) {
1059-
file = FilePathHelper.getTempFileFromContentUri(this, mImageUri);
1048+
String filePath = FilePathHelper.getPath(this, mFileUri);
1049+
if (filePath != null) {
1050+
file = new File(filePath);
1051+
} else if ("content".equalsIgnoreCase(mFileUri.getScheme())) {
1052+
file = FilePathHelper.getTempFileFromContentUri(this, mFileUri);
10601053
}
10611054
}
10621055

10631056
if (file == null) {
1064-
Toast.makeText(this, R.string.invalid_image, Toast.LENGTH_SHORT).show();
1057+
Toast.makeText(this, R.string.invalid_file, Toast.LENGTH_SHORT).show();
10651058
return;
10661059
}
10671060
// upload the file asynchronously to the server
@@ -1082,7 +1075,7 @@ private void uploadFile(File file) {
10821075

10831076
// MultipartBody.Part is used to send also the actual file name
10841077
MultipartBody.Part body =
1085-
MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
1078+
MultipartBody.Part.createFormData("file", file.getName(), requestFile);
10861079

10871080
final String loadingMsg = getResources().getString(R.string.uploading_message);
10881081

app/src/main/java/com/zulip/android/util/FilePathHelper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,16 @@ public static File getTempFileFromContentUri(Context context, Uri uri) {
157157
File tempFile = null;
158158
try {
159159
is = context.getContentResolver().openInputStream(uri);
160-
tempFile = File.createTempFile("imgUpload", ".jpg");
160+
161+
// set prefix and suffix of temp file as actual filename and extension respectively
162+
String fileName = uri.getLastPathSegment();
163+
final String[] split = fileName.split(":");
164+
int extensionIndex = split[1].lastIndexOf('.');
165+
int lastIndex = split[1].lastIndexOf('/');
166+
String name = split[1].substring(lastIndex+1, extensionIndex);
167+
String ext = split[1].substring(extensionIndex);
168+
169+
tempFile = File.createTempFile(name + "-", ext);
161170
tempFile.deleteOnExit();
162171
FileOutputStream out = new FileOutputStream(tempFile);
163172
try {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@
109109
<string name="un_star_message">Unstar message</string>
110110
<string name="login_failed">Unable to login</string>
111111
<string name="uploading_message">Uploading</string>
112-
<string name="cannot_upload_image">Cannot upload image</string>
113-
<string name="cannot_find_image">Could not find the image</string>
112+
<string name="cannot_upload_file">Cannot upload file</string>
113+
<string name="cannot_find_file">Cannot find requested file</string>
114114
<string name="failed_to_upload">Failed to upload</string>
115-
<string name="invalid_image">Unable to load that image</string>
115+
<string name="invalid_file">Invalid file</string>
116116
<string name="content_description_cancel_icon">cancel icon</string>
117117
<string name="content_description_search_icon">search icon</string>
118118
<string name="hint_search_people">Search people</string>

0 commit comments

Comments
 (0)