WiFiP2pSyncActivity.java 23 KB

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