Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.ArrayList;
  3. import java.util.Calendar;
  4. import java.util.List;
  5. import com.google.android.gms.common.ConnectionResult;
  6. import com.google.android.gms.common.GooglePlayServicesUtil;
  7. import com.google.android.gms.common.api.GoogleApiClient;
  8. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  9. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  10. import com.google.android.gms.location.LocationRequest;
  11. import com.google.android.gms.location.LocationServices;
  12. import android.app.Activity;
  13. import android.location.Location;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. import android.content.Context;
  23. import android.net.ConnectivityManager;
  24. import android.net.NetworkInfo;
  25. import android.os.AsyncTask;
  26. import android.text.method.ScrollingMovementMethod;
  27. import android.widget.ProgressBar;
  28.  
  29. public class MainActivity extends Activity implements ConnectionCallbacks,
  30. OnConnectionFailedListener {
  31. // NEW CONTENT
  32. private static final String TAG = MainActivity.class.getSimpleName();
  33. private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
  34. private GoogleApiClient mGoogleApiClient;
  35. private Location mLastLocation;
  36.  
  37. //ORIGINAL CONTENT
  38. TextView output;
  39. ProgressBar pb;
  40. List<MyTask> tasks;
  41. private Button btnNewShowLocation;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_main);
  47.  
  48. output = (TextView) findViewById(R.id.textView);
  49. output.setMovementMethod(new ScrollingMovementMethod());
  50. pb = (ProgressBar) findViewById(R.id.progressBar1);
  51. pb.setVisibility(View.INVISIBLE);
  52. btnNewShowLocation = (Button) findViewById(R.id.btnNewShowLocation);
  53. tasks = new ArrayList<>();
  54.  
  55. // NEW CONTENT
  56. if (checkPlayServices()) {
  57. // Building the GoogleApi client
  58. buildGoogleApiClient();
  59. }
  60.  
  61.  
  62. // ORIGINAL CONTENT
  63. btnNewShowLocation.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View v) {
  66. if (isOnline()) {
  67. requestData("http://10.0.2.2/index2.php");
  68.  
  69. } else {
  70. Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
  71. }
  72. }
  73. });
  74. }
  75.  
  76. private void displayLocation() {
  77.  
  78.  
  79. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  80. Calendar c = Calendar.getInstance();
  81. System.out.println("Current time => " + c.getTime());
  82. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  83. String formattedDate = df.format(c.getTime());
  84. if (mLastLocation != null) {
  85. double latitude = mLastLocation.getLatitude();
  86. double longitude = mLastLocation.getLongitude();
  87. Toast.makeText(getApplicationContext(), "Latitude = " + latitude + "nLongitude = " + longitude + "nDate = " + formattedDate, Toast.LENGTH_LONG).show();
  88. } else {
  89. Toast.makeText(getApplicationContext(), "Location not found! nIs Location turned ON in Settings?", Toast.LENGTH_LONG).show();
  90. }
  91. }
  92.  
  93. @Override
  94. public boolean onCreateOptionsMenu(Menu menu) {
  95. getMenuInflater().inflate(R.menu.main, menu);
  96. return true;
  97. }
  98.  
  99. @Override
  100. public boolean onOptionsItemSelected(MenuItem item) {
  101. if (item.getItemId() == R.id.action_get_data) {
  102. }
  103. return false;
  104. }
  105.  
  106. private void requestData(String uri) {
  107.  
  108. RequestPackage p = new RequestPackage();
  109. p.setMethod("POST");
  110. p.setUri(uri);
  111. p.setParams("longitude", "Fake Value 1");
  112. p.setParams("latitude", "Fake Value 2");
  113. p.setParams("date", "Fake Value 3");
  114.  
  115. MyTask task = new MyTask();
  116. task.execute(p);
  117. }
  118.  
  119. protected void updateDisplay(String result) {
  120. output.append(result + "n");
  121. }
  122.  
  123. protected boolean isOnline() {
  124. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  125. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  126. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. // NEW CONTENT
  133. protected synchronized void buildGoogleApiClient() {
  134. mGoogleApiClient = new GoogleApiClient.Builder(this)
  135. .addConnectionCallbacks(this)
  136. .addOnConnectionFailedListener(this)
  137. .addApi(LocationServices.API).build();
  138. }
  139. /**
  140. * Method to verify google play services on the device
  141. */
  142. private boolean checkPlayServices() {
  143. int resultCode = GooglePlayServicesUtil
  144. .isGooglePlayServicesAvailable(this);
  145. if (resultCode != ConnectionResult.SUCCESS) {
  146. if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  147. GooglePlayServicesUtil.getErrorDialog(resultCode, this,
  148. PLAY_SERVICES_RESOLUTION_REQUEST).show();
  149. } else {
  150. Toast.makeText(getApplicationContext(),
  151. "This device is not supported.", Toast.LENGTH_LONG)
  152. .show();
  153. finish();
  154. }
  155. return false;
  156. }
  157. return true;
  158. }
  159.  
  160. @Override
  161. protected void onStart() {
  162. super.onStart();
  163. if (mGoogleApiClient != null) {
  164. mGoogleApiClient.connect();
  165. }
  166. }
  167.  
  168. @Override
  169. protected void onResume() {
  170. super.onResume();
  171. checkPlayServices();
  172. }
  173. /**
  174. * Google api callback methods
  175. */
  176. @Override
  177. public void onConnectionFailed(ConnectionResult result) {
  178. // what's going on here...
  179. Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
  180. + result.getErrorCode());
  181. }
  182.  
  183. @Override
  184. public void onConnected(Bundle arg0) {
  185. }
  186.  
  187. @Override
  188. public void onConnectionSuspended(int arg0) {
  189. mGoogleApiClient.connect();
  190. }
  191.  
  192. // ORIGINAL CONTENT
  193. private class MyTask extends AsyncTask<RequestPackage, String, String> {
  194.  
  195. @Override
  196. protected void onPreExecute() {
  197. if (tasks.size() == 0) {
  198. pb.setVisibility(View.VISIBLE);
  199. }
  200. tasks.add(this);
  201. }
  202.  
  203. @Override
  204. protected String doInBackground(RequestPackage... params) {
  205. String content = HttpManager.getData(params[0]);
  206. return content;
  207. }
  208.  
  209. @Override
  210. protected void onPostExecute(String result) {
  211. updateDisplay(result);
  212.  
  213. tasks.remove(this);
  214. if (tasks.size() == 0) {
  215. pb.setVisibility(View.INVISIBLE);
  216. }
  217. }
  218.  
  219. @Override
  220. protected void onProgressUpdate(String... values) {
  221. //updateDisplay(values[0]);
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement