Skip to content

Commit 9f674d2

Browse files
Attachments
1 parent cf867e6 commit 9f674d2

4 files changed

Lines changed: 123 additions & 48 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.open311.android.adapters;
2+
3+
import android.content.Context;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
import android.widget.BaseAdapter;
7+
8+
import org.open311.android.models.Attachment;
9+
10+
import java.util.LinkedList;
11+
import java.util.List;
12+
13+
/**
14+
* Class to hold a list of Attachments and prepare them for upload.
15+
* Created by milo@dogodigi.net on 12/29/16.
16+
*/
17+
18+
public class AttachmentAdapter extends BaseAdapter {
19+
private static final int ITEM_TYPE_ONLINE = 0;
20+
private static final int ITEM_TYPE_DISK = 1;
21+
22+
private LinkedList<Attachment> attachments;
23+
private Context context;
24+
25+
public AttachmentAdapter(Context context) {
26+
super();
27+
this.context = context;
28+
this.attachments = new LinkedList<Attachment>();
29+
}
30+
public AttachmentAdapter(LinkedList<Attachment> attachments){
31+
super();
32+
this.attachments = attachments;
33+
}
34+
public LinkedList<Attachment> getList(){
35+
return this.attachments;
36+
}
37+
@Override
38+
public int getCount() {
39+
return attachments.size();
40+
}
41+
42+
@Override
43+
public Object getItem(int i) {
44+
return attachments.get(i);
45+
}
46+
47+
@Override
48+
public long getItemId(int i) {
49+
return attachments.get(i).hashCode();
50+
}
51+
52+
@Override
53+
public View getView(int i, View view, ViewGroup viewGroup) {
54+
return null;
55+
}
56+
57+
public void delete(Attachment attachment) {
58+
this.attachments.remove(attachment);
59+
}
60+
61+
public void add(Attachment attachment) {
62+
//TODO check to see if the attachment exists, so it will only be handled once
63+
if (!this.attachments.contains(attachment)) {
64+
this.attachments.add(attachment);
65+
}
66+
67+
}
68+
}

app/src/main/java/org/open311/android/fragments/ReportFragment.java

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
import org.open311.android.MapActivity;
6262
import org.open311.android.R;
6363
import org.open311.android.SoundRecorderActivity;
64+
import org.open311.android.adapters.AttachmentAdapter;
6465
import org.open311.android.helpers.MyReportsFile;
66+
import org.open311.android.models.Attachment;
6567
import org.open311.android.network.POSTServiceRequestDataWrapper;
6668
import org.open311.android.adapters.ServicesAdapter;
6769

@@ -70,6 +72,7 @@
7072
import java.io.Serializable;
7173
import java.text.Normalizer;
7274
import java.text.SimpleDateFormat;
75+
import java.util.ArrayList;
7376
import java.util.Date;
7477
import java.util.Iterator;
7578
import java.util.LinkedList;
@@ -89,8 +92,6 @@ public class ReportFragment extends Fragment {
8992
private LinkedList<AttributeInfo> attrInfoList;
9093
private LinkedList<Attribute> attributes;
9194
private List<Service> services;
92-
private String imageUri;
93-
private Uri audioUri;
9495
private ProgressDialog progress;
9596

9697
private String location;
@@ -107,6 +108,7 @@ public class ReportFragment extends Fragment {
107108
private AudioStatus mAudioStatus;
108109
private FloatingActionButton mSubmitBtn;
109110
private EditText mDescriptionView;
111+
private AttachmentAdapter attachmentAdapter;
110112
private int mPlayTime = 0;
111113
public static final int CAMERA_REQUEST = 101;
112114
public static final int LOCATION_REQUEST = 102;
@@ -303,11 +305,11 @@ private void updateLocation() {
303305

304306
}
305307

306-
public void playAudio() {
308+
public void playAudio(Uri uri) {
307309
try {
308310

309311
mMediaPlayer = new MediaPlayer();
310-
mMediaPlayer.setDataSource(getContext(), audioUri);
312+
mMediaPlayer.setDataSource(getContext(), uri);
311313
mMediaPlayer.prepare();
312314
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
313315
@Override
@@ -338,9 +340,12 @@ public void onCompletion(MediaPlayer mp) {
338340
}
339341
}
340342

341-
public void updateAudio() {
342-
if (audioUri != null) {
343-
Log.d(LOG_TAG, "updateAudio " + audioUri);
343+
public void updateAudio(final Uri uri) {
344+
if (uri != null) {
345+
Attachment _attachment = new Attachment();
346+
_attachment.setUri(uri);
347+
attachmentAdapter.add(_attachment);
348+
Log.d(LOG_TAG, "updateAudio " + uri);
344349
TextView filename = (TextView) getActivity().findViewById(R.id.audio_text2);
345350
OnClickListener playClicked = new OnClickListener() {
346351
public void onClick(View v) {
@@ -350,15 +355,15 @@ public void onClick(View v) {
350355
mPlayTime = 0;
351356
mAudioStatus = AudioStatus.STOPPED;
352357
} else {
353-
playAudio();
358+
playAudio(uri);
354359
}
355360
}
356361
};
357362
playBtn.setOnClickListener(playClicked);
358-
filename.setText(niceName(audioUri));
363+
filename.setText(niceName(uri));
359364
audioviewSwitcher.setDisplayedChild(1);
360365
} else {
361-
resetPhoto();
366+
resetAudio();
362367
}
363368
}
364369

@@ -381,20 +386,21 @@ private String niceName(Uri uri) {
381386
}
382387
}
383388

384-
public void updatePhoto(Boolean broadcast) {
385-
if (imageUri != null) {
386-
Log.d(LOG_TAG, "updatePhoto " + imageUri);
389+
public void updatePhoto(Uri uri, Boolean broadcast) {
390+
if (uri != null) {
391+
Attachment _attachment = new Attachment();
392+
_attachment.setUri(uri);
393+
attachmentAdapter.add(_attachment);
394+
Log.d(LOG_TAG, "updatePhoto " + uri);
387395
if (broadcast) {
388396
// Tell the media gallery the photo is created
389397
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
390-
File f = new File(imageUri);
391-
Uri contentUri = Uri.fromFile(f);
392-
mediaScanIntent.setData(contentUri);
398+
mediaScanIntent.setData(uri);
393399
getContext().sendBroadcast(mediaScanIntent);
394400
}
395401
ImageView image = (ImageView) getActivity().findViewById(R.id.photoPlaceholder);
396402
Log.d(LOG_TAG, "imageView " + image.toString());
397-
Glide.with(getContext()).load(imageUri).asBitmap().into(image);
403+
Glide.with(getContext()).load(uri).asBitmap().into(image);
398404
Log.d(LOG_TAG, "gonna switch!");
399405
photoviewSwitcher.setDisplayedChild(1);
400406
} else {
@@ -404,14 +410,12 @@ public void updatePhoto(Boolean broadcast) {
404410

405411
private void resetAudio() {
406412
audioviewSwitcher.setDisplayedChild(0);
407-
audioUri = null;
408413
TextView audioText = (TextView) getActivity().findViewById(R.id.audio_text);
409414
audioText.setText(R.string.report_hint_sound);
410415
}
411416

412417
private void resetPhoto() {
413418
photoviewSwitcher.setDisplayedChild(0);
414-
imageUri = null;
415419
TextView photoText = (TextView) getActivity().findViewById(R.id.photo_text);
416420
photoText.setText(R.string.report_hint_photo);
417421
}
@@ -469,8 +473,13 @@ public void onCreate(Bundle savedInstanceState) {
469473
public void onActivityCreated(Bundle savedInstanceState) {
470474
super.onActivityCreated(savedInstanceState);
471475
if (savedInstanceState != null) {
472-
if (savedInstanceState.containsKey("imageUri")) {
473-
imageUri = savedInstanceState.getString("imageUri");
476+
if (savedInstanceState.containsKey("attachments")) {
477+
Serializable _att = savedInstanceState.getSerializable("attachments");
478+
try {
479+
attachmentAdapter = new AttachmentAdapter((LinkedList<Attachment>) _att);
480+
} catch (ClassCastException e) {
481+
e.printStackTrace();
482+
}
474483
}
475484
if (savedInstanceState.containsKey("location")) {
476485
location = savedInstanceState.getString("location");
@@ -510,8 +519,8 @@ public void onActivityCreated(Bundle savedInstanceState) {
510519
public void onSaveInstanceState(Bundle savedInstanceState) {
511520
Log.d(LOG_TAG, "onSaveInstanceState");
512521
super.onSaveInstanceState(savedInstanceState);
513-
if (imageUri != null) {
514-
savedInstanceState.putString("imageUri", imageUri);
522+
if (attachmentAdapter != null) {
523+
savedInstanceState.putSerializable("attachments", attachmentAdapter.getList());
515524
}
516525
if (location != null) {
517526
savedInstanceState.putString("location", location);
@@ -540,8 +549,8 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
540549
public void onResume() {
541550
Log.d(LOG_TAG, "onResume");
542551
super.onResume();
543-
updatePhoto(true);
544-
updateAudio();
552+
updatePhoto(null, false);
553+
updateAudio(null);
545554
updateService();
546555
updateLocation();
547556
}
@@ -964,33 +973,30 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
964973
}
965974
if (requestCode == GALLERY_AUDIO_REQUEST) {
966975
if (resultCode == Activity.RESULT_OK) {
967-
audioUri = data.getData();
968-
updateAudio();
976+
updateAudio(data.getData());
969977
} else {
970978
resetAudio();
971979
}
972980
}
973981
if (requestCode == RECORDER_REQUEST) {
974982
if (resultCode == Activity.RESULT_OK) {
975-
audioUri = data.getData();
976-
updateAudio();
983+
updateAudio(data.getData());
977984
} else {
978985
resetAudio();
979986
}
980987
}
981988
if (requestCode == GALLERY_IMAGE_REQUEST) {
982989
if (resultCode == Activity.RESULT_OK) {
983-
imageUri = data.getData().toString();
984-
updatePhoto(false);
990+
updatePhoto(data.getData(), false);
985991
} else {
986992
resetPhoto();
987993
}
988994
}
989995

990996
if (requestCode == CAMERA_REQUEST) {
991997
if (resultCode == Activity.RESULT_OK) {
992-
if (imageUri == null) return;
993-
updatePhoto(true);
998+
if (data.getData() == null) return;
999+
updatePhoto(data.getData(), true);
9941000
} else {
9951001
resetPhoto();
9961002
}
@@ -1029,6 +1035,7 @@ protected String doInBackground(Void... ignore) {
10291035
String result;
10301036
try {
10311037
APIWrapperFactory wrapperFactory = new APIWrapperFactory(((MainActivity) getActivity()).getCurrentCity(), EndpointType.PRODUCTION);
1038+
// todo this has to go as the app should check the attachments
10321039
if (imageUri != null) {
10331040
HTTPNetworkManager networkManager = new HTTPNetworkManager(bitmap);
10341041
wrapperFactory.setNetworkManager(networkManager);
@@ -1226,15 +1233,7 @@ private File createFile(GalleryType type) throws IOException {
12261233
extension, /* suffix */
12271234
storageDir /* directory */
12281235
);
1229-
// Save a file: path for use with ACTION_VIEW intents
1230-
switch (type) {
1231-
case AUDIO:
1232-
audioUri = Uri.fromFile(file);
1233-
break;
1234-
case IMAGE:
1235-
default:
1236-
imageUri = Uri.fromFile(file).getPath();
1237-
}
1236+
12381237
return file;
12391238
}
12401239

app/src/main/java/org/open311/android/models/Attachment.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66
import java.io.Serializable;
77

88
/**
9-
* Created by miblon on 11/23/16.
9+
* Class for attachments that can be part of a open311 report
10+
* Created by milo@dogodigi.net on 11/23/16.
1011
*/
1112

1213
public class Attachment implements Serializable {
1314

15+
private Uri uri;
16+
private int status;
17+
18+
public int getStatus() {
19+
return status;
20+
}
21+
22+
public void setStatus(int status) {
23+
this.status = status;
24+
}
25+
1426
public Uri getUri() {
1527
return uri;
1628
}
@@ -19,8 +31,4 @@ public Attachment setUri(Uri uri) {
1931
this.uri = uri;
2032
return this;
2133
}
22-
23-
private Uri uri;
24-
25-
2634
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.2'
8+
classpath 'com.android.tools.build:gradle:2.2.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)