WiFiP2pSyncActivity.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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.AsyncTask;
  11. import android.os.Bundle;
  12. import android.provider.Settings;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  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_TITLE_LOADING =MainActivity.getContext().getString(R.string.PROGRESS_TITLE_LOADING);// "Loading...";
  46. public static String PROGRESS_TITLE_CONNECTING = MainActivity.getContext().getString(R.string.PROGRESS_TITLE_CONNECTING);//"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. setContentView(R.layout.activity_p2_psync);
  87. assert getActionBar() != null;
  88. getActionBar().setTitle(ACTIONBAR_TITLE);
  89. this.extractFromView();
  90. this.registerListeners();
  91. this.wifiEventHandler().startService();
  92. int seconds = 25;
  93. new Timer().scheduleAtFixedRate(new TimerTask() {
  94. @Override
  95. public void run() {
  96. runOnUiThread(new Runnable() {
  97. @Override
  98. public void run() {
  99. searchForDevices();
  100. }
  101. });
  102. }
  103. }, 1000, seconds * 1000); // search for devices every few seconds
  104. }
  105. @Override
  106. public void onResume() {
  107. super.onResume();
  108. this.wifiEventHandler().startService();
  109. }
  110. @Override
  111. public void onPause() {
  112. if (this.clientTask != null) this.clientTask.interrupt(true);
  113. if (this.hostTask != null) this.hostTask.interrupt(true);
  114. this.wifiEventHandler().disconnect();
  115. this.wifiEventHandler().stopService();
  116. super.onPause();
  117. }
  118. @Override
  119. protected void onStart()
  120. {
  121. super.onStart();
  122. this.wifiEventHandler().startService();
  123. }
  124. @Override
  125. protected void onStop()
  126. {
  127. if (this.clientTask != null) this.clientTask.interrupt(true);
  128. if (this.hostTask != null) this.hostTask.interrupt(true);
  129. this.wifiEventHandler().disconnect();
  130. this.wifiEventHandler().stopService();
  131. super.onStop();
  132. }
  133. @Override
  134. protected void onDestroy(){
  135. if (this.clientTask != null) this.clientTask.interrupt(true);
  136. if (this.hostTask != null) this.hostTask.interrupt(true);
  137. this.wifiEventHandler().stopService();
  138. super.onDestroy();
  139. }
  140. /**
  141. * Returns a instance of the wifi event handler listener object. If no private instance was initiated it creates a new one.
  142. * This object handles all gui changes.
  143. * @return WiFiP2pEventHandler.WiFiP2pEventListener
  144. */
  145. private WiFiP2pEventHandler.WiFiP2pEventListener eventListener(){
  146. if (_p2pEventListener == null){
  147. _p2pEventListener = new WiFiP2pEventHandler.WiFiP2pEventListener() {
  148. WiFiP2pSyncActivity activity = null;
  149. public WiFiP2pEventHandler.WiFiP2pEventListener init(WiFiP2pSyncActivity act){
  150. this.activity = act;
  151. return this;
  152. }
  153. @Override
  154. public void discoveredDevices(List<WifiP2pDevice> peers) {
  155. Log.d("DEBUG_WiFiP2pSyncActivity", "Actualise devices list");
  156. this.activity.updateDeviceListView(peers);
  157. }
  158. @Override
  159. public void wifiP2pIsEnabled(boolean enabled) {
  160. String tmp = enabled? "enabled" : "disabled";
  161. Log.d("DEBUG_WiFiP2pSyncActivity", "Peer to peer is " + tmp + ".");
  162. this.activity.setWifiDirectAvailable(enabled);
  163. }
  164. @Override
  165. public void didConnect(boolean isHost, WifiP2pInfo connectionInfo) {
  166. Log.d("DEBUG_WiFiP2pSyncActivity", "Did connect");
  167. String progressTitle = PROGRESS_TITLE_LOADING;
  168. if (this.activity.progressDialog != null){
  169. this.activity.progressDialog.dismiss();
  170. }
  171. this.activity.progressDialog = ProgressDialog.show(activity, "", progressTitle);
  172. this.activity.setHost(isHost);
  173. if (isHost){
  174. Log.d("DEBUG_WiFiP2pSyncActivity", "Connected as HOST");
  175. this.activity.startHost();
  176. } else {
  177. Log.d("DEBUG_WiFiP2pSyncActivity", "Connected as Client");
  178. this.activity.startClient(connectionInfo);
  179. }
  180. }
  181. @Override
  182. public void failedToConnect() {
  183. Log.d("DEBUG_WiFiP2pSyncActivity", "Failed to connect");
  184. Toast.makeText(this.activity, COULD_NOT_CONNECT_MESSAGE , Toast.LENGTH_LONG).show();
  185. if (this.activity.progressDialog != null){
  186. this.activity.progressDialog.dismiss();
  187. }
  188. }
  189. @Override
  190. public void didDisconnect() {
  191. Log.d("DEBUG_WiFiP2pSyncActivity", "Did disconnect");
  192. if (this.activity.progressDialog != null){
  193. this.activity.progressDialog.dismiss();
  194. }
  195. }
  196. @Override
  197. public void failedToDisconnect() {
  198. Log.d("DEBUG_WiFiP2pSyncActivity", "Failed to disconnect");
  199. //Toast.makeText(this.activity, "Could not disconnect with device. Retry.", Toast.LENGTH_LONG).show();
  200. // Other device did disconnect a while before.
  201. if (this.activity.progressDialog != null){
  202. this.activity.progressDialog.dismiss();
  203. }
  204. }
  205. @Override
  206. public void ownDeviceInformationIsUpdated(WifiP2pDevice device) {
  207. Log.d("DEBUG_WiFiP2pSyncActivity", "Updated device " + device.deviceName + " " + device.deviceAddress + ".");
  208. this.activity.updateOwnDeviceInformation(device);
  209. this.activity.searchForDevices();
  210. }
  211. @Override
  212. public void onConnectionLost() {
  213. Toast.makeText(this.activity, CONNECTION_LOST_MESSAGE , Toast.LENGTH_LONG).show();
  214. if (this.activity.progressDialog != null && this.activity.progressDialog.isShowing()){
  215. this.activity.progressDialog.dismiss();
  216. }
  217. }
  218. }.init(this);
  219. }
  220. return _p2pEventListener;
  221. }
  222. /**
  223. * Returns a instance of the wifi event handler. If no private instance was initiated it creates a new one.
  224. * @return WiFiP2pEventHandler
  225. */
  226. private WiFiP2pEventHandler wifiEventHandler(){
  227. if (this._wifiEventHandler == null){
  228. this._wifiEventHandler = new WiFiP2pEventHandler(this, this.eventListener());
  229. }
  230. return this._wifiEventHandler;
  231. }
  232. /**
  233. * Returns a sync completion listener. If no listener was initiated it creates a new on.
  234. * @return BackgroundTaskCompletionListener
  235. */
  236. private BackgroundTask.BackgroundTaskCompletionListener syncCompletionListener(){
  237. if (_syncCompletionListener == null){
  238. _syncCompletionListener = new BackgroundTask.BackgroundTaskCompletionListener() {
  239. WiFiP2pSyncActivity activity = null;
  240. public BackgroundTask.BackgroundTaskCompletionListener init(WiFiP2pSyncActivity act){
  241. this.activity = act;
  242. return this;
  243. }
  244. @Override
  245. public void didSucceed() {
  246. Toast.makeText(this.activity, SYNCHRONIZATION_COMPLETE_MESSAGE , Toast.LENGTH_SHORT).show();
  247. this.activity.wifiEventHandler().disconnect();
  248. if (this.activity.hostTask != null){
  249. this.activity.hostTask.setInterrupted(true);
  250. this.activity.hostTask = null;
  251. }
  252. //this.activity.clientTask = null;
  253. }
  254. @Override
  255. public void didFail() {
  256. Toast.makeText(this.activity, SYNCHRONIZATION_FAILED_MESSAGE, Toast.LENGTH_LONG).show();
  257. this.activity.wifiEventHandler().disconnect();
  258. if (this.activity.hostTask != null){
  259. this.activity.hostTask.setInterrupted(true);
  260. this.activity.hostTask = null;
  261. }
  262. //this.activity.clientTask = null;
  263. }
  264. }.init(this);
  265. }
  266. return _syncCompletionListener;
  267. }
  268. /**
  269. * Updates the device list on the ui thread.
  270. * @param peers
  271. */
  272. private void updateDeviceListView(List<WifiP2pDevice> peers)
  273. {
  274. mTxtP2PSearchProgress.setVisibility(View.GONE);
  275. this.discoveredDevices = new ArrayList<WifiP2pDevice>();
  276. this.discoveredDevices.addAll(peers);
  277. WiFiPeerListAdapter listAdapter = (WiFiPeerListAdapter) this.mLstP2PDevices.getAdapter();
  278. listAdapter.addItems(peers);
  279. // Run the update process on the gui thread, otherwise the list wont be updated.
  280. this.runOnUiThread(new Runnable() {
  281. private ListView listView;
  282. @Override
  283. public void run() {
  284. WiFiPeerListAdapter adapter = (WiFiPeerListAdapter) this.listView.getAdapter();
  285. this.listView.setAdapter(null);
  286. adapter.notifyDataSetChanged();
  287. this.listView.setAdapter(adapter);
  288. }
  289. public Runnable init(ListView listview) {
  290. this.listView = listview;
  291. return this;
  292. }
  293. }.init(this.mLstP2PDevices));
  294. Log.d("DEBUG_WiFiP2pSyncActivity", " Discovered "+peers.size()+" devices.");
  295. if (peers.size() == 0){
  296. this.searchForDevices();
  297. }
  298. }
  299. /**
  300. * Starts the Host task. Informs the user by a little toast.
  301. */
  302. private void startHost()
  303. {
  304. //if (this.hostTask == null || this.hostTask.isInterrupted()){
  305. Log.d("DEBUG_WiFiP2pSyncActivity", "Starting HOST Task");
  306. //Toast.makeText(this, PERFORMING_TASK_AS_HOST , Toast.LENGTH_SHORT).show();
  307. this.hostTask = new SyncHostTask(this.ownDevice, this.syncCompletionListener(), getApplicationContext());
  308. this.executingTask = this.hostTask;
  309. this.hostTask.execute();
  310. //} else {
  311. // Log.d("DEBUG_WiFiP2pSyncActivity", "Preventing third device for any syncing.");
  312. //}
  313. }
  314. /**
  315. * Starts the wifi direct client task. Informs the user by a little toast.
  316. * @param info the WifiP2pInfo contains the groupOwnerAddress which is needed for the client task.
  317. */
  318. private void startClient(WifiP2pInfo info)
  319. {
  320. Log.d("DEBUG_WiFiP2pSyncActivity", "Starting CLIENT Task");
  321. this.clientTask = new SyncClientTask( info.groupOwnerAddress.getHostAddress(),this.ownDevice, this.syncCompletionListener(), getApplicationContext() );
  322. this.executingTask = this.clientTask;
  323. this.clientTask.execute();
  324. }
  325. /**
  326. * Try to connect to the given device and shows a simple progress dialog.
  327. * @param device
  328. */
  329. private void connectTo(WifiP2pDevice device){
  330. String connectionTitle = PROGRESS_TITLE_CONNECTING;
  331. if (device != null){
  332. if (progressDialog == null){
  333. this.progressDialog = ProgressDialog.show(this, "", PROGRESS_TITLE_CONNECTING);
  334. } else {
  335. this.progressDialog.setTitle(PROGRESS_TITLE_CONNECTING);
  336. }
  337. new Thread()
  338. {
  339. public void run()
  340. {
  341. try
  342. {
  343. sleep(20);
  344. }
  345. catch (Exception e)
  346. {
  347. }
  348. progressDialog.setTitle(PROGRESS_TITLE_CONNECTING);
  349. }
  350. }.start();
  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. }