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

Commit 66ed138

Browse files
Sam1301kunall17
authored andcommitted
Add attachment uploads feature.
Fixes: #398
1 parent f3b1eec commit 66ed138

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.app.DatePickerDialog;
1717
import android.app.SearchManager;
1818
import android.content.BroadcastReceiver;
19+
import android.content.ClipData;
1920
import android.content.ComponentName;
2021
import android.content.Context;
2122
import android.content.DialogInterface;
@@ -152,6 +153,10 @@ public class ZulipActivity extends BaseActivity implements
152153
private static final int REQUEST_TAKE_PHOTO = 2;
153154
private static final Interpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new FastOutSlowInInterpolator();
154155
private static final int HIDE_FAB_AFTER_SEC = 5;
156+
private static final int REQUEST_PICK_FILE = 3;
157+
// row number which is used to differentiate the 'All private messages'
158+
// row from the people
159+
final int allPeopleId = -1;
155160
public MessageListFragment currentList;
156161
public CommonProgressDialog commonProgressDialog;
157162
FloatingActionButton fab;
@@ -541,7 +546,7 @@ public Cursor runQuery(CharSequence charSequence) {
541546
handleSentText(intent);
542547
} else {
543548
// Handle single file being sent
544-
handleSentFile(intent);
549+
handleSentFile((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
545550
}
546551
}
547552
// if device doesn't have camera, disable camera button
@@ -866,12 +871,12 @@ protected void onNewIntent(Intent intent) {
866871
String type = intent.getType();
867872

868873
if (Intent.ACTION_SEND.equals(action) && type != null) {
869-
if (type.startsWith("image/")) {
870-
// Handle single image being sent
871-
handleSentFile(intent);
872-
} else if ("text/plain".equals(type)) {
874+
if ("text/plain".equals(type)) {
873875
// Handle text being sent
874876
handleSentText(intent);
877+
} else {
878+
// Handle single file being sent
879+
handleSentFile((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
875880
}
876881
}
877882

@@ -908,10 +913,31 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
908913
// activity transition animation
909914
ActivityTransitionAnim.transition(ZulipActivity.this);
910915
}
916+
917+
// TODO: rearrange code
918+
// in else if upload file sent and show notification for it
919+
else if (requestCode == REQUEST_PICK_FILE && resultCode == RESULT_OK) {
920+
List<Uri> fileUris = new ArrayList<>();
921+
if (data.getData() != null) {
922+
fileUris.add(data.getData());
923+
}
924+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
925+
ClipData clipData = data.getClipData();
926+
if (clipData != null) {
927+
for (int i = 0; i < clipData.getItemCount(); i++) {
928+
fileUris.add(clipData.getItemAt(i).getUri());
929+
}
930+
}
931+
}
932+
for (Uri file : fileUris) {
933+
handleSentFile(file);
934+
}
935+
}
911936
}
912937

913938
/**
914939
* Function invoked when a user shares a text with the zulip app
940+
*
915941
* @param intent passed to the activity with action SEND
916942
*/
917943
private void handleSentText(Intent intent) {
@@ -927,11 +953,11 @@ private void handleSentText(Intent intent) {
927953
/**
928954
* Function invoked when a user shares an image with the zulip app
929955
*
930-
* @param intent passed to the activity with action SEND
956+
* @param fileUri obtained from intent passed to the activity
931957
*/
932958
@SuppressLint("InlinedApi")
933-
private void handleSentFile(Intent intent) {
934-
mFileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
959+
private void handleSentFile(Uri fileUri) {
960+
mFileUri = fileUri;
935961
if (mFileUri != null) {
936962
// check if user has granted read external storage permission
937963
// for Android 6.0 or higher
@@ -2197,12 +2223,31 @@ public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
21972223
case R.id.legal:
21982224
openLegal();
21992225
break;
2226+
case R.id.attach:
2227+
dispatchPickIntent();
2228+
break;
22002229
default:
22012230
return super.onOptionsItemSelected(item);
22022231
}
22032232
return true;
22042233
}
22052234

2235+
private void dispatchPickIntent() {
2236+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
2237+
intent.setType("*/*");
2238+
intent.addCategory(Intent.CATEGORY_OPENABLE);
2239+
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
2240+
2241+
// For Api level greater than or equal to 18, allow user to select multiple files
2242+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
2243+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
2244+
}
2245+
2246+
if (intent.resolveActivity(getPackageManager()) != null) {
2247+
startActivityForResult(intent, REQUEST_PICK_FILE);
2248+
}
2249+
}
2250+
22062251
/**
22072252
* Switches the current Day/Night mode to Night/Day mode
22082253
*

app/src/main/res/menu/options.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
</menu>
2929
</item>
3030

31+
<item
32+
android:id="@+id/attach"
33+
android:title="@string/attach" />
34+
3135
<item
3236
android:id="@+id/daynight"
3337
android:title="@string/switch_theme" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,5 @@
167167
<string name="people_drawer_recent_pm_label">Recent Private Messages</string>
168168
<string name="people_drawer_others_label">New Private Message</string>
169169
<string name="mentions">\@-mentions</string>
170+
<string name="attach">Attach</string>
170171
</resources>

0 commit comments

Comments
 (0)