Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkourlas committed May 24, 2015
1 parent 80788cc commit 1b5cc07
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.3.1 ##

* Fix error in push notification instructions
* Bug fixes

## 0.3.0 ##

* Support for push notifications
Expand Down
4 changes: 2 additions & 2 deletions voipms-sms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
applicationId "net.kourlas.voipms_sms"
minSdkVersion 16
targetSdkVersion 22
versionCode 104
versionName "0.3.0"
versionCode 105
versionName "0.3.1"
}

compileOptions {
Expand Down
2 changes: 1 addition & 1 deletion voipms-sms/src/main/assets/credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1>Application</h1>

<p>VoIP.ms SMS<br>
Version 0.3.0</p>
Version 0.3.1</p>

<p>Copyright (C) 2015 Michael Kourlas and other contributors</p>

Expand Down
4 changes: 2 additions & 2 deletions voipms-sms/src/main/assets/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ <h2>Push Notifications</h2>
<p>This application supports push notifications using Google Cloud Messaging. To enable push notifications:</p>
<ul>
<li>make sure your device is connected to the Internet, then enable notifications in Settings; and</li>
<li>copy and paste the following URL in the the following URL into the "SMS URL Callback" field of the SMS settings
for your DID on the VoIP.ms portal: <a href="http://voipmssms-kourlas.rhcloud.com/sms_callback?did={DID}">http://voipmssms-kourlas.rhcloud.com/sms_callback?did={DID}</a></li>
<li>access the SMS settings for your DID on the VoIP.ms portal and enable the \'SMS URL Callback\' feature,
entering the following URL into the neighbouring field: <a href="http://voipmssms-kourlas.rhcloud.com/sms_callback?did={TO}">http://voipmssms-kourlas.rhcloud.com/sms_callback?did={TO}</a></li>
</ul>
<p>You can make changes to VoIP.ms DID settings <a href="https://voip.ms/m/managedid.php">here</a>.</p>
<p>When push notifications are enabled, the application stores your DID and GCM registration ID on a server run by the
Expand Down
3 changes: 2 additions & 1 deletion voipms-sms/src/main/java/net/kourlas/voipms_sms/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ public void onClick(DialogInterface dialog, int id) {
updateSmsDatabase(sourceActivity, true, false);
}

Gcm.getInstance(applicationContext).registerForGcm(sourceActivity, false);
Gcm.getInstance(applicationContext).registerForGcm(sourceActivity, false,
false);
}
});
builder.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onRefresh() {
Api.getInstance(getApplicationContext()).updateSmsDatabase(conversationsActivity, true, false);
Gcm.getInstance(getApplicationContext()).registerForGcm(conversationsActivity, false);
Gcm.getInstance(getApplicationContext()).registerForGcm(conversationsActivity, false, false);
}
});

Expand Down Expand Up @@ -201,7 +201,7 @@ public void onResume() {
Api.getInstance(getApplicationContext()).updateSmsDatabase(conversationsActivity, true, false);
}

Gcm.getInstance(getApplicationContext()).registerForGcm(this, false);
Gcm.getInstance(getApplicationContext()).registerForGcm(this, false, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
builder.setPositiveButton(R.string.ok, null);
builder.show();

Gcm.getInstance(getActivity().getApplicationContext()).registerForGcm(getActivity(), true);
Gcm.getInstance(getActivity().getApplicationContext()).registerForGcm(getActivity(), true,
true);
}
}
}
Expand Down
55 changes: 28 additions & 27 deletions voipms-sms/src/main/java/net/kourlas/voipms_sms/gcm/Gcm.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import net.kourlas.voipms_sms.R;
import net.kourlas.voipms_sms.Utils;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -96,20 +98,27 @@ private void registerInBackground(final Activity activity, final boolean showFee
new AsyncTask<Boolean, Void, List<Object>>() {
@Override
protected List<Object> doInBackground(Boolean... params) {
List<Object> list = new ArrayList<Object>();
list.add(showFeedback);

try {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(applicationContext);

String registrationId = gcm.register(SENDER_ID);
sendRegistrationIdToBackend(registrationId);

preferences.setRegistrationId(registrationId);
preferences.setRegistrationIdVersion(getAppVersion(applicationContext));

List<Object> list = new ArrayList<Object>();
list.add(showFeedback);
list.add(sendRegistrationIdToBackend());
list.add(true);
return list;
} catch (IOException ex) {
List<Object> list = new ArrayList<Object>();
list.add(showFeedback);
list.add(false);
return list;
} catch (ParseException ex) {
list.add(false);
return list;
} catch (ClassCastException ex) {
list.add(false);
return list;
}
Expand All @@ -122,56 +131,48 @@ protected void onPostExecute(List<Object> list) {

if (!success) {
if (showFeedback) {
showDialog(activity, "Google Cloud Messaging registration failed. Please try again later.");
showDialog(activity, applicationContext.getResources().getString(R.string.gcm_fail));
}
} else {
if (showFeedback) {
showDialog(activity, "Google Cloud Messaging registration succeeded.");
showDialog(activity, applicationContext.getResources().getString(R.string.gcm_success));

}
}
}
}.execute();
}

private boolean sendRegistrationIdToBackend() {
try {
String registrationBackendUrl = "https://voipmssms-kourlas.rhcloud.com/register?" +
"did=" + URLEncoder.encode(preferences.getDid(), "UTF-8") + "&" +
"reg_id=" + URLEncoder.encode(preferences.getRegistrationId(), "UTF-8");
JSONObject result = Utils.getJson(registrationBackendUrl);
String status = (String) result.get("status");
return status.equals("success");
} catch (IOException ex) {
return false;
} catch (org.json.simple.parser.ParseException ex) {
return false;
} catch (ClassCastException ex) {
return false;
}
private boolean sendRegistrationIdToBackend(String registrationId) throws IOException, ParseException {
String registrationBackendUrl = "https://voipmssms-kourlas.rhcloud.com/register?" +
"did=" + URLEncoder.encode(preferences.getDid(), "UTF-8") + "&" +
"reg_id=" + URLEncoder.encode(registrationId, "UTF-8");
JSONObject result = Utils.getJson(registrationBackendUrl);
String status = (String) result.get("status");
return status.equals("success");
}

public void registerForGcm(Activity activity, boolean showFeedback) {
public void registerForGcm(Activity activity, boolean showFeedback, boolean force) {
if (preferences.getNotificationsEnabled()) {
if (preferences.getDid().equals("")) {
if (showFeedback) {
showDialog(activity, "Google Cloud Messaging registration requires that a DID be set.");
showDialog(activity, applicationContext.getResources().getString(R.string.gcm_did));
}
return;
}

Boolean playServices = Gcm.getInstance(applicationContext).checkPlayServices(activity);
if (playServices == null) {
if (showFeedback) {
showDialog(activity, "This device does not support Google Play Services.");
showDialog(activity, applicationContext.getResources().getString(R.string.gcm_support));
}
} else if (playServices) {
String registrationId = Gcm.getInstance(applicationContext).getRegistrationId();
if (registrationId.isEmpty()) {
if (registrationId.isEmpty() || force) {
Gcm.getInstance(applicationContext).registerInBackground(activity, showFeedback);
} else {
if (showFeedback) {
showDialog(activity, "Google Cloud Messaging registration succeeded.");
showDialog(activity, applicationContext.getResources().getString(R.string.gcm_success));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion voipms-sms/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<string name="preferences_category_sms_notifications">Notifications</string>
<string name="preferences_sms_notification">Notifications</string>
<string name="preferences_sms_notification_summary">Enable push notifications using the developer\'s Google Cloud Messaging server</string>
<string name="preferences_enable_notifications_text">To enable push notifications, you must enter the following URL in the \'SMS URL Callback\' field of the SMS settings for your DID on the VoIP.ms portal:\n\nhttp://voipmssms-kourlas.rhcloud.com/sms_callback?did={DID}\n\nNote that the developer\'s Google Cloud Messaging server will store your DID to forward callbacks from VoIP.ms to your device. See Help for more details.</string>
<string name="preferences_enable_notifications_text">To enable push notifications, you must access the SMS settings for your DID on the VoIP.ms portal and enable the \'SMS URL Callback\' feature, entering the following URL into the neighbouring field:\n\nhttp://voipmssms-kourlas.rhcloud.com/sms_callback?did={TO}\n\nNote that the developer\'s Google Cloud Messaging server will store your DID to forward callbacks from VoIP.ms to your device. See Help for more details.</string>
<string name="preferences_sms_notification_ringtone">Sound</string>
<string name="preferences_sms_notification_ringtone_default_value" translatable="false">content://settings/system/notification_sound</string>
<string name="preferences_sms_notification_vibrate">Vibrate</string>
Expand Down Expand Up @@ -108,4 +108,8 @@
<string name="cancel">Cancel</string>
<string name="ok">OK</string>
<string name="conversation_action_info">View details</string>
<string name="gcm_success">Google Cloud Messaging registration succeeded.</string>
<string name="gcm_support">This device does not support Google Play Services.</string>
<string name="gcm_did">Google Cloud Messaging registration requires that a DID be set.</string>
<string name="gcm_fail">Google Cloud Messaging registration failed. Please try again later.</string>
</resources>

0 comments on commit 1b5cc07

Please sign in to comment.