Skip to content

Commit 51d9185

Browse files
Ian CraggsGerrit Code Review @ Eclipse.org
authored andcommitted
Merge "Adding the ability for users to choose whether they want to recieve notifications of incoming messages in the Android Sample App. This can be set when creating a subscription Signed-off-by: James Sutton <james.sutton@uk.ibm.com>" into develop
2 parents ee82eda + 2bd8b12 commit 51d9185

9 files changed

Lines changed: 100 additions & 35 deletions

File tree

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/activity/Connection.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import android.content.Context;
5+
import android.content.Intent;
56
import android.text.Html;
67
import android.text.Spanned;
78

@@ -34,6 +35,8 @@ public class Connection {
3435
* Basic information about the client
3536
*/
3637

38+
private static final String activityClass = "org.eclipse.paho.android.sample.activity.MainActivity";
39+
3740
/** ClientHandle for this Connection object **/
3841
private String clientHandle = null;
3942

@@ -135,6 +138,7 @@ public void updateConnection(String clientId, String host, int port, boolean tls
135138
this.tlsConnection = tlsConnection;
136139
MqttAndroidClient client = new MqttAndroidClient(context, uri, clientId);
137140
this.client = client;
141+
138142
}
139143

140144

@@ -471,6 +475,22 @@ public void messageArrived(String topic, MqttMessage message){
471475
messageHistory.add(0, msg);
472476
if(subscriptions.containsKey(topic)){
473477
subscriptions.get(topic).setLastMessage(new String(message.getPayload()));
478+
if(subscriptions.get(topic).isEnableNotifications()){
479+
//create intent to start activity
480+
Intent intent = new Intent();
481+
intent.setClassName(context, activityClass);
482+
intent.putExtra("handle", clientHandle);
483+
484+
//format string args
485+
Object[] notifyArgs = new String[3];
486+
notifyArgs[0] = this.getId();
487+
notifyArgs[1] = new String(message.getPayload());
488+
notifyArgs[2] = topic;
489+
490+
//notify the user
491+
Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent, R.string.notifyTitle);
492+
493+
}
474494
}
475495

476496
for(IReceivedMessageListener listener : receivedMessageListeners){

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/activity/MqttCallbackHandler.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public MqttCallbackHandler(Context context, String clientHandle)
5454
@Override
5555
public void connectionLost(Throwable cause) {
5656
if (cause != null) {
57+
Log.d(TAG, "Connection Lost: " + cause.getMessage());
5758
Connection c = Connections.getInstance(context).getConnection(clientHandle);
5859
c.addAction("Connection Lost");
5960
c.changeConnectionStatus(Connection.ConnectionStatus.DISCONNECTED);
@@ -82,19 +83,6 @@ public void messageArrived(String topic, MqttMessage message) throws Exception {
8283
//get the string from strings.xml and format
8384
String messageString = context.getString(R.string.messageRecieved, new String(message.getPayload()), topic+";qos:"+message.getQos()+";retained:"+message.isRetained());
8485

85-
//create intent to start activity
86-
Intent intent = new Intent();
87-
intent.setClassName(context, activityClass);
88-
intent.putExtra("handle", clientHandle);
89-
90-
//format string args
91-
Object[] notifyArgs = new String[3];
92-
notifyArgs[0] = c.getId();
93-
notifyArgs[1] = new String(message.getPayload());
94-
notifyArgs[2] = topic;
95-
96-
//notify the user
97-
Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent, R.string.notifyTitle);
9886
Log.i(TAG, messageString);
9987

10088
//update client history

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/activity/SubscriptionFragment.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import android.widget.AdapterView;
1616
import android.widget.ArrayAdapter;
1717
import android.widget.Button;
18+
import android.widget.CompoundButton;
1819
import android.widget.EditText;
1920
import android.widget.ListView;
2021
import android.widget.Spinner;
22+
import android.widget.Switch;
2123

2224
import org.eclipse.paho.android.sample.R;
2325
import org.eclipse.paho.android.sample.model.Subscription;
@@ -110,14 +112,18 @@ public void onNothingSelected(AdapterView<?> parent) {
110112
}
111113
});
112114

115+
final Switch notifySwitch = (Switch) promptView.findViewById(R.id.show_notifications_switch);
116+
117+
118+
119+
113120
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(((AppCompatActivity) getActivity()).getSupportActionBar().getThemedContext());
114121
alertDialogBuilder.setView(promptView);
115-
alertDialogBuilder.setCancelable(true).setPositiveButton("OK", new DialogInterface.OnClickListener() {
122+
alertDialogBuilder.setCancelable(true).setPositiveButton(R.string.subscribe_ok, new DialogInterface.OnClickListener() {
116123
public void onClick(DialogInterface dialog, int id) {
117124
String topic = topicText.getText().toString();
118125

119-
System.out.println("OK clicked! " + topic + " : " + temp_qos_value);
120-
Subscription subscription = new Subscription(topic, temp_qos_value, connection.handle());
126+
Subscription subscription = new Subscription(topic, temp_qos_value, connection.handle(), notifySwitch.isChecked());
121127
subscriptions.add(subscription);
122128
try {
123129
connection.addNewSubscription(subscription);
@@ -129,7 +135,7 @@ public void onClick(DialogInterface dialog, int id) {
129135
}
130136

131137
;
132-
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
138+
}).setNegativeButton(R.string.subscribe_cancel, new DialogInterface.OnClickListener() {
133139
public void onClick(DialogInterface dialog, int id) {
134140
dialog.cancel();
135141
}

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/components/SubscriptionListItemAdapter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ public View getView(final int position, View convertView, ViewGroup parent){
3535
ImageView topicDeleteButton = (ImageView) rowView.findViewById(R.id.topic_delete_image);
3636
TextView qosTextView = (TextView) rowView.findViewById(R.id.qos_label);
3737
topicTextView.setText(topics.get(position).getTopic());
38-
qosTextView.setText("QoS: " + topics.get(position).getQos());
38+
String qosString = context.getString(R.string.qos_text, topics.get(position).getQos());
39+
qosTextView.setText(qosString);
40+
TextView notifyTextView = (TextView) rowView.findViewById(R.id.show_notifications_label);
41+
String notifyString = context.getString(R.string.notify_text, (topics.get(position).isEnableNotifications() ? context.getString(R.string.enabled) : context.getString(R.string.disabled)));
42+
notifyTextView.setText(notifyString);
3943

4044
topicDeleteButton.setOnClickListener(new View.OnClickListener() {
4145
@Override
4246
public void onClick(View v) {
4347

44-
for(OnUnsubscribeListner listner : unsubscribeListners){
48+
for (OnUnsubscribeListner listner : unsubscribeListners) {
4549
listner.onUnsubscribe(topics.get(position));
4650
}
4751
topics.remove(position);

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/internal/Persistence.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.database.sqlite.SQLiteDatabase;
88
import android.database.sqlite.SQLiteOpenHelper;
99
import android.provider.BaseColumns;
10+
import android.util.Log;
1011

1112
import org.eclipse.paho.android.sample.activity.Connection;
1213
import org.eclipse.paho.android.sample.model.Subscription;
@@ -24,6 +25,8 @@
2425
*/
2526
public class Persistence extends SQLiteOpenHelper implements BaseColumns {
2627

28+
public static final String TAG = "Persistence";
29+
2730
/** The version of the database **/
2831
public static final int DATABASE_VERSION = 1;
2932

@@ -78,6 +81,8 @@ public class Persistence extends SQLiteOpenHelper implements BaseColumns {
7881
public static final String SUBSCRIPTIONS_COLUMN_TOPIC = "topic";
7982
/** Table column for the subscription qos **/
8083
public static final String SUBSCRIPTIONS_COLUMN_QOS = "qos";
84+
/** Table column for the subscription enable notification setting **/
85+
public static final String SUBSCRIPTIONS_COLUMN_NOTIFY = "notify";
8186

8287

8388
//sql lite data types
@@ -115,6 +120,7 @@ public class Persistence extends SQLiteOpenHelper implements BaseColumns {
115120
COLUMN_CLIENT_HANDLE + TEXT_TYPE + COMMA_SEP +
116121
COLUMN_HOST + TEXT_TYPE + COMMA_SEP +
117122
SUBSCRIPTIONS_COLUMN_TOPIC + TEXT_TYPE + COMMA_SEP +
123+
SUBSCRIPTIONS_COLUMN_NOTIFY + INT_TYPE + COMMA_SEP +
118124
SUBSCRIPTIONS_COLUMN_QOS + INT_TYPE + ");";
119125

120126
/** Delete tables entry **/
@@ -191,9 +197,6 @@ public void updateConnection(Connection connection) throws PersistenceException{
191197
String[] whereArgs = new String[1];
192198
whereArgs[0] = String.valueOf(connection.persistenceId());
193199
int rowsUpdated = db.update(TABLE_CONNECTIONS, getValues(connection), whereClause, whereArgs);
194-
if(rowsUpdated == 0){
195-
System.out.println("No rows updated for some reason...");
196-
}
197200
}
198201

199202
ContentValues getValues(Connection connection){
@@ -239,6 +242,7 @@ public long persistSubscription(Subscription subscription) throws PersistenceExc
239242
ContentValues values = new ContentValues();
240243
values.put(COLUMN_CLIENT_HANDLE, subscription.getClientHandle());
241244
values.put(SUBSCRIPTIONS_COLUMN_TOPIC, subscription.getTopic());
245+
values.put(SUBSCRIPTIONS_COLUMN_NOTIFY, subscription.isEnableNotifications() ? 1 : 0); //convert boolean to int and then put in values
242246
values.put(SUBSCRIPTIONS_COLUMN_QOS, subscription.getQos());
243247

244248
long newRowId = db.insert(TABLE_SUBSCRIPTIONS, null, values);
@@ -257,8 +261,8 @@ public long persistSubscription(Subscription subscription) throws PersistenceExc
257261
* @param subscription The subscription to delete from the database
258262
*/
259263
public void deleteSubscription(Subscription subscription) {
264+
Log.d(TAG, "Deleting Subscription: " + subscription.toString());
260265
SQLiteDatabase db = getWritableDatabase();
261-
System.out.println("Persistence- deleteSubscription: " + subscription.getPersistenceId());
262266
db.delete(TABLE_SUBSCRIPTIONS, _ID + "=?", new String[]{String.valueOf(subscription.getPersistenceId())});
263267
db.close();
264268
//don't care if it failed, means it's not in the db therefore no need to delete
@@ -300,6 +304,7 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx
300304
String[] subscriptionColumns = {
301305
COLUMN_CLIENT_HANDLE,
302306
SUBSCRIPTIONS_COLUMN_TOPIC,
307+
SUBSCRIPTIONS_COLUMN_NOTIFY,
303308
SUBSCRIPTIONS_COLUMN_QOS,
304309
_ID
305310
};
@@ -338,9 +343,9 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx
338343
int timeout = c.getInt(c.getColumnIndexOrThrow(COLUMN_TIME_OUT));
339344

340345
//get all values that need converting and convert integers to booleans in line using "condition ? trueValue : falseValue"
341-
boolean cleanSession = c.getInt(c.getColumnIndexOrThrow(COLUMN_CLEAN_SESSION)) == 1 ? true : false;
342-
boolean retained = c.getInt(c.getColumnIndexOrThrow(COLUMN_RETAINED)) == 1 ? true : false;
343-
boolean ssl = c.getInt(c.getColumnIndexOrThrow(COLUMN_ssl)) == 1 ? true : false;
346+
boolean cleanSession = c.getInt(c.getColumnIndexOrThrow(COLUMN_CLEAN_SESSION)) == 1 ;
347+
boolean retained = c.getInt(c.getColumnIndexOrThrow(COLUMN_RETAINED)) == 1;
348+
boolean ssl = c.getInt(c.getColumnIndexOrThrow(COLUMN_ssl)) == 1;
344349

345350
//rebuild objects starting with the connect options
346351
MqttConnectOptions opts = new MqttConnectOptions();
@@ -373,10 +378,11 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx
373378
Long sub_id = sub_c.getLong(sub_c.getColumnIndexOrThrow(_ID));
374379
String sub_clientHandle = sub_c.getString(sub_c.getColumnIndexOrThrow(COLUMN_CLIENT_HANDLE));
375380
String sub_topic = sub_c.getString(sub_c.getColumnIndexOrThrow(SUBSCRIPTIONS_COLUMN_TOPIC));
381+
boolean sub_notify = sub_c.getInt(sub_c.getColumnIndexOrThrow(SUBSCRIPTIONS_COLUMN_NOTIFY)) == 1;
376382
int sub_qos = sub_c.getInt(sub_c.getColumnIndexOrThrow(SUBSCRIPTIONS_COLUMN_QOS));
377-
Subscription sub = new Subscription(sub_topic, sub_qos, sub_clientHandle);
383+
Subscription sub = new Subscription(sub_topic, sub_qos, sub_clientHandle, sub_notify);
378384
sub.setPersistenceId(sub_id);
379-
System.out.println("Restoring Subscription: " + sub.toString());
385+
Log.d(TAG, "Restoring Subscription: " + sub.toString());
380386
subscriptions.add(sub);
381387
}
382388

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/model/Subscription.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ public class Subscription {
1010
private String lastMessage;
1111
private String clientHandle;
1212
private long persistenceId;
13+
private boolean enableNotifications;
1314

14-
public Subscription(String topic, int qos, String clientHandle){
15+
public Subscription(String topic, int qos, String clientHandle, boolean enableNotifications){
1516
this.topic = topic;
1617
this.qos = qos;
1718
this.clientHandle = clientHandle;
19+
this.enableNotifications = enableNotifications;
1820
}
1921

2022
public String getTopic() {
@@ -57,13 +59,22 @@ public void setPersistenceId(long persistenceId) {
5759
this.persistenceId = persistenceId;
5860
}
5961

62+
public boolean isEnableNotifications() {
63+
return enableNotifications;
64+
}
65+
66+
public void setEnableNotifications(boolean enableNotifications) {
67+
this.enableNotifications = enableNotifications;
68+
}
69+
6070
@Override
6171
public String toString() {
6272
return "Subscription{" +
6373
"topic='" + topic + '\'' +
6474
", qos=" + qos +
6575
", clientHandle='" + clientHandle + '\'' +
6676
", persistenceId='" + persistenceId + '\'' +
77+
", enableNotifications='" + enableNotifications + '\''+
6778
'}';
6879
}
6980
}

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/res/layout/subscription_dialog.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@
3333
android:id="@+id/subscription_qos_spinner" />
3434

3535

36+
<Switch
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:textAppearance="?android:attr/textAppearanceMedium"
40+
android:textColor="?android:attr/textColorSecondary"
41+
android:text="@string/notify_label"
42+
android:id="@+id/show_notifications_switch" />
43+
44+
3645
</LinearLayout>

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/res/layout/subscription_list_item.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@
1010
android:layout_alignParentBottom="true"
1111
android:layout_alignParentTop="true"
1212
android:layout_marginRight="6dip"
13-
android:contentDescription="TODO"
1413
android:src="@drawable/ic_topic" />
1514

1615
<TextView
1716
android:id="@+id/qos_label"
18-
android:layout_width="fill_parent"
17+
android:layout_width="100dip"
1918
android:layout_height="26dip"
2019
android:layout_alignParentBottom="true"
21-
android:layout_alignParentRight="true"
2220
android:layout_toRightOf="@id/icon"
2321
android:ellipsize="marquee"
2422
android:singleLine="true"
25-
android:text="QoS:"
23+
android:text="@string/qos_text"
2624
android:textSize="12sp" />
2725

26+
<TextView
27+
android:id="@+id/show_notifications_label"
28+
android:layout_width="fill_parent"
29+
android:layout_height="26dip"
30+
android:layout_alignParentBottom="true"
31+
android:layout_toRightOf="@id/qos_label"
32+
android:text="@string/notify_text"
33+
/>
34+
2835
<TextView
2936
android:id="@+id/message_text"
3037
android:layout_width="fill_parent"
@@ -34,7 +41,7 @@
3441
android:layout_alignWithParentIfMissing="true"
3542
android:layout_toRightOf="@id/icon"
3643
android:gravity="center_vertical"
37-
android:text="Example application"
44+
android:text="@string/topic_label"
3845
android:textSize="16sp" />
3946

4047

@@ -43,7 +50,6 @@
4350
android:layout_width="wrap_content"
4451
android:layout_height="fill_parent"
4552
android:layout_marginRight="6dip"
46-
android:contentDescription="TODO"
4753
android:src="@drawable/ic_delete"
4854
android:layout_alignEnd="@+id/message_text"
4955
android:layout_alignParentRight="true" />

org.eclipse.paho.android.service/org.eclipse.paho.android.sample/src/main/res/values/strings.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@
108108
<string name="failure_connect">Client failed to connect.&lt;br/&gt; &lt;small&gt;Reason: %s&lt;/small&gt;</string>
109109
<string name="client_connected">Client connected successfully</string>
110110

111+
<!-- Strings for Subscriptions Fragment -->
112+
<string name="topic_label">Topic</string>
113+
<string name="qos_label">QoS</string>
114+
<string name="notify_label">Show Notifications</string>
115+
<string name="subscribe_ok">Ok</string>
116+
<string name="subscribe_cancel">Cancel</string>
117+
<string name="qos_text" >QoS: %1$s</string>
118+
<string name="notify_text">Notifications: %1$s</string>
119+
120+
111121

112122
<!-- Strings for Help Fragment -->
113123
<string name="help_and_feedback">Help &amp; Feedback</string>
@@ -116,4 +126,9 @@
116126
<string name="help_email_button">Get Help or send Feedback</string>
117127
<string name="help_paho_website">Go to the Paho Website</string>
118128
<string name="help_enable_logging">Enable Paho Logging</string>
129+
130+
131+
132+
<string name="enabled">Enabled</string>
133+
<string name="disabled">Disabled</string>
119134
</resources>

0 commit comments

Comments
 (0)