├── paho.mqtt.android.example
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ └── layout
│ │ │ │ ├── history_row.xml
│ │ │ │ └── activity_scrolling.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── paho
│ │ │ └── mqtt
│ │ │ └── java
│ │ │ └── example
│ │ │ └── HistoryAdapter.java
│ ├── test
│ │ └── java
│ │ │ └── paho
│ │ │ └── mqtt
│ │ │ └── java
│ │ │ └── example
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── paho
│ │ └── mqtt
│ │ └── java
│ │ └── example
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── org.eclipse.paho.android.sample
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── menu
│ │ │ │ ├── menu_main.xml
│ │ │ │ ├── menu_connection.xml
│ │ │ │ └── menu_edit_connection.xml
│ │ │ ├── drawable-hdpi
│ │ │ │ ├── ic_add.png
│ │ │ │ ├── ic_done.png
│ │ │ │ ├── ic_help.png
│ │ │ │ ├── ic_delete.png
│ │ │ │ ├── ic_topic.png
│ │ │ │ ├── ic_cloud_off_dark.png
│ │ │ │ └── ic_cloud_done_dark.png
│ │ │ ├── drawable-mdpi
│ │ │ │ ├── ic_add.png
│ │ │ │ ├── ic_done.png
│ │ │ │ ├── ic_help.png
│ │ │ │ ├── ic_delete.png
│ │ │ │ ├── ic_topic.png
│ │ │ │ ├── ic_cloud_off_dark.png
│ │ │ │ └── ic_cloud_done_dark.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ ├── ic_add.png
│ │ │ │ ├── ic_delete.png
│ │ │ │ ├── ic_done.png
│ │ │ │ ├── ic_help.png
│ │ │ │ ├── ic_topic.png
│ │ │ │ ├── ic_cloud_done_dark.png
│ │ │ │ └── ic_cloud_off_dark.png
│ │ │ ├── drawable-xxhdpi
│ │ │ │ ├── ic_add.png
│ │ │ │ ├── ic_done.png
│ │ │ │ ├── ic_help.png
│ │ │ │ ├── ic_topic.png
│ │ │ │ ├── ic_delete.png
│ │ │ │ ├── ic_cloud_done_dark.png
│ │ │ │ └── ic_cloud_off_dark.png
│ │ │ ├── drawable
│ │ │ │ └── paho_logo_full.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── integers.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── text_select_values.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── layout
│ │ │ │ ├── toolbar.xml
│ │ │ │ ├── switch_layout.xml
│ │ │ │ ├── text_select.xml
│ │ │ │ ├── text_input_dialog.xml
│ │ │ │ ├── fragment_connection.xml
│ │ │ │ ├── fragment_subscriptions.xml
│ │ │ │ ├── fragment_connection_history.xml
│ │ │ │ ├── fragment_home.xml
│ │ │ │ ├── nav_drawer_row.xml
│ │ │ │ ├── message_list_item.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── subscription_dialog.xml
│ │ │ │ ├── fragment_manage.xml
│ │ │ │ ├── fragment_help.xml
│ │ │ │ ├── subscription_list_item.xml
│ │ │ │ ├── fragment_navigation_drawer.xml
│ │ │ │ └── fragment_publish.xml
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ └── values-v17
│ │ │ │ └── styles.xml
│ │ ├── ic_launcher-web.png
│ │ ├── java
│ │ │ └── org
│ │ │ │ └── eclipse
│ │ │ │ └── paho
│ │ │ │ └── android
│ │ │ │ └── sample
│ │ │ │ ├── components
│ │ │ │ ├── ITextSelectCallback.java
│ │ │ │ ├── MessageListItemAdapter.java
│ │ │ │ ├── SubscriptionListItemAdapter.java
│ │ │ │ └── TextSelectComponent.java
│ │ │ │ ├── internal
│ │ │ │ ├── IReceivedMessageListener.java
│ │ │ │ ├── PersistenceException.java
│ │ │ │ └── Connections.java
│ │ │ │ ├── model
│ │ │ │ ├── NavDrawerItem.java
│ │ │ │ ├── ReceivedMessage.java
│ │ │ │ └── Subscription.java
│ │ │ │ ├── activity
│ │ │ │ ├── HomeFragment.java
│ │ │ │ ├── MessagesFragment.java
│ │ │ │ ├── ActivityConstants.java
│ │ │ │ ├── MqttTraceCallback.java
│ │ │ │ ├── HistoryFragment.java
│ │ │ │ ├── Notify.java
│ │ │ │ ├── ConnectionFragment.java
│ │ │ │ ├── MqttCallbackHandler.java
│ │ │ │ ├── ManageConnectionFragment.java
│ │ │ │ ├── PublishFragment.java
│ │ │ │ ├── HelpFragment.java
│ │ │ │ └── SubscriptionFragment.java
│ │ │ │ └── adapter
│ │ │ │ └── NavigationDrawerAdapter.java
│ │ └── AndroidManifest.xml
│ └── androidTest
│ │ └── java
│ │ └── org
│ │ └── eclipse
│ │ └── paho
│ │ └── android
│ │ └── sample
│ │ └── ApplicationTest.java
├── proguard-rules.pro
└── build.gradle
├── org.eclipse.paho.android.service
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── values-v11
│ │ │ │ └── styles.xml
│ │ │ └── values-v14
│ │ │ │ └── styles.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── org
│ │ │ └── eclipse
│ │ │ └── paho
│ │ │ └── android
│ │ │ └── service
│ │ │ ├── Status.java
│ │ │ ├── MqttServiceBinder.java
│ │ │ ├── MqttTraceHandler.java
│ │ │ ├── MqttDeliveryTokenAndroid.java
│ │ │ ├── MessageStore.java
│ │ │ ├── ParcelableMqttMessage.java
│ │ │ ├── MqttServiceConstants.java
│ │ │ ├── AlarmPingSender.java
│ │ │ └── MqttTokenAndroid.java
│ └── androidTest
│ │ ├── assets
│ │ ├── test.bks
│ │ ├── test.properties
│ │ └── updatingCertificates.md
│ │ ├── java
│ │ └── org
│ │ │ └── eclipse
│ │ │ └── paho
│ │ │ └── android
│ │ │ ├── TestCaseNotifier.java
│ │ │ └── TestProperties.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
├── .project
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── waitForEmulator.sh
├── settings.gradle
├── .github
├── PULL_REQUEST_TEMPLATE.md
└── ISSUE_TEMPLATE.md
├── .travis.yml
├── gradle.properties
├── org.eclipse.paho.android.iml
├── edl-v10
├── about.html
├── .gitignore
├── gradlew.bat
├── CONTRIBUTING.md
├── gradlew
└── README.md
/paho.mqtt.android.example/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/org.eclipse.paho.android.sample/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/org.eclipse.paho.android.service/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/org.eclipse.paho.android.service/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
December 9, 2013
10 |The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise 13 | indicated below, the Content is provided to you under the terms and conditions of the 14 | Eclipse Public License Version 1.0 ("EPL") and Eclipse Distribution License Version 1.0 ("EDL"). 15 | A copy of the EPL is available at 16 | http://www.eclipse.org/legal/epl-v10.html 17 | and a copy of the EDL is available at 18 | http://www.eclipse.org/org/documents/edl-v10.php. 19 | For purposes of the EPL, "Program" will mean the Content.
20 | 21 |If you did not receive this Content directly from the Eclipse Foundation, the Content is 22 | being redistributed by another party ("Redistributor") and different terms and conditions may 23 | apply to your use of any object code in the Content. Check the Redistributor's license that was 24 | provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise 25 | indicated below, the terms and conditions of the EPL still apply to any source code in the Content 26 | and such source code may be obtained at http://www.eclipse.org.
27 | 28 | 29 | -------------------------------------------------------------------------------- /org.eclipse.paho.android.sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 |
22 | * Implementation of the IMqttDeliveryToken interface for use from within the
23 | * MqttAndroidClient implementation
24 | */
25 | class MqttDeliveryTokenAndroid extends MqttTokenAndroid
26 | implements IMqttDeliveryToken {
27 |
28 | // The message which is being tracked by this token
29 | private MqttMessage message;
30 |
31 | MqttDeliveryTokenAndroid(MqttAndroidClient client,
32 | Object userContext, IMqttActionListener listener, MqttMessage message) {
33 | super(client, userContext, listener);
34 | this.message = message;
35 | }
36 |
37 | /**
38 | * @see org.eclipse.paho.client.mqttv3.IMqttDeliveryToken#getMessage()
39 | */
40 | @Override
41 | public MqttMessage getMessage() throws MqttException {
42 | return message;
43 | }
44 |
45 | void setMessage(MqttMessage message) {
46 | this.message = message;
47 | }
48 |
49 | void notifyDelivery(MqttMessage delivered) {
50 | message = delivered;
51 | super.notifyComplete();
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/org.eclipse.paho.android.sample/src/main/res/layout/fragment_manage.xml:
--------------------------------------------------------------------------------
1 |
21 | * Mechanism for persisting messages until we know they have been received
22 | *
22 | * A way to flow MqttMessages via Bundles/Intents
23 | *
26 | * An application will probably use this only when receiving a message from a
27 | * Service in a Bundle - the necessary code will be something like this :-
28 | *
41 | * It is unlikely that an application will directly use the methods which are
42 | * specific to this class.
43 | * Used for the column names in the database
29 | */
30 | String DUPLICATE = "duplicate";
31 | String RETAINED = "retained";
32 | String QOS = "qos";
33 | String PAYLOAD = "payload";
34 | String DESTINATION_NAME = "destinationName";
35 | String CLIENT_HANDLE = "clientHandle";
36 | String MESSAGE_ID = "messageId";
37 |
38 | /* Tags for actions passed between the Activity and the Service */
39 | String SEND_ACTION = "send";
40 | String UNSUBSCRIBE_ACTION = "unsubscribe";
41 | String SUBSCRIBE_ACTION = "subscribe";
42 | String DISCONNECT_ACTION = "disconnect";
43 | String CONNECT_ACTION = "connect";
44 | String CONNECT_EXTENDED_ACTION = "connectExtended";
45 | String MESSAGE_ARRIVED_ACTION = "messageArrived";
46 | String MESSAGE_DELIVERED_ACTION = "messageDelivered";
47 | String ON_CONNECTION_LOST_ACTION = "onConnectionLost";
48 | String TRACE_ACTION = "trace";
49 |
50 | /* Identifies an Intent which calls back to the Activity */
51 | String CALLBACK_TO_ACTIVITY = MqttService.TAG
52 | + ".callbackToActivity"+"."+VERSION;
53 |
54 | /* Identifiers for extra data on Intents broadcast to the Activity */
55 | String CALLBACK_ACTION = MqttService.TAG + ".callbackAction";
56 | String CALLBACK_STATUS = MqttService.TAG + ".callbackStatus";
57 | String CALLBACK_CLIENT_HANDLE = MqttService.TAG + "."
58 | + CLIENT_HANDLE;
59 | String CALLBACK_ERROR_MESSAGE = MqttService.TAG
60 | + ".errorMessage";
61 | String CALLBACK_EXCEPTION_STACK = MqttService.TAG
62 | + ".exceptionStack";
63 | String CALLBACK_INVOCATION_CONTEXT = MqttService.TAG + "."
64 | + "invocationContext";
65 | String CALLBACK_ACTIVITY_TOKEN = MqttService.TAG + "."
66 | + "activityToken";
67 | String CALLBACK_DESTINATION_NAME = MqttService.TAG + '.'
68 | + DESTINATION_NAME;
69 | String CALLBACK_MESSAGE_ID = MqttService.TAG + '.'
70 | + MESSAGE_ID;
71 | String CALLBACK_RECONNECT = MqttService.TAG + ".reconnect";
72 | String CALLBACK_SERVER_URI = MqttService.TAG + ".serverURI";
73 | String CALLBACK_MESSAGE_PARCEL = MqttService.TAG + ".PARCEL";
74 | String CALLBACK_TRACE_SEVERITY = MqttService.TAG
75 | + ".traceSeverity";
76 | String CALLBACK_TRACE_TAG = MqttService.TAG + ".traceTag";
77 | String CALLBACK_TRACE_ID = MqttService.TAG + ".traceId";
78 | String CALLBACK_ERROR_NUMBER = MqttService.TAG
79 | + ".ERROR_NUMBER";
80 |
81 | String CALLBACK_EXCEPTION = MqttService.TAG + ".exception";
82 |
83 | //Intent prefix for Ping sender.
84 | String PING_SENDER = MqttService.TAG + ".pingSender.";
85 |
86 | //Constant for wakelock
87 | String PING_WAKELOCK = MqttService.TAG + ".client.";
88 | String WAKELOCK_NETWORK_INTENT = MqttService.TAG + "";
89 |
90 | //Trace severity levels
91 | String TRACE_ERROR = "error";
92 | String TRACE_DEBUG = "debug";
93 | String TRACE_EXCEPTION = "exception";
94 |
95 |
96 | //exception code for non MqttExceptions
97 | int NON_MQTT_EXCEPTION = -1;
98 |
99 | }
--------------------------------------------------------------------------------
/org.eclipse.paho.android.sample/src/main/java/org/eclipse/paho/android/sample/activity/ManageConnectionFragment.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 1999, 2014 IBM Corp.
3 | *
4 | * All rights reserved. This program and the accompanying materials
5 | * are made available under the terms of the Eclipse Public License v1.0
6 | * and Eclipse Distribution License v1.0 which accompany this distribution.
7 | *
8 | * The Eclipse Public License is available at
9 | * http://www.eclipse.org/legal/epl-v10.html
10 | * and the Eclipse Distribution License is available at
11 | * http://www.eclipse.org/org/documents/edl-v10.php.
12 | *
13 | * Authors:
14 | * - James Sutton - Created file 10-2015
15 | */
16 | package org.eclipse.paho.android.sample.activity;
17 |
18 | import android.os.Bundle;
19 | import android.support.v4.app.Fragment;
20 | import android.support.v4.app.FragmentManager;
21 | import android.support.v4.app.FragmentTransaction;
22 | import android.view.LayoutInflater;
23 | import android.view.View;
24 | import android.view.ViewGroup;
25 | import android.widget.Button;
26 | import android.widget.TextView;
27 |
28 | import org.eclipse.paho.android.sample.R;
29 | import org.eclipse.paho.android.sample.internal.Connections;
30 |
31 | import java.util.Map;
32 |
33 |
34 | public class ManageConnectionFragment extends Fragment {
35 | private Connection connection;
36 | private Map This class implements the {@link MqttPingSender} pinger interface
37 | * allowing applications to send ping packet to server every keep alive interval.
38 | *
24 | * Implementation of the IMqttToken interface for use from within the
25 | * MqttAndroidClient implementation
26 | */
27 |
28 | class MqttTokenAndroid implements IMqttToken {
29 |
30 | private IMqttActionListener listener;
31 |
32 | private volatile boolean isComplete;
33 |
34 | private volatile MqttException lastException;
35 |
36 | private Object waitObject = new Object();
37 |
38 | private MqttAndroidClient client;
39 |
40 | private Object userContext;
41 |
42 | private String[] topics;
43 |
44 | private IMqttToken delegate; // specifically for getMessageId
45 |
46 | private MqttException pendingException;
47 |
48 | /**
49 | * Standard constructor
50 | *
51 | * @param client used to pass MqttAndroidClient object
52 | * @param userContext used to pass context
53 | * @param listener optional listener that will be notified when the action completes. Use null if not required.
54 | */
55 | MqttTokenAndroid(MqttAndroidClient client,
56 | Object userContext, IMqttActionListener listener) {
57 | this(client, userContext, listener, null);
58 | }
59 |
60 | /**
61 | * Constructor for use with subscribe operations
62 | *
63 | * @param client used to pass MqttAndroidClient object
64 | * @param userContext used to pass context
65 | * @param listener optional listener that will be notified when the action completes. Use null if not required.
66 | * @param topics topics to subscribe to, which can include wildcards.
67 | */
68 | MqttTokenAndroid(MqttAndroidClient client,
69 | Object userContext, IMqttActionListener listener, String[] topics) {
70 | this.client = client;
71 | this.userContext = userContext;
72 | this.listener = listener;
73 | this.topics = topics;
74 | }
75 |
76 | /**
77 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#waitForCompletion()
78 | */
79 | @Override
80 | public void waitForCompletion() throws MqttException, MqttSecurityException {
81 | synchronized (waitObject) {
82 | try {
83 | waitObject.wait();
84 | }
85 | catch (InterruptedException e) {
86 | // do nothing
87 | }
88 | }
89 | if (pendingException != null) {
90 | throw pendingException;
91 | }
92 | }
93 |
94 | /**
95 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#waitForCompletion(long)
96 | */
97 | @Override
98 | public void waitForCompletion(long timeout) throws MqttException,
99 | MqttSecurityException {
100 | synchronized (waitObject) {
101 | try {
102 | waitObject.wait(timeout);
103 | }
104 | catch (InterruptedException e) {
105 | // do nothing
106 | }
107 | if (!isComplete) {
108 | throw new MqttException(MqttException.REASON_CODE_CLIENT_TIMEOUT);
109 | }
110 | if (pendingException != null) {
111 | throw pendingException;
112 | }
113 | }
114 | }
115 |
116 | /**
117 | * notify successful completion of the operation
118 | */
119 | void notifyComplete() {
120 | synchronized (waitObject) {
121 | isComplete = true;
122 | waitObject.notifyAll();
123 | if (listener != null) {
124 | listener.onSuccess(this);
125 | }
126 | }
127 | }
128 |
129 | /**
130 | * notify unsuccessful completion of the operation
131 | */
132 | void notifyFailure(Throwable exception) {
133 | synchronized (waitObject) {
134 | isComplete = true;
135 | if (exception instanceof MqttException) {
136 | pendingException = (MqttException) exception;
137 | }
138 | else {
139 | pendingException = new MqttException(exception);
140 | }
141 | waitObject.notifyAll();
142 | if (exception instanceof MqttException) {
143 | lastException = (MqttException) exception;
144 | }
145 | if (listener != null) {
146 | listener.onFailure(this, exception);
147 | }
148 | }
149 |
150 | }
151 |
152 | /**
153 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#isComplete()
154 | */
155 | @Override
156 | public boolean isComplete() {
157 | return isComplete;
158 | }
159 |
160 | void setComplete(boolean complete) {
161 | isComplete = complete;
162 | }
163 |
164 | /**
165 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getException()
166 | */
167 | @Override
168 | public MqttException getException() {
169 | return lastException;
170 | }
171 |
172 | void setException(MqttException exception) {
173 | lastException = exception;
174 | }
175 |
176 | /**
177 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getClient()
178 | */
179 | @Override
180 | public IMqttAsyncClient getClient() {
181 | return client;
182 | }
183 |
184 | /**
185 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#setActionCallback(IMqttActionListener)
186 | */
187 | @Override
188 | public void setActionCallback(IMqttActionListener listener) {
189 | this.listener = listener;
190 | }
191 |
192 | /**
193 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getActionCallback()
194 | */
195 | @Override
196 | public IMqttActionListener getActionCallback() {
197 | return listener;
198 | }
199 |
200 | /**
201 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getTopics()
202 | */
203 | @Override
204 | public String[] getTopics() {
205 | return topics;
206 | }
207 |
208 | /**
209 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#setUserContext(Object)
210 | */
211 | @Override
212 | public void setUserContext(Object userContext) {
213 | this.userContext = userContext;
214 |
215 | }
216 |
217 | /**
218 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getUserContext()
219 | */
220 | @Override
221 | public Object getUserContext() {
222 | return userContext;
223 | }
224 |
225 | void setDelegate(IMqttToken delegate) {
226 | this.delegate = delegate;
227 | }
228 |
229 | /**
230 | * @see org.eclipse.paho.client.mqttv3.IMqttToken#getMessageId()
231 | */
232 | @Override
233 | public int getMessageId() {
234 | return (delegate != null) ? delegate.getMessageId() : 0;
235 | }
236 |
237 | @Override
238 | public MqttWireMessage getResponse() {
239 | return delegate.getResponse();
240 | }
241 |
242 | @Override
243 | public boolean getSessionPresent() {
244 | return delegate.getSessionPresent();
245 | }
246 |
247 | @Override
248 | public int[] getGrantedQos() {
249 | return delegate.getGrantedQos();
250 | }
251 |
252 | }
253 |
--------------------------------------------------------------------------------
24 | *
33 | */
34 | interface MessageStore {
35 |
36 | /**
37 | * External representation of a stored message
38 | */
39 | interface StoredMessage {
40 | /**
41 | * @return the identifier for the message within the store
42 | */
43 | String getMessageId();
44 |
45 | /**
46 | * @return the identifier of the client which stored this message
47 | */
48 | String getClientHandle();
49 |
50 | /**
51 | * @return the topic on which the message was received
52 | */
53 | String getTopic();
54 |
55 | /**
56 | * @return the identifier of the client which stored this message
57 | */
58 | MqttMessage getMessage();
59 | }
60 |
61 | /**
62 | * Store a message and return an identifier for it
63 | *
64 | * @param clientHandle
65 | * identifier for the client
66 | * @param message
67 | * message to be stored
68 | * @return a unique identifier for it
69 | */
70 | String storeArrived(String clientHandle, String Topic,
71 | MqttMessage message);
72 |
73 | /**
74 | * Discard a message - called when we are certain that an arrived message
75 | * has reached the application.
76 | *
77 | * @param clientHandle
78 | * identifier for the client
79 | * @param id
80 | * id of message to be discarded
81 | */
82 | boolean discardArrived(String clientHandle, String id);
83 |
84 | /**
85 | * Get all the stored messages, usually for a specific client
86 | *
87 | * @param clientHandle
88 | * identifier for the client - if null, then messages for all
89 | * clients are returned
90 | */
91 | Iterator
30 | *
39 | *
40 | *
31 | * private void messageArrivedAction(Bundle data) {
32 | * ParcelableMqttMessage message = (ParcelableMqttMessage) data
33 | * .getParcelable(MqttServiceConstants.CALLBACK_MESSAGE_PARCEL);
34 | * Use the normal {@link MqttMessage} methods on the the message object.
35 | * }
36 | *
37 | *
38 | * MqttCallbackHandler object
42 | * @param context The application's context
43 | * @param clientHandle The handle to a {@link Connection} object
44 | */
45 | public MqttCallbackHandler(Context context, String clientHandle)
46 | {
47 | this.context = context;
48 | this.clientHandle = clientHandle;
49 | }
50 |
51 | /**
52 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#connectionLost(java.lang.Throwable)
53 | */
54 | @Override
55 | public void connectionLost(Throwable cause) {
56 | if (cause != null) {
57 | Log.d(TAG, "Connection Lost: " + cause.getMessage());
58 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
59 | c.addAction("Connection Lost");
60 | c.changeConnectionStatus(Connection.ConnectionStatus.DISCONNECTED);
61 |
62 | String message = context.getString(R.string.connection_lost, c.getId(), c.getHostName());
63 |
64 | //build intent
65 | Intent intent = new Intent();
66 | intent.setClassName(context, activityClass);
67 | intent.putExtra("handle", clientHandle);
68 |
69 | //notify the user
70 | Notify.notifcation(context, message, intent, R.string.notifyTitle_connectionLost);
71 | }
72 | }
73 |
74 | /**
75 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage)
76 | */
77 | @Override
78 | public void messageArrived(String topic, MqttMessage message) throws Exception {
79 |
80 | //Get connection object associated with this object
81 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
82 | c.messageArrived(topic, message);
83 | //get the string from strings.xml and format
84 | String messageString = context.getString(R.string.messageRecieved, new String(message.getPayload()), topic+";qos:"+message.getQos()+";retained:"+message.isRetained());
85 |
86 | Log.i(TAG, messageString);
87 |
88 | //update client history
89 | c.addAction(messageString);
90 |
91 | }
92 |
93 | /**
94 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)
95 | */
96 | @Override
97 | public void deliveryComplete(IMqttDeliveryToken token) {
98 | // Do nothing
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/org.eclipse.paho.android.sample/src/main/res/layout/fragment_publish.xml:
--------------------------------------------------------------------------------
1 | Connections is a singleton class which stores all the connection objects
30 | * in one central place so they can be passed between activities using a client handle.
31 | */
32 | public class Connections {
33 |
34 | /** Singleton instance of Connections**/
35 | private static Connections instance = null;
36 |
37 | /** List of {@link Connection} object **/
38 | private HashMapConnections, if Connections has yet to be created, it will
65 | * create and return that instance.
66 | * @param context The applications context used to create the Connections object if it is not already initialised
67 | * @return Connections instance
68 | */
69 | public synchronized static Connections getInstance(Context context){
70 | if(instance == null){
71 | instance = new Connections(context);
72 | }
73 | return instance;
74 | }
75 |
76 | /**
77 | * Finds and returns a {@link Connection} object that the given client handle points to
78 | * @param handle The handle to the {@link Connection} to return
79 | * @return a connection associated with the client handle, null if one is not found
80 | */
81 | public Connection getConnection(String handle){
82 | return connections.get(handle);
83 | }
84 |
85 | /**
86 | * Adds a {@link Connection} object to the collection of connections associated with this object
87 | * @param connection {@link Connection} to add
88 | */
89 | public void addConnection(Connection connection){
90 | connections.put(connection.handle(), connection);
91 | try{
92 | persistence.persistConnection(connection);
93 | } catch (PersistenceException e){
94 | // @todo Handle this error more appropriately
95 | //error persisting well lets just swallow this
96 | e.printStackTrace();
97 | }
98 |
99 | }
100 |
101 | // --Commented out by Inspection START (12/10/2016, 10:21):
102 | // /**
103 | // * Create a fully initialised MqttAndroidClient for the parameters given
104 | // * @param context The Applications context
105 | // * @param serverURI The ServerURI to connect to
106 | // * @param clientId The clientId for this client
107 | // * @return new instance of MqttAndroidClient
108 | // */
109 | // public MqttAndroidClient createClient(Context context, String serverURI, String clientId){
110 | // return new MqttAndroidClient(context, serverURI, clientId);
111 | // }
112 | // --Commented out by Inspection STOP (12/10/2016, 10:21)
113 |
114 | /**
115 | * Get all the connections associated with this Connections object.
116 | * @return Map of connections
117 | */
118 | public Map