|
@@ -442,12 +442,25 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class ClientAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
+ public static class ClientAsyncTask extends AsyncTask<String, Integer, Integer> {
|
|
|
private Context context;
|
|
|
private int port;
|
|
|
private String host;
|
|
|
private static final int SOCKET_TIMEOUT = 10000;
|
|
|
private int tryNum = 0;
|
|
|
+ private final ProgressDialog progressDialog;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPreExecute() {
|
|
|
+ super.onPreExecute();
|
|
|
+
|
|
|
+ this.progressDialog.setMessage("Synchronizing data with other device...");
|
|
|
+ this.progressDialog.setIndeterminate(false);
|
|
|
+ this.progressDialog.setMax(100);
|
|
|
+ this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
+ this.progressDialog.setCancelable(true);
|
|
|
+ this.progressDialog.show();
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @param context
|
|
@@ -456,10 +469,11 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
|
|
|
this.context = context;
|
|
|
this.host = host;
|
|
|
this.port = port;
|
|
|
+ this.progressDialog = new ProgressDialog(context);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- protected Void doInBackground(Void... params) {
|
|
|
+ protected Integer doInBackground(String... params) {
|
|
|
Socket socket = new Socket();
|
|
|
tryNum++;
|
|
|
|
|
@@ -469,16 +483,56 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
|
|
|
socket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT);
|
|
|
|
|
|
Log.i("client", "connected to server");
|
|
|
- OutputStream stream = socket.getOutputStream();
|
|
|
+ publishProgress(1);
|
|
|
+
|
|
|
+ ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
|
|
|
+ ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
|
|
|
+
|
|
|
+ // Client sends first!
|
|
|
+ SyncInfo thisSyncInfo = new SyncInfo();
|
|
|
+ thisSyncInfo.deviceMap.put("dev4", 12L);
|
|
|
+ thisSyncInfo.deviceMap.put("dev5", 13L);
|
|
|
+
|
|
|
+ oos.writeObject(thisSyncInfo);
|
|
|
+ oos.flush();
|
|
|
+ publishProgress(2);
|
|
|
+
|
|
|
+ // --- 1. Receive sync info
|
|
|
+ SyncInfo otherSyncInfo = null;
|
|
|
|
|
|
- Log.i("client", "sending: " + "HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am " + host);
|
|
|
- stream.write(("HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am " + host).getBytes());
|
|
|
- stream.flush();
|
|
|
- stream.close();
|
|
|
+ try {
|
|
|
+ otherSyncInfo = (SyncInfo) ois.readObject();
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ publishProgress(3);
|
|
|
+
|
|
|
+ // --- 2. Send sync data
|
|
|
+ SyncData thisSyncData = new SyncData();
|
|
|
+ thisSyncData.records.add(new Record());
|
|
|
+ oos.writeObject(thisSyncData);
|
|
|
+ oos.flush();
|
|
|
+ publishProgress(4);
|
|
|
+
|
|
|
+ // --- 3. Receive sync data
|
|
|
+ SyncData otherSyncData = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ otherSyncData = (SyncData) ois.readObject();
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ publishProgress(5);
|
|
|
+
|
|
|
+ // --
|
|
|
+
|
|
|
+ ois.close();
|
|
|
+ oos.close();
|
|
|
socket.close();
|
|
|
} catch (IOException e) {
|
|
|
- if(tryNum >= 12) {
|
|
|
- return null;
|
|
|
+ if(tryNum >= 5) {
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
long seconds_to_wait = (long) Math.min(60, Math.pow(2, tryNum));
|
|
@@ -492,7 +546,18 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onProgressUpdate(Integer... progress) {
|
|
|
+ this.progressDialog.setProgress(progress[0] * (100 / 5));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostExecute(Integer unused) {
|
|
|
+ this.progressDialog.dismiss();
|
|
|
}
|
|
|
}
|
|
|
|