Skip to content

Commit

Permalink
Adding functionality to enable/disable sms forwarding on mobile phones
Browse files Browse the repository at this point in the history
  • Loading branch information
teisen committed Dec 17, 2013
1 parent f82bccc commit 92a2738
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
58 changes: 58 additions & 0 deletions src/com/techventus/server/voice/Voice.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.techventus.server.voice.datatypes.AllSettings;
import com.techventus.server.voice.datatypes.Greeting;
import com.techventus.server.voice.datatypes.Group;
import com.techventus.server.voice.datatypes.Phone;
import com.techventus.server.voice.datatypes.records.SMSThread;
import com.techventus.server.voice.exception.AuthenticationException;
import com.techventus.server.voice.exception.ERROR_CODE;
Expand Down Expand Up @@ -202,6 +203,9 @@ public class Voice {
/** The Constant generalSettingsURLString. */
final static String generalSettingsURLString = "https://www.google.com/voice/b/0/settings/editGeneralSettings/";

/** The Constant editForwardingSMSURLString. */
final static String editForwardingSMSURLString = "https://www.google.com/voice/b/0/settings/editForwardingSms/";

/** The Constant phonesInfoURLString. */
final static String phonesInfoURLString = "https://www.google.com/voice/b/0/settings/tab/phones";

Expand Down Expand Up @@ -1362,6 +1366,8 @@ private String phonesEnableDisableApply(String paraString) throws IOException {
// page]

//
if (PRINT_TO_CONSOLE) System.out.println(phoneEnableURLString);
if (PRINT_TO_CONSOLE) System.out.println(paraString);
URL requestURL = new URL(phoneEnableURLString);

URLConnection conn = requestURL.openConnection();
Expand Down Expand Up @@ -1520,6 +1526,58 @@ public String setDoNotDisturb(boolean dndEnabled) throws IOException {
return postSettings(requestURL, paraString);
}

/**
* Activated or deactivated the SMS Forwarding for a particular phone
*
* @param smsEnable true to enable sms forwarding, false to disable it
* @param ID The id of the phone to enable/disable
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
public String setSmsEnabled(boolean smsEnable, int ID) throws IOException {

// only allow editing of type 2 phones
for (int i = 0; i < settings.getPhones().length; i++) {
Phone ph = settings.getPhones()[i];
if(ph.getId() == ID) {
if(ph.getType() != 2) {
if (PRINT_TO_CONSOLE) System.out.println("Cannot change sms Enabled on phone of type "+ph.getType() + " only availible on type 2");
return null;
}
}
}

String enabled;

if(smsEnable &! settings.isPhoneSmsEnabled(ID)) {
if (PRINT_TO_CONSOLE) System.out.println("Enabling sms for phone "+ID);
enabled = "1";
} else if(settings.isPhoneSmsEnabled(ID)) {
if (PRINT_TO_CONSOLE) System.out.println("Disabling sms for phone "+ID);
enabled = "0";
} else {
// do not make changes to phones that are already in the same state
if (PRINT_TO_CONSOLE) System.out.println("Phone "+ID + " is already in the requested state. "+smsEnable);
return null;
}

URL requestURL = new URL(editForwardingSMSURLString);

String paraString = "";
paraString += URLEncoder.encode("enabled", enc) + "="
+ URLEncoder.encode(enabled+"", enc);
paraString += "&" + URLEncoder.encode("phoneId", enc) + "="
+ URLEncoder.encode(ID+"", enc);
paraString += "&" + URLEncoder.encode("_rnr_se", enc) + "="
+ URLEncoder.encode(rnrSEE, enc);

if (PRINT_TO_CONSOLE) System.out.println(requestURL);
if (PRINT_TO_CONSOLE) System.out.println(paraString);

return postSettings(requestURL, paraString);

}

/**
* Applies the settings for this group.
*
Expand Down
24 changes: 18 additions & 6 deletions src/com/techventus/server/voice/datatypes/AllSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,25 @@ public void setPhoneEnabled(int phoneId) {
settings.setmDisabledIdList(lNewDisabledList);
}

private String printArray(DisabledId[] pArray) {
String printer = "";
for (int i = 0; i < pArray.length; i++) {
printer+=pArray[i].toString();
/**
*
* Query smsEnabled status - if id not found, then it returnes false
* @param phoneId
* @return true if the Phone is smsEnabled. Otherwise it returns false.
*/
public boolean isPhoneSmsEnabled(int phoneId) {
boolean ret = false;
try {
for (int i = 0; i < phones.length; i++) {
if(phones[i].getId() == phoneId) {
ret = phones[i].getSmsEnabled();
}
}
} catch (NullPointerException e) {
ret = false;
}
return printer;
}
return ret;
}

/**
* @return the phoneList
Expand Down
8 changes: 8 additions & 0 deletions src/com/techventus/server/voice/datatypes/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ public String getFormattedNumber() {
public int getId() {
return id;
}

/**
* returns the value for smsEnabled for this phone
* @return
*/
public boolean getSmsEnabled() {
return smsEnabled;
}

/**
* @return the incomingAccessNumber
Expand Down
51 changes: 48 additions & 3 deletions src/test/test.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private static void listTests() {
System.out.println("18. List Starred Conversations. ");
System.out.println("19. View SMSThread Info");
System.out.println("20. SMS to Existing Conversation");
System.out.println("21. Phone SMS enable/disable");

int testNr = 0;
try {
Expand Down Expand Up @@ -176,7 +177,7 @@ private static void runTest(String userName, String pass, int testNr) {
System.out.println("Current phone status:");
for (int j = 0; j < phones.length; j++) {
phonesToChangeStatus[j] = phones[j].getId();
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()));
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()) );
}

//Disable all phones
Expand All @@ -189,7 +190,7 @@ private static void runTest(String userName, String pass, int testNr) {
// Output
System.out.println("After deactivate multi:");
for (int j = 0; j < phones.length; j++) {
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()));
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()) );
}

//Enable all phones
Expand All @@ -201,7 +202,7 @@ private static void runTest(String userName, String pass, int testNr) {
phones = voice.getSettings(true).getPhones();
System.out.println("After activate multi:");
for (int j = 0; j < phones.length; j++) {
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()));
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + !voice.getSettings(false).isPhoneDisabled(phones[j].getId()) );
}

System.out.println("******** Finished Test "+testNr+" ********");
Expand Down Expand Up @@ -645,6 +646,48 @@ private static void runTest(String userName, String pass, int testNr) {
}
break;

case 21: // 1: phone smsEnable
{
System.out.println("******** Starting Test "+testNr+" ********");
Phone[] phones = voice.getSettings(false).getPhones();
// create int Array from all phone ids
int[] phonesToChangeStatus = new int[phones.length];

System.out.println("Current phone status:");
for (int j = 0; j < phones.length; j++) {
phonesToChangeStatus[j] = phones[j].getId();
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + " SmsEnabled?="+phones[j].getSmsEnabled());
}

System.out.println("Disable sms all phones:");
for (int j = 0; j < phones.length; j++) {
voice.setSmsEnabled(false,phones[j].getId());
}


phones = voice.getSettings(true).getPhones();
// Output
System.out.println("After deactivate multi:");
for (int j = 0; j < phones.length; j++) {
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + " SmsEnabled?="+phones[j].getSmsEnabled());
}

System.out.println("Enable sms all phones:");
for (int j = 0; j < phones.length; j++) {
voice.setSmsEnabled(true,phones[j].getId());
}

// Output
phones = voice.getSettings(true).getPhones();
System.out.println("After activate multi:");
for (int j = 0; j < phones.length; j++) {
System.out.println(phones[j].getName() + " " + phones[j].getId() + " " + " SmsEnabled?="+phones[j].getSmsEnabled());
}

System.out.println("******** Finished Test "+testNr+" ********");
}
break;



default:
Expand All @@ -656,8 +699,10 @@ private static void runTest(String userName, String pass, int testNr) {

} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} catch (JSONException e) {
e.printStackTrace();
System.exit(1);
}
listTests(); // List the Tests again
}
Expand Down

0 comments on commit 92a2738

Please sign in to comment.