Android Open Source - android-component-location W Location






From Project

Back to project page android-component-location.

License

The source code is released under:

MIT License

If you think the Android project android-component-location 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.yingchn.android.activity;
//  ww w .  j a va2 s  . com
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.text.TextUtils;
import android.util.Log;

import com.yingchn.android.AppContext;

public class WLocation{
  public static Location l = null;
  public static ArrayList<WLocationListener> ls = new ArrayList<WLocationListener>();
  public static boolean requesting = false;
  public static boolean requestingGps = false;
  static int requests = 0;
  
    public static boolean isLocationEnabled(Context context) {
        int locationMode = 0;
        String locationProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(),
                        Settings.Secure.LOCATION_MODE);

            } catch (SettingNotFoundException e) {
                e.printStackTrace();
            }

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;

        } else {
            locationProviders = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }

    }

  // ??????????????????
  public static Location cachedLocation() {
    SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(AppContext.getContext());
    String str = localSharedPreferences.getString("cachedLocationProvier",
        null);
    if (str != null) {
      l = new Location(str);
      l.setLatitude(localSharedPreferences.getFloat("cachedLat", 0.0F));
      l.setLongitude(localSharedPreferences.getFloat("cachedLon", 0.0F));
      l.setAccuracy(localSharedPreferences.getFloat("cachedAcc", 0.0F));
    }
    return l;
  }
  
  // ??GPS???????????
  public static void getGPS(Context context) {
    final LocationManager gpsLm = (LocationManager) context
        .getSystemService(Context.LOCATION_SERVICE);

    if (gpsLm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      Log.v("WLocation", "Requesting GPS Update");
      requestingGps = true;
      final LocationListener locationListener;
      gpsLm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500L,
          0.0F, (locationListener = new LocationListener() {
            int updates = 0;

            public void onLocationChanged(Location location) {
              Log.v("WLocation", "Got GPS Update");
              WLocation.setLocation(location);
              this.updates = (1 + this.updates);
              if (this.updates >= 3) {
                WLocation.requesting = false;
                /*
                 * WRemote.remote().updateUserLocation(
                 * location.getLatitude(),
                 * location.getLongitude());
                 */
                Log.v("rainx", "3 times");
                gpsLm.removeUpdates(this);
                WLocation.requestingGps = false;
              }
            }

            public void onProviderDisabled(
                String paramAnonymousString) {
              Log.v("WLocation", "Location Disabled: "
                  + paramAnonymousString);
            }

            public void onProviderEnabled(
                String paramAnonymousString) {
              Log.v("WLocation", "Location Enabled: "
                  + paramAnonymousString);
            }

            public void onStatusChanged(
                String paramAnonymousString,
                int paramAnonymousInt,
                Bundle paramAnonymousBundle) {
              switch (paramAnonymousInt) {
              default:
                return;
              case 2:
                Log.v("WLocation",
                    "Location provider is available");
              case 0:
                Log.v("WLocation",
                    "Location provider is out of service");
                return;
              case 1:

                Log.v("WLocation",
                    "Location provider is unavailable");
              }
            }
          }));
      // ???????GPS Provider,??????
      Timer timer = new Timer();
      TimerTask task = new TimerTask() {
        public void run() {
          if (WLocation.requestingGps) {
            Log.v("rainx", "GPS ????");
            WLocation.requestingGps = false;
            gpsLm.removeUpdates(locationListener);
          }
        }
      };
      timer.schedule(task, 5000); // ???????5?
    }
  }
  
  
  // ??????????????
  public static boolean getLocation() {
      if(!WLocation.isLocationEnabled(AppContext.getContext()))
          return false;
      
    // ???????? ???
    if (requesting)
      return true;
    // ?????????locationprovider??
    final LocationManager lm = (LocationManager) AppContext.getContext()
         .getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);
      if (providers.contains(LocationManager.PASSIVE_PROVIDER))
        providers.remove(LocationManager.PASSIVE_PROVIDER);
      
      // ??
    String provideName;
    // ?????????location
    Location location = null;
    
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
      provideName = LocationManager.GPS_PROVIDER;
      Log.v("W", "Provider is " + provideName);
      location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    } 
    
    if (location == null && providers.contains(LocationManager.NETWORK_PROVIDER)) {
      provideName = LocationManager.NETWORK_PROVIDER;
      location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    // ????????location???
    if (location != null) {
      Log.v("W",
          "Last known netowrk is " + location.getLatitude() + ","
              + location.getLongitude() + " Acc: " + location.getAccuracy());
      
      long timediff;
      if (l != null) {
        timediff = System.currentTimeMillis() - l.getTime();
      } else {
        timediff = 0;
      }
      Log.v("W", "Last network staleness is " + timediff / 1000L);
      if ((timediff < 3600000L) && (location.getAccuracy() < 1000.0F) && setLocation(location)) {
        return true;
      }
    }
    
    // ?????????provider, ???????????????, ???????????noprovider
    if (!providers.contains(LocationManager.GPS_PROVIDER) && !providers.contains(LocationManager.NETWORK_PROVIDER)) {
      
      //TODO here is async original
      if (WLocation.l == null)
            {
              Location localLocation = WLocation.cachedLocation();
              if (localLocation == null) {
                Iterator<WLocationListener> localIterator = WLocation.ls.iterator();
                while (localIterator.hasNext())
                  ((WLocation.WLocationListener)localIterator.next()).noLocation();
              } else {
                WLocation.setLocation(localLocation);  
              }
            }
      return true;
    }
    
    
    // ?????????provider ????????
    Criteria criteria = new Criteria();
    criteria.setCostAllowed(false);
    // ???????????
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); // ?????????
    // getBestProvider ????????????????????????
    String provider = lm.getBestProvider(criteria, true);
    Log.i("8023", "------???????" + provider);
    WLocation.requesting = true;
    
    if (provider == null) {
      return false;
    }
    
    lm.requestLocationUpdates(provider, 1000L, 1.0F,
        new LocationListener() {
          // ????
          int updates = 0;

          public void onLocationChanged(
              Location paramAnonymousLocation) {
            Log.v("WRemote", "Got a location "
                + paramAnonymousLocation.toString());
            WLocation.setLocation(paramAnonymousLocation);
            updates = (1 + updates);
            if (this.updates == 3) {
              lm.removeUpdates(this);
              WLocation.requesting = false;
            }
          }

          public void onProviderDisabled(String paramAnonymousString) {
            Log.v("WLocation", "Provider disabled: "
                + paramAnonymousString);
          }

          public void onProviderEnabled(String paramAnonymousString) {
            Log.v("WLocation", "Provider enabled: "
                + paramAnonymousString);
          }

          public void onStatusChanged(String paramAnonymousString,
              int paramAnonymousInt, Bundle paramAnonymousBundle) {
            Log.v("rainx", "status change to " + paramAnonymousInt);
            switch (paramAnonymousInt) {
            case 1:
            case 2:
            }
          }
        });

    return true;
  }

  /**
   * ??location???????????????????locationListener????????????????????????????????????????????false
   */
  private static boolean setLocation(Location location) {
    Log.v("WLocation", "setting location");
    if ((l == null) || (location.getAccuracy() <= l.getAccuracy())) {
      Log.v("WLocation", "set location");
      l = location;
      Iterator<WLocationListener> localIterator = ls.iterator();
      while (true) {
        if (!localIterator.hasNext()) {
          SharedPreferences.Editor localEditor = PreferenceManager.getDefaultSharedPreferences(AppContext.getContext()).edit();
          localEditor.putString("cachedLocationProvier",
              l.getProvider());
          localEditor.putFloat("cachedLat", (float) l.getLatitude());
          localEditor.putFloat("cachedLon", (float) l.getLongitude());
          localEditor.putFloat("cachedAcc", l.getAccuracy());
          localEditor.commit();
          return true;
        }
        ((WLocationListener) localIterator.next()).gotLocation(l);
      }
    }
    return false;
  }
  
  public static abstract interface WLocationListener {
    public abstract void gotLocation(Location location);
      
    public abstract void noLocation();
  }


}




Java Source Code List

android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.yingchn.android.AppContext.java
com.yingchn.android.MyApplication.java
com.yingchn.android.activity.ArbitraryFragmentActivity.java
com.yingchn.android.activity.MainActivity.java
com.yingchn.android.activity.WLocation.java
com.yingchn.android.fragment.LogFragment.java
com.yingchn.android.fragment.SettingFragment.java
com.yingchn.android.location.MyLocationManager.java
com.yingchn.android.util.FragmentUtil.java