1717import android .content .SharedPreferences ;
1818import android .content .pm .PackageManager ;
1919import android .graphics .Bitmap ;
20- import android .graphics .Color ;
2120import android .net .Uri ;
2221import android .os .AsyncTask ;
2322import android .os .Build ;
2827import android .support .v4 .app .Fragment ;
2928import android .support .v4 .content .ContextCompat ;
3029import android .support .v7 .widget .LinearLayoutCompat ;
30+ import android .text .Editable ;
31+ import android .text .TextWatcher ;
3132import android .util .Log ;
3233import android .view .LayoutInflater ;
3334import android .view .View ;
3435import android .view .View .OnClickListener ;
3536import android .view .ViewGroup ;
3637import android .view .WindowManager ;
37- import android .view .inputmethod .InputMethodManager ;
3838import android .widget .EditText ;
3939import android .widget .ImageView ;
4040
@@ -105,8 +105,9 @@ public class ReportFragment extends Fragment {
105105 private ViewSwitcher photoviewSwitcher ;
106106 private ViewSwitcher audioviewSwitcher ;
107107 private AudioStatus mAudioStatus ;
108+ private FloatingActionButton mSubmitBtn ;
109+ private EditText mDescriptionView ;
108110 private int mPlayTime = 0 ;
109-
110111 public static final int CAMERA_REQUEST = 101 ;
111112 public static final int LOCATION_REQUEST = 102 ;
112113 public static final int GALLERY_IMAGE_REQUEST = 103 ;
@@ -172,20 +173,38 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle st
172173 LinearLayoutCompat btnService = (LinearLayoutCompat ) view .findViewById (R .id .serviceButton );
173174 LinearLayoutCompat btnLocation = (LinearLayoutCompat ) view .findViewById (R .id .locationButton );
174175 playBtn = (ImageView ) view .findViewById (R .id .audioView2 );
175- View descriptionView = view .findViewById (R .id .report_description_textbox );
176- FloatingActionButton btnSubmit = (FloatingActionButton ) view .findViewById (R .id .report_submit );
176+ mDescriptionView = ( EditText ) view .findViewById (R .id .report_description_textbox );
177+ mSubmitBtn = (FloatingActionButton ) view .findViewById (R .id .report_submit );
177178 photoviewSwitcher = (ViewSwitcher ) view .findViewById (R .id .report_photoviewswitcher );
178179 audioviewSwitcher = (ViewSwitcher ) view .findViewById (R .id .report_audioviewswitcher );
179180 ImageView photoPlaceholder = (ImageView ) view .findViewById ((R .id .photoPlaceholder ));
180181
181182 //Hide the keyboard unless the descriptionView is selected
182- descriptionView .setOnFocusChangeListener (new View .OnFocusChangeListener () {
183+ mDescriptionView .setOnFocusChangeListener (new View .OnFocusChangeListener () {
183184 @ Override
184185 public void onFocusChange (View v , boolean hasFocus ) {
185186 if (!hasFocus ) {
186187 v .clearFocus ();
187188 hideKeyBoard (getActivity ());
189+ } else {
190+ mDescriptionView .addTextChangedListener (new TextWatcher () {
191+ @ Override
192+ public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
193+
194+ }
195+
196+ @ Override
197+ public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
198+
199+ }
200+
201+ @ Override
202+ public void afterTextChanged (Editable editable ) {
203+ validate ();
204+ }
205+ });
188206 }
207+
189208 }
190209 });
191210 photoPlaceholder .setOnClickListener (new OnClickListener () {
@@ -241,7 +260,7 @@ public void onClick(View v) {
241260 }
242261 });
243262
244- btnSubmit .setOnClickListener (new OnClickListener () {
263+ mSubmitBtn .setOnClickListener (new OnClickListener () {
245264 @ Override
246265 public void onClick (View v ) {
247266 hideKeyBoard (getActivity ());
@@ -259,6 +278,7 @@ private void updateService() {
259278 ImageView icon = (ImageView ) getActivity ().findViewById (R .id .serviceView );
260279 icon .setColorFilter (ContextCompat .getColor (getContext (), R .color .colorAccent ), android .graphics .PorterDuff .Mode .MULTIPLY );
261280 }
281+ validate (); //required, so validate
262282 }
263283
264284 private void updateLocation () {
@@ -275,6 +295,7 @@ private void updateLocation() {
275295 } else {
276296 text .setText (R .string .report_hint_location );
277297 }
298+ validate (); //required, so validate
278299 } catch (Exception e ) {
279300 e .printStackTrace ();
280301 }
@@ -434,7 +455,6 @@ public void onCreate(Bundle savedInstanceState) {
434455 attributes = new LinkedList <Attribute >();
435456 attrInfoList = new LinkedList <AttributeInfo >();
436457 installationId = ((MainActivity ) getActivity ()).getInstallationId ();
437-
438458 new RetrieveServicesTask ().execute (); // Load services-list in the background
439459
440460 // Don't show the keyboard if it isn't already shown,
@@ -545,52 +565,30 @@ private boolean validate() {
545565
546566 boolean isValid = true ;
547567
548- EditText description = (EditText ) getActivity ().findViewById (R .id .report_description_textbox );
549-
550- String descText = description .getText ().toString ();
551-
552- // Filter description text. Replace everything that is not a word character,
553- // whitespace or one of the symbols .?(),!:;@
554- String pattern = "[^\\ w\\ s\\ .\\ ?\\ (\\ ),!:;@]" ;
555- description .setText (
556- Normalizer .normalize (descText , Normalizer .Form .NFD ).replaceAll (pattern , "" )
557- );
558-
568+ if (mDescriptionView .getText ().length () == 0 ) {
569+ isValid = false ;
570+ }
559571 if (serviceCode == null ) {
560572 isValid = false ;
561- resetLocation ();
562573 }
563574
564575 if (latitude == null || longitude == null ) {
565576 isValid = false ;
566- resetLocation ();
567577 }
568578
569- if (description .getText ().length () == 0 ) {
570- isValid = false ;
571- description .setHintTextColor (Color .RED );
572- }
573579
574580 if (attrInfoList .size () != 0 ) {
575581 isValid = (attrInfoList .size () == attributes .size ());
576582
577583 }
578584 if (!isValid ) {
579- String result = getString (R .string .failure_posting_service );
580- Snackbar .make (getView (), result , Snackbar .LENGTH_SHORT )
581- .show ();
585+ mSubmitBtn .setVisibility (View .INVISIBLE );
582586 } else {
583- showFab ( );
587+ mSubmitBtn . setVisibility ( View . VISIBLE );
584588 }
585-
586589 return isValid ;
587590 }
588591
589- private void showFab () {
590- View fab = getActivity ().findViewById (R .id .report_submit );
591- fab .setVisibility (View .VISIBLE );
592- }
593-
594592 private Boolean checkAnonymous () {
595593 SharedPreferences settings = getActivity ().getPreferences (Context .MODE_PRIVATE );
596594 String email = settings .getString ("email" , null );
@@ -859,15 +857,18 @@ private void handleCamera() {
859857 }
860858
861859 private void onSubmitButtonClicked () {
862- Log .d (LOG_TAG , "Submit Button was clicked. " );
863-
860+ Log .d (LOG_TAG , "onSubmitButtonClicked " );
861+ final String pattern = "[^ \\ w \\ s \\ . \\ ? \\ ( \\ ),!:;@]" ;
864862 // Check the form result and post the service request
865863 if (!validate ()) {
864+ String result = getString (R .string .failure_posting_service );
865+ Snackbar .make (getView (), result , Snackbar .LENGTH_SHORT )
866+ .show ();
866867 return ;
867868 }
868869
870+
869871 final TextView address = (TextView ) getActivity ().findViewById (R .id .location_text );
870- final EditText description = (EditText ) getActivity ().findViewById (R .id .report_description_textbox );
871872
872873 final POSTServiceRequestDataWrapper data = new POSTServiceRequestDataWrapper (
873874 serviceCode ,
@@ -884,9 +885,13 @@ private void onSubmitButtonClicked() {
884885 if (name != null ) data .setName (name );
885886 if (email != null ) data .setEmail (email );
886887 if (phone != null ) data .setPhone (phone );
888+
887889 data .setDeviceId (installationId )
888890 .setAddress (address .getText ().toString ())
889- .setDescription (description .getText ().toString ());
891+ .setDescription (
892+ Normalizer .normalize (
893+ mDescriptionView .getText ().toString (), Normalizer .Form .NFD )
894+ .replaceAll (pattern , "" ));
890895
891896 if (imageUri != null ) {
892897 Glide .with (getActivity ().getApplicationContext ())
@@ -923,7 +928,9 @@ public void onClick(DialogInterface dialog, int which) {
923928 public void onClick (DialogInterface dialog , int which ) {
924929 data .setDeviceId (installationId )
925930 .setAddress (address .getText ().toString ())
926- .setDescription (description .getText ().toString ());
931+ .setDescription (Normalizer .normalize (
932+ mDescriptionView .getText ().toString (), Normalizer .Form .NFD )
933+ .replaceAll (pattern , "" ));
927934
928935 if (imageUri != null ) {
929936 Glide .with (getActivity ().getApplicationContext ())
0 commit comments