WiFiP2pSyncActivity.java 21 KB

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