Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package campus.smart.bulletin.bulletinboard.Fragments;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.Toast;
- import com.basgeekball.awesomevalidation.AwesomeValidation;
- import com.basgeekball.awesomevalidation.ValidationStyle;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import campus.smart.bulletin.bulletinboard.BuildConfig;
- import campus.smart.bulletin.bulletinboard.Configuration.Settings;
- import campus.smart.bulletin.bulletinboard.LoggedInUser;
- import campus.smart.bulletin.bulletinboard.R;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class FeedbackFragment extends Fragment {
- private Spinner spnFeedbackContent;
- private EditText etFeedbackEmail, etFeedbackContent;
- private ProgressDialog loading;
- public FeedbackFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View v = inflater.inflate(R.layout.fragment_feedback, container, false);
- setHasOptionsMenu(true);
- spnFeedbackContent = v.findViewById(R.id.spn_feedback_type);
- etFeedbackEmail = v.findViewById(R.id.et_feedback_email);
- etFeedbackContent = v.findViewById(R.id.et_feedback_content);
- etFeedbackEmail.setText(LoggedInUser.getLoggedInUser().getEmail());
- return v;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.action_send_feedback:
- //send the feedback
- if (isOnline(getContext())) {
- boolean validateContent = true;
- String emailTo = Settings.feedbackReceiveEmail;
- String subject = "Feedback : " + spnFeedbackContent.getSelectedItem().toString();
- String body = "Email : " + etFeedbackEmail.getText().toString()
- + "\nContent : " + etFeedbackContent.getText().toString()
- + "\nFeedback Date & Time : " + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").format(new Date());
- AwesomeValidation mAwesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
- mAwesomeValidation.addValidation(getActivity(), R.id.et_feedback_email, android.util.Patterns.EMAIL_ADDRESS, R.string.error_email);
- if (etFeedbackContent.getText().toString().length() == 0) {
- etFeedbackContent.setError(getResources().getString(R.string.error_empty));
- validateContent = false;
- }
- if (mAwesomeValidation.validate() && validateContent) {
- loading = ProgressDialog.show(getContext(), getResources().getString(R.string.progress_send), getResources().getString(R.string.progress_wait), false, false);
- new sendFeedback(emailTo, subject, includeDeviceInfo(body)).execute();
- etFeedbackEmail.setText("");
- etFeedbackContent.setText("");
- }
- } else {
- Toast.makeText(getContext(), "Please Connect to Internet and Try Again.", Toast.LENGTH_SHORT).show();
- }
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- private boolean isOnline(Context context) {
- ConnectivityManager cm =
- (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo netInfo = cm.getActiveNetworkInfo();
- if (netInfo != null && netInfo.isConnectedOrConnecting()) {
- return true;
- }
- return false;
- }
- private String includeDeviceInfo(String body) {
- String deviceInfo = "Device Info : "
- + "\nOS & API : " + currentVersion()
- + "\nBrand : " + Build.BRAND
- + "\nManufacturer : " + Build.MANUFACTURER
- + "\nDevice : " + Build.DEVICE
- + "\nHardware : " + Build.HARDWARE
- + "\nModel : " + Build.MODEL
- + "\nApplication Version : " + BuildConfig.VERSION_NAME + "\n\n";
- return deviceInfo + body;
- }
- private String currentVersion() {
- double release = Double.parseDouble(Build.VERSION.RELEASE.substring(0, 1));
- String codeName = "Unsupported";//below Jelly bean OR above Oreo
- if (release >= 4.1 && release < 4.4) codeName = "Jelly Bean";
- else if (release < 5) codeName = "Kit Kat";
- else if (release < 6) codeName = "Lollipop";
- else if (release < 7) codeName = "Marshmallow";
- else if (release < 8) codeName = "Nougat";
- else if (release < 9) codeName = "Oreo";
- return codeName + " v" + Build.VERSION.RELEASE + ", API Level: " + Build.VERSION.SDK_INT;
- }
- private class sendFeedback extends AsyncTask<Void, Void, Boolean> {
- private String emailTo, subject, body;
- final String username = "lamkl-wa14@student.tarc.edu.my";
- final String password = "zx32as65qw98";
- public sendFeedback(String emailTo, String subject, String body) {
- this.emailTo = emailTo;
- this.subject = subject;
- this.body = body;
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected Boolean doInBackground(Void... arg0) {
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(emailTo));
- message.setSubject(subject);
- message.setText(body);
- Transport.send(message);
- return true;
- } catch (MessagingException e) {
- return false;
- }
- }
- @Override
- protected void onPostExecute(Boolean b) {
- super.onPostExecute(b);
- if (b) {
- Toast.makeText(getContext(), "Feedback sent, your feedback is appreciated!", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getContext(), "Error while sending feedback, Please try again.", Toast.LENGTH_SHORT).show();
- }
- loading.dismiss();
- }
- }
- }
Add Comment
Please, Sign In to add comment