Android Open Source - wit-android-sdk Wit Context Setter






From Project

Back to project page wit-android-sdk.

License

The source code is released under:

/** * Copyright (c) 2014, Wit.ai, Inc. All rights reserved. * * You are hereby granted a non-exclusive, worldwide, royalty-free license to * use, copy, modify, and distribute this software in source c...

If you think the Android project wit-android-sdk 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 ai.wit.sdk;
// w w w. j  a va  2  s.  c  om
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

import com.google.gson.JsonObject;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import android.content.Context;

/**
 * Created by aric on 10/29/14.
 */
public class WitContextSetter {

    private static String KEY_REFTIME = "reference_time";
    private static String KEY_LOCATION = "location";
    private Context _androidContext;

    public WitContextSetter(JsonObject contextData, Context androidContext) {
        _androidContext = androidContext;
        ensureTime(contextData);
        ensureLocation(contextData);
    }

    protected void ensureTime(JsonObject context) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
        String nowAsISO8601 = df.format(new Date());
        context.addProperty(KEY_REFTIME, nowAsISO8601);
    }

    protected void ensureLocation(JsonObject context) {
        if (_androidContext == null) {
            return ;
        }
        Location loc = getLocation();
        if (loc == null) {
            return ;
        }
        JsonObject jsonLocation = new JsonObject();
        jsonLocation.addProperty("latitude", loc.getLatitude());
        jsonLocation.addProperty("longitude", loc.getLongitude());
        context.add(KEY_LOCATION, jsonLocation);
    }

    /**
     * This function should only be called when the _androidContext instance variable is set
     * @return Location object
     */
    protected Location getLocation() {
        LocationManager lm = (LocationManager) _androidContext.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        String locationProvider = lm.getBestProvider(criteria, true);
        if (locationProvider == null) {
            return null;
        }
        Location loc = lm.getLastKnownLocation(locationProvider);

        return loc;
    }
}




Java Source Code List

ai.wit.sdk.IWitCoordinator.java
ai.wit.sdk.IWitListener.java
ai.wit.sdk.WitContextSetter.java
ai.wit.sdk.WitMessageRequestTask.java
ai.wit.sdk.WitMic.java
ai.wit.sdk.WitRequest.java
ai.wit.sdk.WitSpeechRequestTask.java
ai.wit.sdk.WitTest.java
ai.wit.sdk.Wit.java
ai.wit.sdk.model.WitOutcome.java
ai.wit.sdk.model.WitResponse.java