WiFiP2pSyncActivity.java 23 KB

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