Skip to content

Commit a5a8de7

Browse files
committed
Using try/with
1 parent 660e8c0 commit a5a8de7

9 files changed

Lines changed: 93 additions & 189 deletions

File tree

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/localization/ClientThread.java

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,14 @@
3030
import edu.fiu.adwise.homomorphic_encryption.paillier.PaillierPublicKey;
3131
import edu.fiu.adwise.homomorphic_encryption.socialistmillionaire.bob;
3232

33-
public class ClientThread implements Runnable
34-
{
33+
public class ClientThread implements Runnable {
3534
private final static String TAG = "CLIENT_THREAD";
36-
37-
private ObjectOutputStream toServer = null;
38-
private ObjectInputStream fromServer = null;
39-
4035
//Pass Data back to class by reference...
4136
private background findMe;
4237
private TrainActivity trainMe;
4338
private background getColumns;
44-
45-
//Data Objects
46-
private SendTrainingData sendTraining; //Training Data
47-
private SendLocalizationData transmission; //For Encrypted Paillier/DGK Transmission
48-
39+
private SendTrainingData sendTraining;
40+
private SendLocalizationData transmission;
4941
// Have all Keys in case comparison is needed!!
5042
private final DGKPublicKey pubKey = KeyMaster.DGKpk;
5143
private final DGKPrivateKey privKey = KeyMaster.DGKsk;
@@ -66,8 +58,7 @@ public ClientThread(TrainActivity trainActivity) {
6658
Purpose: Force mySQL Database to process data from
6759
training data.
6860
*/
69-
public ClientThread()
70-
{
61+
public ClientThread() {
7162
this.LOCALIZATIONSCHEME = LOCALIZATION_SCHEME.from_int(-2);
7263
}
7364

@@ -93,12 +84,11 @@ public ClientThread (SendTrainingData in) {
9384

9485
public void run () {
9586
Object in;
96-
try
97-
{
98-
clientSocket = new Socket(SQLDatabase, portNumber);
99-
// Prepare I/O Stream
100-
this.toServer = new ObjectOutputStream(clientSocket.getOutputStream());
101-
this.fromServer = new ObjectInputStream(clientSocket.getInputStream());
87+
try (
88+
Socket clientSocket = new Socket(SQLDatabase, portNumber);
89+
ObjectOutputStream toServer = new ObjectOutputStream(clientSocket.getOutputStream());
90+
ObjectInputStream fromServer = new ObjectInputStream(clientSocket.getInputStream())
91+
) {
10292
Log.d(TAG, "I/O Streams set!");
10393

10494
switch(LOCALIZATIONSCHEME) {
@@ -110,16 +100,6 @@ public void run () {
110100
toServer.writeObject(KeyMaster.map_name);
111101
toServer.flush();
112102

113-
// Following this patch the server needs to know the Phone as well
114-
if(MainActivity.multi_phone) {
115-
String [] phone_data = MainActivity.getPhoneData();
116-
for (String s: phone_data) {
117-
Log.d(TAG, s);
118-
}
119-
toServer.writeObject(phone_data);
120-
toServer.flush();
121-
}
122-
123103
in = fromServer.readObject();
124104
if(in instanceof Double []) {
125105
trainMe.existingX = (Double []) in;
@@ -155,7 +135,6 @@ public void run () {
155135
break;
156136
case GET_COLUMN:
157137
toServer.writeObject("Get Lookup Columns");
158-
159138
// Send the Map with all (x, y)
160139
toServer.writeObject(KeyMaster.map_name);
161140
toServer.flush();
@@ -179,7 +158,6 @@ public void run () {
179158
MainActivity.bad_train.show();
180159
}
181160
break;
182-
183161
case PLAIN_MIN:
184162
case PLAIN_DMA:
185163
case PLAIN_MCA:
@@ -189,7 +167,7 @@ public void run () {
189167
case DGK_MIN:
190168
case DGK_MCA:
191169
case DGK_DMA:
192-
localize();
170+
localize(toServer, fromServer);
193171
break;
194172
default:
195173
Log.d(TAG, "Error at Thread run: No Valid Object was sent here");
@@ -209,7 +187,10 @@ public void run () {
209187
}
210188
}
211189

212-
private void localize() throws IOException, ClassNotFoundException, HomomorphicException {
190+
private void localize(
191+
ObjectOutputStream toServer,
192+
ObjectInputStream fromServer
193+
) throws IOException, ClassNotFoundException, HomomorphicException {
213194
bob andrew;
214195
BigInteger [] location;
215196
BigInteger divisor;

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/localization/KeyMaster.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public static void init() throws HomomorphicException {
5050
finished = true;
5151
}
5252

53-
public void run()
54-
{
53+
public void run() {
5554
try {
5655
init();
5756
} catch (HomomorphicException e) {

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/localization/background.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public final class background {
4545
List<LocalizationResult> fromServer = new ArrayList<>();
4646
Double [] coordinates = new Double[2];
4747
private final LOCALIZATION_SCHEME LOCALIZATION_SCHEME;
48-
4948
//Keys and read Keys
5049
private static final PaillierPublicKey pk = KeyMaster.pk;
5150
private static final DGKPublicKey DGKpk = KeyMaster.DGKpk;
@@ -197,8 +196,7 @@ private Float[] doInBackground() {
197196
location[1] = fromServer.get(0).getY().floatValue();
198197
}
199198
else {
200-
Phase3();
201-
return location;
199+
return Phase3();
202200
}
203201
publishProgress(10);
204202
}
@@ -211,8 +209,8 @@ private Float[] doInBackground() {
211209
try {
212210
if(LOCALIZATION_SCHEME == DGK_DMA) {
213211
for (int i = 0; i < MainActivity.VECTOR_SIZE; i++) {
214-
S2[i] = DGKOperations.encrypt(-2 * RSS_send[i], DGKpk);
215-
S3_comp[i] = DGKOperations.encrypt(RSS_send[i] * RSS_send[i], DGKpk);
212+
S2[i] = DGKOperations.encrypt(-2L * RSS_send[i], DGKpk);
213+
S3_comp[i] = DGKOperations.encrypt((long) RSS_send[i] * RSS_send[i], DGKpk);
216214
}
217215
t = new Thread( new ClientThread(
218216
new SendLocalizationData(MAC_send, S2, null, S3_comp,
@@ -222,8 +220,8 @@ private Float[] doInBackground() {
222220
}
223221
else {
224222
for (int i = 0; i < MainActivity.VECTOR_SIZE; i++) {
225-
S2[i] = DGKOperations.encrypt(-2 * RSS_send[i], DGKpk);
226-
S3_plaintext += RSS_send[i] * RSS_send[i];
223+
S2[i] = DGKOperations.encrypt(-2L * RSS_send[i], DGKpk);
224+
S3_plaintext += (long) RSS_send[i] * RSS_send[i];
227225
}
228226

229227
try {
@@ -292,8 +290,8 @@ private Float[] doInBackground() {
292290
try {
293291
if(LOCALIZATION_SCHEME == PAILLIER_DMA) {
294292
for (int i = 0; i < MainActivity.VECTOR_SIZE; i++) {
295-
S2[i] = PaillierCipher.encrypt(-2 * RSS_send[i], pk);
296-
S3_comp[i] = PaillierCipher.encrypt(RSS_send[i] * RSS_send[i], pk);
293+
S2[i] = PaillierCipher.encrypt(-2L * RSS_send[i], pk);
294+
S3_comp[i] = PaillierCipher.encrypt((long) RSS_send[i] * RSS_send[i], pk);
297295
}
298296
(t = new Thread(new ClientThread(
299297
new SendLocalizationData(MAC_send, S2, null, S3_comp,
@@ -304,8 +302,8 @@ private Float[] doInBackground() {
304302
}
305303
else {
306304
for (int i = 0; i < MainActivity.VECTOR_SIZE; i++) {
307-
S2[i] = PaillierCipher.encrypt(-2 * RSS_send[i], pk);
308-
S3_plaintext += RSS_send[i] * RSS_send[i];
305+
S2[i] = PaillierCipher.encrypt(-2L * RSS_send[i], pk);
306+
S3_plaintext += (long) RSS_send[i] * RSS_send[i];
309307
}
310308
S3 = PaillierCipher.encrypt(S3_plaintext, pk);
311309

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/sensors/WifiReceiver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public void unregisterReceiver(Context context) {
6666

6767
// Try this...
6868
// https://stackoverflow.com/questions/13238600/use-registerreceiver-for-non-activity-and-non-service-class
69-
public void onReceive(Context context, Intent intent)
70-
{
69+
public void onReceive(Context context, Intent intent) {
7170
String action = intent.getAction();
7271
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
7372
get_data();

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/ui/AddMapActivity.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,26 @@
1616
import android.os.Bundle;
1717
import android.util.Log;
1818
import android.view.View;
19-
import android.view.View.OnClickListener;
2019
import android.widget.Button;
2120
import android.widget.EditText;
2221
import android.widget.ImageView;
2322
import android.widget.Switch;
2423
import android.widget.Toast;
2524

26-
import edu.fiu.adwise.fingerprint_localization.sensors.localization.KeyMaster;
25+
import edu.fiu.adwise.fingerprint_localization.localization.KeyMaster;
2726
import edu.fiu.reu2017.R;
2827

2928
import static edu.fiu.adwise.fingerprint_localization.ui.MainActivity.SQLDatabase;
3029
import static edu.fiu.adwise.fingerprint_localization.ui.MainActivity.TIMEOUT;
3130
import static edu.fiu.adwise.fingerprint_localization.ui.MainActivity.portNumber;
3231

33-
public class AddMapActivity extends Activity
34-
{
32+
public class AddMapActivity extends Activity {
3533
EditText textTargetUri;
3634
ImageView targetImage;
3735
Bitmap bitmap = null;
38-
3936
private Switch mode;
4037
/** Called when the activity is first created. */
41-
public void onCreate(Bundle savedInstanceState)
42-
{
38+
public void onCreate(Bundle savedInstanceState) {
4339
super.onCreate(savedInstanceState);
4440
setContentView(R.layout.activity_add_map);
4541
Button buttonLoadImage = findViewById(R.id.loadimage);
@@ -50,26 +46,19 @@ public void onCreate(Bundle savedInstanceState)
5046
mode = findViewById(R.id.pick);
5147
Button send = findViewById(R.id.send);
5248
send.setOnClickListener(new communicate());
53-
buttonLoadImage.setOnClickListener(new OnClickListener()
54-
{
55-
public void onClick(View arg0)
56-
{
57-
Intent intent = new Intent(Intent.ACTION_PICK,
58-
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
59-
startActivityForResult(intent, 0);
60-
}
49+
buttonLoadImage.setOnClickListener(arg0 -> {
50+
Intent intent = new Intent(Intent.ACTION_PICK,
51+
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
52+
startActivityForResult(intent, 0);
6153
});
6254
}
6355

64-
private class communicate implements View.OnClickListener, Runnable
65-
{
66-
public void onClick(View v)
67-
{
56+
private class communicate implements View.OnClickListener, Runnable {
57+
public void onClick(View v) {
6858
new Thread(this).start();
6959
}
7060

71-
public void run()
72-
{
61+
public void run() {
7362
byte [] encoded_image;
7463
try {
7564
try (Socket ClientSocket = new Socket()) {
@@ -160,8 +149,7 @@ public void run()
160149

161150
}
162151

163-
protected void onActivityResult(int requestCode, int resultCode, Intent data)
164-
{
152+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
165153
super.onActivityResult(requestCode, resultCode, data);
166154

167155
if (resultCode == RESULT_OK) {

REU2017/app/src/main/java/edu/fiu/adwise/fingerprint_localization/ui/LocalizeActivity.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.github.chrisbanes.photoview.OnPhotoTapListener;
2020
import com.github.chrisbanes.photoview.PhotoViewAttacher;
2121

22-
import edu.fiu.adwise.fingerprint_localization.sensors.localization.KeyMaster;
22+
import edu.fiu.adwise.fingerprint_localization.localization.KeyMaster;
2323
import edu.fiu.adwise.fingerprint_localization.distance_computation.LOCALIZATION_SCHEME;
2424
import edu.fiu.reu2017.R;
2525
import edu.fiu.adwise.fingerprint_localization.localization.background;
@@ -84,16 +84,12 @@ public void onClick(View v) {
8484
}
8585
}
8686

87-
private class attach implements OnPhotoTapListener
88-
{
87+
private class attach implements OnPhotoTapListener {
8988
public void onPhotoTap (ImageView view, float x, float y) {
9089
// CLEAR RED DOT, RE-LOAD
91-
imageView.post(new Runnable() {
92-
public void run() {
90+
imageView.post(() ->
9391
imageView.setImageBitmap(Bitmap.createScaledBitmap(KeyMaster.map, imageView.getWidth(),
94-
imageView.getHeight(), false));
95-
}
96-
});
92+
imageView.getHeight(), false)));
9793
my_Attach.update();
9894

9995
if(scan_complete) {

0 commit comments

Comments
 (0)