Skip to content

Commit

Permalink
more progress on #21, can register and verify almost
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Nov 6, 2017
1 parent e4a1f0d commit 75c5236
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class PreferenceManager {
private static final String REMOTE_ACCESS_ONION = "remote_access_onion";
private static final String REMOTE_ACCESS_CRED = "remote_access_credential";

private static final String SIGNAL_USERNAME = "signal_username";


private Context context;

public PreferenceManager(Context context) {
Expand All @@ -61,6 +64,17 @@ public PreferenceManager(Context context) {
this.prefsEditor = appSharedPrefs.edit();
}

public String getSignalUsername ()
{
return appSharedPrefs.getString(SIGNAL_USERNAME,null);
}

public void setSignalUsername (String signalUsername)
{
prefsEditor.putString(SIGNAL_USERNAME,signalUsername);
prefsEditor.commit();
}

public void activateRemoteAccess (boolean active) {
prefsEditor.putBoolean(REMOTE_ACCESS_ACTIVE,active);
prefsEditor.commit();
Expand Down
132 changes: 127 additions & 5 deletions src/main/java/info/guardianproject/phoneypot/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@


import java.io.File;
import java.util.ArrayList;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AlphabetIndexer;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


Expand Down Expand Up @@ -128,11 +135,12 @@ else if (camera.equals(PreferenceManager.OFF))
findViewById(R.id.action_activate_signal).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activateSignal();

activateSignalPrompt();
}
});

checkSignalUsername();

findViewById(R.id.action_configure_mic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -144,6 +152,24 @@ public void onClick(View view) {

}

private void checkSignalUsername ()
{
if (preferences.getSignalUsername() != null)
{
TextView tv = (TextView)findViewById(R.id.label_signal_status);
tv.setText("Current Signal Number: " + preferences.getSignalUsername());

Button btnTestSignal = (Button)findViewById(R.id.action_test_signal);
btnTestSignal.setVisibility(View.VISIBLE);
btnTestSignal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendTestSignal();
}
});
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand Down Expand Up @@ -287,11 +313,107 @@ public void onRadioButtonClicked(View view) {
}
}

private void activateSignal ()
private void activateSignalPrompt ()
{
SignalSender.getInstance(this).register("+17185697272");
// SignalSender.getInstance(this).verify("+17185697272","484177");
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//number of code
final EditText input = new EditText(this);

// add a button
builder.setPositiveButton("Register", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
preferences.setSignalUsername(input.getText().toString());
activateSignal(input.getText().toString(),null);
checkSignalUsername();
}
});

// add a button
builder.setNeutralButton("Verify", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
activateSignal(preferences.getSignalUsername(),input.getText().toString());
}
});
// add a button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});


// create and show the alert dialog
AlertDialog dialog = builder.create();

dialog.setTitle("Active Signal");
dialog.setMessage("Register a new phone number ((+12125551212)) with Signal in order to send secure notifications");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
dialog.setView(input); // uncomment this line

dialog.show();

}

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

if (TextUtils.isEmpty(verifyCode))
sender.register();
else
sender.verify(verifyCode);

}

private void sendTestSignal ()
{

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//number of code
final EditText input = new EditText(this);

// add a button
builder.setPositiveButton("Send Test", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SignalSender sender =SignalSender.getInstance(SettingsActivity.this, preferences.getSignalUsername());
ArrayList<String> recip = new ArrayList<>();
recip.add(input.getText().toString());
sender.sendMessage(recip,getString(R.string.signal_test_message));
}
});

// add a button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});


// create and show the alert dialog
AlertDialog dialog = builder.create();

dialog.setTitle("Send Test Signal");
dialog.setMessage("Enter a phone number (+12125551212) to send a test message to");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
dialog.setView(input); // uncomment this line

dialog.show();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.asamk.Signal;
import org.asamk.signal.Main;

import java.util.ArrayList;
import java.util.HashMap;

/**
Expand All @@ -17,30 +18,37 @@ public class SignalSender {

private Context mContext;
private static SignalSender mInstance;
private String mUsername; //aka your signal phone number

private SignalSender(Context context)
private SignalSender(Context context, String username)
{
mContext = context;
mUsername = username;
}

public static synchronized SignalSender getInstance (Context context)
public static synchronized SignalSender getInstance (Context context, String username)
{
if (mInstance == null)
{
mInstance = new SignalSender(context);
mInstance = new SignalSender(context, username);
}

return mInstance;
}

public void register (final String phoneNumber)
public void setUsername (String username)
{
mUsername = username;
}

public void register ()
{
execute (new Runnable() {
public void run() {
Main mainSignal = new Main(mContext);
HashMap<String, Object> map = new HashMap<>();

map.put("username", phoneNumber);
map.put("username", mUsername);
map.put("command", "register");
map.put("voice", false);

Expand All @@ -50,14 +58,14 @@ public void run() {
});
}

public void verify (final String phoneNumber, final String verificationCode)
public void verify (final String verificationCode)
{
execute (new Runnable() {
public void run() {
Main mainSignal = new Main(mContext);
HashMap<String, Object> map = new HashMap<>();

map.put("username", phoneNumber);
map.put("username", mUsername);
map.put("command", "verify");
map.put("verificationCode", verificationCode);

Expand All @@ -67,6 +75,24 @@ public void run() {
});
}

public void sendMessage (final ArrayList<String> recipients, final String message)
{
execute (new Runnable() {
public void run() {
Main mainSignal = new Main(mContext);
HashMap<String, Object> map = new HashMap<>();

map.put("endsession",false);
map.put("recipient", recipients);
map.put("command", "send");
map.put("message", message);

Namespace ns = new Namespace(map);
mainSignal.handleCommands(ns);
}
});
}

private void execute (Runnable runnable)
{
new Thread (runnable).start();
Expand Down
23 changes: 23 additions & 0 deletions src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@
android:id="@+id/action_activate_signal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"

>

<TextView
android:id="@+id/label_signal_status"
android:text=""
android:layout_alignParentLeft="true"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Text Message"
android:id="@+id/action_test_signal"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>

</ScrollView>
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@
<string name="data_pressure">PRESSURE</string>
<string name="data_power">POWER</string>
<string name="share_event_action">Share event...</string>
<string name="signal_test_message">This is a test message from Haven</string>
</resources>

0 comments on commit 75c5236

Please sign in to comment.