Android Open Source - anti-piracy-android-app Current Subregion Helper






From Project

Back to project page anti-piracy-android-app.

License

The source code is released under:

Apache License

If you think the Android project anti-piracy-android-app 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 mil.nga.giat.asam.util;
// w w  w.j  a  v  a2 s .  co m
import java.util.List;
import java.util.Locale;

import mil.nga.giat.asam.model.SubregionBean;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;


public class CurrentSubregionHelper implements LocationListener {

    private Location mCurrentLocation;
    private List<SubregionBean> mSubregions;
    private Integer mCurrentSubregion;
    private LocationManager mLocationManager;
    
    public CurrentSubregionHelper(Context context, List<SubregionBean> subregions) {
        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        mCurrentLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        mSubregions = subregions;
    }
    
    public synchronized int getCurrentSubregion() {
        if (mCurrentSubregion == null) {
            for (SubregionBean subregion : mSubregions) {
                if (subregionContainsCurrentLocation(subregion.getGeoPoints())) {
                    mCurrentSubregion = subregion.getSubregionId();
                    AsamLog.i(String.format(Locale.US, CurrentSubregionHelper.class.getName() + ":Found subregion %d for lat:%f lon:%f", mCurrentSubregion, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
                    break;
                }
            }
            mLocationManager.removeUpdates(this);
            return mCurrentSubregion == null ? 11 : mCurrentSubregion;
        }
        return mCurrentSubregion;
    }

    @Override
    public synchronized void onLocationChanged(Location location) {
        AsamLog.i(CurrentSubregionHelper.class.getName() + ":Got a better geolocation fix");
        mCurrentLocation = location;
        mLocationManager.removeUpdates(this);
        for (SubregionBean subregion : mSubregions) {
            if (subregionContainsCurrentLocation(subregion.getGeoPoints())) {
                mCurrentSubregion = subregion.getSubregionId();
                AsamLog.i(String.format(Locale.US, CurrentSubregionHelper.class.getName() + ":Found better subregion %d for lat:%f lon:%f", mCurrentSubregion, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
                break;
            }
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    
    private boolean subregionContainsCurrentLocation(List<SubregionBean.GeoPoint> points) {
        if (mCurrentLocation == null) {
            return false;
        }
        boolean contains = false;
        for (int i = 0, j = points.size() - 1; i < points.size() - 0; j = i++) {
            if (((points.get(i).latitude > mCurrentLocation.getLatitude()) != (points.get(j).latitude > mCurrentLocation.getLatitude())) && (mCurrentLocation.getLongitude() < (points.get(j).longitude - points.get(i).longitude) * (mCurrentLocation.getLatitude() - points.get(i).latitude) / (points.get(j).latitude - points.get(i).latitude) + points.get(i).longitude)) {
                contains = !contains;
            }
        }
        return contains;
    }
}




Java Source Code List

mil.nga.giat.asam.AsamListActivity.java
mil.nga.giat.asam.AsamListFragment.java
mil.nga.giat.asam.AsamListReportTabletActivity.java
mil.nga.giat.asam.AsamReportActivity.java
mil.nga.giat.asam.AsamReportFragment.java
mil.nga.giat.asam.Asam.java
mil.nga.giat.asam.DisclaimerActivity.java
mil.nga.giat.asam.DisclaimerDialogFragment.java
mil.nga.giat.asam.InfoActivity.java
mil.nga.giat.asam.InfoDialogFragment.java
mil.nga.giat.asam.InfoFragment.java
mil.nga.giat.asam.LaunchScreenActivity.java
mil.nga.giat.asam.LegalActivity.java
mil.nga.giat.asam.LegalDetailsActivity.java
mil.nga.giat.asam.LegalDetailsFragment.java
mil.nga.giat.asam.LegalFragment.java
mil.nga.giat.asam.LegalTabletActivity.java
mil.nga.giat.asam.MainActivity.java
mil.nga.giat.asam.PreferencesActivity.java
mil.nga.giat.asam.PreferencesDialogFragment.java
mil.nga.giat.asam.SortAsamListDialogFragment.java
mil.nga.giat.asam.TextQueryActivity.java
mil.nga.giat.asam.TextQueryDialogFragment.java
mil.nga.giat.asam.connectivity.NetworkChangeReceiver.java
mil.nga.giat.asam.connectivity.OfflineBannerFragment.java
mil.nga.giat.asam.db.AsamDbHelper.java
mil.nga.giat.asam.jackson.deserializer.Deserializer.java
mil.nga.giat.asam.jackson.deserializer.FeatureDeserializer.java
mil.nga.giat.asam.jackson.deserializer.GeometryDeserializer.java
mil.nga.giat.asam.map.AllAsamsMapActivity.java
mil.nga.giat.asam.map.AllAsamsMapTabletActivity.java
mil.nga.giat.asam.map.BackgroundTileProvider.java
mil.nga.giat.asam.map.OfflineMap.java
mil.nga.giat.asam.map.SingleAsamMapActivity.java
mil.nga.giat.asam.map.SubregionMapActivity.java
mil.nga.giat.asam.model.AsamBean.java
mil.nga.giat.asam.model.AsamJsonParser.java
mil.nga.giat.asam.model.AsamMapClusterBean.java
mil.nga.giat.asam.model.LegalBean.java
mil.nga.giat.asam.model.SubregionBean.java
mil.nga.giat.asam.model.SubregionTextParser.java
mil.nga.giat.asam.model.TextQueryParametersBean.java
mil.nga.giat.asam.net.AsamWebService.java
mil.nga.giat.asam.util.AsamConstants.java
mil.nga.giat.asam.util.AsamListContainer.java
mil.nga.giat.asam.util.AsamLog.java
mil.nga.giat.asam.util.AsamUtils.java
mil.nga.giat.asam.util.CurrentSubregionHelper.java
mil.nga.giat.asam.util.ManifestMetaData.java
mil.nga.giat.asam.util.SyncTime.java
mil.nga.giat.asam.widget.AsamArrayAdapter.java