Skip to content

Commit

Permalink
Merge pull request #1 from TpmKranz/feature-testsettings
Browse files Browse the repository at this point in the history
Add proper UI response for changed settings
  • Loading branch information
Tom committed Nov 20, 2015
2 parents 620bf4c + 04fd2b0 commit c5a8633
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 45 deletions.
155 changes: 129 additions & 26 deletions app/src/main/java/org/tpmkranz/smsforward/SetupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class SetupActivity extends AppCompatActivity {
Expand All @@ -32,6 +42,7 @@ public class SetupActivity extends AppCompatActivity {
private FloatingActionButton fab;
private EditText inputEmail, inputPassword, inputServer, inputPort, inputTarget, inputPubkey;
private SharedPreferences settings;
private HashMap<String,String> currentSettings;


static {
Expand All @@ -52,11 +63,18 @@ protected void onCreate(Bundle savedInstanceState) {
inputTarget = (EditText) findViewById(R.id.input_target);
inputPubkey = (EditText) findViewById(R.id.input_pubkey);
settings = this.getSharedPreferences(SHAREDPREFSNAME, Context.MODE_PRIVATE);
inputEmail.setText(settings.getString(SHAREDPREFSEMAIL, ""));
inputServer.setText(settings.getString(SHAREDPREFSSERVER, ""));
inputPort.setText(settings.getString(SHAREDPREFSPORT, ""));
inputTarget.setText(settings.getString(SHAREDPREFSTARGET, ""));
inputPubkey.setText(settings.getString(SHAREDPREFSPUBKEY, ""));
currentSettings = new HashMap<>();
readSettings(true);
inputEmail.setText(currentSettings.get(SHAREDPREFSEMAIL));
inputEmail.addTextChangedListener(new ClearErrorOnInputListener(inputEmail));
inputServer.setText(currentSettings.get(SHAREDPREFSSERVER));
inputServer.addTextChangedListener(new ClearErrorOnInputListener(inputServer));
inputPort.setText(currentSettings.get(SHAREDPREFSPORT));
inputPort.addTextChangedListener(new ClearErrorOnInputListener(inputPort));
inputTarget.setText(currentSettings.get(SHAREDPREFSTARGET));
inputTarget.addTextChangedListener(new ClearErrorOnInputListener(inputTarget));
inputPubkey.setText(currentSettings.get(SHAREDPREFSPUBKEY));
inputPubkey.addTextChangedListener(new ClearErrorOnInputListener(inputPubkey));
receiver = new ComponentName(this, SMSListener.class);
pm = getPackageManager();
}
Expand All @@ -70,24 +88,47 @@ protected void onResume(){
switchUIStates(enabled);
}

private void readSettings(boolean fromPrefs){
currentSettings.put(SHAREDPREFSEMAIL, fromPrefs ? settings.getString(SHAREDPREFSEMAIL, "") : inputEmail.getText().toString());
currentSettings.put(SHAREDPREFSPASSWORD, fromPrefs || inputPassword.getText().toString().isEmpty() ? settings.getString(SHAREDPREFSPASSWORD, "") : inputPassword.getText().toString());
currentSettings.put(SHAREDPREFSSERVER, fromPrefs ? settings.getString(SHAREDPREFSSERVER, "") : inputServer.getText().toString());
currentSettings.put(SHAREDPREFSPORT, fromPrefs ? settings.getString(SHAREDPREFSPORT, "") : inputPort.getText().toString());
currentSettings.put(SHAREDPREFSTARGET, fromPrefs ? settings.getString(SHAREDPREFSTARGET, "") : inputTarget.getText().toString());
currentSettings.put(SHAREDPREFSPUBKEY, fromPrefs ? settings.getString(SHAREDPREFSPUBKEY, "") : inputPubkey.getText().toString());
}

private boolean haveSettingsChanged(){
return !getInputText(inputEmail).equals(settings.getString(SHAREDPREFSEMAIL, ""))
|| (!getInputText(inputPassword).equals(settings.getString(SHAREDPREFSPASSWORD, ""))
return !getInputText(inputEmail).equals(currentSettings.get(SHAREDPREFSEMAIL))
|| (!getInputText(inputPassword).equals(currentSettings.get(SHAREDPREFSPASSWORD))
&& !getInputText(inputPassword).equals(""))
|| !getInputText(inputServer).equals(settings.getString(SHAREDPREFSSERVER, ""))
|| !getInputText(inputPort).equals(settings.getString(SHAREDPREFSPORT, ""))
|| !getInputText(inputTarget).equals(settings.getString(SHAREDPREFSTARGET, ""))
|| !getInputText(inputPubkey).equals(settings.getString(SHAREDPREFSPUBKEY, ""));
|| !getInputText(inputServer).equals(currentSettings.get(SHAREDPREFSSERVER))
|| !getInputText(inputPort).equals(currentSettings.get(SHAREDPREFSPORT))
|| !getInputText(inputTarget).equals(currentSettings.get(SHAREDPREFSTARGET))
|| !getInputText(inputPubkey).equals(currentSettings.get(SHAREDPREFSPUBKEY));
}

private boolean areSettingsValid(){
return getInputText(inputEmail).contains("@")
&& !(getInputText(inputPassword).isEmpty() && settings.getString(SHAREDPREFSPASSWORD, "").isEmpty())
&& getInputText(inputServer).contains(".")
&& !getInputText(inputPort).isEmpty()
&& getInputText(inputTarget).contains("@")
&& (getInputText(inputPubkey).contains("-----BEGIN PGP PUBLIC KEY BLOCK-----")
|| getInputText(inputPubkey).isEmpty());
private List<EditText> invalidSettings(){
ArrayList<EditText> invalidSettings = new ArrayList<EditText>();
((TextInputLayout)inputEmail.getParent()).setError(null);
((TextInputLayout)inputPassword.getParent()).setError(null);
((TextInputLayout)inputServer.getParent()).setError(null);
((TextInputLayout)inputPort.getParent()).setError(null);
((TextInputLayout)inputTarget.getParent()).setError(null);
((TextInputLayout)inputPubkey.getParent()).setError(null);
if (!getInputText(inputEmail).contains("@"))
invalidSettings.add(inputEmail);
if (getInputText(inputPassword).isEmpty() && currentSettings.get(SHAREDPREFSPASSWORD).isEmpty())
invalidSettings.add(inputPassword);
if (!getInputText(inputServer).contains("."))
invalidSettings.add(inputServer);
if (getInputText(inputPort).isEmpty())
invalidSettings.add(inputPort);
if (!getInputText(inputTarget).contains("@"))
invalidSettings.add(inputTarget);
if (!(getInputText(inputPubkey).contains("-----BEGIN PGP PUBLIC KEY BLOCK-----")
|| getInputText(inputPubkey).isEmpty()))
invalidSettings.add(inputPubkey);
return invalidSettings;
}

private boolean isListenerListening(ComponentName listener){
Expand Down Expand Up @@ -132,17 +173,37 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

public void enableBroadcastReceiver(View view) {
if (areSettingsValid()) {
List<EditText> invalidSettings = invalidSettings();
if (invalidSettings.isEmpty()) {
if (haveSettingsChanged()) {
askUserToTestSettings();
// TODO write only on successful testing
writeSettings();
askUserToTestSettings();
}else{
pm.setComponentEnabledSetting(receiver,
(enabled ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED : PackageManager.COMPONENT_ENABLED_STATE_ENABLED),
PackageManager.DONT_KILL_APP);
}
pm.setComponentEnabledSetting(receiver,
(enabled ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED : PackageManager.COMPONENT_ENABLED_STATE_ENABLED),
PackageManager.DONT_KILL_APP);
enabled = isListenerListening(receiver);
switchUIStates(enabled);
}else{
for (EditText settingInput: invalidSettings){
String errorMessage;
if (settingInput.equals(inputEmail))
errorMessage = getResources().getString(R.string.invalid_email);
else if (settingInput.equals(inputPassword))
errorMessage = getResources().getString(R.string.invalid_password);
else if (settingInput.equals(inputServer))
errorMessage = getResources().getString(R.string.invalid_server);
else if (settingInput.equals(inputPort))
errorMessage = getResources().getString(R.string.invalid_port);
else if (settingInput.equals(inputTarget))
errorMessage = getResources().getString(R.string.invalid_email);
else if (settingInput.equals(inputPubkey))
errorMessage = getResources().getString(R.string.invalid_pubkey);
else
errorMessage = getResources().getString(R.string.invalid_something);
((TextInputLayout)settingInput.getParent()).setError(errorMessage);
}
}
}

Expand All @@ -156,12 +217,54 @@ private void writeSettings() {
editor.putString(SHAREDPREFSTARGET, getInputText(inputTarget));
editor.putString(SHAREDPREFSPUBKEY, getInputText(inputPubkey));
editor.apply();
readSettings(false);
}

private void askUserToTestSettings() {
Snackbar
.make(findViewById(R.id.setup_layout), R.string.snackbar_settings_changed, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.snackbar_settings_confirmed, new SnackBarSettingsConfirmationListener())
.show();
Intent testMailIntent = new Intent(this, EmailSenderService.class);
testMailIntent.putExtra(EmailSenderService.INTENTEXTRASUBJECT, getResources().getString(R.string.email_subject_test));
testMailIntent.putExtra(EmailSenderService.INTENTEXTRABODY, getResources().getString(R.string.email_body_test));
startService(testMailIntent);
}

public void expandIntro(View view) {
TextView intro = (TextView) view;
boolean isSingleLine = intro.getLineCount() == 1;
String introHint = getResources().getString(isSingleLine ? R.string.hint_intro1 : R.string.hint_intro0);
((TextInputLayout) intro.getParent()).setHint(introHint);
intro.setEllipsize(isSingleLine ? null : TextUtils.TruncateAt.END);
intro.setSingleLine(!isSingleLine);
}

private class ClearErrorOnInputListener implements TextWatcher{

TextInputLayout wrapper;

public ClearErrorOnInputListener(EditText input){
wrapper = (TextInputLayout) input.getParent();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void afterTextChanged(Editable s) {
wrapper.setError(null);
}
}

private class SnackBarSettingsConfirmationListener implements View.OnClickListener{

@Override
public void onClick(View v) {
enableBroadcastReceiver(v);
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_setup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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" android:fitsSystemWindows="true"
tools:context=".SetupActivity">
tools:context=".SetupActivity"
android:id="@+id/setup_layout">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
Expand Down
37 changes: 26 additions & 11 deletions app/src/main/res/layout/content_setup.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_setup" tools:context=".SetupActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10pt"
android:textStyle="italic"
android:text="@string/intro"/>
android:hint="@string/hint_intro1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:textSize="10pt"
android:textStyle="italic"
android:text="@string/intro"
android:scrollHorizontally="true"
android:editable="false"
android:background="@android:color/transparent"
android:textColor="@android:color/tertiary_text_light"
android:textColorHighlight="@android:color/tertiary_text_light"
android:onClick="expandIntro"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -92,4 +107,4 @@
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</ScrollView>
24 changes: 17 additions & 7 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
<resources>
<string name="app_name">SMS Forward</string>
<string name="action_settings">Settings</string>
<string name="hint_email">Email address</string>
<string name="hint_password">Password</string>
<string name="hint_server">SMTP Server</string>
<string name="hint_port">Port</string>
<string name="hint_target">Target address</string>
<string name="hint_email">Email address*</string>
<string name="hint_password">Password*</string>
<string name="hint_server">SMTP Server*</string>
<string name="hint_port">Port*</string>
<string name="hint_target">Target address*</string>
<string name="hint_pubkey">Public PGP key</string>
<string name="intro">Here you can set the email account to be used for sending SMS contents.\nThe button starts the service that catches SMS and forwards them via the specified email account.</string>
<string name="intro">Here you can set up the email account to be used for sending SMS contents.\nThe button starts the receiver that catches and forwards SMS via email.\nIf you\'ve changed settings along the way, a test mail will be sent and you\'ll be asked to confirm its receipt to really start the receiver.</string>
<string name="service_notification_ticker">Forwarding SMS via Email</string>
<string name="snackbar_settings_changed">Confirm that you\'ve received the test mail</string>
<string name="snackbar_settings_confirmed">Confirm</string>
<string name="email_body_template" formatted="false">SMS from %s:%n%n%s</string>
<string name="email_subject_template" formatted="false">%d new SMS</string>
<string name="email_subject_test">SMS Forward Test Mail</string>
<string name="email_body_test">If you see this mail, your settings are set correctly and your SMS should be forwarded properly to this mail address.</string>
<string name="email_body_test">If you see this mail, your settings are set correctly and your SMS should be forwarded properly to this mail address once you\'ve confirmed the receipt of this mail on your device.</string>
<string name="invalid_email">Not a valid email address</string>
<string name="invalid_password">No password stored or set here</string>
<string name="invalid_server">Not a valid fully qualified domain name</string>
<string name="invalid_port">Not a valid port</string>
<string name="invalid_pubkey">Not a valid PGP pubkey</string>
<string name="invalid_something">Not a valid input</string>
<string name="hint_intro0">Tap to expand explanation</string>
<string name="hint_intro1">Tap to collapse explanation</string>
</resources>

0 comments on commit c5a8633

Please sign in to comment.