WiFiP2pSyncActivity.java 22 KB

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