123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- 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.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupMenu;
- 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;
- 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 preferences, 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 */
- Button add_email_button = (Button) findViewById(R.id.addEmailButton);
- add_email_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- createNewEmailDialog();
- }
- });
- /*creates accountmanager by clicking on profil */
- View accountView = findViewById(R.id.accountView);
- accountView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- createNewEmailDialog();
- }
- });
- /* invoke preferences */
- preferences = getSharedPreferences("UserPreferences", Context.MODE_PRIVATE);
- mailServerCredentials = getSharedPreferences("Credentials", Context.MODE_PRIVATE);
- /* invoke toolbar */
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- /* invoke drawer */
- DrawerLayout drawer = findViewById(drawer_layout);
- NavigationView navigationView = findViewById(R.id.nav_view);
- /*
- 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);
- });
- 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);
- }
- });
- /* 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);
- }
- @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) {
- /* define View window */
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
- final View changeMailServerSettingsView = getLayoutInflater().inflate(R.layout.mail_credentials_customizer, null);
- 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);
- /* 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();
- }
- public void askForChangeMailServerSettingsDialog(String name, String email, String password) {
- /* 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);
- }
- })
- .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 createNewEmailDialog(){
- /* 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();
- SharedPreferences.Editor preferencesEditor = preferences.edit();
- credentialsEditor = mailServerCredentials.edit();
- if (! Python.isStarted()) {
- Python.start(new AndroidPlatform(this));
- }
- /* store user input */
- newemail_save_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /* store user input (only needed for DEBUGGING) */
- String name = newemail_name.getText().toString();
- String email = newemail_email.getText().toString();
- String password = newemail_password.getText().toString();
- Data.Builder builder = new Data.Builder();
- builder.putString(emailData, email)
- .putString(passwordData, password)
- .putString(nameData, name);
- if (!MailFunctions.validateEmail(newemail_email) | !MailFunctions.validateName(newemail_name) | !MailFunctions.validatePassword(newemail_password)) {
- return;
- }
- /* connect to mail server and print various debugging output */
- showToast("Probe Connection ...");
- if (MailFunctions.canConnect(MailFunctions.getImapHostFromEmail(email), email, password) == Boolean.TRUE) {
- showToast("was able to connect");
- /*
- Intent intent = new Intent(getBaseContext(), DownloadWorker.class);
- intent.putExtra("Email", email);
- intent.putExtra("Password", password);
- startActivity(intent);
- */
- //startActivityForResult(intent, MainActivity.NEW_WORD_ACTIVITY_REQUEST_CODE);
- /*
- List folders = MailFunctions.listMailboxes(MailFunctions.getIMAPConnection(MailFunctions.getImapHostFromEmail(email), email, password));
- for (int i = 0; i < folders.size(); i++) {
- showToast(folders.get(i).toString());
- // TODO: select right folder to store, Synchronization
- /*gives list of Message Objects/dictionaries */
- /* List p = MailFunctions.fetchMailsFromBox(
- MailFunctions.getIMAPConnection(
- MailFunctions.getImapHostFromEmail(email), email, password),
- folders.get(i).toString(), "list");
- System.out.println(folders.get(i).toString());
- System.out.println(p);
- }
- =======
- // Intent intent = new Intent(getBaseContext(), ReadInMailsActivity.class);
- // intent.putExtra("Email", email);
- // intent.putExtra("Password", password);
- // startActivity(intent);
- // startActivityForResult(intent, MainActivity.NEW_WORD_ACTIVITY_REQUEST_CODE);
- >>>>>>> 5f6a1c7705d3f893ac179bbc5641f5813f31fb08
- // List folders = MailFunctions.listMailboxes(MailFunctions.getIMAPConnection(MailFunctions.getImapHostFromEmail(email), email, password));
- // for (int i = 0; i < folders.size(); i++) {
- // showToast(folders.get(i).toString());
- // // TODO: select right folder to store, Synchronization
- // /*gives list of Message Objects/dictionaries
- // List p = MailFunctions.fetchMailsFromBox(
- // MailFunctions.getIMAPConnection(
- // MailFunctions.getImapHostFromEmail(email), email, password),
- // folders.get(i).toString(), "list");
- // System.out.println(folders.get(i).toString());
- // System.out.println(p);
- // }
- /* TODO: replace legacy strings down below completely with serialized settings json string */
- preferencesEditor.putString("name", name);
- preferencesEditor.putString("email", email);
- preferencesEditor.putString("password", password);
- preferencesEditor.apply();
- /* init custom gson with hook to parse booleans correctly */
- GsonBuilder gsonBuilder = new GsonBuilder();
- gsonBuilder.registerTypeAdapter(Boolean.class, new BooleanTypeAdapter());
- Gson gson = gsonBuilder.create();
- /* safe mail server login credentials */
- newMailServerCredentials = new MailServerCredentials(
- name, email, password, MailFunctions.getImapHostFromEmail(email), MailFunctions.getSmtpHostFromEmail(email), 993, 587, "");
- String newCredentialsJson = gson.toJson(newMailServerCredentials);
- System.out.println(newCredentialsJson);
- credentialsEditor.putString("data", newCredentialsJson);
- credentialsEditor.apply();
- /* download all messages from mail server */
- /* read login credentials from SharedPreferences */
- SharedPreferences credentialsReader = getSharedPreferences("UserPreferences", Context.MODE_PRIVATE);
- String readJsonData = credentialsReader.getString("data", "");
- MailServerCredentials readMailServerCredentials = gson.fromJson(readJsonData, MailServerCredentials.class);
- dialog.dismiss();
- /*makes request to worker and gives data to it*/
- mEmailViewModel.applyDownload(builder.build());
- /* fetch and print draft messages */
- String fetchedMails = MailFunctions.fetchMailsFromBox(MailFunctions.getIMAPConnection(newMailServerCredentials.getImapHost(),
- newMailServerCredentials.getUsername(), newMailServerCredentials.getPassword(), newMailServerCredentials.getImapPort()), "Drafts");
- System.out.println(fetchedMails);
- /* parse messages in arraylist of Message class and loop through it */
- Type messageType = new TypeToken<ArrayList<Message>>(){}.getType();
- ArrayList<Message> messages = gson.fromJson(fetchedMails, messageType);
- for (int i = 0; i < messages.size(); i++) {
- System.out.println("Message #" + i);
- System.out.println(messages.get(i).toString());
- }
- } else {
- askForChangeMailServerSettingsDialog(name, email, password);
- }
- }
- });
- 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;
- }
- }
|