|
@@ -54,6 +54,9 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
private WifiP2pDevice mDevice;
|
|
|
private WifiP2pDevice mOtherDevice;
|
|
|
|
|
|
+ private ArrayList<WifiP2pDevice> discoveredDevices = new ArrayList<WifiP2pDevice>();
|
|
|
+
|
|
|
+
|
|
|
|
|
|
public boolean isHost() {
|
|
|
return isHost;
|
|
@@ -78,8 +81,6 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
this.extractFromView();
|
|
|
this.registerListeners();
|
|
|
|
|
|
- this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, new ArrayList()));
|
|
|
- this.mLstP2PDevices.setOnItemClickListener(this);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -104,6 +105,7 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
@Override
|
|
|
protected void onStop()
|
|
|
{
|
|
|
+ this.wifiEventHandler().disconnect();
|
|
|
this.wifiEventHandler().onPause();
|
|
|
super.onStop();
|
|
|
}
|
|
@@ -125,8 +127,8 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
|
|
|
@Override
|
|
|
public void discoveredDevices(List<WifiP2pDevice> peers) {
|
|
|
- Log.d("WiFiP2pSyncActivity", "Actualise devices" + ".");
|
|
|
- this.activity.refreshDeviceList(peers);
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Actualise devices list" + ".");
|
|
|
+ this.activity.updateDeviceListView(peers);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -171,7 +173,7 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
public void deviceIsUpdated(WifiP2pDevice device) {
|
|
|
Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
|
|
|
this.activity.updateDevice(device);
|
|
|
- this.activity.wifiEventHandler().discoverDevices();
|
|
|
+ this.activity.searchForDevices();
|
|
|
|
|
|
}
|
|
|
}.init(this);
|
|
@@ -199,12 +201,34 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
}
|
|
|
@Override
|
|
|
public void didSucceed() {
|
|
|
- Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
|
|
|
+ this.activity.runOnUiThread(new Runnable() {
|
|
|
+ private WiFiP2pSyncActivity activity;
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
|
|
|
+ this.activity.wifiEventHandler().disconnect();
|
|
|
+ }
|
|
|
+ public Runnable init(WiFiP2pSyncActivity activity){
|
|
|
+ this.activity = activity;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }.init(this.activity));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void didFail() {
|
|
|
- Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
|
|
|
+ this.activity.runOnUiThread(new Runnable() {
|
|
|
+ private WiFiP2pSyncActivity activity;
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
|
|
|
+ this.activity.wifiEventHandler().disconnect();
|
|
|
+ }
|
|
|
+ public Runnable init(WiFiP2pSyncActivity activity){
|
|
|
+ this.activity = activity;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }.init(this.activity));
|
|
|
}
|
|
|
}.init(this);
|
|
|
}
|
|
@@ -212,20 +236,44 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
}
|
|
|
|
|
|
|
|
|
- private void refreshDeviceList(List<WifiP2pDevice> peers){
|
|
|
- this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, peers));
|
|
|
+ private void updateDeviceListView(List<WifiP2pDevice> peers){
|
|
|
+ mTxtP2PSearchProgress.setVisibility(View.GONE);
|
|
|
+
|
|
|
+ this.discoveredDevices = new ArrayList<WifiP2pDevice>();
|
|
|
+ this.discoveredDevices.addAll(peers);
|
|
|
+ WiFiPeerListAdapter listAdapter = (WiFiPeerListAdapter) this.mLstP2PDevices.getAdapter();
|
|
|
+ listAdapter.addItems(peers);
|
|
|
+ this.runOnUiThread(new Runnable() {
|
|
|
+ private ListView listView;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ WiFiPeerListAdapter adapter = (WiFiPeerListAdapter) this.listView.getAdapter();
|
|
|
+ this.listView.setAdapter(null);
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+ this.listView.setAdapter(adapter);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Runnable init(ListView listview) {
|
|
|
+ this.listView = listview;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }.init(this.mLstP2PDevices));
|
|
|
+ Log.d("WiFiP2pSyncActivity", " Discovered "+peers.size()+" devices.");
|
|
|
}
|
|
|
|
|
|
private void startHost(){
|
|
|
Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
|
|
|
Toast.makeText(this, "Acting as Host.", Toast.LENGTH_SHORT).show();
|
|
|
this.hostTask = new SyncHostTask(this.syncCompletionListener(), getApplicationContext());
|
|
|
+ this.hostTask.execute();
|
|
|
}
|
|
|
|
|
|
private void startClient(WifiP2pInfo info){
|
|
|
Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
|
|
|
Toast.makeText(this, "Acting as Client.", Toast.LENGTH_SHORT).show();
|
|
|
- this.clientTask = new SyncClientTask("" + info.groupOwnerAddress, this.syncCompletionListener(), getApplicationContext() );
|
|
|
+ this.clientTask = new SyncClientTask(info.groupOwnerAddress.getHostAddress(), this.syncCompletionListener(), getApplicationContext() );
|
|
|
+ this.clientTask.execute();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -253,6 +301,11 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
mDevice = device;
|
|
|
}
|
|
|
|
|
|
+ private void searchForDevices(){
|
|
|
+ mTxtP2PSearchProgress.setVisibility(View.VISIBLE);
|
|
|
+ this.wifiEventHandler().discoverDevices();
|
|
|
+ }
|
|
|
+
|
|
|
/********************** UI ************************/
|
|
|
|
|
|
|
|
@@ -263,7 +316,7 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
|
|
|
((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
|
|
|
mDevice = null;
|
|
|
- this.refreshDeviceList(new ArrayList<WifiP2pDevice>());
|
|
|
+ this.updateDeviceListView(new ArrayList<WifiP2pDevice>());
|
|
|
|
|
|
Toast.makeText(this, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
|
|
|
}
|
|
@@ -285,10 +338,10 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
}
|
|
|
|
|
|
public void registerListeners(){
|
|
|
+ /*
|
|
|
mTxtP2PChangeDeviceName.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
|
public void onClick(View v) {
|
|
|
- /*
|
|
|
Method method1 = null;
|
|
|
try {
|
|
|
method1 = mManager.getClass().getDeclaredMethod("setDeviceName", WifiP2pManager.Channel.class);
|
|
@@ -299,9 +352,18 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
e.printStackTrace();
|
|
|
} catch (IllegalAccessException e) {
|
|
|
e.printStackTrace();
|
|
|
- }*/
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
+ });*/
|
|
|
+
|
|
|
+ if (this.mLstP2PDevices.getOnItemClickListener() != this)
|
|
|
+ this.mLstP2PDevices.setOnItemClickListener(this);
|
|
|
+
|
|
|
+ if (this.mLstP2PDevices.getAdapter() == null){
|
|
|
+ this.discoveredDevices = new ArrayList();
|
|
|
+ WiFiPeerListAdapter listAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, this.discoveredDevices);
|
|
|
+ this.mLstP2PDevices.setAdapter(listAdapter);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -328,6 +390,18 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
|
|
|
super(context, textViewResourceId, objects);
|
|
|
items = objects;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return items.size();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void addItems(List<WifiP2pDevice> devicesToAdd){
|
|
|
+ items.clear();
|
|
|
+ items.addAll(devicesToAdd);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
View v = convertView;
|