Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/2.1.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
catt-stefano committed Mar 28, 2017
2 parents ab4497a + cb4016e commit b8d4de1
Show file tree
Hide file tree
Showing 42 changed files with 2,294 additions and 336 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ bintray {
websiteUrl = 'https://github.com/catt-stefano/near-sdk-core'
}
configurations = ['archives']
publish = true
}

// Upload docs on ftp
Expand Down Expand Up @@ -179,9 +180,9 @@ dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.google.code.gson:gson:2.6.2'
compile 'org.altbeacon:android-beacon-library:2.9.2'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.android.gms:play-services-location:10.2.0'
compile "com.google.firebase:firebase-messaging:${playServicesVersion}"
compile "com.google.firebase:firebase-core:${playServicesVersion}"
compile "com.google.android.gms:play-services-location:${playServicesVersion}"

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.7.6"
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/it/near/sdk/GlobalState.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public GlobalState(Context mContext) {
this.mContext = mContext;
}

public static GlobalState getInstance(Context context){
if(mInstance == null)
{
public static GlobalState getInstance(Context context) {
if (mInstance == null) {
mInstance = new GlobalState(context);
}
return mInstance;
Expand All @@ -33,7 +32,7 @@ public static GlobalState getInstance(Context context){
public Context getmContext() {
return mContext;
}

public RecipesManager getRecipesManager() {
return recipesManager;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/it/near/sdk/NearItManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ private void plugInSetup() {
SharedPreferences recipeCoolerSP = application.getSharedPreferences(RecipeCooler.NEAR_RECIPECOOLER_PREFSNAME,0);
RecipeCooler recipeCooler = new RecipeCooler(recipeCoolerSP);
SharedPreferences recipeManagerSP = application.getSharedPreferences(RecipesManager.PREFS_NAME, 0);
recipesManager = new RecipesManager(application, GlobalConfig.getInstance(application), recipeCooler, recipeManagerSP);
recipesManager = new RecipesManager(application,
GlobalConfig.getInstance(application),
recipeCooler,
recipeManagerSP);

GlobalState.getInstance(application).setRecipesManager(recipesManager);

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/it/near/sdk/communication/NearAsyncHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import android.content.Context;
import android.os.Looper;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BuildConfig;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.SyncHttpClient;
Expand All @@ -28,8 +26,8 @@
*/
public class NearAsyncHttpClient {

public static AsyncHttpClient syncHttpClient = new SyncHttpClient();
public static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
private static AsyncHttpClient syncHttpClient = new SyncHttpClient();
private static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

public static RequestHandle nearGet(Context context, String url, ResponseHandlerInterface responseHandler) throws AuthenticationException {
return getClient().get(context,
Expand All @@ -40,7 +38,7 @@ public static RequestHandle nearGet(Context context, String url, ResponseHandler
}

private static AsyncHttpClient getClient() {
if (Looper.myLooper() == null){
if (Looper.myLooper() == null) {
return syncHttpClient;
}
return asyncHttpClient;
Expand All @@ -66,7 +64,7 @@ public static RequestHandle nearPut(Context context, String url, String requestB
responseHandler);
}

public RequestHandle nearMock(Context context, String url, NearJsonHttpResponseHandler responseHandlerInterface, int mockResId){
public RequestHandle nearMock(Context context, String url, NearJsonHttpResponseHandler responseHandlerInterface, int mockResId) {
String mockedString = context.getApplicationContext().getResources().getString(mockResId);
JSONObject mockedResponse = null;
try {
Expand All @@ -78,7 +76,7 @@ public RequestHandle nearMock(Context context, String url, NearJsonHttpResponseH
return null;
}

public RequestHandle nearMock(Context context, String url, String requestBody, NearJsonHttpResponseHandler responseHandlerInterface, int mockResId){
public RequestHandle nearMock(Context context, String url, String requestBody, NearJsonHttpResponseHandler responseHandlerInterface, int mockResId) {
return nearMock(context, url, responseHandlerInterface, mockResId);
}

Expand All @@ -91,7 +89,7 @@ private static Header[] getHeaders(Context context) throws AuthenticationExcepti
};
}

private static String buildUrl(Context context, String relativeUrl){
private static String buildUrl(Context context, String relativeUrl) {
String baseUrl = context.getResources().getString(R.string.API_BASE_URL) + relativeUrl;

return baseUrl.replace("%5B", "[").replace("%5D", "]");
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/it/near/sdk/geopolis/GeopolisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* - The background period between scans is 8 seconds.
* - The background length of a scan 1 second.
* - The period to wait before finalizing a region exit.
*
* <p>
* In our current plugin representation:
* - this is a "pulse" plugin
* - the plugin name is: beacon-forest
Expand Down Expand Up @@ -130,17 +130,16 @@ private void registerResetReceiver() {
/**
* Refresh the configuration of the component. The list of beacons to altBeaconMonitor will be downloaded from the APIs.
* If there's an error in refreshing the configuration, a cached version will be used instead.
*
*/
public void refreshConfig(){
public void refreshConfig() {
Uri url = Uri.parse(Constants.API.PLUGINS_ROOT).buildUpon()
.appendPath("geopolis")
.appendPath("nodes")
.appendQueryParameter("filter[app_id]",GlobalConfig.getInstance(mApplication).getAppId())
.appendQueryParameter("include", "**.children")
.build();
try {
httpClient.nearGet(mApplication, url.toString(), new NearJsonHttpResponseHandler(){
httpClient.nearGet(mApplication, url.toString(), new NearJsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.d(TAG, response.toString());
Expand Down Expand Up @@ -168,7 +167,7 @@ public void startRadarOnNodes(List<Node> nodes) {
// altBeaconMonitor.setUpMonitor(nodes);
}

public void startRadar(){
public void startRadar() {
if (isRadarStarted(mApplication)) return;
setRadarState(true);
List<Node> nodes = nodesManager.getNodes();
Expand All @@ -177,7 +176,7 @@ public void startRadar(){
geofenceMonitor.startGFRadar();
}

public void stopRadar(){
public void stopRadar() {
setRadarState(false);
altBeaconMonitor.stopRadar();
geofenceMonitor.stopGFRadar();
Expand All @@ -186,6 +185,7 @@ public void stopRadar(){

/**
* Notify the RECIPES_PATH manager of the occurance of a registered pulse.
*
* @param pulseAction the action of the pulse to notify
* @param pulseBundle the region identifier of the pulse
*/
Expand All @@ -197,19 +197,20 @@ private void firePulse(String pulseAction, String pulseBundle) {

BroadcastReceiver regionEventsReceiver = new BroadcastReceiver() {
public static final String TAG = "RegionEventReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Log.d(this.TAG, "receiverEvent");
Log.d(TAG, "receiverEvent");
if (!intent.hasExtra(NODE_ID)) return;
// trim the package name
String packageName = mApplication.getPackageName();
String action = intent.getAction().replace(packageName + ".", "");
Node node = nodesManager.nodeFromId(intent.getStringExtra(NODE_ID));
if (node == null) return;
switch (action){
switch (action) {
case GF_ENTRY_ACTION_SUFFIX:
trackAndFirePulse(node, Events.ENTER_PLACE);
if (node.getChildren() != null){
if (node.getChildren() != null) {
geofenceMonitor.setUpMonitor(GeoFenceMonitor.geofencesOnEnter(nodesManager.getNodes(), node));
altBeaconMonitor.addRegions(node.getChildren());
}
Expand Down Expand Up @@ -241,11 +242,12 @@ public void onReceive(Context context, Intent intent) {

/**
* Tracks the geographical interaction and fires the proper pulse. It does nothing if the identifier is null.
*
* @param node
* @param event
*/
private void trackAndFirePulse(Node node, String event) {
if (node != null || node.getIdentifier() != null){
if (node != null || node.getIdentifier() != null) {
trackEvent(node.getIdentifier(), event);
firePulse(event, node.getIdentifier());
}
Expand All @@ -256,7 +258,7 @@ private void trackAndFirePulse(Node node, String event) {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "reset intent received");
if (intent.getBooleanExtra(GeoFenceSystemEventsReceiver.LOCATION_STATUS, false)){
if (intent.getBooleanExtra(GeoFenceSystemEventsReceiver.LOCATION_STATUS, false)) {
startRadarOnNodes(nodesManager.getNodes());
} else {
altBeaconMonitor.stopRadar();
Expand All @@ -276,21 +278,22 @@ private void trackEvent(String identifier, String event) {
.appendPath(TRACKING_RES).build();
NearNetworkUtil.sendTrack(mApplication, url.toString(), buildTrackBody(identifier, event));
} catch (JSONException e) {
Log.d(TAG, "Unable to send track: " + e.toString());
Log.d(TAG, "Unable to send track: " + e.toString());
}
}


/**
* Compute the HTTP request body from the region identifier in jsonAPI format.
*
* @param identifier the node identifier
* @param event the event
* @param event the event
* @return the correctly formed body
* @throws JSONException
*/
private String buildTrackBody(String identifier, String event) throws JSONException {
HashMap<String, Object> map = new HashMap<>();
map.put("identifier" , identifier);
map.put("identifier", identifier);
map.put("event", event);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date now = new Date(System.currentTimeMillis());
Expand All @@ -303,19 +306,19 @@ private String buildTrackBody(String identifier, String event) throws JSONExcept
}



/**
* Returns whether the app started the location radar.
*
* @param context the context object
* @return whether the app started the location radar
*/
public static boolean isRadarStarted(Context context){
public static boolean isRadarStarted(Context context) {
String PACK_NAME = context.getApplicationContext().getPackageName();
SharedPreferences sp = context.getSharedPreferences(PACK_NAME + PREFS_SUFFIX, 0);
return sp.getBoolean(RADAR_ON, false);
}

private void setRadarState(boolean b){
private void setRadarState(boolean b) {
sp.edit().putBoolean(RADAR_ON, b).apply();

}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/it/near/sdk/geopolis/NodesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import it.near.sdk.morpheusnear.Morpheus;
import it.near.sdk.utils.NearJsonAPIUtils;

import static com.google.gson.internal.$Gson$Preconditions.checkNotNull;
import static it.near.sdk.utils.NearUtils.checkNotNull;


/**
* Manages the geopolis node structure.
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/it/near/sdk/geopolis/NodesManager2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package it.near.sdk.geopolis;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import it.near.sdk.geopolis.beacons.BeaconNode;
import it.near.sdk.morpheusnear.Morpheus;
import it.near.sdk.utils.NearJsonAPIUtils;

import static it.near.sdk.utils.NearUtils.safe;

public class NodesManager2 {

private List<Node> nodes;

private Morpheus morpheus;
private List<Node> emptyList = Collections.<Node>emptyList();
;

public NodesManager2(Morpheus morpheus) {
this.morpheus = morpheus;
}

public List<Node> setNodes(JSONObject jsonObject) {
nodes = NearJsonAPIUtils.parseList(morpheus, jsonObject, Node.class);
return nodes;
}

public List<Node> getRoots() {
if (nodes != null) {
return nodes;
} else {
return emptyList;
}
}

public Node getNode(String nodeId) {
return nodes != null ? findNodeIn(nodes, nodeId) : null;
}

private Node findNodeIn(List<Node> nodes, String id) {
for (Node node : nodes) {
if (node.getId().equals(id)) {
return node;
} else if (node.getChildren() != null) {
Node foundNode = findNodeIn(node.getChildren(), id);
if (foundNode != null) return foundNode;
}
}
return null;
}

public List<Node> getMonitoredNodesOnEnter(String nodeId) {

if (nodes == null) return emptyList;
Node node = findNodeIn(nodes, nodeId);
if (node == null || BeaconNode.isBeacon(node)) return emptyList;

return getMonitoredNodesOnEnter(node);
}

private List<Node> getMonitoredNodesOnEnter(Node node) {
List<Node> regionsToMonitor = new ArrayList<>();
regionsToMonitor.addAll(getSibilings(node));

for (Node child : safe(node.getChildren())) {
if (!BeaconNode.isBeacon(child))
regionsToMonitor.add(child);
}
return regionsToMonitor;
}

private List<Node> getSibilings(Node node) {
if (node.getParent() != null)
return node.getParent().getChildren();
return nodes;
}

public List<Node> getMonitoredNodesOnExit(String nodeId) {
if (nodes == null) return emptyList;
Node node = findNodeIn(nodes, nodeId);
if (node == null || BeaconNode.isBeacon(node)) return emptyList;
if (node.getParent() == null) return nodes;
return getMonitoredNodesOnEnter(node.getParent());
}


public List<Node> getRangedNodesOnEnter(String nodeId) {
if (nodes == null) return emptyList;
Node node = findNodeIn(nodes, nodeId);
if (node == null || BeaconNode.isBeacon(node) || node.getChildren() == null) return emptyList;
List<Node> beaconsToReturn = new ArrayList<>();
for (Node child : safe(node.getChildren())) {
if (BeaconNode.isBeacon(child))
beaconsToReturn.add(child);
}
return beaconsToReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import it.near.sdk.geopolis.Node;
import it.near.sdk.geopolis.NodesManager;
import it.near.sdk.geopolis.beacons.ranging.BeaconDynamicRadar;
import it.near.sdk.utils.AppLifecycleMonitor;
import it.near.sdk.utils.AppVisibilityDetector;
import it.near.sdk.utils.OnLifecycleEventListener;

Expand Down
Loading

0 comments on commit b8d4de1

Please sign in to comment.