WiFiP2pSyncActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct.ui;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.net.wifi.p2p.WifiP2pDevice;
  5. import android.net.wifi.p2p.WifiP2pInfo;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import android.widget.RelativeLayout;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import android.widget.ViewAnimator;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import de.tudarmstadt.informatik.hostage.R;
  21. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.BackgroundTask;
  22. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.WiFiP2pEventHandler;
  23. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncClientTask;
  24. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncHostTask;
  25. /**
  26. * Created by Julien on 14.01.2015.
  27. */
  28. public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemClickListener {
  29. private SyncClientTask clientTask;
  30. private SyncHostTask hostTask;
  31. private boolean isHost;
  32. private WiFiP2pEventHandler _wifiEventHandler = null;
  33. private WiFiP2pEventHandler.WiFiP2pEventListener _p2pEventListener = null;
  34. private BackgroundTask.BackgroundTaskCompletionListener _syncCompletionListener = null;
  35. private TextView mTxtP2PDeviceName;
  36. private TextView mTxtP2PDeviceStatus;
  37. private ViewAnimator mViewAnimator;
  38. private RelativeLayout mDevicesContainer;
  39. private TextView mTxtP2PSearchProgress;
  40. private ListView mLstP2PDevices;
  41. private RelativeLayout mWelcomeContainer;
  42. private TextView mTxtP2PNotAvailable;
  43. private TextView mTxtP2PChangeDeviceName;
  44. private WifiP2pDevice mDevice;
  45. private WifiP2pDevice mOtherDevice;
  46. public boolean isHost() {
  47. return isHost;
  48. }
  49. public void setHost(boolean isHost) {
  50. this.isHost = isHost;
  51. }
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. this.wifiEventHandler();
  56. setContentView(R.layout.activity_p2_psync);
  57. assert getActionBar() != null;
  58. getActionBar().setTitle("WifiDirect Synchronization");
  59. this.extractFromView();
  60. this.registerListeners();
  61. this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, new ArrayList()));
  62. this.mLstP2PDevices.setOnItemClickListener(this);
  63. }
  64. @Override
  65. public void onResume() {
  66. super.onResume();
  67. this.wifiEventHandler().onResume();
  68. }
  69. @Override
  70. public void onPause() {
  71. super.onPause();
  72. this.wifiEventHandler().onPause();
  73. }
  74. private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
  75. if (_p2pEventListener == null){
  76. _p2pEventListener = new WiFiP2pEventHandler.WiFiP2pEventListener() {
  77. WiFiP2pSyncActivity activity = null;
  78. public WiFiP2pEventHandler.WiFiP2pEventListener init(WiFiP2pSyncActivity act){
  79. this.activity = act;
  80. return this;
  81. }
  82. @Override
  83. public void discoveredDevices(List<WifiP2pDevice> peers) {
  84. Log.d("WiFiP2pSyncActivity", "Actualise devices" + ".");
  85. this.activity.refreshDeviceList(peers);
  86. }
  87. @Override
  88. public void wifiP2pIsEnabled(boolean enabled) {
  89. String tmp = enabled? "enabled" : "disabled";
  90. Log.d("WiFiP2pSyncActivity", "Peer to peer is " + tmp + ".");
  91. this.activity.setWifiDirectAvailable(enabled);
  92. }
  93. @Override
  94. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
  95. Log.d("WiFiP2pSyncActivity", "Did connect" + ".");
  96. this.activity.setHost(isHost);
  97. if (isHost){
  98. Log.d("WiFiP2pSyncActivity", "Connected as HOST" + ".");
  99. this.activity.startHost();
  100. } else {
  101. Log.d("WiFiP2pSyncActivity", "Connected as Client" + ".");
  102. this.activity.startClient(connectionInfo);
  103. }
  104. }
  105. @Override
  106. public void failedToConnect() {
  107. Log.d("WiFiP2pSyncActivity", "Failed to connect" + ".");
  108. Toast.makeText(this.activity, "Could not connect to device. Retry.", Toast.LENGTH_LONG).show();
  109. }
  110. @Override
  111. public void didDisconnect() {
  112. Log.d("WiFiP2pSyncActivity", "Did disconnect" + ".");
  113. }
  114. @Override
  115. public void failedToDisconnect() {
  116. Log.d("WiFiP2pSyncActivity", "Failed to disconnect" + ".");
  117. Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
  118. }
  119. @Override
  120. public void deviceIsUpdated(WifiP2pDevice device) {
  121. Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
  122. this.activity.updateDevice(device);
  123. }
  124. }.init(this);
  125. }
  126. return _p2pEventListener;
  127. }
  128. private WiFiP2pEventHandler wifiEventHandler(){
  129. if (this._wifiEventHandler == null){
  130. this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
  131. }
  132. return this._wifiEventHandler;
  133. }
  134. private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
  135. if (_syncCompletionListener == null){
  136. _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
  137. WiFiP2pSyncActivity activity = null;
  138. public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
  139. this.activity = act;
  140. return this;
  141. }
  142. @Override
  143. public void didSucceed() {
  144. Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
  145. }
  146. @Override
  147. public void didFail() {
  148. Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
  149. }
  150. }.init(this);
  151. }
  152. return _syncCompletionListener;
  153. }
  154. private void refreshDeviceList(List<WifiP2pDevice> peers){
  155. this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, peers));
  156. }
  157. private void startHost(){
  158. Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
  159. Toast.makeText(this, "Acting as Host.", Toast.LENGTH_SHORT).show();
  160. this.hostTask = new SyncHostTask(this.syncCompletionListener(), getApplicationContext());
  161. }
  162. private void startClient(WifiP2pInfo info){
  163. Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
  164. Toast.makeText(this, "Acting as Client.", Toast.LENGTH_SHORT).show();
  165. this.clientTask = new SyncClientTask("" + info.groupOwnerAddress, this.syncCompletionListener(), getApplicationContext() );
  166. }
  167. private static String getDeviceStatus(int deviceStatus) {
  168. switch (deviceStatus) {
  169. case WifiP2pDevice.AVAILABLE:
  170. return "Available";
  171. case WifiP2pDevice.INVITED:
  172. return "Invited";
  173. case WifiP2pDevice.CONNECTED:
  174. return "Connected";
  175. case WifiP2pDevice.FAILED:
  176. return "Failed";
  177. case WifiP2pDevice.UNAVAILABLE:
  178. return "Unavailable";
  179. default:
  180. return "Unknown";
  181. }
  182. }
  183. private void updateDevice(WifiP2pDevice device){
  184. mTxtP2PDeviceName.setText(device.deviceName);
  185. mTxtP2PDeviceStatus.setText(getDeviceStatus(device.status));
  186. mDevice = device;
  187. }
  188. /********************** UI ************************/
  189. public void setWifiDirectAvailable(boolean enabled){
  190. if (enabled){
  191. mTxtP2PNotAvailable.setVisibility(View.GONE);
  192. } else {
  193. mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
  194. ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
  195. mDevice = null;
  196. this.refreshDeviceList(new ArrayList<WifiP2pDevice>());
  197. Toast.makeText(this, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
  198. }
  199. }
  200. private void extractFromView(){
  201. this.mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
  202. this.mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
  203. this.mTxtP2PChangeDeviceName = (TextView) findViewById(R.id.txtP2PChangeDeviceName);
  204. this.mViewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  205. this.mDevicesContainer = (RelativeLayout) findViewById(R.id.devicesContainer);
  206. this.mWelcomeContainer = (RelativeLayout) findViewById(R.id.welcomeContainer);
  207. this.mTxtP2PSearchProgress = (TextView) findViewById(R.id.txtP2PSearchProgress);
  208. this.mLstP2PDevices = (ListView) findViewById(R.id.lstP2PDevices);
  209. this.mTxtP2PNotAvailable = (TextView) findViewById(R.id.txtP2PNotAvailable);
  210. }
  211. public void registerListeners(){
  212. mTxtP2PChangeDeviceName.setOnClickListener(new View.OnClickListener() {
  213. @Override
  214. public void onClick(View v) {
  215. /*
  216. Method method1 = null;
  217. try {
  218. method1 = mManager.getClass().getDeclaredMethod("setDeviceName", WifiP2pManager.Channel.class);
  219. method1.invoke(mManager, mChannel, "Android_fc546");
  220. } catch (NoSuchMethodException e) {
  221. e.printStackTrace();
  222. } catch (InvocationTargetException e) {
  223. e.printStackTrace();
  224. } catch (IllegalAccessException e) {
  225. e.printStackTrace();
  226. }*/
  227. }
  228. });
  229. }
  230. @Override
  231. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  232. final WifiP2pDevice device = (WifiP2pDevice) this.mLstP2PDevices.getAdapter().getItem(position);
  233. this.wifiEventHandler().connect(device);
  234. mOtherDevice = device;
  235. }
  236. /**
  237. * Array adapter for ListFragment that maintains WifiP2pDevice list.
  238. */
  239. private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
  240. private List<WifiP2pDevice> items;
  241. /**
  242. * @param context
  243. * @param textViewResourceId
  244. * @param objects
  245. */
  246. public WiFiPeerListAdapter(Context context, int textViewResourceId,
  247. List<WifiP2pDevice> objects) {
  248. super(context, textViewResourceId, objects);
  249. items = objects;
  250. }
  251. @Override
  252. public View getView(int position, View convertView, ViewGroup parent) {
  253. View v = convertView;
  254. if (v == null) {
  255. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  256. v = vi.inflate(R.layout.row_devices, null);
  257. }
  258. WifiP2pDevice device = items.get(position);
  259. if (device != null) {
  260. TextView top = (TextView) v.findViewById(R.id.device_name);
  261. TextView bottom = (TextView) v.findViewById(R.id.device_details);
  262. if (top != null) {
  263. top.setText(device.deviceName);
  264. }
  265. if (bottom != null) {
  266. bottom.setText(getDeviceStatus(device.status));
  267. }
  268. }
  269. return v;
  270. }
  271. }
  272. }