Skip to content

Commit

Permalink
Merge branch 'master' of github.com:brigadinhos/LocMess
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrosado committed May 18, 2017
2 parents 0e3387a + 211f45f commit bc22c9e
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 14 deletions.
12 changes: 8 additions & 4 deletions LocMessClient/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />

<uses-feature android:name="android.hardware.location.gps" />

<application
Expand All @@ -27,11 +30,7 @@
android:label="@string/app_name"
android:screenOrientation="portrait" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
Expand All @@ -45,6 +44,11 @@
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddMessage"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pt.ulisboa.tecnico.cmu.tg14.locmessclient.DataObjects;

/**
* Created by brigadinhos on 17/05/2017.
*/

public class DataObject {
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package pt.ulisboa.tecnico.cmu.tg14.locmessclient.DataObjects;

import java.io.DataOutput;
import java.util.List;
import java.util.UUID;

/**
* Created by brigadinhos on 26/04/2017.
*/

public class Message {
public class Message extends DataObject {

private UUID id;
private long creationTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created by basilio on 06-05-2017.
*/

public class Profile {
public class Profile extends DataObject {
private String key;
private String value;
private String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.WifiService;
import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Utils.FeedReaderDbHelper;

import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Utils.Network.ServerActions;
import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Utils.ServiceManager;


Expand Down Expand Up @@ -182,9 +183,8 @@ public boolean onNavigationItemSelected(final MenuItem item) {
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
logout();
startActivity(intent);
}
Expand Down Expand Up @@ -230,7 +230,6 @@ public void onFragmentInteraction(Uri uri) {
private void logout() {
// delete all sensitive data from database
// TODO : do I need to stop services?

FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getApplicationContext());
// dbHelper.dropDatabase(activity);
dbHelper.deleteAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void putExtras(Intent i) {
i.putExtra("mMessageContent", mMessageContent);
i.putExtra("mStartTime", mStartTime);
i.putExtra("mEndTime", mEndTime);
i.putExtra("mIsDecentralized", mSwitch.isChecked());
i.putExtra("mIsDecentralized", mSwitch.isChecked()); // if false is centralized
i.putExtra("mID", mID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public void onClick(View view) {
message.setBlackList(blackList);

dbHelper.insertMessage(message);

if (mIsDecentralized) {
// add the message to mule table because is decentralized
dbHelper.insertMessageMule(message);
}

finish();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import pt.ulisboa.tecnico.cmu.tg14.locmessclient.DataObjects.DataObject;

/**
* Created by brigadinhos on 17/05/2017.
*/

public class BluetoothConnection {

private Context context;

private BluetoothAdapter mBTAdapter;
private AcceptThread acceptThread;
private ConnectThread connectThread;
private ConnectedThread connectedThread;

private BluetoothDevice mDevice;
private UUID deviceUUID;

private static final String appName = "LocMess";
private static final UUID LOCMESS_UUID = UUID.fromString("c70f6eff-bef2-44c8-a6d4-82b0e2f0913d");

private List<DataObject> objs;

public BluetoothConnection(Context context) {
this.context = context;
startSession();
}

private class AcceptThread extends Thread {
private final BluetoothServerSocket bsSocket;

public AcceptThread() {
BluetoothServerSocket socketTemp = null;

try {
socketTemp = mBTAdapter.listenUsingInsecureRfcommWithServiceRecord(appName, LOCMESS_UUID);
} catch (IOException e) {
e.printStackTrace();
}
bsSocket = socketTemp;
}

public void run() {
BluetoothSocket bSocket = null;

try {
bSocket = bsSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}

if (bSocket != null) {
connected(bSocket, mDevice);
}
}

public void cancel() {
try {
bsSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private class ConnectThread extends Thread {
private BluetoothSocket socket;

public ConnectThread(BluetoothDevice device, UUID uuid) {
mDevice = device;
deviceUUID = uuid;
}

public void run() {
BluetoothSocket temp = null;

try {
temp = mDevice.createRfcommSocketToServiceRecord(deviceUUID);
} catch (IOException e) {
e.printStackTrace();
}

socket = temp;

mBTAdapter.cancelDiscovery();

try {
socket.connect();
} catch (IOException e) {
e.printStackTrace();
// can not connect so close the socket
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}

connected(socket, mDevice);
}

public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private class ConnectedThread extends Thread {
InputStream input = null;
OutputStream output = null;
BluetoothSocket socket;

public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;

try {
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

}

public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = input.read(buffer);
ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(buffer));
objs = (ArrayList<DataObject>) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
// end the connection
break;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void write(byte[] bytes) {
try {
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

public synchronized void startSession() {
if (connectThread != null) {
connectThread.cancel();
connectThread = null;
}
if (acceptThread == null) {
acceptThread = new AcceptThread();
acceptThread.start();
}
}

public void startClient(BluetoothDevice device, UUID uuid) {
connectThread = new ConnectThread(device, uuid);
connectThread.start();
}

private void connected(BluetoothSocket socket, BluetoothDevice device) {
connectedThread = new ConnectedThread(socket);
connectedThread.start();
}

public void write(byte[] out) {
connectedThread.write(out);
}

}
Loading

0 comments on commit bc22c9e

Please sign in to comment.