Android Open Source - clever-weather Location Getter






From Project

Back to project page clever-weather.

License

The source code is released under:

GNU General Public License

If you think the Android project clever-weather 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.listotechnologies.cleverweather;
/*from   w w  w .ja v a2 s  . com*/
import android.content.Context;
import android.location.Location;
import android.os.Looper;

public class LocationGetter {
    private final Context mContext;
    private Location mLocation = null;
    private final Object mGotLocationLock = new Object();
    private LocationHelper mLocationHelper = null;
    private int mUpdateTimeout;
    private int mLocationExpiryMins;

    private final LocationHelper.LocationResultListener mLocationResult = new LocationHelper.LocationResultListener() {
        @Override
        public void onGotLocation(Location location) {
            synchronized (mGotLocationLock) {
                LocationGetter.this.mLocation = location;
                mGotLocationLock.notifyAll();
                Looper.myLooper().quit();
            }
        }
    };

    public LocationGetter(Context context, int updateTimeoutSecs, int locationExpiryMins) {
        if (context == null)
            throw new IllegalArgumentException("context == null");

        mContext = context;
        mUpdateTimeout = updateTimeoutSecs;
        mLocationExpiryMins = locationExpiryMins;
    }

    public synchronized Location getLocation() {
        try {
            synchronized (mGotLocationLock) {
                new Thread() {
                    public void run() {
                        Looper.prepare();
                        getLocationHelper().getLocation(mLocationResult);
                        Looper.loop();
                    }
                }.start();

                mGotLocationLock.wait((mUpdateTimeout + 1) * 1000);
            }
        } catch (InterruptedException e1) {
            //e1.printStackTrace();
        }
        return mLocation;
    }

    public LocationHelper getLocationHelper() {
        if (mLocationHelper == null) {
            mLocationHelper = new LocationHelper(mContext, false);
            mLocationHelper.setUpdateTimeout(mUpdateTimeout);
            mLocationHelper.setLocationExpiry(mLocationExpiryMins);
        }
        return mLocationHelper;
    }

    public boolean isLocationEnabled() {
        return getLocationHelper().isGpsEnabled() || getLocationHelper().isNetworkEnabled();
    }
}




Java Source Code List

com.example.android.common.view.SlidingTabLayout.java
com.example.android.common.view.SlidingTabStrip.java
com.listotechnologies.cleverweather.ApplicationTest.java
com.listotechnologies.cleverweather.CitiesFragment.java
com.listotechnologies.cleverweather.CleverWeatherDbHelper.java
com.listotechnologies.cleverweather.CleverWeatherProviderClient.java
com.listotechnologies.cleverweather.CleverWeatherProviderExtended.java
com.listotechnologies.cleverweather.CleverWeatherProvider.java
com.listotechnologies.cleverweather.ForecastParser.java
com.listotechnologies.cleverweather.ForecastsActivity.java
com.listotechnologies.cleverweather.ForecastsFragment.java
com.listotechnologies.cleverweather.LocationGetter.java
com.listotechnologies.cleverweather.LocationHelper.java
com.listotechnologies.cleverweather.ProvinceActivity.java
com.listotechnologies.cleverweather.ProvincesFragment.java
com.listotechnologies.cleverweather.SearchCitiesActivity.java
com.listotechnologies.cleverweather.TabbedActivity.java
com.listotechnologies.cleverweather.TwoPaneFragment.java