Эх сурвалжийг харах

stared using GSON, add email input now responsive, aks if you want to change further connection settings when failed to connect

Noah Vogt 3 жил өмнө
parent
commit
911b78859d

+ 5 - 0
app/build.gradle

@@ -57,6 +57,8 @@ android {
 
 dependencies {
 
+    implementation 'com.google.code.gson:gson:2.8.7'
+
     implementation 'androidx.appcompat:appcompat:1.2.0'
     implementation 'com.google.android.material:material:1.3.0'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
@@ -82,4 +84,7 @@ dependencies {
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+
+
+
 }

+ 19 - 0
app/src/main/java/com/noahvogt/miniprojekt/MailFunctions.java

@@ -55,6 +55,25 @@ public class MailFunctions {
         }
     }
 
+    public static String getImapHostFromEmail(String email) {
+        String topLevelHost = email.substring(email.lastIndexOf("@") + 1);
+        if (topLevelHost.endsWith("edubs.ch")) {
+            return "teamwork.edubs.ch";
+        } else {
+            return "imap." + topLevelHost;
+        }
+    }
+
+    public static String getSmtpHostFromEmail(String email) {
+        String topLevelHost = email.substring(email.lastIndexOf("@") + 1);
+        if (topLevelHost.equals("noahvogt.com")) {
+            return "mail.noahvogt.com";
+        } else {
+            return "smtp." + topLevelHost;
+        }
+    }
+
+
     public static boolean validateEmail(EditText emailAddress) {
         String email = emailAddress.getText().toString().trim();
 

+ 20 - 0
app/src/main/java/com/noahvogt/miniprojekt/MailServerCredentials.java

@@ -0,0 +1,20 @@
+package com.noahvogt.miniprojekt;
+
+public class MailServerCredentials {
+    private String mImapHost;
+    private String mSmtpHost;
+    private String mUsername;
+    private String mPassword;
+    private int mImapPort;
+    private String mName;
+
+    public MailServerCredentials(String name, String username, String password, String imapHost, String smtpHost, int imapPort) {
+        mName = name;
+        mUsername = username;
+        mPassword = password;
+
+        mImapHost = imapHost;
+        mImapPort = imapPort;
+        mSmtpHost = smtpHost;
+    }
+}

+ 77 - 12
app/src/main/java/com/noahvogt/miniprojekt/MainActivity.java

@@ -1,6 +1,7 @@
 package com.noahvogt.miniprojekt;
 
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
@@ -17,6 +18,7 @@ import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.fragment.app.DialogFragment;
 
 
+import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 import com.google.android.material.floatingactionbutton.FloatingActionButton;
 import com.noahvogt.miniprojekt.ui.DataBase.Message;
 import com.noahvogt.miniprojekt.ui.home.CustomAdapter;
@@ -43,8 +45,14 @@ import com.noahvogt.miniprojekt.ui.show.MessageShowFragment;
 import com.noahvogt.miniprojekt.ui.slideshow.EmailViewModel;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+import com.google.gson.Gson;
+
+
 
 import static com.noahvogt.miniprojekt.R.id.drawer_layout;
 
@@ -59,7 +67,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
     private AlertDialog dialog;
     private EditText newemail_name, newemail_email, newemail_password; /* may not be private */
 
-    SharedPreferences preferences;
+    SharedPreferences preferences, mailServerCredentials;
 
     /* empty descriptor */
     public MainActivity() {}
@@ -108,6 +116,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
 
         /* invoke preferences */
         preferences = getSharedPreferences("UserPreferences", Context.MODE_PRIVATE);
+        mailServerCredentials = getSharedPreferences("Credentials", Context.MODE_PRIVATE);
 
         /* invoke toolbar */
         Toolbar toolbar = findViewById(R.id.toolbar);
@@ -227,7 +236,40 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
     /* better leave empty to avoid any listener disambiguity */
     public void onClick(View view) { }
 
+    public void changeMailServerSettingsDialog() {
+        // define View window
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
+        final View changeMailServerSettingsView = getLayoutInflater().inflate(R.layout.mail_credentials_customizer, null);
 
+        // open View window
+        dialogBuilder.setView(changeMailServerSettingsView);
+        dialog = dialogBuilder.create();
+        dialog.show();
+    }
+
+    public void askForChangeMailServerSettingsDialog() {
+        // 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();
+                    }
+                })
+                .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 void createNewEmailDialog(){
         /* define View window */
@@ -251,6 +293,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
         dialog.show();
 
         SharedPreferences.Editor preferencesEditor = preferences.edit();
+        SharedPreferences.Editor credentialsEditor = mailServerCredentials.edit();
 
         if (! Python.isStarted()) {
             Python.start(new AndroidPlatform(this));
@@ -272,18 +315,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
 
                 /* connect to mail server and print various debugging output */
                 showToast("Probe Connection ...");
-                if (MailFunctions.canConnect(name, email, password) == Boolean.TRUE) {
+                if (MailFunctions.canConnect(MailFunctions.getImapHostFromEmail(email), email, password) == Boolean.TRUE) {
                     showToast("was able to connect");
-                    List l =  MailFunctions.listMailboxes(MailFunctions.getIMAPConnection(name, email, password));
+                    List l =  MailFunctions.listMailboxes(MailFunctions.getIMAPConnection(MailFunctions.getImapHostFromEmail(email), email, password));
                     for (int i = 0; i < l.size(); i++) {
                         showToast(l.get(i).toString());
                         // TODO: select right folder to store, Synchronization
                         /*gives list of Message Objects/dictionaries */
-                        List p = MailFunctions.fetchMailsFromBox(MailFunctions.getIMAPConnection(name, email, password), l.get(i).toString(), "list");
+                        /*List p = MailFunctions.fetchMailsFromBox(MailFunctions.getIMAPConnection(name, email, password), l.get(i).toString(), "list");
                         System.out.println(l.get(i).toString());
-                        System.out.println(p);
+                        System.out.println(p);*/
                     }
 
+
                     /*Message word = new Message(
                             messageCreateFragment.replyIntent.getStringExtra(messageCreateFragment.EXTRA_TO),
                             null,
@@ -296,23 +340,44 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
                     mEmailViewModel.insert(word);
 
                      */
+
+
                     preferencesEditor.putString("name", name);
                     preferencesEditor.putString("email", email);
                     preferencesEditor.putString("password", password);
                     preferencesEditor.apply();
-                } else {
-                    showToast("failed to connect");
 
-                    /* show all strings the user gave, this will later be stored to a secure database and checked for validation */
-                    showToast(name);
-                    showToast(email);
-                    showToast(password);
-                }
+                    /* ArrayList<String> newUserSettings = new ArrayList<String>();
+                    newUserSettings.add(name);
+                    newUserSettings.add(email);
+                    newUserSettings.add(password);
+                    System.out.println("newUserSettings: " + newUserSettings);
 
 
+                    //Retrieve the values
+                    Set<String> oldSet = mailServerCredentials.getStringSet("UserSettings", null);
+
+                    //Set the values
+                    Set<String> newSet = new HashSet<String>();
+                    newSet.addAll(newUserSettings);
+                    credentialsEditor.putStringSet("UserSettings", newSet);
+                    credentialsEditor.commit();*/
+
+                    Gson gson = new Gson();
+                    MailServerCredentials newMailServerCredentials = new MailServerCredentials(
+                            name, password, email, MailFunctions.getImapHostFromEmail(email), MailFunctions.getSmtpHostFromEmail(email), 993);
+                    String newCredentialsJson = gson.toJson(newMailServerCredentials);
+                    System.out.println(newCredentialsJson);
+                    credentialsEditor.putString("data", newCredentialsJson);
+                    credentialsEditor.apply();
+
+                } else {
+                    askForChangeMailServerSettingsDialog();
+                }
             }
         });
 
+
         newemail_cancel_button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

+ 154 - 0
app/src/main/res/layout/mail_credentials_customizer.xml

@@ -0,0 +1,154 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <TextView
+        android:id="@+id/textView3"
+
+        style="@style/TextAppearance.AppCompat.Widget.TextView.SpinnerItem"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="32dp"
+        android:layout_marginLeft="32dp"
+        android:layout_marginTop="32dp"
+        android:layout_marginEnd="32dp"
+        android:layout_marginRight="32dp"
+
+        android:gravity="center_horizontal"
+        android:text="Customizing Email Connection Settings"
+        android:textSize="24sp"
+
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_marginTop="128dp"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/textView3">
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="vertical">
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:id="@+id/custom_mail_server_incoming_server_layout"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="32dp"
+                android:layout_marginLeft="32dp"
+                android:layout_marginTop="32dp"
+                android:layout_marginEnd="32dp"
+                android:layout_marginRight="32dp"
+
+                android:hint="Incoming Server"
+
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="parent">
+
+                <com.google.android.material.textfield.TextInputEditText
+                    android:id="@+id/custom_mail_server_incoming_server_text"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content" />
+            </com.google.android.material.textfield.TextInputLayout>
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:id="@+id/custom_mail_server_outgoing_server_layout"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="32dp"
+                android:layout_marginLeft="32dp"
+                android:layout_marginTop="32dp"
+                android:layout_marginEnd="32dp"
+                android:layout_marginRight="32dp"
+
+                android:hint="Outgoing Server"
+
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/custom_mail_server_incoming_server_layout">
+
+                <com.google.android.material.textfield.TextInputEditText
+                    android:id="@+id/custom_mail_server_outgoing_server_text"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content" />
+            </com.google.android.material.textfield.TextInputLayout>
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:id="@+id/custom_mail_server_incoming_port_layout"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="32dp"
+                android:layout_marginLeft="32dp"
+                android:layout_marginTop="32dp"
+                android:layout_marginEnd="32dp"
+                android:layout_marginRight="32dp"
+
+                android:hint="Incoming Port"
+
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/custom_mail_server_outgoing_server_layout">
+
+                <com.google.android.material.textfield.TextInputEditText
+                    android:id="@+id/custom_mail_server_incoming_port_text"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content" />
+            </com.google.android.material.textfield.TextInputLayout>
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:id="@+id/custom_mail_server_outgoing_port_layout"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="32dp"
+                android:layout_marginLeft="32dp"
+                android:layout_marginTop="32dp"
+                android:layout_marginEnd="32dp"
+                android:layout_marginRight="32dp"
+
+                android:hint="Outgoing Port"
+
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/custom_mail_server_incoming_port_layout">
+
+                <com.google.android.material.textfield.TextInputEditText
+                    android:id="@+id/custom_mail_server_outgoing_port_text"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content" />
+            </com.google.android.material.textfield.TextInputLayout>
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:id="@+id/custom_mail_server_username_layout"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginStart="32dp"
+                android:layout_marginLeft="32dp"
+                android:layout_marginTop="32dp"
+                android:layout_marginEnd="32dp"
+                android:layout_marginRight="32dp"
+
+                android:hint="Server Username"
+
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/custom_mail_server_outgoing_port_layout">
+
+                <com.google.android.material.textfield.TextInputEditText
+                    android:id="@+id/custom_mail_server_username_text"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content" />
+            </com.google.android.material.textfield.TextInputLayout>
+
+        </LinearLayout>
+    </ScrollView>
+</androidx.constraintlayout.widget.ConstraintLayout>