Guest User

Untitled

a guest
Jun 19th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. package campus.smart.bulletin.bulletinboard.Fragments;
  2.  
  3.  
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.os.AsyncTask;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.support.v4.app.Fragment;
  12. import android.view.LayoutInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.EditText;
  17. import android.widget.Spinner;
  18. import android.widget.Toast;
  19.  
  20. import com.basgeekball.awesomevalidation.AwesomeValidation;
  21. import com.basgeekball.awesomevalidation.ValidationStyle;
  22.  
  23. import java.text.SimpleDateFormat;
  24. import java.util.Date;
  25. import java.util.Properties;
  26.  
  27. import javax.mail.Message;
  28. import javax.mail.MessagingException;
  29. import javax.mail.PasswordAuthentication;
  30. import javax.mail.Session;
  31. import javax.mail.Transport;
  32. import javax.mail.internet.InternetAddress;
  33. import javax.mail.internet.MimeMessage;
  34.  
  35. import campus.smart.bulletin.bulletinboard.BuildConfig;
  36. import campus.smart.bulletin.bulletinboard.Configuration.Settings;
  37. import campus.smart.bulletin.bulletinboard.LoggedInUser;
  38. import campus.smart.bulletin.bulletinboard.R;
  39.  
  40. /**
  41. * A simple {@link Fragment} subclass.
  42. */
  43. public class FeedbackFragment extends Fragment {
  44. private Spinner spnFeedbackContent;
  45. private EditText etFeedbackEmail, etFeedbackContent;
  46. private ProgressDialog loading;
  47.  
  48. public FeedbackFragment() {
  49. // Required empty public constructor
  50. }
  51.  
  52. @Override
  53. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  54. Bundle savedInstanceState) {
  55. // Inflate the layout for this fragment
  56. View v = inflater.inflate(R.layout.fragment_feedback, container, false);
  57.  
  58. setHasOptionsMenu(true);
  59.  
  60. spnFeedbackContent = v.findViewById(R.id.spn_feedback_type);
  61. etFeedbackEmail = v.findViewById(R.id.et_feedback_email);
  62. etFeedbackContent = v.findViewById(R.id.et_feedback_content);
  63.  
  64. etFeedbackEmail.setText(LoggedInUser.getLoggedInUser().getEmail());
  65.  
  66. return v;
  67. }
  68.  
  69. @Override
  70. public boolean onOptionsItemSelected(MenuItem item) {
  71. switch (item.getItemId()) {
  72. case R.id.action_send_feedback:
  73. //send the feedback
  74. if (isOnline(getContext())) {
  75. boolean validateContent = true;
  76. String emailTo = Settings.feedbackReceiveEmail;
  77. String subject = "Feedback : " + spnFeedbackContent.getSelectedItem().toString();
  78. String body = "Email : " + etFeedbackEmail.getText().toString()
  79. + "\nContent : " + etFeedbackContent.getText().toString()
  80. + "\nFeedback Date & Time : " + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").format(new Date());
  81.  
  82. AwesomeValidation mAwesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
  83. mAwesomeValidation.addValidation(getActivity(), R.id.et_feedback_email, android.util.Patterns.EMAIL_ADDRESS, R.string.error_email);
  84.  
  85. if (etFeedbackContent.getText().toString().length() == 0) {
  86. etFeedbackContent.setError(getResources().getString(R.string.error_empty));
  87. validateContent = false;
  88. }
  89.  
  90. if (mAwesomeValidation.validate() && validateContent) {
  91. loading = ProgressDialog.show(getContext(), getResources().getString(R.string.progress_send), getResources().getString(R.string.progress_wait), false, false);
  92. new sendFeedback(emailTo, subject, includeDeviceInfo(body)).execute();
  93. etFeedbackEmail.setText("");
  94. etFeedbackContent.setText("");
  95. }
  96.  
  97. } else {
  98. Toast.makeText(getContext(), "Please Connect to Internet and Try Again.", Toast.LENGTH_SHORT).show();
  99. }
  100. return true;
  101. default:
  102. return super.onOptionsItemSelected(item);
  103. }
  104. }
  105.  
  106. private boolean isOnline(Context context) {
  107. ConnectivityManager cm =
  108. (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  109. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  110. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  111. return true;
  112. }
  113. return false;
  114. }
  115.  
  116. private String includeDeviceInfo(String body) {
  117. String deviceInfo = "Device Info : "
  118. + "\nOS & API : " + currentVersion()
  119. + "\nBrand : " + Build.BRAND
  120. + "\nManufacturer : " + Build.MANUFACTURER
  121. + "\nDevice : " + Build.DEVICE
  122. + "\nHardware : " + Build.HARDWARE
  123. + "\nModel : " + Build.MODEL
  124. + "\nApplication Version : " + BuildConfig.VERSION_NAME + "\n\n";
  125.  
  126. return deviceInfo + body;
  127. }
  128.  
  129. private String currentVersion() {
  130. double release = Double.parseDouble(Build.VERSION.RELEASE.substring(0, 1));
  131. String codeName = "Unsupported";//below Jelly bean OR above Oreo
  132. if (release >= 4.1 && release < 4.4) codeName = "Jelly Bean";
  133. else if (release < 5) codeName = "Kit Kat";
  134. else if (release < 6) codeName = "Lollipop";
  135. else if (release < 7) codeName = "Marshmallow";
  136. else if (release < 8) codeName = "Nougat";
  137. else if (release < 9) codeName = "Oreo";
  138. return codeName + " v" + Build.VERSION.RELEASE + ", API Level: " + Build.VERSION.SDK_INT;
  139. }
  140.  
  141. private class sendFeedback extends AsyncTask<Void, Void, Boolean> {
  142. private String emailTo, subject, body;
  143. final String username = "lamkl-wa14@student.tarc.edu.my";
  144. final String password = "zx32as65qw98";
  145.  
  146. public sendFeedback(String emailTo, String subject, String body) {
  147. this.emailTo = emailTo;
  148. this.subject = subject;
  149. this.body = body;
  150. }
  151.  
  152. @Override
  153. protected void onPreExecute() {
  154. super.onPreExecute();
  155. }
  156.  
  157. @Override
  158. protected Boolean doInBackground(Void... arg0) {
  159. Properties props = new Properties();
  160. props.put("mail.smtp.auth", "true");
  161. props.put("mail.smtp.starttls.enable", "true");
  162. props.put("mail.smtp.host", "smtp.gmail.com");
  163. props.put("mail.smtp.port", "587");
  164.  
  165. Session session = Session.getInstance(props,
  166. new javax.mail.Authenticator() {
  167. protected PasswordAuthentication getPasswordAuthentication() {
  168. return new PasswordAuthentication(username, password);
  169. }
  170. });
  171. try {
  172. Message message = new MimeMessage(session);
  173. message.setRecipients(Message.RecipientType.TO,
  174. InternetAddress.parse(emailTo));
  175. message.setSubject(subject);
  176. message.setText(body);
  177.  
  178. Transport.send(message);
  179. return true;
  180. } catch (MessagingException e) {
  181. return false;
  182. }
  183.  
  184. }
  185.  
  186. @Override
  187. protected void onPostExecute(Boolean b) {
  188. super.onPostExecute(b);
  189. if (b) {
  190. Toast.makeText(getContext(), "Feedback sent, your feedback is appreciated!", Toast.LENGTH_SHORT).show();
  191. } else {
  192. Toast.makeText(getContext(), "Error while sending feedback, Please try again.", Toast.LENGTH_SHORT).show();
  193. }
  194. loading.dismiss();
  195. }
  196. }
  197. }
Add Comment
Please, Sign In to add comment