Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add action href for map pins #149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.json.JSONObject;

public class JasonGeoAction {

public static final String COORDS_STRING_FORMAT = "%f,%f";

public void get(final JSONObject action, JSONObject data, final JSONObject event, final Context context) {
try {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Expand All @@ -23,7 +26,7 @@ public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);

JSONObject ret = new JSONObject();
String val = String.format("%f,%f", location.getLatitude(), location.getLongitude());
String val = String.format(COORDS_STRING_FORMAT, location.getLatitude(), location.getLongitude());
ret.put("coord", val);
ret.put("value", val);
JasonHelper.next("success", action, ret, event, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
import org.json.JSONObject;

public class JasonComponent {

public static final String INTENT_ACTION_CALL = "call";
public static final String ACTION_PROP = "action";
public static final String DATA_PROP = "data";
public static final String HREF_PROP = "href";
public static final String TYPE_PROP = "type";
public static final String OPTIONS_PROP = "options";

public static View build(View view, final JSONObject component, final JSONObject parent, final Context root_context) {
int width = 0;
int height = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jasonette.seed.Component;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
Expand All @@ -22,12 +24,26 @@
import com.jasonette.seed.Helper.JasonHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import timber.log.Timber;

import static com.jasonette.seed.Action.JasonGeoAction.COORDS_STRING_FORMAT;


public class JasonMapComponent extends JasonComponent {

public class JasonMapComponent {
static int EQUATOR_LENGTH = 40075004;

private static final String DEFAULT_PIN_ACTION_PROP = "default_action";

private static final String MAP_PIN_TITLE_PROP = "title";
private static final String MAP_PIN_COORD_PROP = "coord";
private static final String MAP_PIN_DESCRIPTION_PROP = "description";

public static final String JS_FALSE = "false";

public static View build(View view, final JSONObject component, final JSONObject parent, final Context context) {
if(view == null){
try {
Expand Down Expand Up @@ -101,6 +117,7 @@ private static LatLng getCoordinates(JSONObject position) {
}

static class MapReadyHandler implements OnMapReadyCallback {

private JSONObject component;
private MapView view;
private Context context;
Expand All @@ -122,7 +139,7 @@ private double getZoomForMetersWide(final double meters, final double width, fin
}

@Override
public void onMapReady(GoogleMap map) {
public void onMapReady(final GoogleMap map) {
try {
// Add pins to the map
if (component.has("pins")) {
Expand All @@ -144,6 +161,9 @@ public void onMapReady(GoogleMap map) {
marker.showInfoWindow();
}
}
if (pin.has(ACTION_PROP)) {
marker.setTag(pin);
}
}
}

Expand All @@ -167,6 +187,42 @@ public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.zoomTo(zoom));
}
}

// Attach listener for pin 'clicks'
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker marker) {
try {
if (marker.getTag() == null) {
return;
}
JSONObject pinJSONObject = (JSONObject) marker.getTag();
if (pinJSONObject.has(ACTION_PROP)) {
JSONObject pinData = new JSONObject().put(MAP_PIN_TITLE_PROP, marker.getTitle());
LatLng pos = marker.getPosition();
pinData.put(MAP_PIN_COORD_PROP, String.format(COORDS_STRING_FORMAT
, pos.latitude, pos.longitude));
pinData.put(MAP_PIN_DESCRIPTION_PROP, marker.getSnippet());
Intent intent = new Intent(INTENT_ACTION_CALL);
intent.putExtra(ACTION_PROP, pinJSONObject.get(ACTION_PROP).toString());
intent.putExtra(DATA_PROP, pinData.toString());
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
else if (pinJSONObject.has(HREF_PROP)) {
Intent intent = new Intent(INTENT_ACTION_CALL);
JSONObject href = new JSONObject();
href.put(TYPE_PROP, "$href");
href.put(OPTIONS_PROP, pinJSONObject.get(HREF_PROP).toString());
intent.putExtra(ACTION_PROP, pinJSONObject.get(ACTION_PROP).toString());
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
} catch (JSONException e) {
Timber.e(e);
}
}
});

} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
Expand Down Expand Up @@ -211,4 +267,4 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
};
}
}
56 changes: 28 additions & 28 deletions app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
Expand All @@ -30,7 +30,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.webkit.CookieManager;
Expand All @@ -43,13 +42,13 @@
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.jasonette.seed.Component.JasonComponentFactory;
import com.jasonette.seed.Component.JasonImageComponent;
Expand Down Expand Up @@ -817,21 +816,7 @@ public void onReceive(Context context, Intent intent) {
String event_string = intent.getStringExtra("event");

// Wrap return value with $jason
JSONObject data;

// Detect if the result is JSONObject, JSONArray, or String
if(data_string.trim().startsWith("[")) {
// JSONArray
JSONArray json = new JSONArray(data_string);
data = new JSONObject().put("$jason", new JSONArray(data_string));
} else if(data_string.trim().startsWith("{")){
// JSONObject
JSONObject json = new JSONObject(data_string);
data = new JSONObject().put("$jason", new JSONObject(data_string));
} else {
// String
data = new JSONObject().put("$jason", data_string);
}
JSONObject data = addToObject("$jason", data_string);

// call next
call(action_string, data.toString(), event_string, JasonViewActivity.this);
Expand All @@ -849,14 +834,7 @@ public void onReceive(Context context, Intent intent) {
String event_string = intent.getStringExtra("event");

// Wrap return value with $jason
JSONObject data;
if(data_string.startsWith("[")) {
JSONArray json = new JSONArray(data_string);
data = new JSONObject().put("$jason", new JSONArray(data_string));
} else {
JSONObject json = new JSONObject(data_string);
data = new JSONObject().put("$jason", new JSONObject(data_string));
}
JSONObject data = addToObject("$jason", data_string);

// call next
call(action_string, data.toString(), event_string, JasonViewActivity.this);
Expand All @@ -865,6 +843,7 @@ public void onReceive(Context context, Intent intent) {
}
}
};

private BroadcastReceiver onCall = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -876,15 +855,36 @@ public void onReceive(Context context, Intent intent) {
data_string = new JSONObject().toString();
}

// Wrap return value with $jason
JSONObject data = addToObject("$jason", data_string);

// call next
call(action_string, data_string, event_string, JasonViewActivity.this);
call(action_string, data.toString(), event_string, JasonViewActivity.this);
} catch (Exception e){
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
}
};


private JSONObject addToObject(String prop, String json_data) {
JSONObject data = new JSONObject();
try {
// Detect if the result is JSONObject, JSONArray, or String
if(json_data.trim().startsWith("[")) {
// JSONArray
data = new JSONObject().put("$jason", new JSONArray(json_data));
} else if(json_data.trim().startsWith("{")){
// JSONObject
data = new JSONObject().put("$jason", new JSONObject(json_data));
} else {
// String
data = new JSONObject().put("$jason", json_data);
}
} catch (Exception e){
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
return data;
}



Expand Down
39 changes: 25 additions & 14 deletions app/src/main/java/com/jasonette/seed/Helper/JasonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.graphics.Typeface;
import android.support.v4.content.LocalBroadcastManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.WindowManager;

Expand All @@ -27,7 +26,10 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import timber.log.Timber;

public class JasonHelper {

public static JSONObject style(JSONObject component, Context root_context) {
JSONObject style = new JSONObject();
try {
Expand All @@ -45,7 +47,7 @@ public static JSONObject style(JSONObject component, Context root_context) {
}
}
} catch (Exception e){
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
}

try {
Expand All @@ -60,7 +62,7 @@ public static JSONObject style(JSONObject component, Context root_context) {
}
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
}
return style;
}
Expand All @@ -76,7 +78,7 @@ public static JSONObject merge(JSONObject old, JSONObject add) {
}
return stub;
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
return new JSONObject();
}
}
Expand All @@ -100,23 +102,33 @@ public static void next(String type, JSONObject action, Object data, final JSONO
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.e(e);
}
}

/**
* Parse a string as either a JSON Object or Array.
*
* @param json
* @return a JSONObject or JSONArray based on the Json string,
* return an emptry JSONObject if json param is null or on parsing supplied json string.
*/
public static Object objectify(String json) {
try {
if (json == null) {
return new JSONObject();
}
if (json.trim().startsWith("[")) {
// JSONArray
return new JSONArray(json);
} else if (json.trim().startsWith("{")) {
return new JSONObject(json);
} else {
return new Object();
return new JSONObject();
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
return new Object();
Timber.w(e, "error objectifying: %s", json);
return new JSONObject();
}
}

Expand All @@ -128,7 +140,7 @@ public static ArrayList<JSONObject> toArrayList(JSONArray jsonArray) {
list.add(jsonArray.getJSONObject(i));
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
}
return list;
}
Expand Down Expand Up @@ -265,7 +277,7 @@ public static Object read_json(String fn, Context context) {// throws IOExceptio
ret = jr;
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
return new JSONObject();
}
return ret;
Expand All @@ -284,7 +296,7 @@ public static void permission_exception(String actionName, Context context) {
intent.putExtra("action", alert_action.toString());
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
}
}

Expand Down Expand Up @@ -342,7 +354,7 @@ public static void dispatchIntent(String name, JSONObject action, JSONObject dat

((Launcher) ((JasonViewActivity) context).getApplicationContext()).once(name, handler);
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.w(e);
}

if (intent != null) {
Expand Down Expand Up @@ -373,8 +385,7 @@ public static JSONObject preserve(JSONObject callback, JSONObject action, JSONOb
callback.put("options", callback_options);
return callback;
} catch (Exception e) {
Log.d("Error", "wasn't able to preserve stack");
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Timber.e(e, "wasn't able to preserve stack for action: %s", action);
return callback;
}
}
Expand Down