Skip to content

Commit

Permalink
Change Switch logic for enabling/disabling remote notification
Browse files Browse the repository at this point in the history
- switch is now only set when we can make a remote notification which means
  presence of remote number and a verified signal number

Signed-off-by: Arka Prava Basu <[email protected]>
  • Loading branch information
archie94 committed Feb 13, 2019
1 parent 5e76092 commit a3c36eb
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/havenapp/main/PreferenceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class PreferenceManager {

// keeping the key value same for data migration.
static final String REMOTE_PHONE_NUMBER = "sms_number";
static final String REMOTE_NOTIFICATION_ACTIVE = "remote_notification_active";

private Context context;

Expand Down Expand Up @@ -248,6 +249,15 @@ public String getMicrophoneSensitivity() {
return appSharedPrefs.getString(MICROPHONE_SENSITIVITY, MEDIUM);
}

public void setRemoteNotificationActive(boolean isRemoteNotificationActive) {
prefsEditor.putBoolean(REMOTE_NOTIFICATION_ACTIVE, isRemoteNotificationActive);
prefsEditor.apply();
}

public boolean isRemoteNotificationActive() {
return appSharedPrefs.getBoolean(REMOTE_NOTIFICATION_ACTIVE, false);
}

public void setRemotePhoneNumber(@NonNull String remotePhoneNumber) {
prefsEditor.putString(REMOTE_PHONE_NUMBER, remotePhoneNumber.trim());
prefsEditor.apply();
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/havenapp/main/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -84,6 +85,26 @@ public void onCreatePreferences(Bundle bundle, String s) {

}

SwitchPreference switchPreference =
(SwitchPreference) findPreference(PreferenceManager.REMOTE_NOTIFICATION_ACTIVE);

switchPreference.setChecked(preferences.isRemoteNotificationActive());

switchPreference.setOnPreferenceChangeListener((preference, newValue) -> {
// user wants to enable/disable remote notification

boolean enabled = (Boolean) newValue;

if (enabled && !canSendRemoteNotification()) {
collectDataForRemoteNotification();
}

preferences.setRemoteNotificationActive(enabled && canSendRemoteNotification());
switchPreference.setChecked(enabled && canSendRemoteNotification());

return false;
});

findPreference(PreferenceManager.REMOTE_PHONE_NUMBER).setOnPreferenceClickListener(preference -> {
if (preferences.getRemotePhoneNumber().isEmpty()) {
((EditTextPreference) findPreference(PreferenceManager.REMOTE_PHONE_NUMBER)).setText(getCountryCode());
Expand Down Expand Up @@ -199,6 +220,31 @@ public void onCreatePreferences(Bundle bundle, String s) {

}

private boolean canSendRemoteNotification() {
String remotePhoneNumber = preferences.getRemotePhoneNumber();
String signalUsername = preferences.getSignalUsername();
return !remotePhoneNumber.isEmpty() && !getCountryCode().equalsIgnoreCase(remotePhoneNumber) &&
!TextUtils.isEmpty(signalUsername);
}

/**
* Collect data required for Remote notification with Signal.
* We need a remote phone number and a verified signal Username.
*/
@SuppressLint("RestrictedApi")
private void collectDataForRemoteNotification() {
String remotePhoneNumber = preferences.getRemotePhoneNumber();
if (remotePhoneNumber.isEmpty() || getCountryCode().equalsIgnoreCase(remotePhoneNumber)) {
findPreference(PreferenceManager.REMOTE_PHONE_NUMBER).performClick();
}
String signalUsername = preferences.getSignalUsername();
if (TextUtils.isEmpty(signalUsername)) {
findPreference(PreferenceManager.REGISTER_SIGNAL).performClick();
} else {
activateSignal(signalUsername, null);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand Down Expand Up @@ -227,6 +273,10 @@ protected void save() {

preferences.setSignalUsername(((EditTextPreference) findPreference(PreferenceManager.REGISTER_SIGNAL)).getText());

boolean remoteNotificationActive =
((SwitchPreference) findPreference(PreferenceManager.REMOTE_NOTIFICATION_ACTIVE)).isChecked();
preferences.setRemoteNotificationActive(remoteNotificationActive);

boolean remoteAccessActive = ((SwitchPreference) findPreference(PreferenceManager.REMOTE_ACCESS_ACTIVE)).isChecked();

preferences.activateRemoteAccess(remoteAccessActive);
Expand Down Expand Up @@ -347,6 +397,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
activateSignal(preferences.getSignalUsername(), text);
break;
}
case PreferenceManager.REMOTE_NOTIFICATION_ACTIVE:
// todo not - needed: Test this out.
break;
case PreferenceManager.REMOTE_PHONE_NUMBER:
setPhoneNumber();
break;
Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Verzögerungszeit setzen
</string>

<string name="sms_label">"Bei Alarm SMS oder Signal Nachricht senden "</string>
<string name="remote_notification_label">"Bei Alarm SMS oder Signal Nachricht senden "</string>

<string name="sms_hint">+4917712345678</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Temporizador
</string>

<string name="sms_label">
<string name="remote_notification_label">
Enviar SMS o mensaje de alerta por Signal
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Déterminer un délai
</string>

<string name="sms_label">
<string name="remote_notification_label">
Envoyer SMS ou des alertes par message Signal
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Imposta il timer di avvio
</string>

<string name="sms_label">
<string name="remote_notification_label">
Invia SMS o messaggi via Signal
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
開始遅延時間
</string>

<string name="sms_label">
<string name="remote_notification_label">
ショートメールで警告を送信
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Definir tempo de atraso
</string>

<string name="sms_label">
<string name="remote_notification_label">
Enviar mensagens de alerta de SMS ou Signal
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Установка времени задержки
</string>

<string name="sms_label">
<string name="remote_notification_label">
Оповещать по SMS или через Signal
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-sr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Vreme odlaganja
</string>

<string name="sms_label">
<string name="remote_notification_label">
Šalji SMS ili Signal upozorenja
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Bekleme Süresi
</string>

<string name="sms_label">
<string name="remote_notification_label">
SMS veya Signal ile uyarı mesajı gönder
</string>

Expand Down
2 changes: 1 addition & 1 deletion src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
设置延时
</string>

<string name="sms_label">
<string name="remote_notification_label">
使用发送短信或 Signal 发送警告
</string>

Expand Down
4 changes: 2 additions & 2 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Set Delay Time
</string>

<string name="sms_label">
Send SMS or Signal message alerts
<string name="remote_notification_label">
Send Signal message alerts
</string>

<string name="sms_hint">
Expand Down
6 changes: 3 additions & 3 deletions src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
<PreferenceCategory android:title="@string/label_notifications">

<SwitchPreference
android:defaultValue="true"
android:key="sms_active"
android:title="@string/sms_label" />
android:defaultValue="false"
android:key="remote_notification_active"
android:title="@string/remote_notification_label" />

<EditTextPreference
style="@style/AppPreference.DialogPreferenceSave"
Expand Down

0 comments on commit a3c36eb

Please sign in to comment.