diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java index 04988e6..e936db2 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java @@ -8,6 +8,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Configuration; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; @@ -22,33 +23,48 @@ import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.annimon.stream.Stream; +import com.auth0.android.jwt.Claim; +import com.auth0.android.jwt.JWT; +import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.model.DutyHourViewModel; +import com.uam.wmi.findmytutor.model.Feedback; +import com.uam.wmi.findmytutor.model.JwtToken; import com.uam.wmi.findmytutor.model.TutorTabViewModel; import com.uam.wmi.findmytutor.model.User; +import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.service.BackgroundLocalizationService; +import com.uam.wmi.findmytutor.service.FeedbackService; import com.uam.wmi.findmytutor.utils.ActiveFragment; import com.uam.wmi.findmytutor.utils.PrefUtils; +import com.uam.wmi.findmytutor.utils.RestApiHelper; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; +import okhttp3.ResponseBody; +import retrofit2.Response; import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus; @@ -118,9 +134,9 @@ public abstract class BaseActivity startActivity(i); finish(); } else if (itemName.equals(getResources().getString(R.string.navigation_item_feedback))) { - showNoteDialog(BaseActivity.this, R.string.email_subject_feedback); + showNoteDialog(BaseActivity.this, "Feedback"); } else if (itemName.equals(getResources().getString(R.string.navigation_item_bug))) { - showNoteDialog(BaseActivity.this, R.string.email_subject_bug); + showNoteDialog(BaseActivity.this, "Bug report"); } sideDrawer.closeDrawers(); @@ -370,73 +386,94 @@ public abstract class BaseActivity MenuItem item = navigationView.getMenu().findItem(itemId); item.setChecked(true); } - /** - * Email client intent to send support mail - * Appends the necessary device information to email body - * useful when providing support - */ - private void sendFeedback(Context context, int subject, String mode) { - String appVersion = null; - String body = null; - try { - appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; - if( mode == "full"){ - body = "\n\n-----------------------------\nPlease don't remove this information\n Device OS: Android \n Device OS version: " + - Build.VERSION.RELEASE + "\n App Version: " + appVersion + "\n Device Brand: " + Build.BRAND + - "\n Device Model: " + Build.MODEL + "\n Device Manufacturer: " + Build.MANUFACTURER + "\n Marian Kowalski!!!"; - } else if ( mode == "anonymous"){ - body = "\n\n-----------------------------\nPlease don't remove this information\n Device OS: Android \n Device OS version: " + - Build.VERSION.RELEASE + "\n App Version: " + appVersion + "\n Device Brand: " + Build.BRAND + - "\n Device Model: " + Build.MODEL + "\n Device Manufacturer: " + Build.MANUFACTURER; - } - } catch (PackageManager.NameNotFoundException e) { - } - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("message/rfc822"); - intent.putExtra(Intent.EXTRA_EMAIL, new String[]{context.getString(R.string.fmt_email)}); - intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(subject)); - intent.putExtra(Intent.EXTRA_TEXT, body); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_email_client))); - } - private void showNoteDialog(Context context, int subject) { + private void showNoteDialog(Context context, String subject) { + LayoutInflater layoutInflaterAndroid = LayoutInflater.from(BaseActivity.this); - //View view = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null); + View view = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null); AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BaseActivity.this); - //alertDialogBuilderUserInput.setView(view); - alertDialogBuilderUserInput.setNegativeButton(R.string.cancel, (dialog, id) -> { - // User cancelled the dialog - }); + alertDialogBuilderUserInput.setView(view); final AlertDialog alertDialog = alertDialogBuilderUserInput.create(); - alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() { + EditText userInput = view.findViewById(R.id.feedback_input); + CheckBox isAnonymous = view.findViewById(R.id.feedback_is_anonymous); + + alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"SEND", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { - // Write your code here to execute after dialog - // closed - Toast.makeText(getApplicationContext(), - "You clicked on OK", Toast.LENGTH_SHORT).show(); - } - }); + String body = userInput.getText().toString(); + boolean mode = isAnonymous.isChecked(); + sendFeedback(context,subject,body,mode); -/* Button yes = (Button) alertDialog.findViewById(R.id.feedback_modal_yes); - Button no = (Button) alertDialog.findViewById(R.id.feedback_modal_no);*/ -/* yes.setOnClickListener(new View.OnClickListener(){ - public void onClick(View v){ - sendFeedback(context,subject,"full"); } }); - no.setOnClickListener(new View.OnClickListener(){ - public void onClick(View v){ - sendFeedback(context,subject,"anonymous"); - } - });*/ alertDialog.show(); + } + + /** + * Email client intent to send support mail + * Appends the necessary device information to email body + * useful when providing support + */ + private void sendFeedback(Context context, String header, String body, boolean mode) { + String appVersion = null; + String metadata = null; + try { + appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName; + if( !mode ){ + metadata = "\n-----------------------------\n" + + "User ID: " + PrefUtils.getUserId(context) + "\n" + + "Device OS: Android\n" + + "Device OS version: " + Build.VERSION.RELEASE + + "App Version: " + appVersion + "\n" + + "Device Brand: " + Build.BRAND + "\n" + + "Device Model: " + Build.MODEL + "\n" + + "Device Manufacturer: " + Build.MANUFACTURER + "\n" + + "-----------------------------\n"; + body = metadata + body; + header = header + " - " + PrefUtils.getUserFirstName(context) + " " + PrefUtils.getUserLastName(context); + } + } catch (PackageManager.NameNotFoundException e) { + } + FeedbackService feedbackService = ApiClient.getClient(context).create(FeedbackService.class); + Feedback userFeedback = new Feedback(mode,header,body); + CompositeDisposable disposable = new CompositeDisposable(); + disposable.add(feedbackService.postFeedback(userFeedback) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::handleResponse, this::handleError)); + + + + +/* Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("message/rfc822"); + intent.putExtra(Intent.EXTRA_EMAIL, new String[]{context.getString(R.string.fmt_email)}); + intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(subject)); + intent.putExtra(Intent.EXTRA_TEXT, body); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(Intent.createChooser(intent, context.getString(R.string.choose_email_client)));*/ + } + private void handleResponse(Response resp) { + Toast.makeText(getApplicationContext(), "Thank you for subbmiting your feedback", Toast.LENGTH_SHORT).show(); + } + + private void handleError(Throwable error) { + if (error instanceof HttpException) { + + ResponseBody responseBody = ((HttpException) error).response().errorBody(); + Toast.makeText(getApplicationContext(), + RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show(); + + } else { + Toast.makeText(getApplicationContext(), + "Network error " + error.getMessage(), Toast.LENGTH_SHORT).show(); + Log.d("FEEDBACK",error.getMessage()); + } } abstract int getNavigationMenuItemId(); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/model/Feedback.java b/app/src/main/java/com/uam/wmi/findmytutor/model/Feedback.java new file mode 100644 index 0000000..1726c75 --- /dev/null +++ b/app/src/main/java/com/uam/wmi/findmytutor/model/Feedback.java @@ -0,0 +1,47 @@ + +package com.uam.wmi.findmytutor.model; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Feedback { + + @SerializedName("isAnonymous") + @Expose + private Boolean isAnonymous; + @SerializedName("header") + @Expose + private String header; + @SerializedName("body") + @Expose + private String body; + + public Feedback(boolean isAnonymous, String header, String body){ + this.isAnonymous = isAnonymous; + this.header = header; + this.body = body; + } + public Boolean getIsAnonymous() { + return isAnonymous; + } + + public void setIsAnonymous(Boolean isAnonymous) { + this.isAnonymous = isAnonymous; + } + + public String getHeader() { + return header; + } + + public void setHeader(String header) { + this.header = header; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/FeedbackService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/FeedbackService.java new file mode 100644 index 0000000..a6d166c --- /dev/null +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/FeedbackService.java @@ -0,0 +1,17 @@ +package com.uam.wmi.findmytutor.service; + +import com.uam.wmi.findmytutor.model.Feedback; +import io.reactivex.Observable; +import io.reactivex.Single; +import retrofit2.Response; +import retrofit2.http.Body; +import retrofit2.http.GET; +import retrofit2.http.POST; + +public interface FeedbackService { + @POST("api/Feedback") + Observable> postFeedback(@Body Feedback feedback); + + @GET("api/Feedback") + Single getFeedback(); +} diff --git a/app/src/main/res/layout/feedback_modal.xml b/app/src/main/res/layout/feedback_modal.xml index 56ed62c..244b8e0 100644 --- a/app/src/main/res/layout/feedback_modal.xml +++ b/app/src/main/res/layout/feedback_modal.xml @@ -1,37 +1,48 @@ - + android:layout_marginTop="50dp" + android:ems="10" + android:maxLength="1000" + android:hint="@string/modal_feedback_hint" + android:inputType="textMultiLine" + android:maxLines="10" + android:scrollbars="vertical" + android:requiresFadingEdge="vertical" + /> -