Advertisement
Guest User

Untitled

a guest
May 5th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.43 KB | None | 0 0
  1. public class CreatePlaceActivity extends Fragment implements
  2. OnClickListener,
  3. GoogleApiClient.ConnectionCallbacks,
  4. GoogleApiClient.OnConnectionFailedListener, LocationListener {
  5. private Button cancelBtn, submitBtn;
  6.  
  7. public boolean geoLocationCheck = false;
  8. private String TAG = "CreatePlaceActivity";
  9.  
  10. public CreatePlaceActivity() {
  11. }
  12.  
  13. @Override
  14. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  15. Bundle savedInstanceState) {
  16.  
  17. View rootView = inflater.inflate(R.layout.create_place_screen,
  18. container, false);
  19. submitBtn = (Button) rootView.findViewById(R.id.submit_btn_id);
  20. submitBtn.setTypeface(typeface);
  21. submitBtn.setOnClickListener(this);
  22. cancelBtn = (Button) rootView.findViewById(R.id.cancel_btn_id);
  23. cancelBtn.setOnClickListener(this);
  24.  
  25. setlocationClient();
  26. return rootView;
  27. }
  28.  
  29. private void setlocationClient() {
  30. int resp = GooglePlayServicesUtil
  31. .isGooglePlayServicesAvailable(getActivity());
  32. if (resp == ConnectionResult.SUCCESS) {
  33.  
  34. locationclient = new GoogleApiClient.Builder(getActivity())
  35. .addApi(LocationServices.API).addConnectionCallbacks(this)
  36. .addOnConnectionFailedListener(this)
  37. .build();
  38. locationclient.connect();
  39. } else {
  40. GooglePalyErrorDialogIsShowing = true;
  41.  
  42. Toast.makeText(getActivity(),// CreatePlaceActivity.this,
  43. "Google Play Service Error " + resp, Toast.LENGTH_LONG)
  44. .show();
  45.  
  46. if (alert == null) {
  47. onLocationChangedbuilder = new AlertDialog.Builder(
  48. getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  49. } else if (alert.isShowing()) {
  50. alert.dismiss();
  51.  
  52. onLocationChangedbuilder = new AlertDialog.Builder(
  53. getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  54.  
  55. }
  56. onLocationChangedbuilder
  57. .setTitle("Alert!")
  58. .setMessage("Google Play Service Error " + resp)
  59. .setPositiveButton("OK",
  60. new DialogInterface.OnClickListener() {
  61.  
  62. @Override
  63. public void onClick(DialogInterface dialog,
  64. int which) {
  65.  
  66. locationLockDialogIsShowing = false;
  67. GooglePalyErrorDialogIsShowing = false;
  68.  
  69. ((MenuActivity) getActivity())
  70. .removeFragment(CreatePlaceActivity.this);
  71.  
  72. }
  73. });
  74.  
  75. alert = onLocationChangedbuilder.create();
  76. alert.setCancelable(false);
  77. try {
  78. alert.show();
  79. } catch (Exception e) {
  80.  
  81. }
  82.  
  83. }
  84.  
  85. }
  86.  
  87. @Override
  88. public void onClick(View v) {
  89. switch (v.getId()) {
  90. case R.id.submit_btn_id:
  91. Log.i(TAG, "lat=" + currentGeoLocation.getLatitude() + "lon="
  92. + currentGeoLocation.getLongitude() + "acc="
  93. + currentGeoLocation.getAccuracy() + "alt="
  94. + currentGeoLocation.getAltitude());
  95. validatePlace();
  96.  
  97. break;
  98. case R.id.cancel_btn_id:
  99. // finish();
  100. ((MenuActivity) getActivity())
  101. .removeFragment(CreatePlaceActivity.this);
  102.  
  103. break;
  104.  
  105. default:
  106. break;
  107. }
  108. }
  109.  
  110.  
  111. @Override
  112. public void onLocationChanged(Location location) {
  113.  
  114. float accuracy = 50;
  115.  
  116. // DateTime dt1 = new DateTime();
  117. // DateTimeFormatter fmt1 = ISODateTimeFormat.dateTime();
  118.  
  119. Time time = new Time();
  120. time.setToNow();
  121. String strdt = String.valueOf(time.toMillis(false));
  122.  
  123. if (location != null) {
  124.  
  125. accuracy = location.getAccuracy();
  126.  
  127. locationLockDialogIsShowing = true;
  128.  
  129. if (globalAccuracy - location.getAccuracy() >= 20
  130. || globalAccuracy - location.getAccuracy() >= -20) {
  131.  
  132. globalAccuracy = location.getAccuracy();
  133. }
  134. }
  135.  
  136.  
  137.  
  138. if (alert == null) {
  139. onLocationChangedbuilder = new AlertDialog.Builder(getActivity(),
  140. AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  141. } else if (alert.isShowing()) {
  142. alert.dismiss();
  143.  
  144. onLocationChangedbuilder = new AlertDialog.Builder(getActivity(),
  145. AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  146.  
  147. }
  148. final float accr = accuracy;
  149. onLocationChangedbuilder
  150. .setTitle("Alert!")
  151. .setMessage(
  152. "Getting the best possible location..Please wait.nCurrent Accuracy is "
  153. + accuracy)
  154. .setPositiveButton("Cancel",
  155. new DialogInterface.OnClickListener() {
  156.  
  157. @Override
  158. public void onClick(DialogInterface dialog,
  159. int which) {
  160. // TODO Auto-generated method stub
  161.  
  162. locationLockDialogIsShowing = false;
  163.  
  164. if (locationclient != null) {
  165.  
  166. locationclient.disconnect();
  167. }
  168.  
  169. Time time = new Time();
  170. time.setToNow();
  171. String strdt = String.valueOf(time
  172. .toMillis(false));
  173.  
  174.  
  175.  
  176. if (alert == null) {
  177.  
  178. } else if (alert.isShowing()) {
  179. alert.dismiss();
  180. }
  181. if (locationLockAlert == null) {
  182.  
  183. } else if (locationLockAlert.isShowing()) {
  184. locationLockAlert.dismiss();
  185. }
  186. // setResult(RESULT_OK);
  187. // finish();
  188. ((MenuActivity) getActivity())
  189. .removeFragment(CreatePlaceActivity.this);
  190. }
  191. });
  192.  
  193. alert = onLocationChangedbuilder.create();
  194. alert.setCancelable(false);
  195. try {
  196. alert.show();
  197. } catch (Exception e) {
  198.  
  199. }
  200. if (location.getAccuracy() <= 16) {
  201.  
  202. DateTime dt = new DateTime();
  203. DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
  204. String str1 = fmt.print(dt);
  205.  
  206. locationLockDialogIsShowing = false;
  207.  
  208. Time timeNow = new Time();
  209. timeNow.setToNow();
  210.  
  211. String str = String.valueOf(timeNow.toMillis(false));
  212.  
  213. // locationLockTimestamp = str1;
  214. // TODO
  215. currentGeoLocation = location;
  216.  
  217. if (locationclient != null) {
  218. if (locationclient.isConnected()) {
  219.  
  220. LocationServices.FusedLocationApi.removeLocationUpdates(
  221. locationclient, this);
  222. locationclient.disconnect();
  223. }
  224. }
  225.  
  226. if (alert.isShowing()) {
  227. alert.dismiss();
  228. }
  229. if (locationLockAlert != null) {
  230.  
  231. if (locationLockAlert.isShowing()) {
  232. locationLockAlert.dismiss();
  233. }
  234. }
  235. showDialog("Alert!",
  236. "GPS Location locked.. You can now continue to create place");
  237.  
  238. }
  239.  
  240.  
  241. }
  242.  
  243. @Override
  244. public void onConnectionFailed(ConnectionResult result) {
  245. // TODO Auto-generated method stub
  246.  
  247. }
  248.  
  249. @Override
  250. public void onConnected(Bundle connectionHint) {
  251. // TODO Auto-generated method stub
  252.  
  253. final float accuracy = 250;
  254.  
  255. if (locationclient != null && locationclient.isConnected()) {
  256.  
  257. if (geoLocationCheck == false) {
  258.  
  259. locationrequest = LocationRequest.create();
  260. locationrequest
  261. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  262.  
  263. locationrequest.setInterval(100);
  264. // locationclient.requestLocationUpdates(locationrequest, this);
  265. LocationServices.FusedLocationApi.requestLocationUpdates(
  266. locationclient, locationrequest, this);
  267.  
  268. locationLockDialogIsShowing = true;
  269.  
  270. if (alert == null) {
  271. onLocationChangedbuilder = new AlertDialog.Builder(
  272. getActivity(),
  273. AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  274. } else if (alert.isShowing()) {
  275. alert.dismiss();
  276.  
  277. onLocationChangedbuilder = new AlertDialog.Builder(
  278. getActivity(),
  279. AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  280.  
  281. }
  282. onLocationChangedbuilder
  283. .setTitle("Alert!")
  284. .setMessage(
  285. "Getting the best possible location..Please wait.nCurrent Accuracy is "
  286. + accuracy)
  287. .setPositiveButton("Cancel",
  288. new DialogInterface.OnClickListener() {
  289.  
  290. @Override
  291. public void onClick(DialogInterface dialog,
  292. int which) {
  293. // TODO Auto-generated method stub
  294.  
  295. locationLockDialogIsShowing = false;
  296.  
  297. if (locationclient != null) {
  298. locationclient.disconnect();
  299. }
  300.  
  301. Time time = new Time();
  302. time.setToNow();
  303. String strdt = String.valueOf(time
  304. .toMillis(false));
  305.  
  306.  
  307. if (alert == null) {
  308. } else if (alert.isShowing()) {
  309. alert.dismiss();
  310. }
  311. if (locationLockAlert == null) {
  312.  
  313. } else if (locationLockAlert
  314. .isShowing()) {
  315. locationLockAlert.dismiss();
  316. }
  317. // setResult(RESULT_OK);
  318. // finish();
  319. ((MenuActivity) getActivity())
  320. .removeFragment(CreatePlaceActivity.this);
  321. }
  322. });
  323.  
  324. alert = onLocationChangedbuilder.create();
  325. alert.setCancelable(false);
  326. try {
  327. alert.show();
  328. } catch (Exception e) {
  329.  
  330. }
  331.  
  332. }
  333. }
  334.  
  335. }
  336.  
  337.  
  338. @Override
  339. public void onConnectionSuspended(int arg0) {
  340. Log.i("fused", "loc client onConnectionSuspended");
  341. }
  342.  
  343. public Dialog showDialog(String title, String msg) {
  344.  
  345. locationLockAlert = new AlertDialog.Builder(getActivity(),
  346. AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create();
  347. locationLockAlert.setTitle(title);
  348. locationLockAlert.setMessage(msg);
  349. locationLockAlert.setButton("OK",
  350. new DialogInterface.OnClickListener() {
  351. public void onClick(DialogInterface dialog, int which) {
  352. locationLockAlert.dismiss();
  353.  
  354. }
  355. });
  356. try {
  357. locationLockAlert.show();
  358. } catch (Exception e) {
  359.  
  360. }
  361. return locationLockAlert;
  362.  
  363. }
  364.  
  365. @Override
  366. public void onNothingSelected(AdapterView<?> parent) {
  367. // TODO Auto-generated method stub
  368.  
  369. }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement