Java tutorial
/* * Copyright 2015 Kostiantyn Aloshychev * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ua.boberproduction.bbr.feedback; import android.app.Fragment; import android.content.res.ColorStateList; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import ua.boberproduction.bbr.MainActivity; import ua.boberproduction.bbr.R; import ua.boberproduction.bbr.util.Utils; /** * UI for sending feedback to the app developers through email. */ public class FeedbackFragment extends Fragment { private EditText nameEditText; private EditText emailEditText; private EditText textEditText; private FloatingActionButton sendButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); setRetainInstance(true); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_feedback, container, false); nameEditText = (EditText) view.findViewById(R.id.feedback_name); emailEditText = (EditText) view.findViewById(R.id.feedback_email); textEditText = (EditText) view.findViewById(R.id.feedback_text); sendButton = (FloatingActionButton) view.findViewById(R.id.send_feedback_btn); ActionBar actionbar = ((MainActivity) getActivity()).getSupportActionBar(); if (actionbar != null) { actionbar.setSubtitle(getString(R.string.action_feedback)); } checkFieldsValidAndActivateButton(); setFieldsTextChangeListeners(); sendButton.setOnClickListener(v -> { if (Utils.isConnectedToNetwork()) { new SendMailTask().execute(); } else { Toast.makeText(getActivity(), getString(R.string.error_network_connection_false), Toast.LENGTH_SHORT).show(); } }); return view; } private void sendMail() { String subject = getString(R.string.app_name) + " feedback"; String body = textEditText.getText().toString(); String email = emailEditText.getText().toString(); String name = nameEditText.getText().toString(); String receiver = "boberproduction@gmail.com"; try { GMailSender sender = new GMailSender(getString(R.string.mail_login), getString(R.string.mail_password)); sender.sendMail(subject, body + "\n\n" + name + "\n" + email, "boberproduction@gmail.com", receiver); } catch (Exception e) { Log.e("SendMail", e.getMessage(), e); } } // check if the text fields contain valid data private void checkFieldsValidAndActivateButton() { if (nameEditText != null) { if (nameEditText.getText().toString().equals("")) { disableSendButton(); return; } } if (emailEditText != null) { if (!android.util.Patterns.EMAIL_ADDRESS.matcher(emailEditText.getText().toString()).matches()) { disableSendButton(); return; } } if (textEditText != null) { if (textEditText.getText().toString().equals("")) { disableSendButton(); return; } } activateSendButton(); } private void activateSendButton() { sendButton.setEnabled(true); sendButton.setBackgroundTintList( ColorStateList.valueOf(ContextCompat.getColor(getActivity(), R.color.colorPrimary))); } private void disableSendButton() { sendButton.setEnabled(false); sendButton.setBackgroundTintList( ColorStateList.valueOf(ContextCompat.getColor(getActivity(), R.color.save_btn_disabled))); } private void setFieldsTextChangeListeners() { nameEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { checkFieldsValidAndActivateButton(); } }); emailEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { checkFieldsValidAndActivateButton(); } }); textEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { checkFieldsValidAndActivateButton(); } }); } // make all widgets uneditable private void disableFields() { nameEditText.setFocusable(false); nameEditText.setBackgroundResource(R.drawable.edittext_outline_disabled); emailEditText.setFocusable(false); emailEditText.setBackgroundResource(R.drawable.edittext_outline_disabled); textEditText.setFocusable(false); textEditText.setBackgroundResource(R.drawable.edittext_outline_disabled); } private class SendMailTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { disableSendButton(); disableFields(); Utils.hideSoftKeyboard(getActivity()); Toast.makeText(getActivity(), getString(R.string.feedback_sending_toast), Toast.LENGTH_SHORT).show(); super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { sendMail(); return null; } @Override protected void onPostExecute(Void aVoid) { if (getActivity().getFragmentManager().getBackStackEntryCount() > 0) { getFragmentManager().popBackStack(); Toast.makeText(getActivity(), getString(R.string.feedback_sent_toast), Toast.LENGTH_SHORT).show(); } super.onPostExecute(aVoid); } } }