WiFiP2pSyncActivity.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. private ArrayList<WifiP2pDevice> discoveredDevices = new ArrayList<WifiP2pDevice>();
  47. public boolean isHost() {
  48. return isHost;
  49. }
  50. public void setHost(boolean isHost) {
  51. this.isHost = isHost;
  52. }
  53. @Override
  54. public void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. this.wifiEventHandler();
  57. setContentView(R.layout.activity_p2_psync);
  58. assert getActionBar() != null;
  59. getActionBar().setTitle("WifiDirect Synchronization");
  60. this.extractFromView();
  61. this.registerListeners();
  62. }
  63. @Override
  64. public void onResume() {
  65. super.onResume();
  66. this.wifiEventHandler().onResume();
  67. }
  68. @Override
  69. public void onPause() {
  70. this.wifiEventHandler().onPause();
  71. super.onPause();
  72. }
  73. @Override
  74. protected void onStart()
  75. {
  76. super.onStart();
  77. this.wifiEventHandler().onResume();
  78. }
  79. @Override
  80. protected void onStop()
  81. {
  82. this.wifiEventHandler().disconnect();
  83. this.wifiEventHandler().onPause();
  84. super.onStop();
  85. }
  86. @Override
  87. protected void onDestroy(){
  88. this.wifiEventHandler().onPause();
  89. super.onDestroy();
  90. }
  91. private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
  92. if (_p2pEventListener == null){
  93. _p2pEventListener = new WiFiP2pEventHandler.WiFiP2pEventListener() {
  94. WiFiP2pSyncActivity activity = null;
  95. public WiFiP2pEventHandler.WiFiP2pEventListener init(WiFiP2pSyncActivity act){
  96. this.activity = act;
  97. return this;
  98. }
  99. @Override
  100. public void discoveredDevices(List<WifiP2pDevice> peers) {
  101. Log.d("WiFiP2pSyncActivity", "Actualise devices list" + ".");
  102. this.activity.updateDeviceListView(peers);
  103. }
  104. @Override
  105. public void wifiP2pIsEnabled(boolean enabled) {
  106. String tmp = enabled? "enabled" : "disabled";
  107. Log.d("WiFiP2pSyncActivity", "Peer to peer is " + tmp + ".");
  108. this.activity.setWifiDirectAvailable(enabled);
  109. }
  110. @Override
  111. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
  112. Log.d("WiFiP2pSyncActivity", "Did connect" + ".");
  113. this.activity.setHost(isHost);
  114. if (isHost){
  115. Log.d("WiFiP2pSyncActivity", "Connected as HOST" + ".");
  116. this.activity.startHost();
  117. } else {
  118. Log.d("WiFiP2pSyncActivity", "Connected as Client" + ".");
  119. this.activity.startClient(connectionInfo);
  120. }
  121. }
  122. @Override
  123. public void failedToConnect() {
  124. Log.d("WiFiP2pSyncActivity", "Failed to connect" + ".");
  125. Toast.makeText(this.activity, "Could not connect to device. Retry.", Toast.LENGTH_LONG).show();
  126. }
  127. @Override
  128. public void didDisconnect() {
  129. Log.d("WiFiP2pSyncActivity", "Did disconnect" + ".");
  130. }
  131. @Override
  132. public void failedToDisconnect() {
  133. Log.d("WiFiP2pSyncActivity", "Failed to disconnect" + ".");
  134. Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
  135. }
  136. @Override
  137. public void deviceIsUpdated(WifiP2pDevice device) {
  138. Log.d("WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
  139. this.activity.updateDevice(device);
  140. this.activity.searchForDevices();
  141. }
  142. }.init(this);
  143. }
  144. return _p2pEventListener;
  145. }
  146. private WiFiP2pEventHandler wifiEventHandler(){
  147. if (this._wifiEventHandler == null){
  148. this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
  149. }
  150. return this._wifiEventHandler;
  151. }
  152. private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
  153. if (_syncCompletionListener == null){
  154. _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
  155. WiFiP2pSyncActivity activity = null;
  156. public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
  157. this.activity = act;
  158. return this;
  159. }
  160. @Override
  161. public void didSucceed() {
  162. this.activity.runOnUiThread(new Runnable() {
  163. private WiFiP2pSyncActivity activity;
  164. @Override
  165. public void run() {
  166. Toast.makeText(this.activity, "Synchronization complete.", Toast.LENGTH_SHORT).show();
  167. this.activity.wifiEventHandler().disconnect();
  168. }
  169. public Runnable init(WiFiP2pSyncActivity activity){
  170. this.activity = activity;
  171. return this;
  172. }
  173. }.init(this.activity));
  174. }
  175. @Override
  176. public void didFail() {
  177. this.activity.runOnUiThread(new Runnable() {
  178. private WiFiP2pSyncActivity activity;
  179. @Override
  180. public void run() {
  181. Toast.makeText(this.activity, "Could not synchronize with device. Retry.", Toast.LENGTH_LONG).show();
  182. this.activity.wifiEventHandler().disconnect();
  183. }
  184. public Runnable init(WiFiP2pSyncActivity activity){
  185. this.activity = activity;
  186. return this;
  187. }
  188. }.init(this.activity));
  189. }
  190. }.init(this);
  191. }
  192. return _syncCompletionListener;
  193. }
  194. private void updateDeviceListView(List<WifiP2pDevice> peers){
  195. mTxtP2PSearchProgress.setVisibility(View.GONE);
  196. this.discoveredDevices = new ArrayList<WifiP2pDevice>();
  197. this.discoveredDevices.addAll(peers);
  198. WiFiPeerListAdapter listAdapter = (WiFiPeerListAdapter) this.mLstP2PDevices.getAdapter();
  199. listAdapter.addItems(peers);
  200. this.runOnUiThread(new Runnable() {
  201. private ListView listView;
  202. @Override
  203. public void run() {
  204. WiFiPeerListAdapter adapter = (WiFiPeerListAdapter) this.listView.getAdapter();
  205. this.listView.setAdapter(null);
  206. adapter.notifyDataSetChanged();
  207. this.listView.setAdapter(adapter);
  208. }
  209. public Runnable init(ListView listview) {
  210. this.listView = listview;
  211. return this;
  212. }
  213. }.init(this.mLstP2PDevices));
  214. Log.d("WiFiP2pSyncActivity", " Discovered "+peers.size()+" devices.");
  215. }
  216. private void startHost(){
  217. Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
  218. Toast.makeText(this, "Acting as Host.", Toast.LENGTH_SHORT).show();
  219. this.hostTask = new SyncHostTask(this.syncCompletionListener(), getApplicationContext());
  220. this.hostTask.execute();
  221. }
  222. private void startClient(WifiP2pInfo info){
  223. Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
  224. Toast.makeText(this, "Acting as Client.", Toast.LENGTH_SHORT).show();
  225. this.clientTask = new SyncClientTask(info.groupOwnerAddress.getHostAddress(), this.syncCompletionListener(), getApplicationContext() );
  226. this.clientTask.execute();
  227. }
  228. private static String getDeviceStatus(int deviceStatus) {
  229. switch (deviceStatus) {
  230. case WifiP2pDevice.AVAILABLE:
  231. return "Available";
  232. case WifiP2pDevice.INVITED:
  233. return "Invited";
  234. case WifiP2pDevice.CONNECTED:
  235. return "Connected";
  236. case WifiP2pDevice.FAILED:
  237. return "Failed";
  238. case WifiP2pDevice.UNAVAILABLE:
  239. return "Unavailable";
  240. default:
  241. return "Unknown";
  242. }
  243. }
  244. private void updateDevice(WifiP2pDevice device){
  245. mTxtP2PDeviceName.setText(device.deviceName);
  246. mTxtP2PDeviceStatus.setText(getDeviceStatus(device.status));
  247. mDevice = device;
  248. }
  249. private void searchForDevices(){
  250. mTxtP2PSearchProgress.setVisibility(View.VISIBLE);
  251. this.wifiEventHandler().discoverDevices();
  252. }
  253. /********************** UI ************************/
  254. public void setWifiDirectAvailable(boolean enabled){
  255. if (enabled){
  256. mTxtP2PNotAvailable.setVisibility(View.GONE);
  257. } else {
  258. mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
  259. ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
  260. mDevice = null;
  261. this.updateDeviceListView(new ArrayList<WifiP2pDevice>());
  262. Toast.makeText(this, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
  263. }
  264. }
  265. private void extractFromView(){
  266. this.mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
  267. this.mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
  268. this.mTxtP2PChangeDeviceName = (TextView) findViewById(R.id.txtP2PChangeDeviceName);
  269. this.mViewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  270. this.mDevicesContainer = (RelativeLayout) findViewById(R.id.devicesContainer);
  271. this.mWelcomeContainer = (RelativeLayout) findViewById(R.id.welcomeContainer);
  272. this.mTxtP2PSearchProgress = (TextView) findViewById(R.id.txtP2PSearchProgress);
  273. this.mLstP2PDevices = (ListView) findViewById(R.id.lstP2PDevices);
  274. this.mTxtP2PNotAvailable = (TextView) findViewById(R.id.txtP2PNotAvailable);
  275. }
  276. public void registerListeners(){
  277. /*
  278. mTxtP2PChangeDeviceName.setOnClickListener(new View.OnClickListener() {
  279. @Override
  280. public void onClick(View v) {
  281. Method method1 = null;
  282. try {
  283. method1 = mManager.getClass().getDeclaredMethod("setDeviceName", WifiP2pManager.Channel.class);
  284. method1.invoke(mManager, mChannel, "Android_fc546");
  285. } catch (NoSuchMethodException e) {
  286. e.printStackTrace();
  287. } catch (InvocationTargetException e) {
  288. e.printStackTrace();
  289. } catch (IllegalAccessException e) {
  290. e.printStackTrace();
  291. }
  292. }
  293. });*/
  294. if (this.mLstP2PDevices.getOnItemClickListener() != this)
  295. this.mLstP2PDevices.setOnItemClickListener(this);
  296. if (this.mLstP2PDevices.getAdapter() == null){
  297. this.discoveredDevices = new ArrayList();
  298. WiFiPeerListAdapter listAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, this.discoveredDevices);
  299. this.mLstP2PDevices.setAdapter(listAdapter);
  300. }
  301. }
  302. @Override
  303. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  304. final WifiP2pDevice device = (WifiP2pDevice) this.mLstP2PDevices.getAdapter().getItem(position);
  305. this.wifiEventHandler().connect(device);
  306. mOtherDevice = device;
  307. }
  308. /**
  309. * Array adapter for ListFragment that maintains WifiP2pDevice list.
  310. */
  311. private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
  312. private List<WifiP2pDevice> items;
  313. /**
  314. * @param context
  315. * @param textViewResourceId
  316. * @param objects
  317. */
  318. public WiFiPeerListAdapter(Context context, int textViewResourceId,
  319. List<WifiP2pDevice> objects) {
  320. super(context, textViewResourceId, objects);
  321. items = objects;
  322. }
  323. @Override
  324. public int getCount() {
  325. return items.size();
  326. }
  327. public void addItems(List<WifiP2pDevice> devicesToAdd){
  328. items.clear();
  329. items.addAll(devicesToAdd);
  330. }
  331. @Override
  332. public View getView(int position, View convertView, ViewGroup parent) {
  333. View v = convertView;
  334. if (v == null) {
  335. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  336. v = vi.inflate(R.layout.row_devices, null);
  337. }
  338. WifiP2pDevice device = items.get(position);
  339. if (device != null) {
  340. TextView top = (TextView) v.findViewById(R.id.device_name);
  341. TextView bottom = (TextView) v.findViewById(R.id.device_details);
  342. if (top != null) {
  343. top.setText(device.deviceName);
  344. }
  345. if (bottom != null) {
  346. bottom.setText(getDeviceStatus(device.status));
  347. }
  348. }
  349. return v;
  350. }
  351. }
  352. }