Skip to content

Commit

Permalink
Keep track of signal verification
Browse files Browse the repository at this point in the history
- signal_username keeps track of registered signal number, signal_verified_username
  will keep track if that has been verified or not
- extract string constants into resources

Signed-off-by: Arka Prava Basu <[email protected]>
  • Loading branch information
archie94 committed Mar 23, 2019
1 parent 1d826da commit 35c968f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
42 changes: 42 additions & 0 deletions src/main/java/org/havenapp/main/PreferenceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class PreferenceManager {
public static final String REMOTE_ACCESS_CRED = "remote_access_credential";

private static final String SIGNAL_USERNAME = "signal_username";
private static final String SIGNAL_VERIFIED_USERNAME = "signal_verified_username";

private static final String FIRST_LAUNCH = "first_launch";

Expand Down Expand Up @@ -119,6 +120,16 @@ public void setFirstLaunch(boolean firstLaunch) {
prefsEditor.commit();
}

/**
* Returns the Signal username registered. This may not be a good way to check for
* Signal set up since this may not be verified.
*
* Usages should be checked with {@link #isSignalVerified()}
*
* @see #isSignalVerified()
*
* @return the Signal username; null when nothing is set up
*/
public String getSignalUsername ()
{
return appSharedPrefs.getString(SIGNAL_USERNAME,null);
Expand All @@ -130,6 +141,37 @@ public void setSignalUsername (String signalUsername)
prefsEditor.commit();
}

/**
* Returns the Signal username verified. This may not be a good way to check for
* Signal set up since this may invalidated by a call to register with a different username.
*
* Usages should be checked with {@link #isSignalVerified()}
*
* @see #isSignalVerified()
*
* @return the verified Signal username; null when no Signal username is verified even though registered.
*/
@Nullable
public String getVerifiedSignalUsername() {
return appSharedPrefs.getString(SIGNAL_VERIFIED_USERNAME, null);
}

public void setVerifiedSignalUsername(String verifiedSignalUsername) {
prefsEditor.putString(SIGNAL_VERIFIED_USERNAME, verifiedSignalUsername);
prefsEditor.commit();
}

/**
* Checks if Signal is registered and verified for the Signal username returned by
* {@link #getSignalUsername()}
*
* @return true iff registered Signal username is same as that of the verified one.
*/
public boolean isSignalVerified() {
return !TextUtils.isEmpty(getSignalUsername()) &&
getSignalUsername().equals(getVerifiedSignalUsername());
}

public void activateRemoteAccess (boolean active) {
prefsEditor.putBoolean(REMOTE_ACCESS_ACTIVE,active);
prefsEditor.commit();
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/havenapp/main/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public void onCreatePreferences(Bundle bundle, String s) {
});

checkSignalUsername();
checkSignalUsernameVerification();
((EditTextPreference) findPreference(PreferenceManager.VERIFY_SIGNAL)).setText("");
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 1);

Expand Down Expand Up @@ -410,6 +411,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
findPreference(PreferenceManager.REGISTER_SIGNAL).setSummary(R.string.register_signal_desc);
}
onRemoteNotificationParameterChange();
checkSignalUsernameVerification();
break;
case PreferenceManager.VERIFY_SIGNAL: {
String text = ((EditTextPreference) findPreference(PreferenceManager.VERIFY_SIGNAL)).getText();
Expand All @@ -418,6 +420,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}
activateSignal(preferences.getSignalUsername(), text);
onRemoteNotificationParameterChange();
checkSignalUsernameVerification();
break;
}
case PreferenceManager.REMOTE_PHONE_NUMBER:
Expand Down Expand Up @@ -585,12 +588,26 @@ private void checkSignalUsername() {
}
}

private void checkSignalUsernameVerification() {
String signalUsername = preferences.getSignalUsername();

// this will fail for all users currently has signal verified
if (checkValidString(signalUsername) &&
signalUsername.equals(preferences.getVerifiedSignalUsername())) {
findPreference(PreferenceManager.VERIFY_SIGNAL)
.setSummary(R.string.verification_dialog_summary_verified);
} else {
findPreference(PreferenceManager.VERIFY_SIGNAL)
.setSummary(R.string.verification_dialog_summary);
}
}

private void activateSignal(String username, String verifyCode) {
SignalSender sender = SignalSender.getInstance(mActivity, username);

if (TextUtils.isEmpty(verifyCode)) {
ProgressDialog progressDialog = ProgressDialog.show(getContext(), "Registering to Signal",
"Please wait while we register you to Signal services");
ProgressDialog progressDialog = ProgressDialog.show(getContext(), getString(R.string.registering_to_signal),
getString(R.string.signal_registration_desc));
sender.register(preferences.getVoiceVerificationEnabled(),
new SignalExecutorTask.TaskResult() {
@Override
Expand All @@ -610,14 +627,16 @@ public void onFailure(@NonNull String msg) {
}
});
} else {
ProgressDialog progressDialog = ProgressDialog.show(getContext(), "Verifying",
"Please wait while we verify your registration to Signal services");
ProgressDialog progressDialog = ProgressDialog.show(getContext(), getString(R.string.verifying_signal),
getString(R.string.verifying_signal_desc));
sender.verify(verifyCode, new SignalExecutorTask.TaskResult() {
@Override
public void onSuccess(@NonNull String msg) {
if (isAdded() && getActivity() != null) {
progressDialog.dismiss();
}
// mark that the current registered signal username is verified
preferences.setVerifiedSignalUsername(preferences.getSignalUsername());
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<string name="sms_dialog_message">Enter a remote phone number to send alert notifications to</string>
<string name="signal_dialog_summary">A phone number to send from using Signal</string>
<string name="verification_dialog_summary">Tap to verify your Signal number</string>
<string name="verification_dialog_summary_verified">Signal number is verified</string>
<string name="send_message_dialog">Enter a phone number to send a test alert notification message to</string>
<string name="send_text_message">Test notifications…</string>
<string name="service_address">Service Address</string>
Expand Down Expand Up @@ -177,5 +178,9 @@
<string name="status_recording_video">Recording…</string>
<string name="status_light">LIGHT DETECTED</string>
<string name="run_disk_cleanup">Clean deleted logs from disk</string>
<string name="registering_to_signal">Registering to Signal</string>
<string name="signal_registration_desc">Please wait while we register you to Signal services</string>
<string name="verifying_signal">Verifying</string>
<string name="verifying_signal_desc">Please wait while we verify your registration to Signal services</string>

</resources>

0 comments on commit 35c968f

Please sign in to comment.