Android Open Source - mobile-connector-sdk-android Engage Location Receiver Geocode






From Project

Back to project page mobile-connector-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project mobile-connector-sdk-android 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.silverpop.engage.location.receiver.plugin;
/*from  w ww.  j a v a2 s  .  co  m*/
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.util.Log;

import com.silverpop.engage.R;
import com.silverpop.engage.XMLAPIManager;
import com.silverpop.engage.config.EngageConfig;
import com.silverpop.engage.config.EngageConfigManager;
import com.silverpop.engage.domain.XMLAPI;
import com.silverpop.engage.location.manager.EngageLocationManager;
import com.silverpop.engage.util.EngageExpirationParser;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by jeremydyer on 6/12/14.
 */
public class EngageLocationReceiverGeocode
        extends EngageLocationReceiverBase {

    private static final String TAG = EngageLocationManager.class.getName();

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d(TAG, "Reverse Geocoding with " + EngageLocationReceiverGeocode.class.getName());
        onLocationReceived(context, (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED));
    }

    protected void onLocationReceived(Context context, Location loc) {

        if (Geocoder.isPresent()) {
            if (EngageConfig.addressCacheExpired(context)) {
                Log.d(TAG, "Address cache is expired. ReverseGeocoding Location Lat: "
                        + loc.getLatitude() + " - Long: " + loc.getLongitude());

                GeocoderAsyncTask geocoderAsyncTask = new GeocoderAsyncTask(context);
                geocoderAsyncTask.execute(loc.getLongitude(), loc.getLatitude());
            }
        }
    }

    private class GeocoderAsyncTask
            extends AsyncTask<Double, Void, Address> {

        private Context context;

        public GeocoderAsyncTask(Context context) {
            this.context = context;
        }

        protected Address doInBackground(Double... loc) {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());

            Double longitude = loc[0];
            Double latitude = loc[1];
            try {
                List<android.location.Address> geoCodeAddresses = geocoder.getFromLocation(latitude, longitude, 1);
                if (geoCodeAddresses != null && geoCodeAddresses.size() > 0) {
                    EngageConfig.storeCurrentAddressCache(geoCodeAddresses.get(0));

                    Date currentAddressBirthDay = new Date();
                    EngageConfig.storeCurrentAddressCacheBirthday(currentAddressBirthDay);

                    String locAcqTimeout = EngageConfigManager.get(context).locationCacheLifespan();
                    EngageExpirationParser exp = new EngageExpirationParser(locAcqTimeout, currentAddressBirthDay);
                    EngageConfig.storeCurrentAddressCacheExpiration(exp.expirationDate());

                    updateUserLastKnownLocation(context);

                    return EngageConfig.currentAddressCache();

                } else {
                    Log.w(TAG, "Unable to Geocode address for Lat: "
                            + latitude + " - Long: " + longitude);
                }
            } catch (IOException e) {
                Log.d(TAG, "Geocoder network service offline");
            }

            return null;
        }
    }

    protected void updateUserLastKnownLocation(Context context) {
        Resources r = context.getResources();
        String lastKnownLocationColumn = EngageConfigManager.get(context).lastKnownLocationColumn();
        String lastKnownLocationTimestampColumn = EngageConfigManager.get(context).lastKnownLocationTimestampColumn();

        String lastKnownLocationTimeFormat = EngageConfigManager.get(context).lastKnownLocationDateFormat();
        SimpleDateFormat sdf = new SimpleDateFormat(lastKnownLocationTimeFormat);

        //Make XMLAPI request to update the last known location.
        Map<String, Object> bodyElements = new HashMap<String, Object>();
        bodyElements.put("LIST_ID", EngageConfigManager.get(context).engageListId());
        bodyElements.put("CREATED_FROM", "1");
        XMLAPI updateLastKnownLocation = new XMLAPI("UpdateRecipient", bodyElements);
        Map<String, Object> syncFields = new HashMap<String, Object>();
        syncFields.put("EMAIL", EngageConfig.primaryUserId(context));
        updateLastKnownLocation.addSyncFields(syncFields);
        Map<String, Object> cols = new HashMap<String, Object>();
        cols.put(lastKnownLocationColumn, sdf.format(new Date()));
        cols.put(lastKnownLocationTimestampColumn, EngageConfig.buildLocationAddress());
        updateLastKnownLocation.addColumns(cols);

        String env = updateLastKnownLocation.envelope();
        Log.d(TAG, env);
        XMLAPIManager.get().postXMLAPI(updateLastKnownLocation, null, null);
    }
}




Java Source Code List

com.silverpop.engage.EngageApplication.java
com.silverpop.engage.UBFManager.java
com.silverpop.engage.XMLAPIManager.java
com.silverpop.engage.augmentation.UBFAugmentationServiceImpl.java
com.silverpop.engage.augmentation.UBFAugmentationService.java
com.silverpop.engage.augmentation.plugin.UBFAddressAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFCoordinatesAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFLocationNameAugmentationPlugin.java
com.silverpop.engage.config.EngageConfigManagerTests.java
com.silverpop.engage.config.EngageConfigManager.java
com.silverpop.engage.config.EngageConfigTest.java
com.silverpop.engage.config.EngageConfig.java
com.silverpop.engage.deeplinking.EngageDeepLinkManager.java
com.silverpop.engage.demo.engagetest.Application.java
com.silverpop.engage.demo.engagetest.EngageNotificationReceiver.java
com.silverpop.engage.demo.engagetest.MainActivity.java
com.silverpop.engage.demo.engagetest.PushReceiver.java
com.silverpop.engage.demo.engagetest.fragment.EngageConfigFragment.java
com.silverpop.engage.demo.engagetest.fragment.UBFAPIFragment.java
com.silverpop.engage.demo.engagetest.fragment.XMLAPIFragment.java
com.silverpop.engage.domain.EngageEvent.java
com.silverpop.engage.domain.JSONable.java
com.silverpop.engage.domain.UBFTests.java
com.silverpop.engage.domain.UBF.java
com.silverpop.engage.domain.XMLAPIEnum.java
com.silverpop.engage.domain.XMLAPITest.java
com.silverpop.engage.domain.XMLAPI.java
com.silverpop.engage.exception.XMLResponseParseException.java
com.silverpop.engage.location.manager.EngageLocationManager.java
com.silverpop.engage.location.manager.plugin.EngageLocationManagerDefault.java
com.silverpop.engage.location.receiver.EngageLocationReceiver.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverBase.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverGeocode.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverHardcodeTest.java
com.silverpop.engage.network.Credential.java
com.silverpop.engage.network.EngageClient.java
com.silverpop.engage.network.RequestCacheWrapper.java
com.silverpop.engage.network.UBFClient.java
com.silverpop.engage.network.XMLAPIClient.java
com.silverpop.engage.response.EngageResponseXMLTests.java
com.silverpop.engage.response.EngageResponseXML.java
com.silverpop.engage.response.XMLAPIResponseNode.java
com.silverpop.engage.store.EngageLocalEventStoreTest.java
com.silverpop.engage.store.EngageLocalEventStore.java
com.silverpop.engage.store.EngageSQLiteHelper.java
com.silverpop.engage.util.EngageExpirationParserTests.java
com.silverpop.engage.util.EngageExpirationParser.java
com.silverpop.engage.util.TimedAsyncTask.java