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. if (!enabled){
  92. Toast.makeText(this.activity, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
  93. }
  94. }
  95. @Override
  96. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
  97. Log.d("WiFiP2pSyncActivity", "Did connect" + ".");
  98. this.activity.setHost(isHost);
  99. if (isHost){
  100. Log.d("WiFiP2pSyncActivity", "Connected as HOST" + ".");
  101. this.activity.startHost();
  102. } else {
  103. Log.d("WiFiP2pSyncActivity", "Connected as Client" + ".");
  104. this.activity.startClient(connectionInfo);
  105. }
  106. }
  107. @Override
  108. public void failedToConnect() {
  109. Log.d("WiFiP2pSyncActivity", "Failed to connect" + ".");
  110. Toast.makeText(this.activity, "Could not connect to device. Retry.", Toast.LENGTH_LONG).show();
  111. }
  112. @Override
  113. public void didDisconnect() {
  114. Log.d("WiFiP2pSyncActivity", "Did disconnect" + ".");
  115. }
  116. @Override
  117. public void failedToDisconnect() {
  118. Log.d("WiFiP2pSyncActivity", "Failed to disconnect" + ".");
  119. Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
  120. }
  121. @Override
  122. public void deviceIsUpdated(WifiP2pDevice device) {
  123. Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
  124. this.activity.updateDevice(device);
  125. }
  126. }.init(this);
  127. }
  128. return _p2pEventListener;
  129. }
  130. private WiFiP2pEventHandler wifiEventHandler(){
  131. if (this._wifiEventHandler == null){
  132. this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
  133. }
  134. return this._wifiEventHandler;
  135. }
  136. private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
  137. if (_syncCompletionListener == null){
  138. _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
  139. WiFiP2pSyncActivity activity = null;
  140. public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
  141. this.activity = act;
  142. return this;
  143. }
  144. @Override
  145. public void didSucceed() {
  146. Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
  147. }
  148. @Override
  149. public void didFail() {
  150. Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
  151. }
  152. }.init(this);
  153. }
  154. return _syncCompletionListener;
  155. }
  156. private void refreshDeviceList(List<WifiP2pDevice> peers){
  157. this.mLstP2PDevices.setAdapter(new WiFiPeerListAdapter(this, R.layout.row_devices, peers));
  158. }
  159. private void startHost(){
  160. Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
  161. Toast.makeText(this, "Acting as Host.", Toast.LENGTH_SHORT).show();
  162. this.hostTask = new SyncHostTask(this.syncCompletionListener(), getApplicationContext());
  163. }
  164. private void startClient(WifiP2pInfo info){
  165. Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
  166. Toast.makeText(this, "Acting as Client.", Toast.LENGTH_SHORT).show();
  167. this.clientTask = new SyncClientTask("" + info.groupOwnerAddress, this.syncCompletionListener(), getApplicationContext() );
  168. }
  169. private static String getDeviceStatus(int deviceStatus) {
  170. switch (deviceStatus) {
  171. case WifiP2pDevice.AVAILABLE:
  172. return "Available";
  173. case WifiP2pDevice.INVITED:
  174. return "Invited";
  175. case WifiP2pDevice.CONNECTED:
  176. return "Connected";
  177. case WifiP2pDevice.FAILED:
  178. return "Failed";
  179. case WifiP2pDevice.UNAVAILABLE:
  180. return "Unavailable";
  181. default:
  182. return "Unknown";
  183. }
  184. }
  185. private void updateDevice(WifiP2pDevice device){
  186. mTxtP2PDeviceName.setText(device.deviceName);
  187. mTxtP2PDeviceStatus.setText(getDeviceStatus(device.status));
  188. mDevice = device;
  189. }
  190. /********************** UI ************************/
  191. public void setWifiDirectAvailable(boolean enabled){
  192. if (enabled){
  193. mTxtP2PNotAvailable.setVisibility(View.GONE);
  194. } else {
  195. mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
  196. ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
  197. mDevice = null;
  198. this.refreshDeviceList(new ArrayList<WifiP2pDevice>());
  199. }
  200. }
  201. private void extractFromView(){
  202. this.mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
  203. this.mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
  204. this.mTxtP2PChangeDeviceName = (TextView) findViewById(R.id.txtP2PChangeDeviceName);
  205. this.mViewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  206. this.mDevicesContainer = (RelativeLayout) findViewById(R.id.devicesContainer);
  207. this.mWelcomeContainer = (RelativeLayout) findViewById(R.id.welcomeContainer);
  208. this.mTxtP2PSearchProgress = (TextView) findViewById(R.id.txtP2PSearchProgress);
  209. this.mLstP2PDevices = (ListView) findViewById(R.id.lstP2PDevices);
  210. this.mTxtP2PNotAvailable = (TextView) findViewById(R.id.txtP2PNotAvailable);
  211. }
  212. public void registerListeners(){
  213. mTxtP2PChangeDeviceName.setOnClickListener(new View.OnClickListener() {
  214. @Override
  215. public void onClick(View v) {
  216. /*
  217. Method method1 = null;
  218. try {
  219. method1 = mManager.getClass().getDeclaredMethod("setDeviceName", WifiP2pManager.Channel.class);
  220. method1.invoke(mManager, mChannel, "Android_fc546");
  221. } catch (NoSuchMethodException e) {
  222. e.printStackTrace();
  223. } catch (InvocationTargetException e) {
  224. e.printStackTrace();
  225. } catch (IllegalAccessException e) {
  226. e.printStackTrace();
  227. }*/
  228. }
  229. });
  230. }
  231. @Override
  232. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  233. final WifiP2pDevice device = (WifiP2pDevice) this.mLstP2PDevices.getAdapter().getItem(position);
  234. this.wifiEventHandler().connect(device);
  235. mOtherDevice = device;
  236. }
  237. /**
  238. * Array adapter for ListFragment that maintains WifiP2pDevice list.
  239. */
  240. private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
  241. private List<WifiP2pDevice> items;
  242. /**
  243. * @param context
  244. * @param textViewResourceId
  245. * @param objects
  246. */
  247. public WiFiPeerListAdapter(Context context, int textViewResourceId,
  248. List<WifiP2pDevice> objects) {
  249. super(context, textViewResourceId, objects);
  250. items = objects;
  251. }
  252. @Override
  253. public View getView(int position, View convertView, ViewGroup parent) {
  254. View v = convertView;
  255. if (v == null) {
  256. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  257. v = vi.inflate(R.layout.row_devices, null);
  258. }
  259. WifiP2pDevice device = items.get(position);
  260. if (device != null) {
  261. TextView top = (TextView) v.findViewById(R.id.device_name);
  262. TextView bottom = (TextView) v.findViewById(R.id.device_details);
  263. if (top != null) {
  264. top.setText(device.deviceName);
  265. }
  266. if (bottom != null) {
  267. bottom.setText(getDeviceStatus(device.status));
  268. }
  269. }
  270. return v;
  271. }
  272. }
  273. }