Java tutorial
/** * Copyright 2014 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.gms.location.sample.locationupdates; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.os.Vibrator; import android.support.v4.view.GestureDetectorCompat; import android.support.v4.view.MotionEventCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.ContextMenu; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import org.json.JSONException; import org.json.JSONObject; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Getting Location Updates. * * Demonstrates how to use the Fused Location Provider API to get updates about a device's * location. The Fused Location Provider is part of the Google Play services location APIs. * * For a simpler example that shows the use of Google Play services to fetch the last known location * of a device, see * https://github.com/googlesamples/android-play-location/tree/master/BasicLocation. * * This sample uses Google Play services, but it does not require authentication. For a sample that * uses Google Play services for authentication, see * https://github.com/googlesamples/android-google-accounts/tree/master/QuickStart. */ public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener, GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener { protected static final String TAG = "location-updates-sample"; private static final String DEBUG_TAG = "Gestures"; /** * The desired interval for location updates. Inexact. Updates may be more or less frequent. */ public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000; /** * The fastest rate for active location updates. Exact. Updates will never be more frequent * than this value. */ public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2; // Keys for storing activity state in the Bundle. protected final static String REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key"; protected final static String LOCATION_KEY = "location-key"; protected final static String LAST_UPDATED_TIME_STRING_KEY = "last-updated-time-string-key"; /** * Provides the entry point to Google Play services. */ protected GoogleApiClient mGoogleApiClient; /** * Stores parameters for requests to the FusedLocationProviderApi. */ protected LocationRequest mLocationRequest; /** * Represents a geographical location. */ protected Location mCurrentLocation; // UI Widgets. protected ImageView mStartUpdatesButton; protected ImageView mStopUpdatesButton; protected TextView mLastUpdateTimeTextView; protected TextView mLatitudeTextView; protected TextView mLongitudeTextView; // Labels. protected String mLatitudeLabel; protected String mLongitudeLabel; protected String mLastUpdateTimeLabel; protected LinearLayout myMainLayout; /** * Tracks the status of the location updates request. Value changes when the user presses the * Start Updates and Stop Updates buttons. */ protected Boolean mRequestingLocationUpdates; /** * Time when the location was updated represented as a String. */ protected String mLastUpdateTime; RequestQueue httpQueue; boolean setTag = false; Vibrator myVibrator; private GestureDetectorCompat mDetector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); httpQueue = Volley.newRequestQueue(this); // Locate the UI widgets. mStartUpdatesButton = (ImageView) findViewById(R.id.start_updates_button); mStopUpdatesButton = (ImageView) findViewById(R.id.stop_updates_button); mLatitudeTextView = (TextView) findViewById(R.id.latitude_text); mLongitudeTextView = (TextView) findViewById(R.id.longitude_text); mLastUpdateTimeTextView = (TextView) findViewById(R.id.last_update_time_text); myMainLayout = (LinearLayout) findViewById(R.id.myMainLayout); // Set labels. mLatitudeLabel = getResources().getString(R.string.latitude_label); mLongitudeLabel = getResources().getString(R.string.longitude_label); mLastUpdateTimeLabel = getResources().getString(R.string.last_update_time_label); mDetector = new GestureDetectorCompat(this, this); mDetector.setOnDoubleTapListener(this); mRequestingLocationUpdates = false; mLastUpdateTime = ""; // Update values using data stored in the Bundle. updateValuesFromBundle(savedInstanceState); // Kick off the process of building a GoogleApiClient and requesting the LocationServices // API. buildGoogleApiClient(); myVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); } /** * Updates fields based on data stored in the bundle. * * @param savedInstanceState The activity state saved in the Bundle. */ private void updateValuesFromBundle(Bundle savedInstanceState) { Log.i(TAG, "Updating values from bundle"); if (savedInstanceState != null) { // Update the value of mRequestingLocationUpdates from the Bundle, and make sure that // the Start Updates and Stop Updates buttons are correctly enabled or disabled. if (savedInstanceState.keySet().contains(REQUESTING_LOCATION_UPDATES_KEY)) { mRequestingLocationUpdates = savedInstanceState.getBoolean(REQUESTING_LOCATION_UPDATES_KEY); setButtonsEnabledState(); } // Update the value of mCurrentLocation from the Bundle and update the UI to show the // correct latitude and longitude. if (savedInstanceState.keySet().contains(LOCATION_KEY)) { // Since LOCATION_KEY was found in the Bundle, we can be sure that mCurrentLocation // is not null. mCurrentLocation = savedInstanceState.getParcelable(LOCATION_KEY); } // Update the value of mLastUpdateTime from the Bundle and update the UI. if (savedInstanceState.keySet().contains(LAST_UPDATED_TIME_STRING_KEY)) { mLastUpdateTime = savedInstanceState.getString(LAST_UPDATED_TIME_STRING_KEY); } //updateUI(); } } /** * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the * LocationServices API. */ protected synchronized void buildGoogleApiClient() { Log.i(TAG, "Building GoogleApiClient"); mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); createLocationRequest(); } /** * Sets up the location request. Android has two location request settings: * {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control * the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in * the AndroidManifest.xml. * <p/> * When the ACCESS_FINE_LOCATION setting is specified, combined with a fast update * interval (5 seconds), the Fused Location Provider API returns location updates that are * accurate to within a few feet. * <p/> * These settings are appropriate for mapping applications that show real-time location * updates. */ protected void createLocationRequest() { mLocationRequest = new LocationRequest(); // Sets the desired interval for active location updates. This interval is // inexact. You may not receive updates at all if no location sources are available, or // you may receive them slower than requested. You may also receive updates faster than // requested if other applications are requesting location at a faster interval. mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); // Sets the fastest rate for active location updates. This interval is exact, and your // application will never receive updates faster than this value. mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } /** * Handles the Start Updates button and requests start of location updates. Does nothing if * updates have already been requested. */ public void startUpdatesButtonHandler(View view) { myVibrator.vibrate(300); myMainLayout.setBackgroundColor(getResources().getColor(R.color.darkblue)); if (!mRequestingLocationUpdates) { mRequestingLocationUpdates = true; setButtonsEnabledState(); startLocationUpdates(); } } /** * Handles the Stop Updates button, and requests removal of location updates. Does nothing if * updates were not previously requested. */ public void stopUpdatesButtonHandler(View view) { myVibrator.vibrate(300); myMainLayout.setBackgroundColor(getResources().getColor(R.color.darkred)); if (mRequestingLocationUpdates) { mRequestingLocationUpdates = false; setButtonsEnabledState(); stopLocationUpdates(); } } /** * Requests location updates from the FusedLocationApi. */ protected void startLocationUpdates() { // The final argument to {@code requestLocationUpdates()} is a LocationListener // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html). LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } /** * Ensures that only one button is enabled at any time. The Start Updates button is enabled * if the user is not requesting location updates. The Stop Updates button is enabled if the * user is requesting location updates. */ private void setButtonsEnabledState() { if (mRequestingLocationUpdates) { mStartUpdatesButton.setEnabled(false); mStopUpdatesButton.setEnabled(true); } else { mStartUpdatesButton.setEnabled(true); mStopUpdatesButton.setEnabled(false); } } /** * Updates the latitude, the longitude, and the last location time in the UI. */ private void updateUI() { try { mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel, mCurrentLocation.getLatitude())); mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel, mCurrentLocation.getLongitude())); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); mLastUpdateTimeTextView.setText(String.format("%s: %s", mLastUpdateTimeLabel, mLastUpdateTime)); PostInformation(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), String.format("%s", mLastUpdateTime)); } catch (NullPointerException e) { e.printStackTrace(); } } /** * Removes location updates from the FusedLocationApi. */ protected void stopLocationUpdates() { // It is a good practice to remove location requests when the activity is in a paused or // stopped state. Doing so helps battery performance and is especially // recommended in applications that request frequent location updates. // The final argument to {@code requestLocationUpdates()} is a LocationListener // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html). LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override public void onResume() { super.onResume(); // Within {@code onPause()}, we pause location updates, but leave the // connection to GoogleApiClient intact. Here, we resume receiving // location updates if the user has requested them. if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) { startLocationUpdates(); } } @Override protected void onPause() { super.onPause(); // Stop location updates to save battery, but don't disconnect the GoogleApiClient object. if (mGoogleApiClient.isConnected()) { stopLocationUpdates(); } } @Override protected void onStop() { mGoogleApiClient.disconnect(); super.onStop(); } /** * Runs when a GoogleApiClient object successfully connects. */ @Override public void onConnected(Bundle connectionHint) { Log.i(TAG, "Connected to GoogleApiClient"); // If the initial location was never previously requested, we use // FusedLocationApi.getLastLocation() to get it. If it was previously requested, we store // its value in the Bundle and check for it in onCreate(). We // do not request it again unless the user specifically requests location updates by pressing // the Start Updates button. // // Because we cache the value of the initial location in the Bundle, it means that if the // user launches the activity, // moves to a new location, and then changes the device orientation, the original location // is displayed as the activity is re-created. if (mCurrentLocation == null) { mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); //Calendar c = Calendar.getInstance(); //int Hr24=c.get(Calendar.HOUR_OF_DAY); //int Min=c.get(Calendar.MINUTE); //int SEC=c.get(Calendar.SECOND); //mLastUpdateTime = String.valueOf(Hr24)+":"+String.valueOf(Min)+":"+String.valueOf(SEC); //new SimpleDateFormat("MM-dd-yyyy-kk-mm").format(new Date()); //updateUI(); } // If the user presses the Start Updates button before GoogleApiClient connects, we set // mRequestingLocationUpdates to true (see startUpdatesButtonHandler()). Here, we check // the value of mRequestingLocationUpdates and if it is true, we start location updates. if (mRequestingLocationUpdates) { startLocationUpdates(); } } /** * Callback that fires when the location changes. */ @Override public void onLocationChanged(Location location) { mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); updateUI(); Toast.makeText(this, getResources().getString(R.string.location_updated_message), Toast.LENGTH_SHORT) .show(); } @Override public void onConnectionSuspended(int cause) { // The connection to Google Play services was lost for some reason. We call connect() to // attempt to re-establish the connection. Log.i(TAG, "Connection suspended"); mGoogleApiClient.connect(); } @Override public void onConnectionFailed(ConnectionResult result) { // Refer to the javadoc for ConnectionResult to see what error codes might be returned in // onConnectionFailed. Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); } /** * Stores activity data in the Bundle. */ public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putBoolean(REQUESTING_LOCATION_UPDATES_KEY, mRequestingLocationUpdates); savedInstanceState.putParcelable(LOCATION_KEY, mCurrentLocation); savedInstanceState.putString(LAST_UPDATED_TIME_STRING_KEY, mLastUpdateTime); super.onSaveInstanceState(savedInstanceState); } public void PostInformation(double lat, double lng, String timestamp) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("Latitude", lat); jsonObject.put("Longitude", lng); jsonObject.put("Timestamp", timestamp); jsonObject.put("Tagged", setTag); } catch (JSONException e) { e.printStackTrace(); } //Log.d("Requset","--------------------------->Sending"); httpQueue.add(new PostJsonObjectResponeObject(Request.Method.POST, "https://actiondriveapi.herokuapp.com/informationlist", jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { myMainLayout.setBackgroundColor(getResources().getColor(R.color.darkblue)); Toast.makeText(MainActivity.this, "ActionDriveApi Post Success", Toast.LENGTH_SHORT).show(); setTag = false; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { myMainLayout.setBackgroundColor(getResources().getColor(R.color.darkblue)); error.printStackTrace(); Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show(); setTag = false; } })); } /* @Override public boolean onTouchEvent(MotionEvent event) { int action = MotionEventCompat.getActionMasked(event); switch (action) { case (MotionEvent.ACTION_DOWN): Log.d("Touch", "Action was DOWN"); Toast.makeText(MainActivity.this, "Tag is set", Toast.LENGTH_SHORT).show(); return true; case (MotionEvent.ACTION_MOVE): Log.d("Touch", "Action was MOVE"); return true; case (MotionEvent.ACTION_UP): Log.d("Touch", "Action was UP"); setTag = true; updateUI(); return true; case (MotionEvent.ACTION_CANCEL): Log.d("Touch", "Action was CANCEL"); return true; case (MotionEvent.ACTION_OUTSIDE): Log.d("Touch", "Movement occurred outside bounds " + "of current screen element"); return true; default: return super.onTouchEvent(event); } }*/ @Override public boolean onTouchEvent(MotionEvent event) { this.mDetector.onTouchEvent(event); // Be sure to call the superclass implementation return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG, "onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString()); myVibrator.vibrate(300); myMainLayout.setBackgroundColor(getResources().getColor(R.color.green)); setTag = true; updateUI(); Toast.makeText(MainActivity.this, "It's beautifull ! Lets set a tag !", Toast.LENGTH_SHORT).show(); return true; } @Override public void onLongPress(MotionEvent event) { stopUpdatesButtonHandler(mStopUpdatesButton); Toast.makeText(MainActivity.this, "Lets Stop/Pause the application !!", Toast.LENGTH_SHORT).show(); Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d(DEBUG_TAG, "onScroll: " + e1.toString() + e2.toString()); return true; } @Override public void onShowPress(MotionEvent event) { Log.d(DEBUG_TAG, "onShowPress: " + event.toString()); } @Override public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString()); return true; } @Override public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString()); return true; } @Override public boolean onDoubleTapEvent(MotionEvent event) { startUpdatesButtonHandler(mStartUpdatesButton); Toast.makeText(MainActivity.this, "Lets Start the application !!", Toast.LENGTH_SHORT).show(); Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString()); return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) (SettingsDialog.newInstance()).show(getFragmentManager(), "SettingsDialog"); return super.onOptionsItemSelected(item); } }