6161import org .open311 .android .MapActivity ;
6262import org .open311 .android .R ;
6363import org .open311 .android .SoundRecorderActivity ;
64+ import org .open311 .android .adapters .AttachmentAdapter ;
6465import org .open311 .android .helpers .MyReportsFile ;
66+ import org .open311 .android .models .Attachment ;
6567import org .open311 .android .network .POSTServiceRequestDataWrapper ;
6668import org .open311 .android .adapters .ServicesAdapter ;
6769
7072import java .io .Serializable ;
7173import java .text .Normalizer ;
7274import java .text .SimpleDateFormat ;
75+ import java .util .ArrayList ;
7376import java .util .Date ;
7477import java .util .Iterator ;
7578import 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
0 commit comments