|
@@ -0,0 +1,330 @@
|
|
|
|
+package de.tudarmstadt.informatik.hostage.sync.wifi_direct.ui;
|
|
|
|
+
|
|
|
|
+import android.app.Activity;
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.net.wifi.p2p.WifiP2pDevice;
|
|
|
|
+import android.net.wifi.p2p.WifiP2pInfo;
|
|
|
|
+import android.os.Bundle;
|
|
|
|
+import android.util.Log;
|
|
|
|
+import android.view.LayoutInflater;
|
|
|
|
+import android.view.View;
|
|
|
|
+import android.view.ViewGroup;
|
|
|
|
+import android.widget.AdapterView;
|
|
|
|
+import android.widget.ArrayAdapter;
|
|
|
|
+import android.widget.ListView;
|
|
|
|
+import android.widget.RelativeLayout;
|
|
|
|
+import android.widget.TextView;
|
|
|
|
+import android.widget.Toast;
|
|
|
|
+import android.widget.ViewAnimator;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import de.tudarmstadt.informatik.hostage.R;
|
|
|
|
+import de.tudarmstadt.informatik.hostage.sync.wifi_direct.BackgroundTask;
|
|
|
|
+import de.tudarmstadt.informatik.hostage.sync.wifi_direct.WiFiP2pEventHandler;
|
|
|
|
+import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncClientTask;
|
|
|
|
+import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncHostTask;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by Julien on 14.01.2015.
|
|
|
|
+ */
|
|
|
|
+public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemClickListener {
|
|
|
|
+
|
|
|
|
+ private SyncClientTask clientTask;
|
|
|
|
+ private SyncHostTask hostTask;
|
|
|
|
+ private boolean isHost;
|
|
|
|
+
|
|
|
|
+ private WiFiP2pEventHandler _wifiEventHandler = null;
|
|
|
|
+
|
|
|
|
+ private WiFiP2pEventHandler.WiFiP2pEventListener _p2pEventListener = null;
|
|
|
|
+ private BackgroundTask.BackgroundTaskCompletionListener _syncCompletionListener = null;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private TextView mTxtP2PDeviceName;
|
|
|
|
+ private TextView mTxtP2PDeviceStatus;
|
|
|
|
+ private ViewAnimator mViewAnimator;
|
|
|
|
+ private RelativeLayout mDevicesContainer;
|
|
|
|
+ private TextView mTxtP2PSearchProgress;
|
|
|
|
+ private ListView mLstP2PDevices;
|
|
|
|
+ private RelativeLayout mWelcomeContainer;
|
|
|
|
+ private TextView mTxtP2PNotAvailable;
|
|
|
|
+ private TextView mTxtP2PChangeDeviceName;
|
|
|
|
+
|
|
|
|
+ private WifiP2pDevice mDevice;
|
|
|
|
+ private WifiP2pDevice mOtherDevice;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public boolean isHost() {
|
|
|
|
+ return isHost;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setHost(boolean isHost) {
|
|
|
|
+ this.isHost = isHost;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onCreate(Bundle savedInstanceState) {
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
+
|
|
|
|
+ this.wifiEventHandler();
|
|
|
|
+
|
|
|
|
+ setContentView(R.layout.activity_p2_psync);
|
|
|
|
+
|
|
|
|
+ assert getActionBar() != null;
|
|
|
|
+ getActionBar().setTitle("WifiDirect Synchronization");
|
|
|
|
+
|
|
|
|
+ this.extractFromView();
|
|
|
|
+ this.registerListeners();
|
|
|
|
+
|
|
|
|
+ this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, new ArrayList()));
|
|
|
|
+ this.mLstP2PDevices.setOnItemClickListener(this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onResume() {
|
|
|
|
+ super.onResume();
|
|
|
|
+ this.wifiEventHandler().onResume();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onPause() {
|
|
|
|
+ super.onPause();
|
|
|
|
+ this.wifiEventHandler().onPause();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
|
|
|
|
+ if (_p2pEventListener == null){
|
|
|
|
+ _p2pEventListener = new WiFiP2pEventHandler.WiFiP2pEventListener() {
|
|
|
|
+ WiFiP2pSyncActivity activity = null;
|
|
|
|
+ public WiFiP2pEventHandler.WiFiP2pEventListener init(WiFiP2pSyncActivity act){
|
|
|
|
+ this.activity = act;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void discoveredDevices(List<WifiP2pDevice> peers) {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Actualise devices" + ".");
|
|
|
|
+ this.activity.refreshDeviceList(peers);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void wifiP2pIsEnabled(boolean enabled) {
|
|
|
|
+ String tmp = enabled? "enabled" : "disabled";
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Peer to peer is " + tmp + ".");
|
|
|
|
+ if (!enabled){
|
|
|
|
+ Toast.makeText(this.activity, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Did connect" + ".");
|
|
|
|
+ this.activity.setHost(isHost);
|
|
|
|
+ if (isHost){
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Connected as HOST" + ".");
|
|
|
|
+ this.activity.startHost();
|
|
|
|
+ } else {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Connected as Client" + ".");
|
|
|
|
+ this.activity.startClient(connectionInfo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void failedToConnect() {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Failed to connect" + ".");
|
|
|
|
+ Toast.makeText(this.activity, "Could not connect to device. Retry.", Toast.LENGTH_LONG).show();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void didDisconnect() {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Did disconnect" + ".");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void failedToDisconnect() {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Failed to disconnect" + ".");
|
|
|
|
+ Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void deviceIsUpdated(WifiP2pDevice device) {
|
|
|
|
+ Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
|
|
|
|
+ this.activity.updateDevice(device);
|
|
|
|
+ }
|
|
|
|
+ }.init(this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return _p2pEventListener;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private WiFiP2pEventHandler wifiEventHandler(){
|
|
|
|
+ if (this._wifiEventHandler == null){
|
|
|
|
+ this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
|
|
|
|
+ }
|
|
|
|
+ return this._wifiEventHandler;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
|
|
|
|
+ if (_syncCompletionListener == null){
|
|
|
|
+ _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
|
|
|
|
+ WiFiP2pSyncActivity activity = null;
|
|
|
|
+ public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
|
|
|
|
+ this.activity = act;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void didSucceed() {
|
|
|
|
+ Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void didFail() {
|
|
|
|
+ Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
|
|
|
|
+ }
|
|
|
|
+ }.init(this);
|
|
|
|
+ }
|
|
|
|
+ return _syncCompletionListener;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void refreshDeviceList(List<WifiP2pDevice> peers){
|
|
|
|
+ this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, peers));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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() );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static String getDeviceStatus(int deviceStatus) {
|
|
|
|
+ switch (deviceStatus) {
|
|
|
|
+ case WifiP2pDevice.AVAILABLE:
|
|
|
|
+ return "Available";
|
|
|
|
+ case WifiP2pDevice.INVITED:
|
|
|
|
+ return "Invited";
|
|
|
|
+ case WifiP2pDevice.CONNECTED:
|
|
|
|
+ return "Connected";
|
|
|
|
+ case WifiP2pDevice.FAILED:
|
|
|
|
+ return "Failed";
|
|
|
|
+ case WifiP2pDevice.UNAVAILABLE:
|
|
|
|
+ return "Unavailable";
|
|
|
|
+ default:
|
|
|
|
+ return "Unknown";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void updateDevice(WifiP2pDevice device){
|
|
|
|
+ mTxtP2PDeviceName.setText(device.deviceName);
|
|
|
|
+ mTxtP2PDeviceStatus.setText(getDeviceStatus(device.status));
|
|
|
|
+ mDevice = device;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /********************** UI ************************/
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void setWifiDirectAvailable(boolean enabled){
|
|
|
|
+ if (enabled){
|
|
|
|
+ mTxtP2PNotAvailable.setVisibility(View.GONE);
|
|
|
|
+ } else {
|
|
|
|
+ mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
|
|
|
|
+ ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
|
|
|
|
+ mDevice = null;
|
|
|
|
+ this.refreshDeviceList(new ArrayList<WifiP2pDevice>());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private void extractFromView(){
|
|
|
|
+ this.mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
|
|
|
|
+ this.mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
|
|
|
|
+ this.mTxtP2PChangeDeviceName = (TextView) findViewById(R.id.txtP2PChangeDeviceName);
|
|
|
|
+
|
|
|
|
+ this.mViewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
|
|
|
|
+ this.mDevicesContainer = (RelativeLayout) findViewById(R.id.devicesContainer);
|
|
|
|
+ this.mWelcomeContainer = (RelativeLayout) findViewById(R.id.welcomeContainer);
|
|
|
|
+ this.mTxtP2PSearchProgress = (TextView) findViewById(R.id.txtP2PSearchProgress);
|
|
|
|
+ this.mLstP2PDevices = (ListView) findViewById(R.id.lstP2PDevices);
|
|
|
|
+ this.mTxtP2PNotAvailable = (TextView) findViewById(R.id.txtP2PNotAvailable);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+ method1.invoke(mManager, mChannel, "Android_fc546");
|
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }*/
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
+ final WifiP2pDevice device = (WifiP2pDevice) this.mLstP2PDevices.getAdapter().getItem(position);
|
|
|
|
+ this.wifiEventHandler().connect(device);
|
|
|
|
+ mOtherDevice = device;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Array adapter for ListFragment that maintains WifiP2pDevice list.
|
|
|
|
+ */
|
|
|
|
+ private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
|
|
|
|
+ private List<WifiP2pDevice> items;
|
|
|
|
+ /**
|
|
|
|
+ * @param context
|
|
|
|
+ * @param textViewResourceId
|
|
|
|
+ * @param objects
|
|
|
|
+ */
|
|
|
|
+ public WiFiPeerListAdapter(Context context, int textViewResourceId,
|
|
|
|
+ List<WifiP2pDevice> objects) {
|
|
|
|
+ super(context, textViewResourceId, objects);
|
|
|
|
+ items = objects;
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
+ View v = convertView;
|
|
|
|
+ if (v == null) {
|
|
|
|
+ LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
+ v = vi.inflate(R.layout.row_devices, null);
|
|
|
|
+ }
|
|
|
|
+ WifiP2pDevice device = items.get(position);
|
|
|
|
+ if (device != null) {
|
|
|
|
+ TextView top = (TextView) v.findViewById(R.id.device_name);
|
|
|
|
+ TextView bottom = (TextView) v.findViewById(R.id.device_details);
|
|
|
|
+ if (top != null) {
|
|
|
|
+ top.setText(device.deviceName);
|
|
|
|
+ }
|
|
|
|
+ if (bottom != null) {
|
|
|
|
+ bottom.setText(getDeviceStatus(device.status));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return v;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|