mirror of
https://github.com/DanielPollithy/flashwifi.git
synced 2025-10-16 11:45:32 +00:00
Billing channel
This commit is contained in:
parent
1081a17b66
commit
9a75b7bca2
@ -59,11 +59,16 @@ public class MainActivity extends AppCompatActivity
|
||||
}
|
||||
|
||||
private void initUi() {
|
||||
Switch switch_ = (Switch) findViewById(R.id.wifiSwitch);
|
||||
final Switch switch_ = (Switch) findViewById(R.id.wifiSwitch);
|
||||
switch_.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
mService.enableService();
|
||||
if (!mBound) {
|
||||
switch_.setChecked(false);
|
||||
Toast.makeText(getApplicationContext(), "Wifi Broadcast not bound", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
mService.enableService();
|
||||
}
|
||||
} else {
|
||||
mService.disableService();
|
||||
}
|
||||
|
||||
@ -13,16 +13,20 @@ import android.net.wifi.p2p.WifiP2pInfo;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.TaskStackBuilder;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
@ -31,7 +35,7 @@ import com.flashwifi.wifip2p.broadcast.WiFiDirectBroadcastService;
|
||||
|
||||
|
||||
public class RoamingActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "RoamingActivity";
|
||||
ArrayList<String> arrayList;
|
||||
ArrayAdapter<String> listAdapter;
|
||||
ListView listView;
|
||||
@ -55,6 +59,7 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction("com.flashwifi.wifip2p.update_ui");
|
||||
filter.addAction("com.flashwifi.wifip2p.update_roaming");
|
||||
|
||||
updateUIReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
@ -74,8 +79,25 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void updateUi(Intent intent) {
|
||||
NetworkInfo network_info = mService.getNetwork_info();
|
||||
WifiP2pInfo p2p_info = mService.getP2p_info();
|
||||
if (intent.getAction().equals("com.flashwifi.wifip2p.update_roaming")) {
|
||||
Log.d(TAG, "updateUi: Received change from AsyncTask");
|
||||
String message = intent.getStringExtra("message");
|
||||
if (message != null) {
|
||||
Log.d(TAG, "updateUi: message=" + message);
|
||||
CheckBox apConnected = (CheckBox)findViewById(R.id.accessPointActive);
|
||||
if (message.equals("AP SUCCESS")) {
|
||||
apConnected.setChecked(true);
|
||||
// when the AP is setup we can start the server
|
||||
startBillingProtocol();
|
||||
} else if (message.equals("AP FAILED")) {
|
||||
apConnected.setChecked(false);
|
||||
Toast.makeText(getApplicationContext(), "Could not create Access point", Toast.LENGTH_LONG).show();
|
||||
} else if (message.equals("AP STOPPED")) {
|
||||
apConnected.setChecked(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//TextView connection_status = (TextView) findViewById(R.id.connection_status);
|
||||
//final View activity_view = findViewById(R.id.chatView);
|
||||
@ -84,9 +106,19 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
|
||||
}
|
||||
|
||||
private void startBillingProtocol() {
|
||||
// setup the flash channel etc...
|
||||
if (mService.isInRoleHotspot()) {
|
||||
mService.startBillingServer();
|
||||
} else {
|
||||
mService.startBillingClient();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
unregisterReceiver(updateUIReceiver);
|
||||
unbindService(mConnection);
|
||||
mBound = false;
|
||||
}
|
||||
@ -102,21 +134,29 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
address = intent.getStringExtra("address");
|
||||
|
||||
initUI();
|
||||
showNotification();
|
||||
|
||||
}
|
||||
|
||||
private void cancelNotification() {
|
||||
NotificationManager mNotificationManager =
|
||||
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
mNotificationManager.cancel(1);
|
||||
}
|
||||
|
||||
|
||||
private void showNotification() {
|
||||
// The id of the channel.
|
||||
String CHANNEL_ID = "com.flashwifi.wifip2p.roaming_1";
|
||||
|
||||
String contentText = mService.isInRoleHotspot() ? "You are providing" : "You are consuming";
|
||||
|
||||
// ToDo: make this work on high API version
|
||||
NotificationCompat.Builder mBuilder =
|
||||
new NotificationCompat.Builder(this)
|
||||
.setSmallIcon(R.drawable.icon_tethering_on)
|
||||
.setContentTitle("My notification")
|
||||
.setContentText("Hello World!");
|
||||
.setContentTitle("IOTA HOTSPOT")
|
||||
.setContentText(contentText);
|
||||
// Creates an explicit intent for an Activity in your app
|
||||
Intent resultIntent = new Intent(this, RoamingActivity.class);
|
||||
|
||||
@ -146,7 +186,6 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
private void initUI() {
|
||||
|
||||
//final EditText input = (EditText) findViewById(R.id.chat_input);
|
||||
//Button button = (Button) findViewById(R.id.btn_send);
|
||||
/*button.setOnClickListener(new View.OnClickListener() {
|
||||
@ -199,13 +238,40 @@ public class RoamingActivity extends AppCompatActivity {
|
||||
};
|
||||
|
||||
private void initUIWithService() {
|
||||
|
||||
TextView info_text = (TextView) findViewById(R.id.info_text);
|
||||
if (mService.isInRoleHotspot()) {
|
||||
info_text.setText(R.string.roaming_title);
|
||||
} else {
|
||||
info_text.setText(R.string.roaming_title_client);
|
||||
}
|
||||
|
||||
stopButton = (Button) findViewById(R.id.stopRoamingButton);
|
||||
stopButton.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
// ToDo: stop the roaming here
|
||||
cancelNotification();
|
||||
if (mService.isInRoleHotspot()){
|
||||
mService.stopAP();
|
||||
} else {
|
||||
mService.disconnectAP();
|
||||
}
|
||||
mService.setRoaming(false);
|
||||
Toast.makeText(getApplicationContext(), "Press BACK now", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
// ToDo: change the status text
|
||||
|
||||
showNotification();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (mBound) {
|
||||
if (mService.isRoaming()) {
|
||||
Toast.makeText(getApplicationContext(), "stop roaming before leaving", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -102,9 +102,9 @@ public class SearchFragment extends Fragment {
|
||||
what = "";
|
||||
}
|
||||
|
||||
// ToDo: do we need this???
|
||||
if (what.equals("connectivity_changed")) {
|
||||
// startNegotiationProtocol();
|
||||
String currentDeviceConnected = intent.getStringExtra("currentDeviceConnected");
|
||||
startNegotiationProtocol(currentDeviceConnected);
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,6 +191,7 @@ public class SearchFragment extends Fragment {
|
||||
toggle.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(final View view) {
|
||||
updateList();
|
||||
if (toggle.isChecked()) {
|
||||
if (mBound) {
|
||||
mService.setInRoleHotspot(false);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.flashwifi.wifip2p.accesspoint;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
@ -15,12 +16,19 @@ import static android.content.Context.WIFI_SERVICE;
|
||||
public class AccessPointTask extends AsyncTask<Context, Void, String> {
|
||||
|
||||
private final static String TAG = "AccessPointTask";
|
||||
private Context context;
|
||||
private Context context;
|
||||
String ssid = "Iotify";
|
||||
String key = "1234567890";
|
||||
WifiManager wifi;
|
||||
WifiInfo w;
|
||||
|
||||
private void sendUpdateUIBroadcastWithMessage(String message){
|
||||
Intent local = new Intent();
|
||||
local.putExtra("message", message);
|
||||
local.setAction("com.flashwifi.wifip2p.update_roaming");
|
||||
context.sendBroadcast(local);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Context... params) {
|
||||
context = params[0];
|
||||
@ -67,17 +75,11 @@ public class AccessPointTask extends AsyncTask<Context, Void, String> {
|
||||
|
||||
if(apstatus)
|
||||
{
|
||||
System.out.println("SUCCESS");
|
||||
//statusView.append("\nAccess Point Created!");
|
||||
//finish();
|
||||
//Intent searchSensorsIntent = new Intent(this,SearchSensors.class);
|
||||
//startActivity(searchSensorsIntent);
|
||||
sendUpdateUIBroadcastWithMessage("AP SUCCESS");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("FAILED");
|
||||
|
||||
//statusView.append("\nAccess Point Creation failed!");
|
||||
sendUpdateUIBroadcastWithMessage("AP FAILED");
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.flashwifi.wifip2p.accesspoint;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
@ -21,9 +22,18 @@ public class ConnectTask extends AsyncTask<Context, Void, String> {
|
||||
WifiManager wifi;
|
||||
WifiInfo w;
|
||||
|
||||
Context context;
|
||||
|
||||
private void sendUpdateUIBroadcastWithMessage(String message){
|
||||
Intent local = new Intent();
|
||||
local.putExtra("message", message);
|
||||
local.setAction("com.flashwifi.wifip2p.update_roaming");
|
||||
context.sendBroadcast(local);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Context... params) {
|
||||
Context context = params[0];
|
||||
context = params[0];
|
||||
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
|
||||
|
||||
WifiInfo info = wifiManager.getConnectionInfo(); //get WifiInfo
|
||||
@ -35,11 +45,6 @@ public class ConnectTask extends AsyncTask<Context, Void, String> {
|
||||
wifiConfig.preSharedKey = String.format("\"%s\"", key);
|
||||
|
||||
Log.d(TAG, "doInBackground: GO to sleep");
|
||||
try {
|
||||
Thread.sleep(30 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.d(TAG, "doInBackground: disconnect");
|
||||
int netId = wifiManager.addNetwork(wifiConfig);
|
||||
|
||||
@ -56,8 +61,10 @@ public class ConnectTask extends AsyncTask<Context, Void, String> {
|
||||
boolean worked = wifiManager.enableNetwork(i.networkId, true);
|
||||
if (worked) {
|
||||
Log.d(TAG, "doInBackground: WORKED enableNetwork");
|
||||
sendUpdateUIBroadcastWithMessage("AP SUCCESS");
|
||||
} else {
|
||||
Log.d(TAG, "doInBackground: ERROROROROROROROORORORORORO------x error");
|
||||
Log.d(TAG, "doInBackground: Error connecting to the network");
|
||||
sendUpdateUIBroadcastWithMessage("AP FAILED");
|
||||
}
|
||||
wifiManager.reconnect();
|
||||
|
||||
@ -67,7 +74,7 @@ public class ConnectTask extends AsyncTask<Context, Void, String> {
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(TAG, "doInBackground: now wait");
|
||||
/*Log.d(TAG, "doInBackground: now wait");
|
||||
|
||||
int number_minutes = 60;
|
||||
for (int i=0; i<number_minutes; i++) {
|
||||
@ -76,7 +83,7 @@ public class ConnectTask extends AsyncTask<Context, Void, String> {
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.flashwifi.wifip2p.accesspoint;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
@ -20,6 +21,13 @@ public class StopAccessPointTask extends AsyncTask<Context, Void, String> {
|
||||
WifiManager wifi;
|
||||
WifiInfo w;
|
||||
|
||||
private void sendUpdateUIBroadcastWithMessage(String message){
|
||||
Intent local = new Intent();
|
||||
local.putExtra("message", message);
|
||||
local.setAction("com.flashwifi.wifip2p.update_roaming");
|
||||
context.sendBroadcast(local);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Context... params) {
|
||||
context = params[0];
|
||||
@ -51,6 +59,8 @@ public class StopAccessPointTask extends AsyncTask<Context, Void, String> {
|
||||
wifi.setWifiEnabled(true);
|
||||
}
|
||||
|
||||
sendUpdateUIBroadcastWithMessage("AP STOPPED");
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -92,6 +92,7 @@ public class BillingClient {
|
||||
// ToDo: mark bill accepted
|
||||
// ToDo: flash object -> diff()
|
||||
// ToDo: sign flash transaction
|
||||
// ToDo: update UI
|
||||
latestBillAnswer = new BillMessageAnswer("id", true, "", false);
|
||||
latestBillAnswerString = gson.toJson(latestBillAnswer);
|
||||
socketWrapper.sendLine(latestBillAnswerString);
|
||||
|
||||
@ -1,5 +1,23 @@
|
||||
package com.flashwifi.wifip2p.billing;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.flashwifi.wifip2p.negotiation.SocketWrapper;
|
||||
import com.flashwifi.wifip2p.protocol.BillMessage;
|
||||
import com.flashwifi.wifip2p.protocol.BillMessageAnswer;
|
||||
import com.flashwifi.wifip2p.protocol.BillingCloseChannel;
|
||||
import com.flashwifi.wifip2p.protocol.BillingCloseChannelAnswer;
|
||||
import com.flashwifi.wifip2p.protocol.BillingOpenChannel;
|
||||
import com.flashwifi.wifip2p.protocol.BillingOpenChannelAnswer;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 1) This class keeps the socket connection alive.
|
||||
* 2) It tracks the state of the communication.
|
||||
@ -10,38 +28,132 @@ package com.flashwifi.wifip2p.billing;
|
||||
*/
|
||||
|
||||
public class BillingServer {
|
||||
private static final String TAG = "BillingServer";
|
||||
private final Gson gson;
|
||||
private State state = State.NOT_PAIRED;
|
||||
private ServerSocket serverSocket;
|
||||
private Socket socket;
|
||||
private SocketWrapper socketWrapper;
|
||||
private int PORT = 9199;
|
||||
private static final int serverTimeoutMillis = 2 * 60 * 1000;
|
||||
|
||||
public BillingServer(int bookedMegabytes, int timeoutMinutes){
|
||||
Accountant.getInstance().start(bookedMegabytes,timeoutMinutes);
|
||||
gson = new GsonBuilder().create();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
public void start() throws IOException {
|
||||
// 0) create deadline guard
|
||||
createDeadlineGuard();
|
||||
// 1) create a socket
|
||||
|
||||
while (state != State.ERROR || state != State.FULLY_ATTACHED) {
|
||||
// 2) accept a connection
|
||||
if (state == State.NOT_PAIRED) {
|
||||
// 3) pair client and hotspot and synchronize states
|
||||
}
|
||||
while (state == State.ROAMING) {
|
||||
// 4) sleep(60)
|
||||
// 5) createNewBill
|
||||
// 6) send the bill and the signed flash object
|
||||
}
|
||||
if (state == State.CLOSED) {
|
||||
// 7) close the channel and sign the final settlement
|
||||
// 8) receive the signed settlement from client
|
||||
}
|
||||
while (state == State.CLOSED) {
|
||||
// 9) reattach and rebroadcast the final transaction
|
||||
}
|
||||
if (state == State.FULLY_ATTACHED) {
|
||||
// DONE!
|
||||
while (state != State.CLOSED) {
|
||||
try {
|
||||
// create server socket
|
||||
serverSocket = new ServerSocket(PORT);
|
||||
serverSocket.setSoTimeout(serverTimeoutMillis);
|
||||
Log.d(TAG, "doInBackground: Server is waiting for connection");
|
||||
|
||||
socket = serverSocket.accept();
|
||||
socket.setSoTimeout(serverTimeoutMillis);
|
||||
Log.d(TAG, "start: Server accepted a connection");
|
||||
|
||||
// wrap the socket
|
||||
socketWrapper = new SocketWrapper(socket);
|
||||
|
||||
if (state == State.NOT_PAIRED) {
|
||||
// await current state of client
|
||||
String clientState = socketWrapper.getLineThrowing();
|
||||
if (!clientState.contains("NOT_PAIRED")) {
|
||||
Log.d(TAG, "start: PROBLEM PROBLEM PROBLEM! States not synced.");
|
||||
// ToDo: How to synchronize client and server states?
|
||||
}
|
||||
// ToDo: send an eventually stored flash channel
|
||||
socketWrapper.sendLine("Hello, Client! I am in state: NOT_PAIRED");
|
||||
|
||||
// receive the BillingOpenChannel message
|
||||
String billingOpenChannelString = socketWrapper.getLineThrowing();
|
||||
BillingOpenChannel billingOpenChannel = gson.fromJson(billingOpenChannelString, BillingOpenChannel.class);
|
||||
|
||||
// answer with billingOpenChannelAnswerString
|
||||
// ToDo: create the flash channel
|
||||
String[] myDigests = new String[]{"1234", "2345", "3456"};
|
||||
BillingOpenChannelAnswer billingOpenChannelAnswer = new BillingOpenChannelAnswer(0, 0, "", "", myDigests);
|
||||
String billingOpenChannelAnswerString = gson.toJson(billingOpenChannelAnswer);
|
||||
socketWrapper.sendLine(billingOpenChannelAnswerString);
|
||||
|
||||
// OK
|
||||
state = State.ROAMING;
|
||||
}
|
||||
|
||||
if (state == State.ROAMING) {
|
||||
String latestBillString;
|
||||
BillMessage latestBill;
|
||||
Bill b;
|
||||
String latestBillAnswerString;
|
||||
BillMessageAnswer latestBillAnswer;
|
||||
|
||||
// loop until roaming ends
|
||||
int count = 0;
|
||||
|
||||
while (state == State.ROAMING) {
|
||||
// sleep 1 minute
|
||||
Log.d(TAG, "start: I go to sleep for 1 minute!");
|
||||
Thread.sleep(60 * 1000);
|
||||
Log.d(TAG, "start: Good morning!");
|
||||
// create new bill
|
||||
// ToDo: integrate real network data
|
||||
// ToDo: calculate time correctly
|
||||
b = Accountant.getInstance().createBill(0,0,1);
|
||||
// ToDo: integrate real flash channel
|
||||
latestBill = new BillMessage(b, "", false);
|
||||
latestBillString = gson.toJson(latestBill);
|
||||
socketWrapper.sendLine(latestBillString);
|
||||
|
||||
// get answer
|
||||
latestBillAnswerString = socketWrapper.getLineThrowing();
|
||||
latestBillAnswer = gson.fromJson(latestBillAnswerString, BillMessageAnswer.class);
|
||||
// ToDo: check signature
|
||||
// ToDo: flash object -> diff()
|
||||
// ToDo: update UI
|
||||
|
||||
// close the channel if the last invoice has <closeAfterwards>
|
||||
// or the last answer had <closeAfterwards>
|
||||
if (latestBill.isCloseAfterwards() || latestBillAnswer.isCloseAfterwards()) {
|
||||
state = State.CLOSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (state == State.CLOSE) {
|
||||
// ToDo: handle the final deposit of the flash channel
|
||||
// ToDo: sign the transaction
|
||||
BillingCloseChannel billingCloseChannel = new BillingCloseChannel(0,0,0,0,"", "", "");
|
||||
String closeChannelString = gson.toJson(billingCloseChannel);
|
||||
socketWrapper.sendLine(closeChannelString);
|
||||
|
||||
String billingCloseChannelAnswerString = socketWrapper.getLineThrowing();
|
||||
BillingCloseChannelAnswer billingCloseChannelAnswer = gson.fromJson(billingCloseChannelAnswerString, BillingCloseChannelAnswer.class);
|
||||
// ToDo: validate the signature
|
||||
state = State.CLOSED;
|
||||
}
|
||||
|
||||
if (state == State.CLOSED) {
|
||||
// ToDo: attach the last transaction to the tangle
|
||||
boolean attached = false;
|
||||
while (!attached) {
|
||||
Log.d(TAG, "start: Attach to tangle please");
|
||||
}
|
||||
state = State.FULLY_ATTACHED;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void createDeadlineGuard() {
|
||||
@ -53,6 +165,7 @@ public class BillingServer {
|
||||
NOT_PAIRED,
|
||||
INITIAL,
|
||||
ROAMING,
|
||||
CLOSE,
|
||||
CLOSED,
|
||||
FULLY_ATTACHED,
|
||||
ERROR
|
||||
|
||||
@ -19,8 +19,11 @@ import android.util.Log;
|
||||
import com.flashwifi.wifip2p.accesspoint.AccessPointTask;
|
||||
import com.flashwifi.wifip2p.accesspoint.ConnectTask;
|
||||
import com.flashwifi.wifip2p.accesspoint.StopAccessPointTask;
|
||||
import com.flashwifi.wifip2p.billing.BillingClient;
|
||||
import com.flashwifi.wifip2p.billing.BillingServer;
|
||||
import com.flashwifi.wifip2p.negotiation.Negotiator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
@ -37,6 +40,9 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
private boolean setup = false;
|
||||
private boolean inRoleHotspot = false;
|
||||
private boolean inRoleConsumer = false;
|
||||
private boolean isRoaming = false;
|
||||
private boolean billingServerIsRunning = false;
|
||||
private boolean billingClientIsRunning = false;
|
||||
|
||||
private boolean enabled = false;
|
||||
|
||||
@ -87,28 +93,76 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
}
|
||||
|
||||
public void startBillingServer(){
|
||||
if (!billingServerIsRunning) {
|
||||
billingServerIsRunning = true;
|
||||
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// ToDo: remove magic numbers
|
||||
BillingServer billingServer = new BillingServer(100, 20);
|
||||
|
||||
try {
|
||||
billingServer.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// ToDo: handle billingServer EXIT CODES
|
||||
// -> close the roaming etc.
|
||||
}
|
||||
};
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void startBillingClient() {
|
||||
if (!billingClientIsRunning) {
|
||||
billingClientIsRunning = true;
|
||||
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// ToDo: remove magic numbers
|
||||
BillingClient billingClient = new BillingClient();
|
||||
// ToDo: get the AP gateway ip address
|
||||
// https://stackoverflow.com/questions/9035784/how-to-know-ip-address-of-the-router-from-code-in-android
|
||||
billingClient.start("192.168.43.1");
|
||||
// ToDo: handle billingServer EXIT CODES
|
||||
// -> close the roaming etc.
|
||||
}
|
||||
};
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
}
|
||||
}
|
||||
|
||||
public void connect2AP(String ssid, String key) {
|
||||
connectTask = new ConnectTask();
|
||||
Log.d("xxxxxxxxxxxxxx", "CONNECT TO THE HOTSPOT");
|
||||
Log.d(TAG, "connect2AP: CONNECT TO THE HOTSPOT");
|
||||
connectTask.execute(getApplicationContext());
|
||||
}
|
||||
|
||||
|
||||
public void disconnectAP() {
|
||||
}
|
||||
|
||||
public void startAP() {
|
||||
Log.d("xxxxxxxxxxxxxx", "start AP");
|
||||
if (!apRuns) {
|
||||
Log.d(TAG, "start Access point");
|
||||
apRuns = true;
|
||||
apTask = new AccessPointTask();
|
||||
apTask.execute(getApplicationContext());
|
||||
} else {
|
||||
Log.d("", "startSocketServer: ALREADY RUNNING");
|
||||
Log.d(TAG, "Access point ALREADY RUNNING");
|
||||
}
|
||||
}
|
||||
|
||||
public void stopAP() {
|
||||
Log.d("xxxxxxxxxxxxxx", "stop AP");
|
||||
Log.d(TAG, "stop AP");
|
||||
if (apRuns) {
|
||||
apRuns = false;
|
||||
new StopAccessPointTask().execute(getApplicationContext());
|
||||
@ -160,10 +214,9 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
public void run() {
|
||||
Negotiator negotiator = new Negotiator(isClient, getWFDMacAddress());
|
||||
String peer_mac_address = null;
|
||||
while (enabled && peer_mac_address == null) {
|
||||
while (!isRoaming && enabled && peer_mac_address == null) {
|
||||
Log.d(TAG, "run: " + enabled);
|
||||
peer_mac_address = negotiator.workAsServer();
|
||||
deletePersistentGroups();
|
||||
// ToDo: use other broadcast for this
|
||||
// sendUpdateUIBroadcast();
|
||||
try {
|
||||
@ -171,17 +224,27 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (peer_mac_address == null) {
|
||||
deletePersistentGroups();
|
||||
}
|
||||
}
|
||||
if (peer_mac_address != null) {
|
||||
// block the discovery mode due to switch to roaming state
|
||||
setRoaming(true);
|
||||
// tell the UI
|
||||
sendStartRoamingBroadcast(peer_mac_address);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
if (!isRoaming()) {
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
} else {
|
||||
Log.d(TAG, "startNegServer: BLOCKED due to roaming state");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendStartRoamingBroadcast(String peer_mac_address) {
|
||||
@ -197,25 +260,35 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
public void run() {
|
||||
Negotiator negotiator = new Negotiator(isClient, getWFDMacAddress());
|
||||
String peer_mac_address = null;
|
||||
while (enabled && peer_mac_address == null) {
|
||||
while (!isRoaming && enabled && peer_mac_address == null) {
|
||||
Log.d(TAG, "run: " + enabled);
|
||||
System.out.println(" *******+ work as client *******");
|
||||
peer_mac_address = negotiator.workAsClient(address.getHostAddress());
|
||||
deletePersistentGroups();
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (peer_mac_address == null) {
|
||||
deletePersistentGroups();
|
||||
}
|
||||
}
|
||||
if (peer_mac_address != null) {
|
||||
// block the discovery mode due to switch to roaming state
|
||||
setRoaming(true);
|
||||
// tell the UI
|
||||
sendStartRoamingBroadcast(peer_mac_address);
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
if (!isRoaming()) {
|
||||
Thread thread = new Thread(task);
|
||||
threads.add(thread);
|
||||
AsyncTask.execute(thread);
|
||||
} else {
|
||||
Log.d(TAG, "startNegotiationClient: BLOCKED due to roaming state");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -313,6 +386,18 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
this.inRoleConsumer = inRoleConsumer;
|
||||
}
|
||||
|
||||
public boolean isRoaming() {
|
||||
return isRoaming;
|
||||
}
|
||||
|
||||
public void setRoaming(boolean roaming) {
|
||||
if (roaming) {
|
||||
stopAllTasks();
|
||||
}
|
||||
isRoaming = roaming;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class used for the client Binder. Because we know this service always
|
||||
* runs in the same process as its clients, we don't need to deal with IPC.
|
||||
@ -388,7 +473,7 @@ public class WiFiDirectBroadcastService extends Service {
|
||||
}
|
||||
|
||||
private void sendUpdateUIBroadcastNewConnection(){
|
||||
Log.d(TAG, "sendUpdateUIBroadcastNewConnection: SEND THEEM SHIIIIIIIT");
|
||||
Log.d(TAG, "sendUpdateUIBroadcastNewConnection: NOTIFY UI ABOUT NEW CONNECTION");
|
||||
Intent local = new Intent();
|
||||
local.setAction("com.flashwifi.wifip2p.update_ui");
|
||||
local.putExtra("what", "connectivity_changed");
|
||||
|
||||
@ -43,15 +43,7 @@ public class PeerListAdapter extends ArrayAdapter<PeerInformation> {
|
||||
TextView tt3 = (TextView) v.findViewById(R.id.description);
|
||||
TextView tt4 = (TextView) v.findViewById(R.id.ipAddr);
|
||||
TextView tt5 = (TextView) v.findViewById(R.id.iotaPrice);
|
||||
TableRow row = (TableRow) v.findViewById(R.id.talkingRow);
|
||||
|
||||
if (row != null) {
|
||||
if (p.isSelected()) {
|
||||
row.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
row.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
WifiP2pDevice device = p.getWifiP2pDevice();
|
||||
if (device != null) {
|
||||
|
||||
@ -269,7 +269,7 @@ public class Negotiator {
|
||||
// ToDo: Don't mock this
|
||||
hotspot_state = HotspotState.GENERATE_PASSWORD;
|
||||
String password = "123456789";
|
||||
String hotspotName = "Iotify-123";
|
||||
String hotspotName = "Iotify";
|
||||
|
||||
// send password and hotspot name
|
||||
NegotiationFinalization finalization = new NegotiationFinalization(hotspotName, password,
|
||||
|
||||
7
app/src/main/res/drawable/listview_colours.xml
Normal file
7
app/src/main/res/drawable/listview_colours.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:drawable="@color/colour_highlight_grey" android:state_pressed="true"/>
|
||||
<item android:drawable="@color/colour_dark_blue" android:state_selected="true"/>
|
||||
<item android:drawable="@color/colour_dark_green" android:state_activated="true"/>
|
||||
<item android:drawable="@color/colour_dark_blue" android:state_focused="true"/>
|
||||
</selector>
|
||||
@ -39,23 +39,24 @@
|
||||
android:text="@string/roamingTitle"
|
||||
android:textSize="20sp" />
|
||||
|
||||
<CheckedTextView
|
||||
<CheckBox
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:layout_marginRight="15dp"
|
||||
android:checkMark="@android:drawable/checkbox_on_background"
|
||||
android:checked="true"
|
||||
android:checked="false"
|
||||
android:clickable="false"
|
||||
android:id="@+id/accessPointActive"
|
||||
android:text="Access point connection" />
|
||||
|
||||
<CheckedTextView
|
||||
<CheckBox
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:layout_marginRight="15dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:checkMark="@android:drawable/checkbox_on_background"
|
||||
android:checked="true"
|
||||
android:checked="false"
|
||||
android:clickable="false"
|
||||
android:text="Flash channel" />
|
||||
|
||||
<Button
|
||||
|
||||
@ -29,8 +29,10 @@
|
||||
|
||||
<ListView
|
||||
android:id="@+id/peer_list"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/cardview_light_background"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@ -1,86 +1,95 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="wrap_content" android:orientation="vertical"
|
||||
android:layout_width="fill_parent">
|
||||
|
||||
<TableRow android:layout_width="fill_parent"
|
||||
android:id="@+id/TableRow01"
|
||||
android:layout_height="wrap_content">
|
||||
<LinearLayout
|
||||
android:layout_height="wrap_content"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent">
|
||||
|
||||
<TextView android:textColor="#000000"
|
||||
android:id="@+id/deviceName"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Device name"
|
||||
android:textStyle="bold"
|
||||
android:gravity="left"
|
||||
android:layout_weight="1"
|
||||
android:typeface="monospace"
|
||||
android:height="40sp" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow android:layout_height="wrap_content"
|
||||
android:layout_width="fill_parent">
|
||||
|
||||
<TextView android:textColor="#000000"
|
||||
android:id="@+id/categoryId"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="macAddress"
|
||||
android:layout_weight="1"
|
||||
android:height="20sp" />
|
||||
|
||||
<TextView android:layout_height="wrap_content"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_weight="1"
|
||||
android:textColor="#000000"
|
||||
android:gravity="right"
|
||||
android:id="@+id/description"
|
||||
android:text="description"
|
||||
android:height="20sp" />
|
||||
</TableRow>
|
||||
<TableRow android:layout_height="wrap_content"
|
||||
android:layout_width="fill_parent">
|
||||
|
||||
<TextView android:textColor="#000000"
|
||||
android:id="@+id/ipAddr"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="ipAddr"
|
||||
android:layout_weight="1"
|
||||
android:height="20sp" />
|
||||
|
||||
<TextView android:layout_height="wrap_content"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_weight="1"
|
||||
android:textColor="#000000"
|
||||
android:gravity="right"
|
||||
android:id="@+id/iotaPrice"
|
||||
android:text="iotaPrice"
|
||||
android:height="20sp" />
|
||||
|
||||
</TableRow>
|
||||
|
||||
<TableRow
|
||||
android:layout_width="fill_parent"
|
||||
<android.support.v7.widget.CardView
|
||||
android:id="@+id/ly_root"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:visibility="invisible"
|
||||
android:id="@+id/talkingRow">
|
||||
android:background="#9f9e9e"
|
||||
android:layout_margin="15dp"
|
||||
app:cardCornerRadius="4dp"
|
||||
android:padding="15dp"
|
||||
android:listSelector="@drawable/listview_colours" >
|
||||
|
||||
|
||||
<ProgressBar
|
||||
style="?android:attr/progressBarStyle"
|
||||
android:layout_width="63dp"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Stop Talking" />
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_margin="15dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/deviceName"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="left"
|
||||
android:layout_marginRight="50dp"
|
||||
android:text="Device name"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/categoryId"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end"
|
||||
android:text="macAddress"
|
||||
android:textColor="#000000" />
|
||||
|
||||
|
||||
</TableRow>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_marginLeft="15dp"
|
||||
android:layout_marginRight="15dp"
|
||||
android:layout_marginBottom="15dp"
|
||||
>
|
||||
|
||||
<TextView android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:textColor="#000000"
|
||||
android:gravity="right"
|
||||
android:id="@+id/description"
|
||||
android:text="description"
|
||||
android:layout_marginRight="20dp"/>
|
||||
|
||||
<TextView android:textColor="#000000"
|
||||
android:id="@+id/ipAddr"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="ipAddr"
|
||||
android:layout_marginRight="20dp"/>
|
||||
|
||||
<TextView android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:textColor="#000000"
|
||||
android:id="@+id/iotaPrice"
|
||||
android:text="iotaPrice" />
|
||||
|
||||
|
||||
</TableLayout>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
</android.support.v7.widget.CardView>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@ -3,4 +3,9 @@
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#07daff</color>
|
||||
|
||||
<color name="black_overlay">#66000000</color>
|
||||
<color name="colour_highlight_grey">#ff666666</color>
|
||||
<color name="colour_dark_blue">#ff000066</color>
|
||||
<color name="colour_dark_green">#ff006600</color>
|
||||
</resources>
|
||||
|
||||
@ -62,5 +62,7 @@
|
||||
<string name="roamingTitle">Roaming</string>
|
||||
<string name="searchIntroduction">Do you want to search for a hotspot?</string>
|
||||
<string name="startSearch">Start Search</string>
|
||||
<string name="roaming_title">Roaming (This is your hotspot)</string>
|
||||
<string name="roaming_title_client">Roaming (You are consuming)</string>
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue
Block a user