Java tutorial
package com.mobiquitynetworks.cordova.mnnotifications; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; import com.mobiquitynetworks.ProximityManager; import com.mobiquitynetworks.model.content.GeoData; import com.mobiquitynetworks.model.content.POI; import com.mobiquitynetworks.model.content.Venue; import com.mobiquitynetworks.model.stats.TrackingAudience; import com.mobiquitynetworks.model.stats.TrackingUser; import org.apache.cordova.*; import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Properties; public class NotificationsManager extends CordovaPlugin implements IntentProxy.BluetoothHandler, IntentProxy.GeoHandler { private static final String TAG = "NotificationsManager"; public static final String EVENT_START_MONITORING = "startMonitoring"; public static final String EVENT_REGISTER_CALLBACK = "registerDelegateCallbackId"; public static final String EVENT_STOP_MONITORING = "stopMonitoring"; public static final String EVENT_AUTHORIZATION_STATUS = "authorizationStatus"; public static final String EVENT_BLUETOOTH_STATE = "bluetoothState"; public static final String EVENT_GEO_INFO = "geoInfo"; private Map<String, CallbackContext> contexts = new HashMap(); @Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { if (action.equals(EVENT_START_MONITORING)) { Log.v(TAG, "EVENT_START_MONITORING"); this.startMonitoring(data, callbackContext); return true; } else if (action.equals(EVENT_REGISTER_CALLBACK)) { Log.v(TAG, "EVENT_REGISTER_CALLBACK"); String event = data.getString(0); Log.v(TAG, event); IntentProxy.setBtListener(this); IntentProxy.setgeoListener(this); PluginResult result = new PluginResult(PluginResult.Status.OK, "Set callback for " + event); result.setKeepCallback(true); this.contexts.put(event, callbackContext); callbackContext.sendPluginResult(result); return true; } return false; } Properties readSDKProperties(Context ctx) { Properties properties = new Properties(); AssetManager assetManager = ctx.getAssets(); if (assetManager == null) return properties; try { InputStream is = assetManager.open("properties/mobiquity.properties"); properties.load(is); return properties; } catch (IOException ioe) { //Empty properties return properties; } } private void startMonitoring(JSONArray data, CallbackContext callbackContext) { Log.v(TAG, "CONFIG START"); final Context c = this.cordova.getActivity().getApplicationContext(); Properties p = readSDKProperties(c); //gets configuration String sConfig; sConfig = "\nMobiquity Networks ProximityService configuration:\n" + "_________________________________________________________________\n" + "app key:\t\t" + p.getProperty("mobiquity.apm.appkey") + "\n" + "license:\t\t" + p.getProperty("mobiquity.apm.secret") + "\n" + "monitoring on period:\t" + p.getProperty("mobiquity.apm.monitoring.on_period") + "\n" + "monitoring off period:\t" + p.getProperty("mobiquity.apm.monitoring.off_period") + "\n" + "use idfa:\t\t" + p.getProperty("mobiquity.apm.stats.use_idfa_when_possible") + "\n" + "Endpoint:\t\t" + p.getProperty("mobiquity.apm.network.endpoint") + "\n" + "debug:\t\t" + p.getProperty("mobiquity.apm.debug") + "\n" + "_________________________________________________________________"; Log.v(TAG, sConfig); Log.v(TAG, "CONFIG STOP"); //data: [ // {"tags": ["development","software"], // "gender":"M","birthday":"1986-11-20","username":"matt@aimatt.com","language":"English","kids":2,"maritalStatus":"married" // }, // {"battery":0.2}] Log.v(TAG, "data: " + data.toString()); if (data.length() > 0) { try { JSONObject user = (JSONObject) data.get(0); Log.v(TAG, "user: " + user.toString()); TrackingUser.Builder userBuilder = new TrackingUser.Builder(); userBuilder.role(TrackingUser.Role.PRODUSER); TrackingAudience.Builder audienceBuilder = new TrackingAudience.Builder(); // Username if (user.getString("username") != null) { userBuilder.username(user.getString("username")); } // Gender String gender = user.getString("gender"); if (gender.equals("M")) { audienceBuilder.gender(TrackingAudience.Gender.MALE); } else if (gender.equals("F")) { audienceBuilder.gender(TrackingAudience.Gender.FEMALE); } // Marital Status String maritalStatus = user.getString("maritalStatus"); if (maritalStatus.equals("married")) { audienceBuilder.maritalStatus(TrackingAudience.MaritalStatus.MARRIED); } else if (maritalStatus.equals("single")) { audienceBuilder.maritalStatus(TrackingAudience.MaritalStatus.SINGLE); } // Kids audienceBuilder.kids(user.getInt("kids")); // Tags JSONArray tagsJson = user.getJSONArray("tags"); if (tagsJson != null) { ArrayList<String> tags = new ArrayList<String>(); int len = tagsJson.length(); for (int i = 0; i < len; i++) { tags.add(tagsJson.get(i).toString()); } audienceBuilder.tags(new HashSet<String>(tags)); } // Education String education = user.getString("education"); if (education.equals("CLL")) { audienceBuilder.education(TrackingAudience.Education.COLLEGE); } else if (education.equals("GS")) { audienceBuilder.education(TrackingAudience.Education.GRAD_SCHOOL); } else if (education.equals("NCLL")) { audienceBuilder.education(TrackingAudience.Education.NON_COLLEGE); } // Ethnicity String ethnicity = user.getString("ethnicity"); if (ethnicity.equals("AA")) { audienceBuilder.ethnicity(TrackingAudience.Ethnicity.AFRICAN_AMERICAN); } else if (ethnicity.equals("AS")) { audienceBuilder.ethnicity(TrackingAudience.Ethnicity.ASIAN); } else if (ethnicity.equals("CC")) { audienceBuilder.ethnicity(TrackingAudience.Ethnicity.CAUSASIAN); } else if (ethnicity.equals("HP")) { audienceBuilder.ethnicity(TrackingAudience.Ethnicity.HISPANIC); } // TODO: Language userBuilder.audience(audienceBuilder.build()); final TrackingUser trackingUser = userBuilder.build(); cordova.getThreadPool().execute(new Runnable() { public void run() { ProximityManager.getInstance().setTrackingUserInformation(trackingUser); ProximityManager.getInstance().startService(c); } }); // TODO: Custom Vars // HashMap<String, String> trackingCustomVars = new HashMap<String, String>(); // trackingCustomVars.put("custom1", "foo"); // trackingCustomVars.put("custom2", "bar"); // ProximityManager.setTrackingCustomVars(trackingCustomVars); String message = "startMonitoring success"; PluginResult result = new PluginResult(PluginResult.Status.OK, message.toString()); result.setKeepCallback(true); callbackContext.sendPluginResult(result); } catch (JSONException e) { Log.e(TAG, "Exception parsing JSON", e); } } } private void stopMonitoring(JSONArray data, CallbackContext callbackContext) { final Context c = this.cordova.getActivity().getApplicationContext(); ProximityManager.getInstance().halt(c); } public void onBluetooth(MNBluetoothState state) { Log.v(TAG, "onBluetooth " + state); CallbackContext context = this.contexts.get(EVENT_BLUETOOTH_STATE); if (context != null) { PluginResult result = new PluginResult(PluginResult.Status.OK, state.toString()); context.sendPluginResult(result); } } public void onGeo(GeoData geoData) { Log.v(TAG, "onGeo " + geoData.toString()); CallbackContext context = this.contexts.get(EVENT_GEO_INFO); if (context != null) { JSONObject o = new JSONObject(); Venue venue = geoData.getVenue(); try { JSONObject v = new JSONObject(); v.put("name", venue.getName()); v.put("lat", venue.getLatitude()); v.put("lng", venue.getLongitude()); o.put("venue", v); JSONArray pois = new JSONArray(); if (geoData.getPois() != null) { for (POI poi : geoData.getPois()) { JSONObject p = new JSONObject(); p.put("name", poi.getName()); p.put("type", poi.getType()); pois.put(p); } } o.put("pois", pois); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } PluginResult result = new PluginResult(PluginResult.Status.OK, o.toString()); context.sendPluginResult(result); } } }