|
@@ -244,12 +244,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
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));
|
|
@@ -265,6 +268,17 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ cancelButton.setOnClickListener(v -> dialog.dismiss());
|
|
|
}
|
|
|
|
|
|
public void askForChangeMailServerSettingsDialog(String name, String email, String password) {
|
|
@@ -282,7 +296,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
}
|
|
|
})
|
|
|
.setNegativeButton("No",new DialogInterface.OnClickListener() {
|
|
|
- public void onClick(DialogInterface dialog,int id) {
|
|
|
+ public void onClick(DialogInterface dialog, int id) {
|
|
|
/* if this button is clicked, close the hole fragment */
|
|
|
dialog.dismiss();
|
|
|
}
|
|
@@ -294,6 +308,66 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
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) {
|
|
|
+ SharedPreferences.Editor preferencesEditor = preferences.edit();
|
|
|
+ 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");
|
|
|
+
|
|
|
+ /* 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();
|
|
|
+
|
|
|
+ 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");
|
|
|
+ dialogContext.dismiss();
|
|
|
+ } else {
|
|
|
+ showToast("Error: cannot add the same email twice");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (wantConnectionFailedDialog)
|
|
|
+ askForChangeMailServerSettingsDialog(name, email, password);
|
|
|
+ else
|
|
|
+ showToast("Error: failed to get connection");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void createNewEmailDialog(){
|
|
|
/* define View window */
|
|
|
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
|
|
@@ -318,12 +392,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
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
|
|
@@ -333,80 +401,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
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);
|
|
|
-
|
|
|
+ /* validate user input before connecting */
|
|
|
if (!MailFunctions.validateEmail(newemail_email) | !MailFunctions.validateName(newemail_name) |
|
|
|
!MailFunctions.validatePassword(newemail_password)) {
|
|
|
return;
|
|
|
}
|
|
|
- /* connect to mail server */
|
|
|
- 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);
|
|
|
-
|
|
|
- /* 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();
|
|
|
-
|
|
|
- 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,
|
|
|
- MailFunctions.getImapHostFromEmail(email), MailFunctions.getSmtpHostFromEmail(email), 993,
|
|
|
- 587, ""));
|
|
|
- credentialsEditor.putString("data", gson.toJson(allUsersCredentials, credentialsType));
|
|
|
- credentialsEditor.putString("currentUser", email);
|
|
|
- credentialsEditor.apply();
|
|
|
- showToast("Success: added new email account");
|
|
|
- } else {
|
|
|
- showToast("Error: cannot add the same email twice");
|
|
|
- }
|
|
|
- } else {
|
|
|
- askForChangeMailServerSettingsDialog(name, email, password);
|
|
|
- }
|
|
|
+ addNewAccountCredentials(name, email, password, 993, 587, MailFunctions.getImapHostFromEmail(email),
|
|
|
+ MailFunctions.getSmtpHostFromEmail(email), dialog, true);
|
|
|
}});
|
|
|
|
|
|
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)
|