Android Open Source - Now_Manager Async G P S Log






From Project

Back to project page Now_Manager.

License

The source code is released under:

Apache License

If you think the Android project Now_Manager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.collinguarino.nowmanager;
//  ww w. j  av a2  s  . com
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.collinguarino.nowmanager.floating.FloatingActionsMenu;
import com.collinguarino.nowmanager.provider.Contracts;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * Created by collinux on 3/26/14.
 */
public class AsyncGPSLog extends AsyncTask<Void, Void, Void> implements LocationListener
{
    private final Context mContext;

    //flag for GPS Status
    boolean isGPSEnabled = false;

    //flag for network status
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    double latitude;
    double longitude;

    //The minimum distance to change updates in metters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 meters

    //The minimum time beetwen updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    //Declaring a Location Manager
    private Location location;
    protected LocationManager locationManager;

    // Location Info
    String addressLine, city, mapQueryString;

    // control main ui element
    FloatingActionsMenu mFam;

    public AsyncGPSLog(Context context, FloatingActionsMenu fam)
    {
        this.mContext = context;
        this.mFam = fam;
        getLocation();
    }

    @Override
    protected void onPostExecute(Void result) {

        if (location != null) {

            if (getAddressLine(mContext) != null && getLocality(mContext) != null) {
                addressLine = getAddressLine(mContext);
                city = getLocality(mContext);
                mapQueryString = addressLine + " "  + city;
            } else {
                mapQueryString = "Unable to get GPS.";
            }

            stopUsingGPS();

            // show a dialog to tell more about the location
            displayLocationDetailDialog();
        }

        if (!canGetLocation) {
            showSettingsAlert();
        }

    }

    private void displayLocationDetailDialog() {
        final Dialog dialog = new Dialog(mContext);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_location_input);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        final EditTextBackEvent eventNameEdit = (EditTextBackEvent) dialog.findViewById(R.id.textInput);
        eventNameEdit.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener() {
            @Override
            public void onImeBack(EditTextBackEvent ctrl, String text) {
                eventNameEdit.setCursorVisible(false);
                eventNameEdit.clearComposingText(); // fixes underline bug
                eventNameEdit.setText(eventNameEdit.getText().toString());
            }
        });
        eventNameEdit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                eventNameEdit.setCursorVisible(true);
                return true;
            }
        });

        final TextView addressDisplay = (TextView) dialog.findViewById(R.id.address);
        addressDisplay.setText(mapQueryString);

        dialog.findViewById(R.id.createFinal).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String eventName = eventNameEdit.getText().toString().trim();
                ContentValues values;
                if (eventNameEdit.getText().toString().trim().length() > 0) {
                    values = Contracts.TimeCards.getInsertValues(eventName, true, false, null, false, mapQueryString);
                } else {
                    values = Contracts.TimeCards.getInsertValues(eventName, true, false, null, false, mapQueryString);
                }

                mContext.getContentResolver().insert(Contracts.TimeCards.CONTENT_URI, values);

                dialog.dismiss();
            }
        });


        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(eventNameEdit, InputMethodManager.SHOW_IMPLICIT);
            }
        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    @Override
    protected void onPreExecute() {
        mFam.collapse();
    }

    public Location getLocation()
    {
        try
        {
            locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);

            //getting GPS status
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {

                this.canGetLocation = true;

                //First get location from Network Provider
                if (isNetworkEnabled)
                {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null)
                    {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        updateGPSCoordinates();
                    }
                }

                //if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled)
                {
                    if (location == null)
                    {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        Log.d("GPS Enabled", "GPS Enabled");

                        if (locationManager != null)
                        {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            updateGPSCoordinates();
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            //e.printStackTrace();
            Log.e("Error : Location", "Impossible to connect to LocationManager", e);
        }

        return location;
    }

    public void updateGPSCoordinates()
    {
        if (location != null)
        {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     */

    public void stopUsingGPS()
    {
        if (locationManager != null)
        {
            locationManager.removeUpdates(AsyncGPSLog.this);
        }
    }

    /**
     * Function to get latitude
     */
    public double getLatitude()
    {
        if (location != null)
        {
            latitude = location.getLatitude();
        }

        return latitude;
    }

    /**
     * Function to get longitude
     */
    public double getLongitude()
    {
        if (location != null)
        {
            longitude = location.getLongitude();
        }

        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     */
    public boolean canGetLocation()
    {
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
            return !this.canGetLocation;
        } else {
            return this.canGetLocation;
        }
    }

    /**
     * Function to show settings alert dialog
     */
    public void showSettingsAlert()
    {
        Toast toast = Toast.makeText(mContext, "Must Have GPS Location Enabled", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM,0,280);
        toast.show();
    }

    /**
     * Get list of address by latitude and longitude
     * @return null or List<Address>
     */
    public List<Address> getGeocoderAddress(Context context)
    {
        if (location != null)
        {
            Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
            try
            {
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                return addresses;
            }
            catch (IOException e)
            {
                //e.printStackTrace();
                Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e);
            }
        }

        return null;
    }

    /**
     * Try to get AddressLine
     * @return null or addressLine
     */
    public String getAddressLine(Context context)
    {
        List<Address> addresses = getGeocoderAddress(context);
        if (addresses != null && addresses.size() > 0)
        {
            Address address = addresses.get(0);
            String addressLine = address.getAddressLine(0);

            return addressLine;
        }
        else
        {
            return null;
        }
    }

    /**
     * Try to get Locality
     * @return null or locality
     */
    public String getLocality(Context context)
    {
        List<Address> addresses = getGeocoderAddress(context);
        if (addresses != null && addresses.size() > 0)
        {
            Address address = addresses.get(0);
            String locality = address.getLocality();

            return locality;
        }
        else
        {
            return null;
        }
    }

    /**
     * Try to get Postal Code
     * @return null or postalCode
     */
    public String getPostalCode(Context context)
    {
        List<Address> addresses = getGeocoderAddress(context);
        if (addresses != null && addresses.size() > 0)
        {
            Address address = addresses.get(0);
            String postalCode = address.getPostalCode();

            return postalCode;
        }
        else
        {
            return null;
        }
    }

    /**
     * Try to get CountryName
     * @return null or postalCode
     */
    public String getCountryName(Context context)
    {
        List<Address> addresses = getGeocoderAddress(context);
        if (addresses != null && addresses.size() > 0)
        {
            Address address = addresses.get(0);
            String countryName = address.getCountryName();

            return countryName;
        }
        else
        {
            return null;
        }
    }

    @Override
    public void onLocationChanged(Location location)
    {
    }

    @Override
    public void onProviderDisabled(String provider)
    {
    }

    @Override
    public void onProviderEnabled(String provider)
    {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }

    @Override
    protected Void doInBackground(Void... Void) {

        location = getLocation();

        return null;
    }

}




Java Source Code List

com.collinguarino.nowmanager.AsyncGPSLog.java
com.collinguarino.nowmanager.EditTextBackEvent.java
com.collinguarino.nowmanager.EditTextMovement.java
com.collinguarino.nowmanager.SwipeDismissListViewTouchListener.java
com.collinguarino.nowmanager.TimeCardAdapter.java
com.collinguarino.nowmanager.TimeCardObject.java
com.collinguarino.nowmanager.floating.AddFloatingActionButton.java
com.collinguarino.nowmanager.floating.FloatingActionButton.java
com.collinguarino.nowmanager.floating.FloatingActionsMenu.java
com.collinguarino.nowmanager.provider.Contracts.java
com.collinguarino.nowmanager.provider.MainDatabaseHelper.java
com.collinguarino.nowmanager.provider.NowManagerProvider.java
com.collinguarino.nowmanager.ui.ActivityMain.java
com.collinguarino.nowmanager.ui.ActivitySettings.java
com.collinguarino.nowmanager.ui.Dialogs.java