ActionListener
48 | **/
49 | private Action action;
50 | /** The arguments passed to be used for formatting strings**/
51 | private String[] additionalArgs;
52 | /** Handle of the {@link Connection} this action was being executed on **/
53 | private String clientHandle;
54 | /** {@link Context} for performing various operations **/
55 | private Context context;
56 |
57 | /**
58 | * Creates a generic action listener for actions performed form any activity
59 | *
60 | * @param context
61 | * The application context
62 | * @param action
63 | * The action that is being performed
64 | * @param clientHandle
65 | * The handle for the client which the action is being performed
66 | * on
67 | * @param additionalArgs
68 | * Used for as arguments for string formating
69 | */
70 | public ActionListener(Context context, Action action,
71 | String clientHandle, String... additionalArgs) {
72 | this.context = context;
73 | this.action = action;
74 | this.clientHandle = clientHandle;
75 | this.additionalArgs = additionalArgs;
76 | }
77 |
78 | /**
79 | * The action associated with this listener has been successful.
80 | *
81 | * @param asyncActionToken
82 | * This argument is not used
83 | */
84 | @Override
85 | public void onSuccess(IMqttToken asyncActionToken) {
86 | switch (action) {
87 | case CONNECT :
88 | connect();
89 | break;
90 | case DISCONNECT :
91 | disconnect();
92 | break;
93 | case SUBSCRIBE :
94 | subscribe();
95 | break;
96 | case PUBLISH :
97 | publish();
98 | break;
99 | }
100 |
101 | }
102 |
103 | /**
104 | * A publish action has been successfully completed, update connection
105 | * object associated with the client this action belongs to, then notify the
106 | * user of success
107 | */
108 | private void publish() {
109 |
110 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
111 | String actionTaken = context.getString(R.string.toast_pub_success,
112 | (Object[]) additionalArgs);
113 | c.addAction(actionTaken);
114 | Notify.toast(context, actionTaken, Toast.LENGTH_SHORT);
115 | }
116 |
117 | /**
118 | * A subscribe action has been successfully completed, update the connection
119 | * object associated with the client this action belongs to and then notify
120 | * the user of success
121 | */
122 | private void subscribe() {
123 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
124 | String actionTaken = context.getString(R.string.toast_sub_success,
125 | (Object[]) additionalArgs);
126 | c.addAction(actionTaken);
127 | Notify.toast(context, actionTaken, Toast.LENGTH_SHORT);
128 |
129 | }
130 |
131 | /**
132 | * A disconnection action has been successfully completed, update the
133 | * connection object associated with the client this action belongs to and
134 | * then notify the user of success.
135 | */
136 | private void disconnect() {
137 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
138 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
139 | String actionTaken = context.getString(R.string.toast_disconnected);
140 | c.addAction(actionTaken);
141 |
142 | }
143 |
144 | /**
145 | * A connection action has been successfully completed, update the
146 | * connection object associated with the client this action belongs to and
147 | * then notify the user of success.
148 | */
149 | private void connect() {
150 |
151 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
152 | c.changeConnectionStatus(Connection.ConnectionStatus.CONNECTED);
153 | c.addAction("Client Connected");
154 |
155 | }
156 |
157 | /**
158 | * The action associated with the object was a failure
159 | *
160 | * @param token
161 | * This argument is not used
162 | * @param exception
163 | * The exception which indicates why the action failed
164 | */
165 | @Override
166 | public void onFailure(IMqttToken token, Throwable exception) {
167 | switch (action) {
168 | case CONNECT :
169 | connect(exception);
170 | break;
171 | case DISCONNECT :
172 | disconnect(exception);
173 | break;
174 | case SUBSCRIBE :
175 | subscribe(exception);
176 | break;
177 | case PUBLISH :
178 | publish(exception);
179 | break;
180 | }
181 |
182 | }
183 |
184 | /**
185 | * A publish action was unsuccessful, notify user and update client history
186 | *
187 | * @param exception
188 | * This argument is not used
189 | */
190 | private void publish(Throwable exception) {
191 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
192 | String action = context.getString(R.string.toast_pub_failed,
193 | (Object[]) additionalArgs);
194 | c.addAction(action);
195 | Notify.toast(context, action, Toast.LENGTH_SHORT);
196 |
197 | }
198 |
199 | /**
200 | * A subscribe action was unsuccessful, notify user and update client history
201 | * @param exception This argument is not used
202 | */
203 | private void subscribe(Throwable exception) {
204 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
205 | String action = context.getString(R.string.toast_sub_failed,
206 | (Object[]) additionalArgs);
207 | c.addAction(action);
208 | Notify.toast(context, action, Toast.LENGTH_SHORT);
209 |
210 | }
211 |
212 | /**
213 | * A disconnect action was unsuccessful, notify user and update client history
214 | * @param exception This argument is not used
215 | */
216 | private void disconnect(Throwable exception) {
217 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
218 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
219 | c.addAction("Disconnect Failed - an error occured");
220 |
221 | }
222 |
223 | /**
224 | * A connect action was unsuccessful, notify the user and update client history
225 | * @param exception This argument is not used
226 | */
227 | private void connect(Throwable exception) {
228 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
229 | c.changeConnectionStatus(Connection.ConnectionStatus.ERROR);
230 | c.addAction("Client failed to connect");
231 |
232 | }
233 |
234 | }
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/ActivityConstants.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import org.eclipse.paho.client.mqttv3.MqttMessage;
16 |
17 | /**
18 | * This Class provides constants used for returning results from an activity
19 | *
20 | */
21 | public class ActivityConstants {
22 |
23 | /** Application TAG for logs where class name is not used*/
24 | static final String TAG = "MQTT Android";
25 |
26 | /*Default values **/
27 |
28 | /** Default QOS value*/
29 | static final int defaultQos = 0;
30 | /** Default timeout*/
31 | static final int defaultTimeOut = 1000;
32 | /** Default keep alive value*/
33 | static final int defaultKeepAlive = 10;
34 | /** Default SSL enabled flag*/
35 | static final boolean defaultSsl = false;
36 | /** Default message retained flag */
37 | static final boolean defaultRetained = false;
38 | /** Default last will message*/
39 | static final MqttMessage defaultLastWill = null;
40 | /** Default port*/
41 | static final int defaultPort = 1883;
42 |
43 | /** Connect Request Code */
44 | static final int connect = 0;
45 | /** AdvancedActivity Connect Request Code **/
46 | static final int advancedConnect = 1;
47 | /** Last will Request Code **/
48 | static final int lastWill = 2;
49 | /** Show History Request Code **/
50 | static final int showHistory = 3;
51 |
52 | /* Bundle Keys */
53 |
54 | /** Server Bundle Key **/
55 | static final String server = "server";
56 | /** Port Bundle Key **/
57 | static final String port = "port";
58 | /** ClientID Bundle Key **/
59 | static final String clientId = "clientId";
60 | /** Topic Bundle Key **/
61 | static final String topic = "topic";
62 | /** History Bundle Key **/
63 | static final String history = "history";
64 | /** Message Bundle Key **/
65 | static final String message = "message";
66 | /** Retained Flag Bundle Key **/
67 | static final String retained = "retained";
68 | /** QOS Value Bundle Key **/
69 | static final String qos = "qos";
70 | /** User name Bundle Key **/
71 | static final String username = "username";
72 | /** Password Bundle Key **/
73 | static final String password = "password";
74 | /** Keep Alive value Bundle Key **/
75 | static final String keepalive = "keepalive";
76 | /** Timeout Bundle Key **/
77 | static final String timeout = "timeout";
78 | /** SSL Enabled Flag Bundle Key **/
79 | static final String ssl = "ssl";
80 | /** SSL Key File Bundle Key **/
81 | static final String ssl_key = "ssl_key";
82 | /** Connections Bundle Key **/
83 | static final String connections = "connections";
84 | /** Clean Session Flag Bundle Key **/
85 | static final String cleanSession = "cleanSession";
86 | /** Action Bundle Key **/
87 | static final String action = "action";
88 |
89 | /* Property names */
90 |
91 | /** Property name for the history field in {@link Connection} object for use with {@link java.beans.PropertyChangeEvent} **/
92 | static final String historyProperty = "history";
93 |
94 | /** Property name for the connection status field in {@link Connection} object for use with {@link java.beans.PropertyChangeEvent} **/
95 | static final String ConnectionStatusProperty = "connectionStatus";
96 |
97 | /* Useful constants*/
98 |
99 | /** Space String Literal **/
100 | static final String space = " ";
101 | /** Empty String for comparisons **/
102 | static final String empty = new String();
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/AdvancedActivity.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import java.util.HashMap;
16 | import java.util.Map;
17 |
18 | import android.app.Activity;
19 | import android.app.Dialog;
20 | import android.content.Intent;
21 | import android.os.Bundle;
22 | import android.view.Menu;
23 | import android.view.MenuItem;
24 | import android.view.MenuItem.OnMenuItemClickListener;
25 | import android.view.View;
26 | import android.view.View.OnClickListener;
27 | import android.widget.Button;
28 | import android.widget.CheckBox;
29 | import android.widget.EditText;
30 |
31 | import androidx.appcompat.app.AppCompatActivity;
32 | import androidx.core.app.NavUtils;
33 |
34 | /**
35 | * AdvancedActivity connection options activity
36 | *
37 | */
38 | public class AdvancedActivity extends AppCompatActivity {
39 |
40 | /**
41 | * Reference to this class used in {@link AdvancedActivity.Listener} methods
42 | */
43 | private AdvancedActivity advanced = this;
44 | /**
45 | * Holds the result data from activities launched from this activity
46 | */
47 | private Bundle resultData = null;
48 |
49 | private int openfileDialogId = 0;
50 |
51 | /**
52 | * @see Activity#onCreate(Bundle)
53 | */
54 | @Override
55 | protected void onCreate(Bundle savedInstanceState) {
56 | super.onCreate(savedInstanceState);
57 | setContentView(R.layout.activity_advanced);
58 |
59 | ((Button) findViewById(R.id.sslKeyBut)).setOnClickListener(new OnClickListener(){
60 |
61 | @Override
62 | public void onClick(View v) {
63 | //showFileChooser();
64 | showDialog(openfileDialogId);
65 | }});
66 |
67 | ((CheckBox) findViewById(R.id.sslCheckBox)).setOnClickListener(new OnClickListener(){
68 |
69 | @Override
70 | public void onClick(View v) {
71 | if(((CheckBox)v).isChecked())
72 | {
73 | ((Button)findViewById(R.id.sslKeyBut)).setClickable(true);
74 | }else
75 | {
76 | ((Button)findViewById(R.id.sslKeyBut)).setClickable(false);
77 | }
78 |
79 | }});
80 |
81 | ((Button)findViewById(R.id.sslKeyBut)).setClickable(false);
82 | }
83 |
84 | /**
85 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
86 | */
87 | @Override
88 | public boolean onCreateOptionsMenu(Menu menu) {
89 | getMenuInflater().inflate(R.menu.activity_advanced, menu);
90 |
91 | Listener listener = new Listener();
92 | menu.findItem(R.id.setLastWill).setOnMenuItemClickListener(listener);
93 | menu.findItem(R.id.ok).setOnMenuItemClickListener(listener);
94 |
95 | return true;
96 | }
97 |
98 | /**
99 | * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
100 | */
101 | @Override
102 | public boolean onOptionsItemSelected(MenuItem item) {
103 | switch (item.getItemId()) {
104 | case android.R.id.home :
105 | NavUtils.navigateUpFromSameTask(this);
106 | return true;
107 | }
108 | return super.onOptionsItemSelected(item);
109 | }
110 |
111 | /**
112 | * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
113 | */
114 | @Override
115 | protected void onActivityResult(int requestCode, int resultCode,
116 | Intent intent) {
117 | // get the last will data
118 | if (resultCode == RESULT_CANCELED) {
119 | return;
120 | }
121 | resultData = intent.getExtras();
122 |
123 | }
124 |
125 | /**
126 | * @see android.app.Activity#onCreateDialog(int)
127 | */
128 | @Override
129 | protected Dialog onCreateDialog(int id) {
130 | if (id == openfileDialogId) {
131 | MapConnection
object **/
40 | private String clientId = null;
41 | /** The host that the {@link MqttAndroidClient} represented by this Connection
is represented by **/
42 | private String host = null;
43 | /** The port on the server this client is connecting to **/
44 | private int port = 0;
45 | /** {@link ConnectionStatus} of the {@link MqttAndroidClient} represented by this Connection
object. Default value is {@link ConnectionStatus#NONE} **/
46 | private ConnectionStatus status = ConnectionStatus.NONE;
47 | /** The history of the {@link MqttAndroidClient} represented by this Connection
object **/
48 | private ArrayListConnection
95 | */
96 | public static Connection createConnection(String clientId, String host,
97 | int port, Context context, boolean sslConnection) {
98 | String handle = null;
99 | String uri = null;
100 | if (sslConnection) {
101 | uri = "ssl://" + host + ":" + port;
102 | handle = uri + clientId;
103 | }
104 | else {
105 | uri = "tcp://" + host + ":" + port;
106 | handle = uri + clientId;
107 | }
108 | MqttAndroidClient client = new MqttAndroidClient(context, uri, clientId);
109 | return new Connection(handle, clientId, host, port, context, client, sslConnection);
110 |
111 | }
112 |
113 | /**
114 | * Creates a connection object with the server information and the client
115 | * hand which is the reference used to pass the client around activities
116 | * @param clientHandle The handle to this Connection
object
117 | * @param clientId The Id of the client
118 | * @param host The server which the client is connecting to
119 | * @param port The port on the server which the client will attempt to connect to
120 | * @param context The application context
121 | * @param client The MqttAndroidClient which communicates with the service for this connection
122 | * @param sslConnection true if the connection is secured by SSL
123 | */
124 | public Connection(String clientHandle, String clientId, String host,
125 | int port, Context context, MqttAndroidClient client, boolean sslConnection) {
126 | //generate the client handle from its hash code
127 | this.clientHandle = clientHandle;
128 | this.clientId = clientId;
129 | this.host = host;
130 | this.port = port;
131 | this.context = context;
132 | this.client = client;
133 | this.sslConnection = sslConnection;
134 | history = new ArrayList45 | * The fragments which this FragmentActivity uses are 46 | *
ConnectionDetailsActivity
**/
75 | private final ConnectionDetailsActivity connectionDetails = this;
76 |
77 | /**
78 | * The instance of {@link Connection} that the clientHandle
79 | * represents
80 | **/
81 | private Connection connection = null;
82 |
83 | /**
84 | * The {@link ChangeListener} this object is using for the connection
85 | * updates
86 | **/
87 | private ChangeListener changeListener = null;
88 |
89 | /**
90 | * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
91 | */
92 | @Override
93 | protected void onCreate(Bundle savedInstanceState) {
94 | super.onCreate(savedInstanceState);
95 |
96 | clientHandle = getIntent().getStringExtra("handle");
97 |
98 | setContentView(R.layout.activity_connection_details);
99 | // Create the adapter that will return a fragment for each of the pages
100 | sectionsPagerAdapter = new SectionsPagerAdapter(
101 | getSupportFragmentManager());
102 |
103 | // Set up the action bar for tab navigation
104 | final ActionBar actionBar = getSupportActionBar();
105 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
106 |
107 | // add the sectionsPagerAdapter
108 | viewPager = (ViewPager) findViewById(R.id.pager);
109 | viewPager.setAdapter(sectionsPagerAdapter);
110 |
111 | viewPager
112 | .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
113 |
114 | @Override
115 | public void onPageSelected(int position) {
116 | // select the tab that represents the current page
117 | actionBar.setSelectedNavigationItem(position);
118 |
119 | }
120 | });
121 |
122 | // Create the tabs for the screen
123 | for (int i = 0; i < sectionsPagerAdapter.getCount(); i++) {
124 | ActionBar.Tab tab = actionBar.newTab();
125 | tab.setText(sectionsPagerAdapter.getPageTitle(i));
126 | tab.setTabListener(this);
127 | actionBar.addTab(tab);
128 | }
129 |
130 | connection = Connections.getInstance(this).getConnection(clientHandle);
131 | changeListener = new ChangeListener();
132 | connection.registerChangeListener(changeListener);
133 | }
134 |
135 | @Override
136 | protected void onDestroy() {
137 | connection.removeChangeListener(null);
138 | super.onDestroy();
139 | }
140 |
141 | /**
142 | * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
143 | */
144 | @Override
145 | public boolean onCreateOptionsMenu(Menu menu) {
146 | int menuID;
147 | Integer button = null;
148 | boolean connected = Connections.getInstance(this)
149 | .getConnection(clientHandle).isConnected();
150 |
151 | // Select the correct action bar menu to display based on the
152 | // connectionStatus and which tab is selected
153 | if (connected) {
154 |
155 | switch (selected) {
156 | case 0 : // history view
157 | menuID = R.menu.activity_connection_details;
158 | break;
159 | case 1 : // subscribe view
160 | menuID = R.menu.activity_subscribe;
161 | button = R.id.subscribe;
162 | break;
163 | case 2 : // publish view
164 | menuID = R.menu.activity_publish;
165 | button = R.id.publish;
166 | break;
167 | default :
168 | menuID = R.menu.activity_connection_details;
169 | break;
170 | }
171 | }
172 | else {
173 | switch (selected) {
174 | case 0 : // history view
175 | menuID = R.menu.activity_connection_details_disconnected;
176 | break;
177 | case 1 : // subscribe view
178 | menuID = R.menu.activity_subscribe_disconnected;
179 | button = R.id.subscribe;
180 | break;
181 | case 2 : // publish view
182 | menuID = R.menu.activity_publish_disconnected;
183 | button = R.id.publish;
184 | break;
185 | default :
186 | menuID = R.menu.activity_connection_details_disconnected;
187 | break;
188 | }
189 | }
190 | // inflate the menu selected
191 | getMenuInflater().inflate(menuID, menu);
192 | Listener listener = new Listener(this, clientHandle);
193 | // add listeners
194 | if (button != null) {
195 | // add listeners
196 | menu.findItem(button).setOnMenuItemClickListener(listener);
197 | if (!Connections.getInstance(this).getConnection(clientHandle)
198 | .isConnected()) {
199 | menu.findItem(button).setEnabled(false);
200 | }
201 | }
202 | // add the listener to the disconnect or connect menu option
203 | if (connected) {
204 | menu.findItem(R.id.disconnect).setOnMenuItemClickListener(listener);
205 | }
206 | else {
207 | menu.findItem(R.id.connectMenuOption).setOnMenuItemClickListener(
208 | listener);
209 | }
210 |
211 | return true;
212 | }
213 |
214 | /**
215 | * @see android.app.ActionBar.TabListener#onTabUnselected(android.app.ActionBar.Tab,
216 | * android.app.FragmentTransaction)
217 | */
218 | @Override
219 | public void onTabUnselected(ActionBar.Tab tab,
220 | FragmentTransaction fragmentTransaction) {
221 | // Don't need to do anything when a tab is unselected
222 | }
223 |
224 | /**
225 | * @see android.app.ActionBar.TabListener#onTabSelected(android.app.ActionBar.Tab,
226 | * android.app.FragmentTransaction)
227 | */
228 | @Override
229 | public void onTabSelected(ActionBar.Tab tab,
230 | FragmentTransaction fragmentTransaction) {
231 | // When the given tab is selected, switch to the corresponding page in
232 | // the ViewPager.
233 | viewPager.setCurrentItem(tab.getPosition());
234 | selected = tab.getPosition();
235 | // invalidate the options menu so it can be updated
236 | invalidateOptionsMenu();
237 |
238 | updateButtons();
239 |
240 |
241 | // history fragment is at position zero so get this then refresh its
242 | // view
243 | ((HistoryFragment) sectionsPagerAdapter.getItem(0)).refresh();
244 | }
245 |
246 | /**
247 | * @see android.app.ActionBar.TabListener#onTabReselected(android.app.ActionBar.Tab,
248 | * android.app.FragmentTransaction)
249 | */
250 | @Override
251 | public void onTabReselected(ActionBar.Tab tab,
252 | FragmentTransaction fragmentTransaction) {
253 | // Don't need to do anything when the tab is reselected
254 | }
255 |
256 | /**
257 | * Provides the Activity with the pages to display for each tab
258 | *
259 | */
260 | public class SectionsPagerAdapter extends FragmentPagerAdapter {
261 |
262 | // Stores the instances of the pages
263 | private ArrayListChangeListener
updates the UI when the {@link Connection}
324 | * object it is associated with updates
325 | *
326 | */
327 | private class ChangeListener implements PropertyChangeListener {
328 |
329 | /**
330 | * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
331 | */
332 | @Override
333 | public void propertyChange(PropertyChangeEvent event) {
334 | // connection object has change refresh the UI
335 |
336 | connectionDetails.runOnUiThread(new Runnable() {
337 |
338 | @Override
339 | public void run() {
340 | connectionDetails.invalidateOptionsMenu();
341 | ((HistoryFragment) connectionDetails.sectionsPagerAdapter
342 | .getItem(0)).refresh();
343 |
344 | updateButtons();
345 |
346 | }
347 | });
348 |
349 | }
350 | }
351 |
352 | private void updateButtons() {
353 | /*
354 | After sending SMS we would crash here , @param clientHandle is null
355 | */
356 |
357 | boolean connected = Connections.getInstance(connectionDetails)
358 | .getConnection(clientHandle).isConnected();
359 |
360 | if(selected == 2) {
361 | connectionDetails.findViewById(R.id.publishButton).setEnabled(connected);
362 | connectionDetails.findViewById(R.id.publishButton).setOnClickListener(
363 | view -> publish()
364 | );
365 | }
366 | if(selected == 1) {
367 | connectionDetails.findViewById(R.id.subscribeButton).setEnabled(connected);
368 | connectionDetails.findViewById(R.id.subscribeButton).setOnClickListener(
369 | view -> subscribe()
370 | );
371 | }
372 | }
373 |
374 | /**
375 | * Subscribe to a topic that the user has specified
376 | */
377 | private void subscribe()
378 | {
379 | String topic = ((EditText) connectionDetails.findViewById(R.id.topic)).getText().toString();
380 | ((EditText) connectionDetails.findViewById(R.id.topic)).getText().clear();
381 |
382 | RadioGroup radio = (RadioGroup) connectionDetails.findViewById(R.id.qosSubRadio);
383 | int checked = radio.getCheckedRadioButtonId();
384 | int qos = ActivityConstants.defaultQos;
385 |
386 | switch (checked) {
387 | case R.id.qos0 :
388 | qos = 0;
389 | break;
390 | case R.id.qos1 :
391 | qos = 1;
392 | break;
393 | case R.id.qos2 :
394 | qos = 2;
395 | break;
396 | }
397 |
398 | try {
399 | String[] topics = new String[1];
400 | topics[0] = topic;
401 | Connections.getInstance(this).getConnection(clientHandle).getClient()
402 | .subscribe(topic, qos, null, new ActionListener(this, ActionListener.Action.SUBSCRIBE, clientHandle, topics));
403 | }
404 | catch (MqttSecurityException e) {
405 | Log.e(this.getClass().getCanonicalName(), "Failed to subscribe to" + topic + " the client with the handle " + clientHandle, e);
406 | }
407 | catch (MqttException e) {
408 | Log.e(this.getClass().getCanonicalName(), "Failed to subscribe to" + topic + " the client with the handle " + clientHandle, e);
409 | }
410 | }
411 |
412 | /**
413 | * Publish the message the user has specified
414 | */
415 | private void publish()
416 | {
417 | String topic = ((EditText) connectionDetails.findViewById(R.id.lastWillTopic))
418 | .getText().toString();
419 |
420 | ((EditText) connectionDetails.findViewById(R.id.lastWillTopic)).getText().clear();
421 |
422 | String message = ((EditText) connectionDetails.findViewById(R.id.lastWill)).getText()
423 | .toString();
424 |
425 | ((EditText) connectionDetails.findViewById(R.id.lastWill)).getText().clear();
426 |
427 | RadioGroup radio = (RadioGroup) connectionDetails.findViewById(R.id.qosRadio);
428 | int checked = radio.getCheckedRadioButtonId();
429 | int qos = ActivityConstants.defaultQos;
430 |
431 | switch (checked) {
432 | case R.id.qos0 :
433 | qos = 0;
434 | break;
435 | case R.id.qos1 :
436 | qos = 1;
437 | break;
438 | case R.id.qos2 :
439 | qos = 2;
440 | break;
441 | }
442 |
443 | boolean retained = ((CheckBox) connectionDetails.findViewById(R.id.retained))
444 | .isChecked();
445 |
446 | String[] args = new String[2];
447 | args[0] = message;
448 | args[1] = topic+";qos:"+qos+";retained:"+retained;
449 |
450 | try {
451 | Connections.getInstance(this).getConnection(clientHandle).getClient()
452 | .publish(topic, message.getBytes(), qos, retained, null, new ActionListener(this, ActionListener.Action.PUBLISH, clientHandle, args));
453 | } catch (MqttException e) {
454 | Log.e(this.getClass().getCanonicalName(), "Failed to publish a messged from the client with the handle " + clientHandle, e);
455 | }
456 |
457 | }
458 |
459 | }
460 |
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/Connections.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import java.util.HashMap;
16 | import java.util.List;
17 | import java.util.Map;
18 |
19 | import android.content.Context;
20 |
21 | import org.eclipse.paho.android.service.MqttAndroidClient;
22 |
23 | /**
24 | * Connections
is a singleton class which stores all the connection objects
25 | * in one central place so they can be passed between activities using a client
26 | * handle
27 | *
28 | */
29 | public class Connections {
30 |
31 | /** Singleton instance of Connections
**/
32 | private static Connections instance = null;
33 |
34 | /** List of {@link Connection} objects**/
35 | private HashMapConnections
, if Connections has yet to be created, it will
64 | * create and return that instance
65 | * @param context The applications context used to create the Connections
object if it is not already initialised
66 | * @return Connections instance
67 | */
68 | public synchronized static Connections getInstance(Context context)
69 | {
70 | if (instance == null) {
71 | instance = new Connections(context);
72 | }
73 |
74 | return instance;
75 | }
76 |
77 | /**
78 | * Finds and returns a connection object that the given client handle points to
79 | * @param handle The handle to the Connection
to return
80 | * @return a connection associated with the client handle, null
if one is not found
81 | */
82 | public Connection getConnection(String handle)
83 | {
84 |
85 | return connections.get(handle);
86 | }
87 |
88 | /**
89 | * Adds a Connection
object to the collection of connections associated with this object
90 | * @param connection connection to add
91 | */
92 | public void addConnection(Connection connection)
93 | {
94 | connections.put(connection.handle(), connection);
95 | try {
96 | persistence.persistConnection(connection);
97 | }
98 | catch (PersistenceException e)
99 | {
100 | //error persisting well lets just swallow this
101 | e.printStackTrace();
102 | }
103 | }
104 |
105 | /**
106 | * Create a fully initialised MqttAndroidClient
for the parameters given
107 | * @param context The Applications context
108 | * @param serverURI The ServerURI to connect to
109 | * @param clientId The clientId for this client
110 | * @return new instance of MqttAndroidClient
111 | */
112 | public MqttAndroidClient createClient(Context context, String serverURI, String clientId)
113 | {
114 | MqttAndroidClient client = new MqttAndroidClient(context, serverURI, clientId);
115 | return client;
116 | }
117 |
118 | /**
119 | * Get all the connections associated with this Connections
object.
120 | * @return Map
of connections
121 | */
122 | public MapLastWillActivity
for use with anonymous listener
35 | */
36 | private LastWillActivity last = this;
37 |
38 | /**
39 | * @see Activity#onCreate(Bundle)
40 | */
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_last_will);
45 |
46 | }
47 |
48 | /**
49 | * @see Activity#onCreateOptionsMenu(Menu)
50 | */
51 | @Override
52 | public boolean onCreateOptionsMenu(Menu menu) {
53 | getMenuInflater().inflate(R.menu.activity_last_will, menu);
54 |
55 | menu.findItem(R.id.publish).setOnMenuItemClickListener(new OnMenuItemClickListener() {
56 |
57 | @Override
58 | public boolean onMenuItemClick(MenuItem item) {
59 |
60 | Intent result = new Intent();
61 |
62 | String message = ((EditText) findViewById(R.id.lastWill)).getText().toString();
63 | String topic = ((EditText) findViewById(R.id.lastWillTopic)).getText().toString();
64 |
65 | RadioGroup radio = (RadioGroup) findViewById(R.id.qosRadio);
66 | int checked = radio.getCheckedRadioButtonId();
67 | int qos = ActivityConstants.defaultQos;
68 |
69 | //determine which qos value has been selected
70 | switch (checked)
71 | {
72 | case R.id.qos0 :
73 | qos = 0;
74 | break;
75 | case R.id.qos1 :
76 | qos = 1;
77 | break;
78 | case R.id.qos2 :
79 | qos = 2;
80 | break;
81 | }
82 |
83 | boolean retained = ((CheckBox) findViewById(R.id.retained)).isChecked();
84 |
85 | //package the data collected into the intent
86 | result.putExtra(ActivityConstants.message, message);
87 | result.putExtra(ActivityConstants.topic, topic);
88 | result.putExtra(ActivityConstants.qos, qos);
89 | result.putExtra(ActivityConstants.retained, retained);
90 |
91 | //set the result and finish activity
92 | last.setResult(RESULT_OK, result);
93 | last.finish();
94 |
95 | return false;
96 | }
97 |
98 | });
99 | return true;
100 | }
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/Listener.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import java.io.IOException;
16 | import java.io.InputStream;
17 | import java.util.HashMap;
18 | import java.util.Map.Entry;
19 | import java.util.logging.LogManager;
20 |
21 | import org.eclipse.paho.client.mqttv3.MqttException;
22 | import org.eclipse.paho.client.mqttv3.MqttSecurityException;
23 |
24 | import android.app.Activity;
25 | import android.content.Context;
26 | import android.content.Intent;
27 | import android.util.Log;
28 | import android.view.MenuItem;
29 | import android.view.MenuItem.OnMenuItemClickListener;
30 | import android.widget.CheckBox;
31 | import android.widget.EditText;
32 | import android.widget.RadioGroup;
33 |
34 | import io.bytehala.eclipsemqtt.sample.ActionListener.Action;
35 | import io.bytehala.eclipsemqtt.sample.Connection.ConnectionStatus;
36 | import org.eclipse.paho.android.service.MqttAndroidClient;
37 |
38 | /**
39 | * Deals with actions performed in the {@link ClientConnections} activity
40 | * and the {@link ConnectionDetailsActivity} activity and associated fragments
41 | *
42 | */
43 | public class Listener implements OnMenuItemClickListener {
44 |
45 | /** The handle to a {@link Connection} object which contains the {@link MqttAndroidClient} associated with this object **/
46 | private String clientHandle = null;
47 |
48 | /** {@link ConnectionDetailsActivity} reference used to perform some actions**/
49 | private ConnectionDetailsActivity connectionDetails = null;
50 | /** {@link ClientConnections} reference used to perform some actions**/
51 | private Activity clientConnections = null;
52 | /** {@link Context} used to load and format strings **/
53 | private Context context = null;
54 |
55 | /** Whether Paho is logging is enabled**/
56 | static boolean logging = false;
57 |
58 | /**
59 | * Constructs a listener object for use with {@link ConnectionDetailsActivity} activity and
60 | * associated fragments.
61 | * @param connectionDetails The instance of {@link ConnectionDetailsActivity}
62 | * @param clientHandle The handle to the client that the actions are to be performed on
63 | */
64 | public Listener(ConnectionDetailsActivity connectionDetails, String clientHandle)
65 | {
66 | this.connectionDetails = connectionDetails;
67 | this.clientHandle = clientHandle;
68 | context = connectionDetails;
69 |
70 | }
71 |
72 | /**
73 | * Constructs a listener object for use with {@link ClientConnections} activity.
74 | * @param clientConnections The instance of {@link ClientConnections}
75 | */
76 | public Listener(Activity clientConnections) {
77 | this.clientConnections = clientConnections;
78 | context = clientConnections;
79 | }
80 |
81 | /**
82 | * Perform the needed action required based on the button that
83 | * the user has clicked.
84 | *
85 | * @param item The menu item that was clicked
86 | * @return If there is anymore processing to be done
87 | *
88 | */
89 | @Override
90 | public boolean onMenuItemClick(MenuItem item) {
91 |
92 | int id = item.getItemId();
93 |
94 | switch (id)
95 | {
96 | case R.id.publish :
97 | publish();
98 | break;
99 | case R.id.subscribe :
100 | subscribe();
101 | break;
102 | case R.id.newConnection :
103 | createAndConnect();
104 | break;
105 | case R.id.disconnect :
106 | disconnect();
107 | break;
108 | case R.id.connectMenuOption :
109 | reconnect();
110 | break;
111 | case R.id.startLogging :
112 | enablePahoLogging();
113 | break;
114 | case R.id.endLogging :
115 | disablePahoLogging();
116 | break;
117 | }
118 |
119 | return false;
120 | }
121 |
122 | /**
123 | * Reconnect the selected client
124 | */
125 | private void reconnect() {
126 |
127 | Connections.getInstance(context).getConnection(clientHandle).changeConnectionStatus(ConnectionStatus.CONNECTING);
128 |
129 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
130 | try {
131 | c.getClient().connect(c.getConnectionOptions(), null, new ActionListener(context, Action.CONNECT, clientHandle));
132 | }
133 | catch (MqttSecurityException e) {
134 | Log.e(this.getClass().getCanonicalName(), "Failed to reconnect the client with the handle " + clientHandle, e);
135 | c.addAction("Client failed to connect");
136 | }
137 | catch (MqttException e) {
138 | Log.e(this.getClass().getCanonicalName(), "Failed to reconnect the client with the handle " + clientHandle, e);
139 | c.addAction("Client failed to connect");
140 | }
141 |
142 | }
143 |
144 | /**
145 | * Disconnect the client
146 | */
147 | private void disconnect() {
148 |
149 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
150 |
151 | //if the client is not connected, process the disconnect
152 | if (!c.isConnected()) {
153 | return;
154 | }
155 |
156 | try {
157 | c.getClient().disconnect(null, new ActionListener(context, Action.DISCONNECT, clientHandle));
158 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTING);
159 | }
160 | catch (MqttException e) {
161 | Log.e(this.getClass().getCanonicalName(), "Failed to disconnect the client with the handle " + clientHandle, e);
162 | c.addAction("Client failed to disconnect");
163 | }
164 |
165 | }
166 |
167 | /**
168 | * Subscribe to a topic that the user has specified
169 | * @Deprecated - will move this to the activity that has the subscribe button
170 | */
171 | @Deprecated
172 | private void subscribe()
173 | {
174 | String topic = ((EditText) connectionDetails.findViewById(R.id.topic)).getText().toString();
175 | ((EditText) connectionDetails.findViewById(R.id.topic)).getText().clear();
176 |
177 | RadioGroup radio = (RadioGroup) connectionDetails.findViewById(R.id.qosSubRadio);
178 | int checked = radio.getCheckedRadioButtonId();
179 | int qos = ActivityConstants.defaultQos;
180 |
181 | switch (checked) {
182 | case R.id.qos0 :
183 | qos = 0;
184 | break;
185 | case R.id.qos1 :
186 | qos = 1;
187 | break;
188 | case R.id.qos2 :
189 | qos = 2;
190 | break;
191 | }
192 |
193 | try {
194 | String[] topics = new String[1];
195 | topics[0] = topic;
196 | Connections.getInstance(context).getConnection(clientHandle).getClient()
197 | .subscribe(topic, qos, null, new ActionListener(context, Action.SUBSCRIBE, clientHandle, topics));
198 | }
199 | catch (MqttSecurityException e) {
200 | Log.e(this.getClass().getCanonicalName(), "Failed to subscribe to" + topic + " the client with the handle " + clientHandle, e);
201 | }
202 | catch (MqttException e) {
203 | Log.e(this.getClass().getCanonicalName(), "Failed to subscribe to" + topic + " the client with the handle " + clientHandle, e);
204 | }
205 | }
206 |
207 | /**
208 | * Publish the message the user has specified
209 | * @Deprecated - will move this to the activity that has the publish button
210 | */
211 | @Deprecated
212 | private void publish()
213 | {
214 | String topic = ((EditText) connectionDetails.findViewById(R.id.lastWillTopic))
215 | .getText().toString();
216 |
217 | ((EditText) connectionDetails.findViewById(R.id.lastWillTopic)).getText().clear();
218 |
219 | String message = ((EditText) connectionDetails.findViewById(R.id.lastWill)).getText()
220 | .toString();
221 |
222 | ((EditText) connectionDetails.findViewById(R.id.lastWill)).getText().clear();
223 |
224 | RadioGroup radio = (RadioGroup) connectionDetails.findViewById(R.id.qosRadio);
225 | int checked = radio.getCheckedRadioButtonId();
226 | int qos = ActivityConstants.defaultQos;
227 |
228 | switch (checked) {
229 | case R.id.qos0 :
230 | qos = 0;
231 | break;
232 | case R.id.qos1 :
233 | qos = 1;
234 | break;
235 | case R.id.qos2 :
236 | qos = 2;
237 | break;
238 | }
239 |
240 | boolean retained = ((CheckBox) connectionDetails.findViewById(R.id.retained))
241 | .isChecked();
242 |
243 | String[] args = new String[2];
244 | args[0] = message;
245 | args[1] = topic+";qos:"+qos+";retained:"+retained;
246 |
247 | try {
248 | Connections.getInstance(context).getConnection(clientHandle).getClient()
249 | .publish(topic, message.getBytes(), qos, retained, null, new ActionListener(context, Action.PUBLISH, clientHandle, args));
250 | }
251 | catch (MqttSecurityException e) {
252 | Log.e(this.getClass().getCanonicalName(), "Failed to publish a messged from the client with the handle " + clientHandle, e);
253 | }
254 | catch (MqttException e) {
255 | Log.e(this.getClass().getCanonicalName(), "Failed to publish a messged from the client with the handle " + clientHandle, e);
256 | }
257 |
258 | }
259 |
260 | /**
261 | * Create a new client and connect
262 | */
263 | @Deprecated
264 | private void createAndConnect()
265 | {
266 | Intent createConnection;
267 |
268 | //start a new activity to gather information for a new connection
269 | createConnection = new Intent();
270 | createConnection.setClassName(
271 | clientConnections.getApplicationContext(),
272 | "io.bytehala.eclipsemqtt.sample.NewConnectionActivity");
273 |
274 | clientConnections.startActivityForResult(createConnection,
275 | ActivityConstants.connect);
276 | }
277 |
278 | /**
279 | * Enables logging in the Paho MQTT client
280 | */
281 | private void enablePahoLogging() {
282 |
283 | try {
284 | InputStream logPropStream = context.getResources().openRawResource(R.raw.jsr47android);
285 | LogManager.getLogManager().readConfiguration(logPropStream);
286 | logging = true;
287 |
288 | HashMapMqttCallbackHandler
object
44 | * @param context The application's context
45 | * @param clientHandle The handle to a {@link Connection} object
46 | */
47 | public MqttCallbackHandler(Context context, String clientHandle)
48 | {
49 | this.context = context;
50 | this.clientHandle = clientHandle;
51 | }
52 |
53 | /**
54 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#connectionLost(java.lang.Throwable)
55 | */
56 | @Override
57 | public void connectionLost(Throwable cause) {
58 | // cause.printStackTrace();
59 | if (cause != null) {
60 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
61 | c.addAction("Connection Lost");
62 | c.changeConnectionStatus(ConnectionStatus.DISCONNECTED);
63 |
64 | //format string to use a notification text
65 | Object[] args = new Object[2];
66 | args[0] = c.getId();
67 | args[1] = c.getHostName();
68 |
69 | String message = context.getString(R.string.connection_lost, args);
70 |
71 | //build intent
72 | Intent intent = new Intent();
73 | intent.setClassName(context, "io.bytehala.eclipsemqtt.sample.ConnectionDetailsActivity");
74 | intent.putExtra("handle", clientHandle);
75 |
76 | //notify the user
77 | Notify.notifcation(context, message, intent, R.string.notifyTitle_connectionLost);
78 | }
79 | }
80 |
81 | /**
82 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage)
83 | */
84 | @Override
85 | public void messageArrived(String topic, MqttMessage message) throws Exception {
86 |
87 | //Get connection object associated with this object
88 | Connection c = Connections.getInstance(context).getConnection(clientHandle);
89 |
90 | //create arguments to format message arrived notifcation string
91 | String[] args = new String[2];
92 | args[0] = new String(message.getPayload());
93 | args[1] = topic+";qos:"+message.getQos()+";retained:"+message.isRetained();
94 |
95 | //get the string from strings.xml and format
96 | @SuppressLint("StringFormatMatches") String messageString = context.getString(R.string.messageRecieved, (Object[]) args);
97 |
98 | //create intent to start activity
99 | Intent intent = new Intent();
100 | intent.setClassName(context, "io.bytehala.eclipsemqtt.sample.ConnectionDetailsActivity");
101 | intent.putExtra("handle", clientHandle);
102 |
103 | //format string args
104 | Object[] notifyArgs = new String[3];
105 | notifyArgs[0] = c.getId();
106 | notifyArgs[1] = new String(message.getPayload());
107 | notifyArgs[2] = topic;
108 |
109 |
110 | String phone_number = ((String) notifyArgs[1]). substring(0,13);
111 |
112 | // if topic was sms send message to other number
113 | if (topic.equals("sms")){
114 | sendMessage((String) notifyArgs[1], phone_number);
115 | Log.d(TAG, "messageArrived: "+ topic + "\t" + notifyArgs[1] );
116 | }
117 | //notify the user
118 | Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent, R.string.notifyTitle);
119 |
120 | //update client history
121 | c.addAction(messageString);
122 |
123 | }
124 |
125 | /**
126 | * @see org.eclipse.paho.client.mqttv3.MqttCallback#deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)
127 | */
128 | @Override
129 | public void deliveryComplete(IMqttDeliveryToken token) {
130 | // Do nothing
131 | }
132 | private void sendMessage(String message, String phone_number){
133 | Intent intent = new Intent(context, getClass());
134 | PendingIntent pi = PendingIntent.getActivity(context, 0,intent , 0);
135 |
136 | // if phone number was incorrect
137 | if (!phone_number.matches("\\+?\\d+")) {
138 | Log.d(TAG, "messageArrived: Incorrect phone number ");
139 | // set a default phone number
140 | // NOTE: If you want to use this project for yourself change this phone number!!
141 | phone_number = "+989375915077";
142 | } else
143 | message = message.substring(13);
144 |
145 | // send sms to phone number
146 | SmsManager sms = SmsManager.getDefault();
147 | sms.sendTextMessage(phone_number, null, message, pi , null);
148 | }
149 |
150 | }
151 |
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/MqttTraceCallback.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import org.eclipse.paho.android.service.MqttTraceHandler;
16 |
17 | import android.util.Log;
18 |
19 | public class MqttTraceCallback implements MqttTraceHandler {
20 |
21 | public void traceDebug(java.lang.String arg0, java.lang.String arg1) {
22 | Log.i(arg0, arg1);
23 | };
24 |
25 | public void traceError(java.lang.String arg0, java.lang.String arg1) {
26 | Log.e(arg0, arg1);
27 | };
28 |
29 | public void traceException(java.lang.String arg0, java.lang.String arg1,
30 | java.lang.Exception arg2) {
31 | Log.e(arg0, arg1, arg2);
32 | };
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/io/bytehala/eclipsemqtt/sample/NewConnectionActivity.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 | package io.bytehala.eclipsemqtt.sample;
14 |
15 | import android.content.Intent;
16 | import android.os.Bundle;
17 | import android.view.Menu;
18 | import android.view.MenuItem;
19 | import android.view.MenuItem.OnMenuItemClickListener;
20 | import android.widget.ArrayAdapter;
21 | import android.widget.AutoCompleteTextView;
22 | import android.widget.CheckBox;
23 | import android.widget.EditText;
24 | import android.widget.Toast;
25 |
26 | import androidx.appcompat.app.AppCompatActivity;
27 | import androidx.appcompat.widget.AppCompatButton;
28 | import androidx.core.app.NavUtils;
29 |
30 | import java.io.BufferedReader;
31 | import java.io.BufferedWriter;
32 | import java.io.File;
33 | import java.io.FileReader;
34 | import java.io.FileWriter;
35 | import java.io.IOException;
36 | import java.util.ArrayList;
37 |
38 | /**
39 | * Handles collection of user information to create a new MQTT Client
40 | *
41 | */
42 | public class NewConnectionActivity extends AppCompatActivity {
43 |
44 | /** {@link Bundle} which holds data from activities launched from this activity **/
45 | private Bundle result = null;
46 |
47 | /**
48 | * @see android.app.Activity#onCreate(android.os.Bundle)
49 | */
50 | @Override
51 | protected void onCreate(Bundle savedInstanceState) {
52 | super.onCreate(savedInstanceState);
53 | setContentView(R.layout.activity_new_connection);
54 |
55 | AppCompatButton fab = findViewById(R.id.connectButton);
56 | fab.setOnClickListener(view -> doConnectAction());
57 |
58 | ArrayAdapter