WiFiP2pSyncActivity.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct.ui;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.net.wifi.p2p.WifiP2pDevice;
  9. import android.net.wifi.p2p.WifiP2pInfo;
  10. import android.os.Bundle;
  11. import android.provider.Settings;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.AdapterView;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.ListView;
  20. import android.widget.RelativeLayout;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23. import android.widget.ViewAnimator;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Timer;
  27. import java.util.TimerTask;
  28. import de.tudarmstadt.informatik.hostage.R;
  29. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.BackgroundTask;
  30. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.WiFiP2pEventHandler;
  31. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncClientTask;
  32. import de.tudarmstadt.informatik.hostage.sync.wifi_direct.sync_tasks.SyncHostTask;
  33. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  34. /**
  35. * Created by Julien on 14.01.2015.
  36. */
  37. public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemClickListener {
  38. public static String CONNECTION_LOST_MESSAGE = MainActivity.getContext().getString(R.string.CONNECTION_LOST_MESSAGE);// "Connection lost permanently, please enable wifi direct.";
  39. public static String COULD_NOT_CONNECT_MESSAGE =MainActivity.getContext().getString(R.string.COULD_NOT_CONNECT_MESSAGE);// "Could not connect to device. Retry.";
  40. public static String SYNCHRONIZATION_COMPLETE_MESSAGE =MainActivity.getContext().getString(R.string.SYNCHRONIZATION_COMPLETE_MESSAGE);// "Synchronization complete.";
  41. public static String SYNCHRONIZATION_FAILED_MESSAGE =MainActivity.getContext().getString(R.string.SYNCHRONIZATION_FAILED_MESSAGE);// "Could not synchronize devices. Retry";
  42. public static String PERFORMING_TASK_AS_HOST =MainActivity.getContext().getString(R.string.PERFORMING_TASK_AS_HOST);// "Acting as Host.";
  43. public static String PERFORMING_TASK_AS_CLIENT = MainActivity.getContext().getString(R.string.PERFORMING_TASK_AS_CLIENT);//"Acting as Client.";
  44. public static String ACTIONBAR_TITLE =MainActivity.getContext().getString(R.string.ACTIONBAR_TITLE);// "WifiDirect Synchronization";
  45. public static String PROGRESS_MESSAGE_LOADING =MainActivity.getContext().getString(R.string.PROGRESS_TITLE_LOADING);// "Loading...";
  46. public static String PROGRESS_MESSAGE_CONNECTING = MainActivity.getContext().getString(R.string.PROGRESS_TITLE_CONNECTING);//"Connecting...";
  47. public static String PROGRESS_TITLE_SYNC = MainActivity.getContext().getString(R.string.PROGRESS_TITLE_SYNC);//"Connecting...";
  48. public static String DEVICE_STATUS_AVAILABLE =MainActivity.getContext().getString(R.string.DEVICE_STATUS_AVAILABLE);// "Available";
  49. public static String DEVICE_STATUS_INVITED =MainActivity.getContext().getString(R.string.DEVICE_STATUS_INVITED);// "Invited";
  50. public static String DEVICE_STATUS_CONNECTED = MainActivity.getContext().getString(R.string.DEVICE_STATUS_CONNECTED);//"Connected";
  51. public static String DEVICE_STATUS_FAILED =MainActivity.getContext().getString(R.string.DEVICE_STATUS_FAILED);// "Failed";
  52. public static String DEVICE_STATUS_UNAVAILABLE =MainActivity.getContext().getString(R.string.DEVICE_STATUS_UNAVAILABLE);// "Unavailable";
  53. public static String DEVICE_STATUS_UNKNOWN =MainActivity.getContext().getString(R.string.DEVICE_STATUS_UNKNOWN);// "Unknown";
  54. public static String WIFI_STATUS_DISABLED_MESSAGE =MainActivity.getContext().getString(R.string.WIFI_STATUS_DISABLED_MESSAGE);// "WiFi Direct down, please enable WiFi Direct";
  55. public static String WIFI_STATUS_ENABLE_BUTTON = MainActivity.getContext().getString(R.string.WIFI_STATUS_ENABLE_BUTTON);//"Enable WiFi Direct";
  56. public static String CANCEL_BUTTON_TITLE =MainActivity.getContext().getString(R.string.CANCEL_BUTTON_TITLE);// "Cancel";
  57. private SyncClientTask clientTask;
  58. private SyncHostTask hostTask;
  59. private BackgroundTask executingTask;
  60. private boolean isHost;
  61. private WiFiP2pEventHandler _wifiEventHandler = null;
  62. private WiFiP2pEventHandler.WiFiP2pEventListener _p2pEventListener = null;
  63. private BackgroundTask.BackgroundTaskCompletionListener _syncCompletionListener = null;
  64. private TextView mTxtP2PDeviceName;
  65. private TextView mTxtP2PDeviceStatus;
  66. private ViewAnimator mViewAnimator;
  67. private RelativeLayout mDevicesContainer;
  68. private TextView mTxtP2PSearchProgress;
  69. private ListView mLstP2PDevices;
  70. private RelativeLayout mWelcomeContainer;
  71. private TextView mTxtP2PNotAvailable;
  72. private TextView mTxtP2PChangeDeviceName;
  73. private WifiP2pDevice ownDevice;
  74. private WifiP2pDevice mOtherDevice;
  75. private ArrayList<WifiP2pDevice> discoveredDevices = new ArrayList<WifiP2pDevice>();
  76. private ProgressDialog progressDialog;
  77. public boolean isHost() {
  78. return isHost;
  79. }
  80. public void setHost(boolean isHost) {
  81. this.isHost = isHost;
  82. }
  83. @Override
  84. public void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. this.wifiEventHandler();
  87. this.progressDialog = new ProgressDialog(this);
  88. this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  89. this.progressDialog.setIndeterminate(true);
  90. this.progressDialog.setTitle(PROGRESS_TITLE_SYNC);
  91. setContentView(R.layout.activity_p2_psync);
  92. assert getActionBar() != null;
  93. getActionBar().setTitle(ACTIONBAR_TITLE);
  94. getActionBar().setDisplayHomeAsUpEnabled(true);
  95. this.extractFromView();
  96. this.registerListeners();
  97. this.wifiEventHandler().startService();
  98. int seconds = 25;
  99. new Timer().scheduleAtFixedRate(new TimerTask() {
  100. @Override
  101. public void run() {
  102. runOnUiThread(new Runnable() {
  103. @Override
  104. public void run() {
  105. searchForDevices();
  106. }
  107. });
  108. }
  109. }, 1000, seconds * 1000); // search for devices every few seconds
  110. }
  111. @Override
  112. public boolean onOptionsItemSelected(MenuItem item) {
  113. if (item.getItemId() == android.R.id.home) {
  114. finish();
  115. }
  116. return super.onOptionsItemSelected(item);
  117. }
  118. @Override
  119. public void onResume() {
  120. super.onResume();
  121. this.wifiEventHandler().startService();
  122. }
  123. @Override
  124. public void onPause() {
  125. if (this.clientTask != null) this.clientTask.interrupt(true);
  126. if (this.hostTask != null) this.hostTask.interrupt(true);
  127. this.wifiEventHandler().disconnect();
  128. this.wifiEventHandler().stopService();
  129. super.onPause();
  130. }
  131. @Override
  132. protected void onStart()
  133. {
  134. super.onStart();
  135. this.wifiEventHandler().startService();
  136. }
  137. @Override
  138. protected void onStop()
  139. {
  140. if (this.clientTask != null) this.clientTask.interrupt(true);
  141. if (this.hostTask != null) this.hostTask.interrupt(true);
  142. this.wifiEventHandler().disconnect();
  143. this.wifiEventHandler().stopService();
  144. super.onStop();
  145. }
  146. @Override
  147. protected void onDestroy(){
  148. if (this.clientTask != null) this.clientTask.interrupt(true);
  149. if (this.hostTask != null) this.hostTask.interrupt(true);
  150. this.wifiEventHandler().stopService();
  151. super.onDestroy();
  152. }
  153. /**
  154. * Returns a instance of the wifi event handler listener object. If no private instance was initiated it creates a new one.
  155. * This object handles all gui changes.
  156. * @return WiFiP2pEventHandler.WiFiP2pEventListener
  157. */
  158. private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
  159. if (_p2pEventListener == null){
  160. _p2pEventListener = new WiFiP2pEventHandler.WiFiP2pEventListener() {
  161. WiFiP2pSyncActivity activity = null;
  162. public WiFiP2pEventHandler.WiFiP2pEventListener init(WiFiP2pSyncActivity act){
  163. this.activity = act;
  164. return this;
  165. }
  166. @Override
  167. public void discoveredDevices(List<WifiP2pDevice> peers) {
  168. Log.d("DEBUG_WiFiP2p", "Activity - Actualise devices list");
  169. this.activity.updateDeviceListView(peers);
  170. }
  171. @Override
  172. public void wifiP2pIsEnabled(boolean enabled) {
  173. String tmp = enabled? "enabled" : "disabled";
  174. Log.d("DEBUG_WiFiP2p", "Activity - Peer to peer is " + tmp + ".");
  175. this.activity.setWifiDirectAvailable(enabled);
  176. }
  177. @Override
  178. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
  179. Log.d("DEBUG_WiFiP2p", "Activity - Did connect");
  180. this.activity.progressDialog.setMessage(PROGRESS_MESSAGE_LOADING);
  181. if (!this.activity.progressDialog.isShowing()){
  182. this.activity.progressDialog.show();
  183. }
  184. this.activity.setHost(isHost);
  185. if (isHost){
  186. Log.d("DEBUG_WiFiP2p", "Activity - Connected as HOST");
  187. this.activity.startHost();
  188. } else {
  189. Log.d("DEBUG_WiFiP2p", "Activity - Connected as Client");
  190. this.activity.startClient(connectionInfo);
  191. }
  192. }
  193. @Override
  194. public void failedToConnect() {
  195. Log.d("DEBUG_WiFiP2p", "Activity - Failed to connect");
  196. Toast.makeText(this.activity, COULD_NOT_CONNECT_MESSAGE , Toast.LENGTH_LONG).show();
  197. if (this.activity.progressDialog != null){
  198. this.activity.progressDialog.dismiss();
  199. }
  200. }
  201. @Override
  202. public void didDisconnect() {
  203. Log.d("DEBUG_WiFiP2p", "Activity - Did disconnect");
  204. if (this.activity.progressDialog != null){
  205. this.activity.progressDialog.dismiss();
  206. }
  207. }
  208. @Override
  209. public void failedToDisconnect() {
  210. Log.d("DEBUG_WiFiP2p", "Activity - Failed to disconnect");
  211. //Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
  212. // Other device did disconnect a while before.
  213. if (this.activity.progressDialog != null &&
  214. ((this.activity.hostTask == null && this.activity.clientTask == null) ||
  215. (this.activity.clientTask != null && this.activity.clientTask.isInterrupted()) ||
  216. (this.activity.hostTask != null && this.activity.hostTask.isInterrupted()) ||
  217. (this.activity.ownDevice == null && this.activity.ownDevice.status != WifiP2pDevice.CONNECTED)
  218. )){
  219. this.activity.progressDialog.dismiss();
  220. }
  221. }
  222. @Override
  223. public void ownDeviceInformationIsUpdated(WifiP2pDevice device) {
  224. Log.d("DEBUG_WiFiP2p", "Activity - Updated device " + device.deviceName + " " + device.deviceAddress + ".");
  225. this.activity.updateOwnDeviceInformation(device);
  226. this.activity.searchForDevices();
  227. }
  228. @Override
  229. public void onConnectionLost() {
  230. Toast.makeText(this.activity, CONNECTION_LOST_MESSAGE , Toast.LENGTH_LONG).show();
  231. if (this.activity.progressDialog != null && this.activity.progressDialog.isShowing()){
  232. this.activity.progressDialog.dismiss();
  233. }
  234. }
  235. }.init(this);
  236. }
  237. return _p2pEventListener;
  238. }
  239. /**
  240. * Returns a instance of the wifi event handler. If no private instance was initiated it creates a new one.
  241. * @return WiFiP2pEventHandler
  242. */
  243. private WiFiP2pEventHandler wifiEventHandler(){
  244. if (this._wifiEventHandler == null){
  245. this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
  246. }
  247. return this._wifiEventHandler;
  248. }
  249. /**
  250. * Returns a sync completion listener. If no listener was initiated it creates a new on.
  251. * @return BackgroundTaskCompletionListener
  252. */
  253. private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
  254. if (_syncCompletionListener == null){
  255. _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
  256. WiFiP2pSyncActivity activity = null;
  257. public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
  258. this.activity = act;
  259. return this;
  260. }
  261. @Override
  262. public void didSucceed() {
  263. Toast.makeText(this.activity, SYNCHRONIZATION_COMPLETE_MESSAGE , Toast.LENGTH_SHORT).show();
  264. this.activity.wifiEventHandler().disconnect();
  265. if (this.activity.hostTask != null){
  266. this.activity.hostTask.setInterrupted(true);
  267. this.activity.hostTask = null;
  268. }
  269. //this.activity.clientTask = null;
  270. }
  271. @Override
  272. public void didFail(String e) {
  273. boolean hasMessage = ((e != null) && (e.length() > 0));
  274. String message = hasMessage ? e : SYNCHRONIZATION_FAILED_MESSAGE;
  275. Toast.makeText(this.activity, message, Toast.LENGTH_LONG).show();
  276. this.activity.wifiEventHandler().disconnect();
  277. if (this.activity.hostTask != null){
  278. this.activity.hostTask.setInterrupted(true);
  279. this.activity.hostTask = null;
  280. }
  281. }
  282. }.init(this);
  283. }
  284. return _syncCompletionListener;
  285. }
  286. /**
  287. * Updates the device list on the ui thread.
  288. * @param peers
  289. */
  290. private void updateDeviceListView(List<WifiP2pDevice> peers)
  291. {
  292. mTxtP2PSearchProgress.setVisibility(View.GONE);
  293. this.discoveredDevices = new ArrayList<WifiP2pDevice>();
  294. this.discoveredDevices.addAll(peers);
  295. WiFiPeerListAdapter listAdapter = (WiFiPeerListAdapter) this.mLstP2PDevices.getAdapter();
  296. listAdapter.addItems(peers);
  297. // Run the update process on the gui thread, otherwise the list wont be updated.
  298. this.runOnUiThread(new Runnable() {
  299. private ListView listView;
  300. @Override
  301. public void run() {
  302. WiFiPeerListAdapter adapter = (WiFiPeerListAdapter) this.listView.getAdapter();
  303. this.listView.setAdapter(null);
  304. adapter.notifyDataSetChanged();
  305. this.listView.setAdapter(adapter);
  306. }
  307. public Runnable init(ListView listview) {
  308. this.listView = listview;
  309. return this;
  310. }
  311. }.init(this.mLstP2PDevices));
  312. Log.d("DEBUG_WiFiP2p", "Activity - Discovered "+peers.size()+" devices.");
  313. if (peers.size() == 0){
  314. this.searchForDevices();
  315. }
  316. }
  317. /**
  318. * Starts the Host task. Informs the user by a little toast.
  319. */
  320. private void startHost()
  321. {
  322. //if (this.hostTask == null || this.hostTask.isInterrupted()){
  323. Log.d("DEBUG_WiFiP2p", "Activity - Starting HOST Task");
  324. //Toast.makeText(this, PERFORMING_TASK_AS_HOST , Toast.LENGTH_SHORT).show();
  325. this.hostTask = new SyncHostTask(this.ownDevice, this.syncCompletionListener(), getApplicationContext());
  326. this.executingTask = this.hostTask;
  327. this.hostTask.execute();
  328. //} else {
  329. // Log.d("DEBUG_WiFiP2p", "Activity - Preventing third device for any syncing.");
  330. //}
  331. }
  332. /**
  333. * Starts the wifi direct client task. Informs the user by a little toast.
  334. * @param info the WifiP2pInfo contains the groupOwnerAddress which is needed for the client task.
  335. */
  336. private void startClient(WifiP2pInfo info)
  337. {
  338. Log.d("DEBUG_WiFiP2p", "Activity - Starting CLIENT Task");
  339. this.clientTask = new SyncClientTask( info.groupOwnerAddress.getHostAddress(),this.ownDevice, this.syncCompletionListener(), getApplicationContext() );
  340. this.executingTask = this.clientTask;
  341. this.clientTask.execute();
  342. }
  343. /**
  344. * Try to connect to the given device and shows a simple progress dialog.
  345. * @param device
  346. */
  347. private void connectTo(WifiP2pDevice device){
  348. if (device != null){
  349. this.progressDialog.setMessage(PROGRESS_MESSAGE_CONNECTING);
  350. this.progressDialog.show();
  351. mOtherDevice = device;
  352. this.wifiEventHandler().connect(device);
  353. }
  354. }
  355. /**
  356. * Returns a localized device status string.
  357. * @param deviceStatus the status to convert.
  358. * @return status string
  359. */
  360. private static String getDeviceStatus(int deviceStatus) {
  361. switch (deviceStatus) {
  362. case WifiP2pDevice.AVAILABLE:
  363. return DEVICE_STATUS_AVAILABLE;
  364. case WifiP2pDevice.INVITED:
  365. return DEVICE_STATUS_INVITED;
  366. case WifiP2pDevice.CONNECTED:
  367. return DEVICE_STATUS_CONNECTED;
  368. case WifiP2pDevice.FAILED:
  369. return DEVICE_STATUS_FAILED;
  370. case WifiP2pDevice.UNAVAILABLE:
  371. return DEVICE_STATUS_UNAVAILABLE;
  372. default:
  373. return DEVICE_STATUS_UNKNOWN;
  374. }
  375. }
  376. /**
  377. * Updates / displays own device information.
  378. * @param device
  379. */
  380. private void updateOwnDeviceInformation(WifiP2pDevice device)
  381. {
  382. mTxtP2PDeviceName.setText(device.deviceName);
  383. mTxtP2PDeviceStatus.setText(getDeviceStatus(device.status));
  384. ownDevice = device;
  385. }
  386. /**
  387. * Method to search for new devices.
  388. */
  389. private void searchForDevices(){
  390. mTxtP2PSearchProgress.setVisibility(View.VISIBLE);
  391. this.wifiEventHandler().discoverDevices();
  392. }
  393. /********************** UI ************************/
  394. /**
  395. * Informs the user about a changed wifi state.
  396. * enabled = true - mTxtP2PNotAvailable is gone
  397. * enabled = false - mTxtP2PNotAvailable stays and a alert box is displayed for a quick navigation to the wifi settings.
  398. * @param enabled
  399. */
  400. public void setWifiDirectAvailable(boolean enabled){
  401. if (enabled){
  402. mTxtP2PNotAvailable.setVisibility(View.GONE);
  403. } else {
  404. mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
  405. ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
  406. ownDevice = null;
  407. this.updateDeviceListView(new ArrayList<WifiP2pDevice>());
  408. this.showWifiDisabledDialog();
  409. //Toast.makeText(this, "WiFi Direct P2P is disabled.", Toast.LENGTH_LONG).show();
  410. }
  411. }
  412. /**
  413. * Displays a AlertDialog that informs the User about the disabled Wifi state and can navigate the user directly to the wifi settings.
  414. */
  415. private void showWifiDisabledDialog(){
  416. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  417. builder.setMessage(WIFI_STATUS_DISABLED_MESSAGE)
  418. .setCancelable(true)
  419. .setPositiveButton(WIFI_STATUS_ENABLE_BUTTON, new DialogInterface.OnClickListener() {
  420. public void onClick(DialogInterface dialog, int id) {
  421. startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
  422. }
  423. })
  424. .setNegativeButton(CANCEL_BUTTON_TITLE, new DialogInterface.OnClickListener() {
  425. public void onClick(DialogInterface dialog, int id) {
  426. finish();
  427. }
  428. });
  429. AlertDialog info = builder.create();
  430. info.show();
  431. }
  432. /**
  433. * Extracts all subview initially from the view hierarchy.
  434. */
  435. private void extractFromView(){
  436. this.mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
  437. this.mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
  438. this.mTxtP2PChangeDeviceName = (TextView) findViewById(R.id.txtP2PChangeDeviceName);
  439. this.mViewAnimator = (ViewAnimator) findViewById(R.id.viewAnimator);
  440. this.mDevicesContainer = (RelativeLayout) findViewById(R.id.devicesContainer);
  441. this.mWelcomeContainer = (RelativeLayout) findViewById(R.id.welcomeContainer);
  442. this.mTxtP2PSearchProgress = (TextView) findViewById(R.id.txtP2PSearchProgress);
  443. this.mLstP2PDevices = (ListView) findViewById(R.id.lstP2PDevices);
  444. this.mTxtP2PNotAvailable = (TextView) findViewById(R.id.txtP2PNotAvailable);
  445. }
  446. /**
  447. * Registers all the gui listeners.
  448. */
  449. public void registerListeners(){
  450. if (this.mLstP2PDevices.getOnItemClickListener() != this)
  451. this.mLstP2PDevices.setOnItemClickListener(this);
  452. if (this.mLstP2PDevices.getAdapter() == null){
  453. this.discoveredDevices = new ArrayList();
  454. WiFiPeerListAdapter listAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, this.discoveredDevices);
  455. this.mLstP2PDevices.setAdapter(listAdapter);
  456. }
  457. }
  458. @Override
  459. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  460. final WifiP2pDevice device = (WifiP2pDevice) this.mLstP2PDevices.getAdapter().getItem(position);
  461. this.connectTo(device);
  462. }
  463. /**
  464. * Array adapter for ListFragment that maintains WifiP2pDevice list.
  465. */
  466. private class WiFiPeerListAdapter extends ArrayAdapter<WifiP2pDevice> {
  467. private List<WifiP2pDevice> items;
  468. /**
  469. * @param context
  470. * @param textViewResourceId
  471. * @param objects
  472. */
  473. public WiFiPeerListAdapter(Context context, int textViewResourceId,
  474. List<WifiP2pDevice> objects) {
  475. super(context, textViewResourceId, objects);
  476. items = objects;
  477. }
  478. @Override
  479. public int getCount() {
  480. return items.size();
  481. }
  482. public void addItems(List<WifiP2pDevice> devicesToAdd){
  483. items.clear();
  484. items.addAll(devicesToAdd);
  485. }
  486. @Override
  487. public View getView(int position, View convertView, ViewGroup parent) {
  488. View v = convertView;
  489. if (v == null) {
  490. LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  491. v = vi.inflate(R.layout.row_devices, null);
  492. }
  493. WifiP2pDevice device = items.get(position);
  494. if (device != null) {
  495. TextView top = (TextView) v.findViewById(R.id.device_name);
  496. TextView bottom = (TextView) v.findViewById(R.id.device_details);
  497. if (top != null) {
  498. top.setText(device.deviceName);
  499. }
  500. if (bottom != null) {
  501. bottom.setText(getDeviceStatus(device.status));
  502. }
  503. }
  504. return v;
  505. }
  506. }
  507. }