Skip to content

Commit f5311fa

Browse files
author
rxiang
committed
Add SSL Key Selector for dev branch
Change-Id: Ia6bf623b6e0a0b4e4ae8584024fc4491bf6820a5 Signed-off-by: rxiang <xiangr@cn.ibm.com>
1 parent 37fd5e8 commit f5311fa

8 files changed

Lines changed: 428 additions & 3 deletions

File tree

org.eclipse.paho.android.service/org.eclipse.paho.android.service.sample/res/layout/activity_advanced.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ IBM Corp.
8181
android:layout_width="wrap_content"
8282
android:layout_height="wrap_content"
8383
android:text="@string/empty" />
84+
85+
<EditText
86+
android:id="@+id/sslKeyLocaltion"
87+
android:layout_width="0dp"
88+
android:layout_height="wrap_content"
89+
android:layout_weight="1"
90+
android:ems="10"
91+
android:enabled="false"
92+
android:hint="@string/sslKeyLocaltion"
93+
android:inputType="text" >
94+
95+
</EditText>
96+
97+
<Button
98+
android:id="@+id/sslKeyBut"
99+
style="?android:attr/buttonStyleSmall"
100+
android:clickable="false"
101+
android:layout_width="wrap_content"
102+
android:layout_height="wrap_content"
103+
android:text="@string/select" />
104+
84105
</LinearLayout>
85106

86107
<LinearLayout
@@ -132,4 +153,4 @@ IBM Corp.
132153
android:inputType="number" />
133154
</LinearLayout>
134155

135-
</RelativeLayout>
156+
</RelativeLayout>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/vw1"
4+
android:layout_width="fill_parent"
5+
android:layout_height="wrap_content"
6+
android:background="#000000"
7+
android:orientation="horizontal"
8+
android:padding="4dp" >
9+
10+
11+
<ImageView
12+
android:id="@+id/filedialogitem_img"
13+
android:layout_width="32dp"
14+
android:layout_height="32dp"
15+
android:layout_margin="4dp"/>
16+
17+
18+
19+
<LinearLayout
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:orientation="vertical" >
23+
24+
25+
26+
27+
28+
<TextView
29+
android:id="@+id/filedialogitem_name"
30+
android:layout_width="fill_parent"
31+
android:layout_height="wrap_content"
32+
android:textColor="#FFFFFF"
33+
android:textSize="18sp"
34+
android:textStyle="bold" />
35+
36+
37+
<TextView
38+
android:id="@+id/filedialogitem_path"
39+
android:layout_width="fill_parent"
40+
android:layout_height="wrap_content"
41+
android:paddingLeft="10dp"
42+
android:textColor="#FFFFFF"
43+
android:textSize="14sp" />
44+
45+
</LinearLayout>
46+
47+
</LinearLayout>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ IBM Corp.
103103
<string name="passwordHint">password</string>
104104
<string name="timeoutHint">60</string>
105105
<string name="keepaliveHint">200</string>
106+
<string name="sslKeyLocaltion">file://</string>
107+
<string name="select">select</string>
106108

107109

108-
</resources>
110+
</resources>

org.eclipse.paho.android.service/org.eclipse.paho.android.service.sample/src/org/eclipse/paho/android/service/sample/ActivityConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public class ActivityConstants {
7777
static final String timeout = "timeout";
7878
/** SSL Enabled Flag Bundle Key **/
7979
static final String ssl = "ssl";
80+
/** SSL Key File Bundle Key **/
81+
static final String ssl_key = "ssl_key";
8082
/** Connections Bundle Key **/
8183
static final String connections = "connections";
8284
/** Clean Session Flag Bundle Key **/

org.eclipse.paho.android.service/org.eclipse.paho.android.service.sample/src/org/eclipse/paho/android/service/sample/Advanced.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212
*/
1313
package org.eclipse.paho.android.service.sample;
1414

15+
import java.util.HashMap;
16+
import java.util.Map;
17+
1518
import android.app.Activity;
19+
import android.app.Dialog;
1620
import android.content.Intent;
1721
import android.os.Bundle;
1822
import android.support.v4.app.NavUtils;
1923
import android.view.Menu;
2024
import android.view.MenuItem;
2125
import android.view.MenuItem.OnMenuItemClickListener;
26+
import android.view.View;
27+
import android.view.View.OnClickListener;
28+
import android.widget.Button;
2229
import android.widget.CheckBox;
2330
import android.widget.EditText;
2431
import org.eclipse.paho.android.service.sample.R;
@@ -37,6 +44,8 @@ public class Advanced extends Activity {
3744
* Holds the result data from activities launched from this activity
3845
*/
3946
private Bundle resultData = null;
47+
48+
private int openfileDialogId = 0;
4049

4150
/**
4251
* @see Activity#onCreate(Bundle)
@@ -45,7 +54,30 @@ public class Advanced extends Activity {
4554
protected void onCreate(Bundle savedInstanceState) {
4655
super.onCreate(savedInstanceState);
4756
setContentView(R.layout.activity_advanced);
48-
57+
58+
((Button) findViewById(R.id.sslKeyBut)).setOnClickListener(new OnClickListener(){
59+
60+
@Override
61+
public void onClick(View v) {
62+
//showFileChooser();
63+
showDialog(openfileDialogId);
64+
}});
65+
66+
((CheckBox) findViewById(R.id.sslCheckBox)).setOnClickListener(new OnClickListener(){
67+
68+
@Override
69+
public void onClick(View v) {
70+
if(((CheckBox)v).isChecked())
71+
{
72+
((Button)findViewById(R.id.sslKeyBut)).setClickable(true);
73+
}else
74+
{
75+
((Button)findViewById(R.id.sslKeyBut)).setClickable(false);
76+
}
77+
78+
}});
79+
80+
((Button)findViewById(R.id.sslKeyBut)).setClickable(false);
4981
}
5082

5183
/**
@@ -89,6 +121,33 @@ protected void onActivityResult(int requestCode, int resultCode,
89121

90122
}
91123

124+
/**
125+
* @see android.app.Activity#onCreateDialog(int)
126+
*/
127+
@Override
128+
protected Dialog onCreateDialog(int id) {
129+
if (id == openfileDialogId) {
130+
Map<String, Integer> images = new HashMap<String, Integer>();
131+
images.put(OpenFileDialog.sRoot, R.drawable.ic_launcher);
132+
images.put(OpenFileDialog.sParent, R.drawable.ic_launcher);
133+
images.put(OpenFileDialog.sFolder, R.drawable.ic_launcher);
134+
images.put("bks", R.drawable.ic_launcher);
135+
images.put(OpenFileDialog.sEmpty, R.drawable.ic_launcher);
136+
Dialog dialog = OpenFileDialog.createDialog(id, this, "openfile",
137+
new CallbackBundle() {
138+
@Override
139+
public void callback(Bundle bundle) {
140+
String filepath = bundle.getString("path");
141+
// setTitle(filepath);
142+
((EditText) findViewById(R.id.sslKeyLocaltion))
143+
.setText(filepath);
144+
}
145+
}, ".bks;", images);
146+
return dialog;
147+
}
148+
return null;
149+
}
150+
92151
/**
93152
* Deals with button clicks for the advanced options page
94153
*
@@ -175,7 +234,13 @@ private void ok() {
175234
.toString();
176235
String password = ((EditText) findViewById(R.id.password))
177236
.getText().toString();
237+
String sslkey = null;
178238
boolean ssl = ((CheckBox) findViewById(R.id.sslCheckBox)).isChecked();
239+
if(ssl)
240+
{
241+
sslkey = ((EditText) findViewById(R.id.sslKeyLocaltion))
242+
.getText().toString();
243+
}
179244
try {
180245
timeout = Integer
181246
.parseInt(((EditText) findViewById(R.id.timeout))
@@ -200,6 +265,7 @@ private void ok() {
200265
intent.putExtra(ActivityConstants.timeout, timeout);
201266
intent.putExtra(ActivityConstants.keepalive, keepalive);
202267
intent.putExtra(ActivityConstants.ssl, ssl);
268+
intent.putExtra(ActivityConstants.ssl_key, sslkey);
203269
//set the result as okay, with the data, and finish
204270
advanced.setResult(RESULT_OK, intent);
205271
advanced.finish();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 org.eclipse.paho.android.service.sample;
14+
15+
16+
import android.os.Bundle;
17+
18+
/**
19+
* For File selector to share data
20+
*
21+
*/
22+
public interface CallbackBundle {
23+
abstract void callback(Bundle bundle);
24+
}

org.eclipse.paho.android.service/org.eclipse.paho.android.service.sample/src/org/eclipse/paho/android/service/sample/ClientConnections.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
import java.beans.PropertyChangeEvent;
1616
import java.beans.PropertyChangeListener;
17+
import java.io.FileInputStream;
18+
import java.io.FileNotFoundException;
1719
import java.util.Map;
1820

1921
import org.eclipse.paho.android.service.MqttAndroidClient;
2022
import org.eclipse.paho.android.service.sample.Connection.ConnectionStatus;
2123
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
2224
import org.eclipse.paho.client.mqttv3.MqttException;
25+
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
2326

2427
import android.app.AlertDialog;
2528
import android.app.ListActivity;
@@ -233,6 +236,7 @@ private void connectAction(Bundle data) {
233236
boolean cleanSession = (Boolean) data.get(ActivityConstants.cleanSession);
234237

235238
boolean ssl = (Boolean) data.get(ActivityConstants.ssl);
239+
String ssl_key = (String) data.get(ActivityConstants.ssl_key);
236240
String uri = null;
237241
if (ssl) {
238242
Log.e("SSLConnection", "Doing an SSL Connect");
@@ -247,6 +251,25 @@ private void connectAction(Bundle data) {
247251

248252
MqttAndroidClient client;
249253
client = Connections.getInstance(this).createClient(this, uri, clientId);
254+
255+
if (ssl){
256+
try {
257+
if(ssl_key != null && !ssl_key.equalsIgnoreCase(""))
258+
{
259+
FileInputStream key = new FileInputStream(ssl_key);
260+
conOpt.setSocketFactory(client.getSSLSocketFactory(key,
261+
"mqtttest"));
262+
}
263+
264+
} catch (MqttSecurityException e) {
265+
Log.e(this.getClass().getCanonicalName(),
266+
"MqttException Occured: ", e);
267+
} catch (FileNotFoundException e) {
268+
Log.e(this.getClass().getCanonicalName(),
269+
"MqttException Occured: SSL Key file not found", e);
270+
}
271+
}
272+
250273
// create a client handle
251274
String clientHandle = uri + clientId;
252275

0 commit comments

Comments
 (0)