123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- package com.noahvogt.miniprojekt;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupMenu;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.widget.Toolbar;
- import androidx.drawerlayout.widget.DrawerLayout;
- import androidx.fragment.app.DialogFragment;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import com.google.gson.GsonBuilder;
- import com.google.gson.reflect.TypeToken;
- import com.noahvogt.miniprojekt.DataBase.Message;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import androidx.lifecycle.ViewModelProvider;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import com.google.android.material.navigation.NavigationView;
- import androidx.navigation.NavController;
- import androidx.navigation.Navigation;
- import androidx.navigation.ui.AppBarConfiguration;
- import androidx.navigation.ui.NavigationUI;
- import androidx.work.Data;
- import com.chaquo.python.Python;
- import com.chaquo.python.android.AndroidPlatform;
- import com.google.android.material.snackbar.Snackbar;
- import com.noahvogt.miniprojekt.data.CustomAdapter;
- import com.noahvogt.miniprojekt.data.EmailViewModel;
- import com.noahvogt.miniprojekt.data.MailFunctions;
- import com.noahvogt.miniprojekt.workers.DownloadWorker;
- import com.noahvogt.miniprojekt.ui.show.MessageShowFragment;
- import com.noahvogt.miniprojekt.data.BooleanTypeAdapter;
- import java.lang.reflect.Type;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.google.gson.Gson;
- import static com.noahvogt.miniprojekt.R.id.drawer_layout;
- import org.w3c.dom.Text;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener, CustomAdapter.SelectedMessage, PopupMenu.OnMenuItemClickListener {
- private AppBarConfiguration mAppBarConfiguration;
- public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
- public static final String emailData = "Email";
- public static final String passwordData = "Password";
- public static final String nameData = "Name";
- public static EmailViewModel mEmailViewModel;
- public static RecyclerView recyclerView;
- private AlertDialog dialog;
- private EditText newemail_name, newemail_email, newemail_password; /* may not be private */
- SharedPreferences mailServerCredentials;
- /* leave descriptor empty */
- public MainActivity() {}
- public String getVisibleFragment(){
- FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
- List<Fragment> fragments = fragmentManager.getFragments();
- if(fragments != null){
- showToast("not null");
- for(Fragment fragment : fragments){
- showToast(fragment.toString());
- if(fragment.isVisible())
- showToast("found visible fragment");
- return "is gallery";
- }
- } else {
- showToast("null");}
- return null;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /* define button listeners */
- /*creates account manager by clicking on profile */
- View accountView = findViewById(R.id.accountView);
- accountView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /* define View window */
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
- final View accountManagerView = getLayoutInflater().inflate(R.layout.account_manager, null);
- AutoCompleteTextView accountSelectorObject =
- (AutoCompleteTextView) accountManagerView.findViewById(R.id.accountSelectorTextView);
- /* get string data for drop down menu */
- String[] dummyMails = getResources().getStringArray(R.array.dummy_emails);
- ArrayAdapter<String> dropDownAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.dropdown_item,
- R.id.textViewDropDownItem, dummyMails);
- accountSelectorObject.setAdapter(dropDownAdapter);
- /* open View window */
- dialogBuilder.setView(accountManagerView);
- dialog = dialogBuilder.create();
- dialog.show();
- }
- });
- /* invoke toolbar */
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- /* invoke drawer */
- DrawerLayout drawer = findViewById(drawer_layout);
- NavigationView navigationView = findViewById(R.id.nav_view);
- View headerView = navigationView.getHeaderView(0);
- /*
- Passing each menu ID as a set of Ids because each
- menu should be considered as top level destinations.
- */
- mAppBarConfiguration = new AppBarConfiguration.Builder(
- R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_archive, R.id.nav_spam)
- .setDrawerLayout(drawer)
- .build();
- NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
- NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
- NavigationUI.setupWithNavController(navigationView, navController);
- /* Lookup the recyclerview in activity layout */
- recyclerView = findViewById(R.id.recyclerView);
- final CustomAdapter adapter = new CustomAdapter(new CustomAdapter.EmailDiff(),this);
- /* Attach the adapter to the recyclerview to populate items */
- recyclerView.setAdapter(adapter);
- recyclerView.setLayoutManager(new LinearLayoutManager(this));
- /* get Inbox Messages in Recyclerviewer at begining is overwritten by Fragments but has to stay*/
- mEmailViewModel = new ViewModelProvider(this).get(EmailViewModel.class);
- mEmailViewModel.getInboxMessage().observe(this, messages -> {
- /* Update the cached copy of the messages in the adapter*/
- adapter.submitList(messages);
- });
- updateNavHeaderText(headerView);
- Button settingButton = findViewById(R.id.settingsButton);
- settingButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(MainActivity.this, SettingsActivity.class);
- startActivity(i);
- }
- });
- Button add_email_button = (Button) findViewById(R.id.addEmailButton);
- add_email_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- createNewEmailDialog(headerView);
- }
- });
- /* Start email Writer*/
- FloatingActionButton message_create_button = findViewById(R.id.messageButton);
- message_create_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- DialogFragment dialog = MessageCreateFragment.newInstance();
- dialog.show(getSupportFragmentManager(), "tag");
- }
- });
- /* start python instance right on startup */
- if (! Python.isStarted()) {
- Python.start(new AndroidPlatform(this));
- }
- }
- /* gets the data from the Email writer and adds it to the Database */
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, MessageCreateFragment.replyIntent);
- /* Creates class for the Date when Email is written */
- Date dNow = new Date();
- SimpleDateFormat ft =
- new SimpleDateFormat("dd.MM.yy");
- System.out.println(dNow);
- // if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
- Message word = new Message(
- MessageCreateFragment.replyIntent.getStringExtra(MessageCreateFragment.EXTRA_TO),
- null,
- null,
- MessageCreateFragment.replyIntent.getStringExtra(MessageCreateFragment.EXTRA_FROM),
- ft.format(dNow),
- MessageCreateFragment.replyIntent.getStringExtra(MessageCreateFragment.EXTRA_SUBJECT),
- MessageCreateFragment.replyIntent.getStringExtra(MessageCreateFragment.EXTRA_MESSAGE),
- "Draft",false);
- mEmailViewModel.insert(word);
- }
- /* set nav header name + email to current user data */
- public void updateNavHeaderText(View headerView) {
- mailServerCredentials = getSharedPreferences("Credentials", Context.MODE_PRIVATE);
- TextView navHeaderNameObject = (TextView) headerView.findViewById(R.id.navHeaderName);
- TextView navHeaderEmailObject = (TextView) headerView.findViewById(R.id.navHeaderEmail);
- String startupUser = mailServerCredentials.getString("currentUser","");
- if (!startupUser.isEmpty()) {
- Gson gson = new Gson();
- String startupJsonData = mailServerCredentials.getString("data", "");
- Type credentialsType = new TypeToken<ArrayList<MailServerCredentials>>(){}.getType();
- ArrayList<MailServerCredentials> startupUsersCredentials = gson.fromJson(startupJsonData, credentialsType);
- for (int i = 0; i < startupUsersCredentials.size(); i++) {
- if (startupUsersCredentials.get(i).getUsername().equals(startupUser)) {
- navHeaderNameObject.setText(startupUser);
- navHeaderEmailObject.setText(startupUsersCredentials.get(i).getName());
- break;
- }
- }
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- /* Inflate the menu; this adds items to the action bar if it is present. */
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onSupportNavigateUp() {
- NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
- return NavigationUI.navigateUp(navController, mAppBarConfiguration)
- || super.onSupportNavigateUp();
- }
- /* better leave empty to avoid any listener disambiguity */
- public void onClick(View view) { }
- public void changeMailServerSettingsDialog(String name, String email, String password, View headerView) {
- /* define View window */
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
- final View changeMailServerSettingsView = getLayoutInflater().inflate(R.layout.mail_credentials_customizer, null);
- /* access objects */
- EditText incomingServerObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_incoming_server_text);
- EditText outgoingServerObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_outgoing_server_text);
- EditText incomingPortObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_incoming_port_text);
- EditText outgoingPortObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_outgoing_port_text);
- EditText serverUsernameObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_username_text);
- EditText passwordObject = (EditText) changeMailServerSettingsView.findViewById(R.id.custom_mail_server_password_text);
- Button saveButton = (Button) changeMailServerSettingsView.findViewById(R.id.saveCustomizeButton);
- Button cancelButton = (Button) changeMailServerSettingsView.findViewById(R.id.cancelCustomizeButton);
- /* set assumed input in corresponding fields */
- incomingServerObject.setText(MailFunctions.getImapHostFromEmail(email));
- outgoingServerObject.setText(MailFunctions.getSmtpHostFromEmail(email));
- incomingPortObject.setText("993");
- outgoingPortObject.setText("587");
- serverUsernameObject.setText(email);
- passwordObject.setText(password);
- /* TODO: add save and cancel button functionality */
- /* open View window */
- dialogBuilder.setView(changeMailServerSettingsView);
- dialog = dialogBuilder.create();
- dialog.show();
- saveButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- addNewAccountCredentials(name, serverUsernameObject.getText().toString(), passwordObject.getText().toString(),
- Integer.parseInt(incomingPortObject.getText().toString()), Integer.parseInt(outgoingPortObject.getText().toString()),
- incomingServerObject.getText().toString(), outgoingServerObject.getText().toString(), dialog, false,
- headerView);
- }
- });
- cancelButton.setOnClickListener(v -> dialog.dismiss());
- }
- public void askForChangeMailServerSettingsDialog(String name, String email, String password, View headerView) {
- /* define View window */
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
- /* open View window */
- dialogBuilder.setTitle("failed to connect :(");
- dialogBuilder
- .setMessage("Do you want to further customize your mail server settings?")
- .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- /*if this button is clicked, close the whole fragment */
- changeMailServerSettingsDialog(name, email, password, headerView);
- }
- })
- .setNegativeButton("No",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- /* if this button is clicked, close the hole fragment */
- dialog.dismiss();
- }
- });
- dialog = dialogBuilder.create();
- dialog.show();
- }
- public static MailServerCredentials newMailServerCredentials;
- public static SharedPreferences.Editor credentialsEditor;
- public void addNewAccountCredentials(String name, String email, String password, int imapPort,
- int smtpPort, String imapHost, String smtpHost, DialogInterface dialogContext,
- boolean wantConnectionFailedDialog, View headerView) {
- credentialsEditor = mailServerCredentials.edit();
- /* connect to mail server */
- showToast("Probe Connection ...");
- if (MailFunctions.canConnect(MailFunctions.getImapHostFromEmail(email), email, password) == Boolean.TRUE) {
- showToast("was able to connect");
- Gson gson = new Gson();
- /* read login credentials from SharedPreferences */
- SharedPreferences initialCredentialsReader = getSharedPreferences(
- "Credentials", Context.MODE_PRIVATE);
- String initialReadJsonData = initialCredentialsReader.getString("data", "");
- Type credentialsType = new TypeToken<ArrayList<MailServerCredentials>>(){}.getType();
- ArrayList<MailServerCredentials> allUsersCredentials = gson.fromJson(initialReadJsonData, credentialsType);
- /* check for unique email */
- boolean newEmailIsUnique = true;
- try {
- for (int i = 0; i < allUsersCredentials.size(); i++) {
- if (allUsersCredentials.get(i).getUsername().equals(email)) {
- newEmailIsUnique = false;
- break;
- }
- }
- } catch (NullPointerException e) {
- System.out.println("creating new arraylist for user credentials, as it seems to be empty");
- allUsersCredentials = new ArrayList<>();
- }
- /* add new email account if the email hasn't been entered before */
- if (newEmailIsUnique) {
- allUsersCredentials.add(new MailServerCredentials(name, email, password, imapHost, smtpHost, imapPort,
- smtpPort, ""));
- credentialsEditor.putString("data", gson.toJson(allUsersCredentials, credentialsType));
- credentialsEditor.putString("currentUser", email);
- credentialsEditor.apply();
- showToast("Success: added new email account");
- updateNavHeaderText(headerView);
- dialogContext.dismiss();
- } else {
- showToast("Error: cannot add the same email twice");
- }
- } else {
- if (wantConnectionFailedDialog)
- askForChangeMailServerSettingsDialog(name, email, password, headerView);
- else
- showToast("Error: failed to get connection");
- }
- }
- public void createNewEmailDialog(View headerView){
- /* define View window */
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
- final View emailPopupView = getLayoutInflater().inflate(R.layout.popup, null);
- /* init text field variables */
- newemail_name = emailPopupView.findViewById(R.id.popup_material_name_asking_text);
- newemail_email = emailPopupView.findViewById(R.id.popup_material_email_asking_text);
- newemail_password = emailPopupView.findViewById(R.id.popup_material_password_asking_text);
- /* init button variables */
- Button newemail_save_button = (Button) emailPopupView.findViewById(R.id.saveButton);
- /* may not be private */
- Button newemail_cancel_button = (Button) emailPopupView.findViewById(R.id.cancelButton);
- /* open View window */
- dialogBuilder.setView(emailPopupView);
- dialog = dialogBuilder.create();
- dialog.show();
- credentialsEditor = mailServerCredentials.edit();
- /* store user input */
- newemail_save_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /* store user input */
- String name = newemail_name.getText().toString();
- String email = newemail_email.getText().toString();
- String password = newemail_password.getText().toString();
- /* validate user input before connecting */
- if (!MailFunctions.validateEmail(newemail_email) | !MailFunctions.validateName(newemail_name) |
- !MailFunctions.validatePassword(newemail_password)) {
- return;
- }
- addNewAccountCredentials(name, email, password, 993, 587, MailFunctions.getImapHostFromEmail(email),
- MailFunctions.getSmtpHostFromEmail(email), dialog, true, headerView);
- }});
- newemail_cancel_button.setOnClickListener(v -> dialog.dismiss());
- }
- /* show debug output in specific view */
- private void showSnackbar(View View, String text) {
- Snackbar.make(View, text, Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- /* like showSnackbar(), but global and uglier */
- private void showToast(String text) {
- Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void selectedMessage(Message messages, EmailViewModel emailViewModel) {
- DialogFragment dialog = MessageShowFragment.newInstance(messages, mEmailViewModel);
- dialog.show(getSupportFragmentManager(), "tag");
- }
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- switch (item.getItemId()){
- case R.id.action_refresh:
- return true;
- }
- return false;
- }
- }
|