MainActivity.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.noahvogt.miniprojekt;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. import androidx.appcompat.app.AlertDialog;
  12. import androidx.appcompat.app.AppCompatActivity;
  13. import androidx.appcompat.widget.Toolbar;
  14. import androidx.drawerlayout.widget.DrawerLayout;
  15. import androidx.fragment.app.DialogFragment;
  16. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  17. import com.noahvogt.miniprojekt.DataBase.Message;
  18. import androidx.fragment.app.Fragment;
  19. import androidx.fragment.app.FragmentManager;
  20. import androidx.lifecycle.ViewModelProvider;
  21. import androidx.recyclerview.widget.LinearLayoutManager;
  22. import androidx.recyclerview.widget.RecyclerView;
  23. import com.google.android.material.navigation.NavigationView;
  24. import androidx.navigation.NavController;
  25. import androidx.navigation.Navigation;
  26. import androidx.navigation.ui.AppBarConfiguration;
  27. import androidx.navigation.ui.NavigationUI;
  28. import com.chaquo.python.Python;
  29. import com.chaquo.python.android.AndroidPlatform;
  30. import com.google.android.material.snackbar.Snackbar;
  31. import com.noahvogt.miniprojekt.data.CustomAdapter;
  32. import com.noahvogt.miniprojekt.data.EmailViewModel;
  33. import com.noahvogt.miniprojekt.data.MailFunctions;
  34. import com.noahvogt.miniprojekt.ui.show.MessageShowFragment;
  35. import java.text.SimpleDateFormat;
  36. import java.util.Date;
  37. import java.util.List;
  38. import static com.noahvogt.miniprojekt.R.id.drawer_layout;
  39. public class MainActivity extends AppCompatActivity implements View.OnClickListener, CustomAdapter.SelectedMessage {
  40. private AppBarConfiguration mAppBarConfiguration;
  41. public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
  42. public static EmailViewModel mEmailViewModel;
  43. public static RecyclerView recyclerView;
  44. private AlertDialog dialog;
  45. private EditText newemail_name, newemail_email, newemail_password; /* may not be private */
  46. SharedPreferences preferences;
  47. /* empty descriptor */
  48. public MainActivity() {}
  49. public String getVisibleFragment(){
  50. FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
  51. List<Fragment> fragments = fragmentManager.getFragments();
  52. if(fragments != null){
  53. showToast("not null");
  54. for(Fragment fragment : fragments){
  55. showToast(fragment.toString());
  56. if(fragment.isVisible())
  57. showToast("found visible fragment");
  58. return "is gallery";
  59. }
  60. } else {
  61. showToast("null");}
  62. return null;
  63. }
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_main);
  68. /* define button listeners */
  69. Button add_email_button = (Button) findViewById(R.id.addEmailButton);
  70. add_email_button.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View v) {
  73. createNewEmailDialog();
  74. }
  75. });
  76. /*creates accountmanager by clicking on profil */
  77. View accountView = findViewById(R.id.accountView);
  78. accountView.setOnClickListener(new View.OnClickListener() {
  79. @Override
  80. public void onClick(View v) {
  81. createNewEmailDialog();
  82. }
  83. });
  84. /* invoke preferences */
  85. preferences = getSharedPreferences("UserPreferences", Context.MODE_PRIVATE);
  86. /* invoke toolbar */
  87. Toolbar toolbar = findViewById(R.id.toolbar);
  88. setSupportActionBar(toolbar);
  89. /* invoke drawer */
  90. DrawerLayout drawer = findViewById(drawer_layout);
  91. NavigationView navigationView = findViewById(R.id.nav_view);
  92. /*
  93. Passing each menu ID as a set of Ids because each
  94. menu should be considered as top level destinations.
  95. */
  96. mAppBarConfiguration = new AppBarConfiguration.Builder(
  97. R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_archive, R.id.nav_spam)
  98. .setDrawerLayout(drawer)
  99. .build();
  100. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  101. NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
  102. NavigationUI.setupWithNavController(navigationView, navController);
  103. /* Lookup the recyclerview in activity layout */
  104. recyclerView = findViewById(R.id.recyclerView);
  105. final CustomAdapter adapter = new CustomAdapter(new CustomAdapter.EmailDiff(),this);
  106. /* Attach the adapter to the recyclerview to populate items */
  107. recyclerView.setAdapter(adapter);
  108. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  109. /* get Inbox Messages in Recyclerviewer at begining is overwritten by Fragments but has to stay*/
  110. mEmailViewModel = new ViewModelProvider(this).get(EmailViewModel.class);
  111. mEmailViewModel.getInboxMessage().observe(this, messages -> {
  112. /* Update the cached copy of the messages in the adapter*/
  113. adapter.submitList(messages);
  114. });
  115. Button settingButton = findViewById(R.id.settingsButton);
  116. settingButton.setOnClickListener(new View.OnClickListener() {
  117. @Override
  118. public void onClick(View v) {
  119. Intent i = new Intent(MainActivity.this, SettingsActivity.class);
  120. startActivity(i);
  121. }
  122. });
  123. /* Start email Writer*/
  124. FloatingActionButton message_create_button = findViewById(R.id.messageButton);
  125. message_create_button.setOnClickListener(new View.OnClickListener() {
  126. @Override
  127. public void onClick(View v) {
  128. DialogFragment dialog = messageCreateFragment.newInstance();
  129. dialog.show(getSupportFragmentManager(), "tag");
  130. }
  131. });
  132. }
  133. /* gets the data from the Email writer and adds it to the Database */
  134. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  135. super.onActivityResult(requestCode, resultCode, messageCreateFragment.replyIntent);
  136. /* Creates class for the Date when Email is written */
  137. Date dNow = new Date();
  138. SimpleDateFormat ft =
  139. new SimpleDateFormat("dd.MM.yy");
  140. // if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
  141. Message word = new Message(
  142. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_TO),
  143. null,
  144. null,
  145. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_FROM),
  146. ft.format(dNow),
  147. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_SUBJECT),
  148. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_MESSAGE),
  149. "Draft",false);
  150. mEmailViewModel.insert(word);
  151. }
  152. @Override
  153. public boolean onCreateOptionsMenu(Menu menu) {
  154. /* Inflate the menu; this adds items to the action bar if it is present. */
  155. getMenuInflater().inflate(R.menu.main, menu);
  156. return true;
  157. }
  158. @Override
  159. public boolean onSupportNavigateUp() {
  160. NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  161. return NavigationUI.navigateUp(navController, mAppBarConfiguration)
  162. || super.onSupportNavigateUp();
  163. }
  164. /* better leave empty to avoid any listener disambiguity */
  165. public void onClick(View view) { }
  166. public void createNewEmailDialog(){
  167. /* define View window */
  168. AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
  169. final View emailPopupView = getLayoutInflater().inflate(R.layout.popup, null);
  170. /* init text field variables */
  171. newemail_name = emailPopupView.findViewById(R.id.popup_material_name_asking_text);
  172. newemail_email = emailPopupView.findViewById(R.id.popup_material_email_asking_text);
  173. newemail_password = emailPopupView.findViewById(R.id.popup_material_password_asking_text);
  174. /* init button variables */
  175. Button newemail_save_button = (Button) emailPopupView.findViewById(R.id.saveButton);
  176. /* may not be private */
  177. Button newemail_cancel_button = (Button) emailPopupView.findViewById(R.id.cancelButton);
  178. /* open View window */
  179. dialogBuilder.setView(emailPopupView);
  180. dialog = dialogBuilder.create();
  181. dialog.show();
  182. SharedPreferences.Editor preferencesEditor = preferences.edit();
  183. if (! Python.isStarted()) {
  184. Python.start(new AndroidPlatform(this));
  185. }
  186. /* store user input */
  187. newemail_save_button.setOnClickListener(new View.OnClickListener() {
  188. @Override
  189. public void onClick(View v) {
  190. /* store user input (only needed for DEBUGGING) */
  191. String name = newemail_name.getText().toString();
  192. String email = newemail_email.getText().toString();
  193. String password = newemail_password.getText().toString();
  194. if (!MailFunctions.validateEmail(newemail_email) | !MailFunctions.validateName(newemail_name) | !MailFunctions.validatePassword(newemail_password)) {
  195. return;
  196. }
  197. /* connect to mail server and print various debugging output */
  198. showToast("Probe Connection ...");
  199. if (MailFunctions.canConnect(name, email, password) == Boolean.TRUE) {
  200. showToast("was able to connect");
  201. List folders = MailFunctions.listMailboxes(MailFunctions.getIMAPConnection(name, email, password));
  202. for (int i = 0; i < folders.size(); i++) {
  203. showToast(folders.get(i).toString());
  204. // TODO: select right folder to store, Synchronization
  205. /*gives list of Message Objects/dictionaries */
  206. List messages = MailFunctions.fetchMailsFromBox(MailFunctions.getIMAPConnection(name, email, password), folders.get(i).toString(), "list");
  207. System.out.println(folders.get(i).toString());
  208. System.out.println(messages.toString());
  209. for (int k = 0; k < messages.size(); k++) {
  210. System.out.println(messages.get(k));
  211. /*work now, but list of Messages not */
  212. System.out.println(MailFunctions.fetchSubject(k));
  213. System.out.println(MailFunctions.fetchFrom(k));
  214. System.out.println(MailFunctions.fetchCC(k));
  215. System.out.println(MailFunctions.fetchBcc(k));
  216. System.out.println(MailFunctions.fetchTo(k));
  217. System.out.println(MailFunctions.fetchDate(k));
  218. System.out.println(MailFunctions.fetchContent(k));
  219. }
  220. }
  221. /*Message word = new Message(
  222. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_TO),
  223. null,
  224. null,
  225. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_FROM),
  226. ft.format(dNow),
  227. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_SUBJECT),
  228. messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_MESSAGE),
  229. "Draft",false);
  230. mEmailViewModel.insert(word);
  231. */
  232. preferencesEditor.putString("name", name);
  233. preferencesEditor.putString("email", email);
  234. preferencesEditor.putString("password", password);
  235. preferencesEditor.apply();
  236. } else {
  237. showToast("failed to connect");
  238. /* show all strings the user gave, this will later be stored to a secure database and checked for validation */
  239. showToast(name);
  240. showToast(email);
  241. showToast(password);
  242. }
  243. }
  244. });
  245. newemail_cancel_button.setOnClickListener(new View.OnClickListener() {
  246. @Override
  247. public void onClick(View v) {
  248. dialog.dismiss();
  249. }
  250. });
  251. }
  252. /* show debug output in specific view */
  253. private void showSnackbar(View View, String text) {
  254. Snackbar.make(View, text, Snackbar.LENGTH_LONG)
  255. .setAction("Action", null).show();
  256. }
  257. /* like showSnackbar(), but global and uglier */
  258. private void showToast(String text) {
  259. Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
  260. }
  261. @Override
  262. public void selectedMessage(Message messages, EmailViewModel emailViewModel) {
  263. DialogFragment dialog = MessageShowFragment.newInstance(messages, mEmailViewModel);
  264. dialog.show(getSupportFragmentManager(), "tag");
  265. }
  266. }