From 1d8868987dcca322e8b4a1cdfb2c58497ba3a567 Mon Sep 17 00:00:00 2001 From: Maksim Lin Date: Thu, 27 Jul 2017 12:16:14 +1000 Subject: [PATCH 1/4] Improve error logging in Core classes --- .../com/jasonette/seed/Core/JasonModel.java | 28 +++++++++---------- .../com/jasonette/seed/Core/JasonParser.java | 6 ++-- .../com/jasonette/seed/Core/JasonRequire.java | 7 +++-- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonModel.java b/app/src/main/java/com/jasonette/seed/Core/JasonModel.java index aef1ea89..d0db3295 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonModel.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonModel.java @@ -3,7 +3,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; -import android.util.Log; import com.jasonette.seed.Helper.JasonHelper; import com.jasonette.seed.Launcher.Launcher; @@ -25,6 +24,7 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import timber.log.Timber; public class JasonModel{ public String url; @@ -56,7 +56,7 @@ public JasonModel(String url, Intent intent, JasonViewActivity view){ try { this.params = new JSONObject(intent.getStringExtra("params")); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -71,7 +71,7 @@ public JasonModel(String url, Intent intent, JasonViewActivity view){ try { this.cache = new JSONObject(str); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -86,7 +86,7 @@ public JasonModel(String url, Intent intent, JasonViewActivity view){ this.session = new JSONObject(str); } } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); }; try { @@ -94,7 +94,7 @@ public JasonModel(String url, Intent intent, JasonViewActivity view){ v.put("url", this.url); ((Launcher)(this.view.getApplicationContext())).setEnv("view", v); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); }; } @@ -125,7 +125,7 @@ public void run() t.start(); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -184,7 +184,7 @@ public void onResponse(Call call, final Response response) throws IOException { } }); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -215,7 +215,7 @@ private void include(String res){ try { latch.await(); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -255,7 +255,7 @@ private void resolve_and_build(String res){ } } } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -289,13 +289,13 @@ public void onFinished(JSONObject resolved_jason) { try { resolve_and_build(resolved_jason.toString()); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } }); JasonParser.getInstance(this.view).parse("json", refs, to_resolve, this.view); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -323,13 +323,13 @@ public void onFinished(JSONObject resolved_jason) { try { resolve_and_build(resolved_jason.toString()); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } }); JasonParser.getInstance(this.view).parse("json", refs, to_resolve, this.view); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -363,7 +363,7 @@ public void set(String name, JSONObject data){ state.put("$env", ((Launcher)(this.view.getApplicationContext())).getEnv()); state.put("$params", params); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } else { diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonParser.java b/app/src/main/java/com/jasonette/seed/Core/JasonParser.java index 63dc5172..bd502edd 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonParser.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonParser.java @@ -42,7 +42,7 @@ public static JasonParser getInstance(Context context){ instance.juice.executeVoidScript(js); instance.juice.getLocker().release(); } catch (Exception e){ - Timber.w(e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } return instance; @@ -93,7 +93,7 @@ public void parse(final String data_type, final JSONObject data, final Object te listener.onFinished(res); } catch (Exception e){ - Timber.w(e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } // thread handling - release handle @@ -101,7 +101,7 @@ public void parse(final String data_type, final JSONObject data, final Object te } }).start(); } catch (Exception e){ - Timber.w(e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } } diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java b/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java index a8628915..89cd959d 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java @@ -21,6 +21,7 @@ import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Response; +import timber.log.Timber; public class JasonRequire implements Runnable{ final String URL; @@ -63,7 +64,7 @@ public void run() Thread t = new Thread(r); t.start(); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); latch.countDown(); } } @@ -140,13 +141,13 @@ public void onResponse(Call call, final Response response) throws IOException { } latch.countDown(); } catch (JSONException e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } }); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } } From 468b2e4b3529aae2ad3066ba4465f8181d089f90 Mon Sep 17 00:00:00 2001 From: Maksim Lin Date: Thu, 27 Jul 2017 13:02:26 +1000 Subject: [PATCH 2/4] Imrove error logging in Component classes --- .../jasonette/seed/Component/JasonComponent.java | 9 ++++----- .../seed/Component/JasonComponentFactory.java | 5 +++-- .../jasonette/seed/Component/JasonMapComponent.java | 13 +++++++------ .../com/jasonette/seed/Section/ItemAdapter.java | 7 ++++--- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/jasonette/seed/Component/JasonComponent.java b/app/src/main/java/com/jasonette/seed/Component/JasonComponent.java index d70cec2e..3a2bf6fd 100644 --- a/app/src/main/java/com/jasonette/seed/Component/JasonComponent.java +++ b/app/src/main/java/com/jasonette/seed/Component/JasonComponent.java @@ -2,20 +2,19 @@ import android.content.Context; import android.graphics.drawable.GradientDrawable; -import android.os.Build; import android.support.v4.content.ContextCompat; -import android.util.Log; import android.view.View; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.jasonette.seed.Core.JasonViewActivity; import com.jasonette.seed.Helper.JasonHelper; -import com.jasonette.seed.R; import com.jasonette.seed.Section.JasonLayout; import org.json.JSONObject; +import timber.log.Timber; + public class JasonComponent { public static View build(View view, final JSONObject component, final JSONObject parent, final Context root_context) { float width = 0; @@ -150,7 +149,7 @@ public static View build(View view, final JSONObject component, final JSONObject return view; } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); return new View(root_context); } } @@ -181,7 +180,7 @@ public void onClick(View v) { } } } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } }; diff --git a/app/src/main/java/com/jasonette/seed/Component/JasonComponentFactory.java b/app/src/main/java/com/jasonette/seed/Component/JasonComponentFactory.java index 73546298..2992fae0 100644 --- a/app/src/main/java/com/jasonette/seed/Component/JasonComponentFactory.java +++ b/app/src/main/java/com/jasonette/seed/Component/JasonComponentFactory.java @@ -1,7 +1,6 @@ package com.jasonette.seed.Component; import android.content.Context; -import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; @@ -9,6 +8,8 @@ import java.util.HashMap; import java.util.Map; +import timber.log.Timber; + public class JasonComponentFactory { Map signature_to_type = new HashMap(); public static View build(View prototype, final JSONObject component, final JSONObject parent, final Context context) { @@ -47,7 +48,7 @@ public static View build(View prototype, final JSONObject component, final JSONO } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } return new View(context); diff --git a/app/src/main/java/com/jasonette/seed/Component/JasonMapComponent.java b/app/src/main/java/com/jasonette/seed/Component/JasonMapComponent.java index c57885a6..5b87ea7c 100644 --- a/app/src/main/java/com/jasonette/seed/Component/JasonMapComponent.java +++ b/app/src/main/java/com/jasonette/seed/Component/JasonMapComponent.java @@ -3,7 +3,6 @@ import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.DisplayMetrics; -import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; @@ -24,6 +23,8 @@ import org.json.JSONArray; import org.json.JSONObject; +import timber.log.Timber; + public class JasonMapComponent { static int EQUATOR_LENGTH = 40075004; @@ -64,7 +65,7 @@ public static View build(View view, final JSONObject component, final JSONObject mapview.getMapAsync(new MapReadyHandler(component, mapview, context)); return mapview; } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } else { try { @@ -78,7 +79,7 @@ public static View build(View view, final JSONObject component, final JSONObject ((MapView)view).onResume(); // Trigger onResume return view; } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } return new View(context); @@ -95,7 +96,7 @@ private static LatLng getCoordinates(JSONObject position) { longitude = Double.parseDouble(r[1]); } } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } return new LatLng(latitude, longitude); } @@ -168,7 +169,7 @@ public void onMapReady(GoogleMap map) { } } } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } } @@ -211,4 +212,4 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return false; } }; -} \ No newline at end of file +} diff --git a/app/src/main/java/com/jasonette/seed/Section/ItemAdapter.java b/app/src/main/java/com/jasonette/seed/Section/ItemAdapter.java index 1237141b..b90bbbe9 100644 --- a/app/src/main/java/com/jasonette/seed/Section/ItemAdapter.java +++ b/app/src/main/java/com/jasonette/seed/Section/ItemAdapter.java @@ -3,7 +3,6 @@ import android.content.Context; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -18,6 +17,8 @@ import java.util.HashMap; import java.util.Map; +import timber.log.Timber; + /******************************************************** * @@ -570,10 +571,10 @@ private void add_spacing(View view, JSONObject item, String type) { } view.requestLayout(); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } } -} \ No newline at end of file +} From 6a8ad4cee6f1eafaeb11968805b026f510facbee Mon Sep 17 00:00:00 2001 From: Maksim Lin Date: Thu, 27 Jul 2017 17:16:59 +1000 Subject: [PATCH 3/4] Improve error logging in JasonImageHelper --- .../com/jasonette/seed/Helper/JasonImageHelper.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/jasonette/seed/Helper/JasonImageHelper.java b/app/src/main/java/com/jasonette/seed/Helper/JasonImageHelper.java index 7ae43100..04c68bf7 100644 --- a/app/src/main/java/com/jasonette/seed/Helper/JasonImageHelper.java +++ b/app/src/main/java/com/jasonette/seed/Helper/JasonImageHelper.java @@ -5,7 +5,6 @@ import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; -import android.util.Log; import com.bumptech.glide.Glide; import com.bumptech.glide.load.model.GlideUrl; @@ -18,10 +17,11 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.net.URI; import java.util.Iterator; +import timber.log.Timber; + public class JasonImageHelper { public interface JasonImageDownloadListener { public void onLoaded(byte[] data, Uri uri); @@ -56,7 +56,7 @@ public void load(){ Uri bitmapUri = Uri.fromFile(file); this.listener.onLoaded(this.data, bitmapUri); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -121,7 +121,7 @@ public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { listener.onLoaded(byteArray, bitmapUri); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } @@ -129,7 +129,7 @@ public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { } } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } } } From 45a79e1ae6522b0ed7ef03247dec08283bf1e7cb Mon Sep 17 00:00:00 2001 From: Maksim Lin Date: Fri, 28 Jul 2017 12:57:43 +1000 Subject: [PATCH 4/4] Implement relative urls in jasonette This will now resolve url values prefixed with a forward-slash as relative to the url of the jasonette json they are included in. This works for urls used for mixins as well as images. --- .../seed/Action/JasonCacheAction.java | 6 +-- .../seed/Action/JasonNetworkAction.java | 20 +++------ .../seed/Component/JasonImageComponent.java | 4 +- .../com/jasonette/seed/Core/JasonRequire.java | 25 ++++++++--- .../seed/Core/JasonViewActivity.java | 44 +++++++++---------- .../jasonette/seed/Helper/JasonHelper.java | 44 ++++++++++++++----- 6 files changed, 83 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/com/jasonette/seed/Action/JasonCacheAction.java b/app/src/main/java/com/jasonette/seed/Action/JasonCacheAction.java index bb05ede6..1f61870f 100644 --- a/app/src/main/java/com/jasonette/seed/Action/JasonCacheAction.java +++ b/app/src/main/java/com/jasonette/seed/Action/JasonCacheAction.java @@ -17,12 +17,12 @@ public void set(final JSONObject action, final JSONObject data, final JSONObject // Merge with the new input JSONObject options = action.getJSONObject("options"); - JSONObject old_cache = new JSONObject(pref.getString(activity.url, "{}")); + JSONObject old_cache = new JSONObject(pref.getString(activity.getUrl(), "{}")); JSONObject new_cache = JasonHelper.merge(old_cache, options); // Update SharedPreferences String stringified_cache = new_cache.toString(); - editor.putString(activity.url, stringified_cache); + editor.putString(activity.getUrl(), stringified_cache); editor.commit(); // Update model @@ -43,7 +43,7 @@ public void reset(final JSONObject action, final JSONObject data, final JSONObje JasonViewActivity activity = (JasonViewActivity) context; SharedPreferences pref = context.getSharedPreferences("cache", 0); SharedPreferences.Editor editor = pref.edit(); - editor.remove(activity.url); + editor.remove(activity.getUrl()); editor.commit(); // Update model diff --git a/app/src/main/java/com/jasonette/seed/Action/JasonNetworkAction.java b/app/src/main/java/com/jasonette/seed/Action/JasonNetworkAction.java index 28f9b4a5..06afda6f 100644 --- a/app/src/main/java/com/jasonette/seed/Action/JasonNetworkAction.java +++ b/app/src/main/java/com/jasonette/seed/Action/JasonNetworkAction.java @@ -1,19 +1,17 @@ package com.jasonette.seed.Action; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.util.Base64; import android.util.Log; -import com.jasonette.seed.Core.JasonViewActivity; import com.jasonette.seed.Helper.JasonHelper; -import com.jasonette.seed.Launcher.Launcher; import org.json.JSONObject; + import java.io.IOException; -import java.net.URI; +import java.net.URL; import java.util.Iterator; import java.util.UUID; @@ -31,7 +29,7 @@ private void _request(final JSONObject callback, final JSONObject action, final try{ final JSONObject options = action.getJSONObject("options"); if(options.has("url")){ - String url = options.getString("url"); + URL url = JasonHelper.resolveUrl(options.getString("url"), context); // method String method = "GET"; @@ -43,8 +41,7 @@ private void _request(final JSONObject callback, final JSONObject action, final SharedPreferences pref = context.getSharedPreferences("session", 0); JSONObject session = null; - URI uri_for_session = new URI(url.toLowerCase()); - String session_domain = uri_for_session.getHost(); + String session_domain = url.getHost(); if(pref.contains(session_domain)){ String str = pref.getString(session_domain, null); @@ -80,7 +77,7 @@ private void _request(final JSONObject callback, final JSONObject action, final } if(method.equalsIgnoreCase("get")) { - Uri.Builder b = Uri.parse(url).buildUpon(); + Uri.Builder b = Uri.parse(url.toString()).buildUpon(); // Attach Params from Session if(session != null && session.has("body")) { @@ -100,7 +97,7 @@ private void _request(final JSONObject callback, final JSONObject action, final Iterator keysIterator = d.keys(); try { while (keysIterator.hasNext()) { - String key = (String) keysIterator.next(); + String key = keysIterator.next(); String val = d.getString(key); b.appendQueryParameter(key, val); } @@ -109,9 +106,6 @@ private void _request(final JSONObject callback, final JSONObject action, final } } - Uri uri = b.build(); - url = uri.toString(); - request = builder .url(url) .build(); @@ -133,7 +127,7 @@ private void _request(final JSONObject callback, final JSONObject action, final Iterator keysIterator = d.keys(); try { while (keysIterator.hasNext()) { - String key = (String) keysIterator.next(); + String key = keysIterator.next(); String val = d.getString(key); bodyBuilder.add(key, val); } diff --git a/app/src/main/java/com/jasonette/seed/Component/JasonImageComponent.java b/app/src/main/java/com/jasonette/seed/Component/JasonImageComponent.java index 46d6d476..503d28d6 100644 --- a/app/src/main/java/com/jasonette/seed/Component/JasonImageComponent.java +++ b/app/src/main/java/com/jasonette/seed/Component/JasonImageComponent.java @@ -18,11 +18,11 @@ import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.load.model.LazyHeaders; import com.bumptech.glide.load.resource.drawable.GlideDrawable; -import com.bumptech.glide.load.resource.gif.GifDrawable; import com.bumptech.glide.request.animation.GlideAnimation; import com.bumptech.glide.request.target.BitmapImageViewTarget; import com.bumptech.glide.request.target.SimpleTarget; import com.jasonette.seed.Helper.JasonHelper; + import org.json.JSONObject; import java.net.URI; @@ -79,7 +79,7 @@ public static Object resolve_url(JSONObject component, Context context){ return url; } else { LazyHeaders.Builder builder = JasonImageComponent.prepare(component, context); - return new GlideUrl(url, builder.build()); + return new GlideUrl(JasonHelper.resolveUrl(url, context), builder.build()); } } catch (Exception e){ Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java b/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java index 89cd959d..9f941d7e 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonRequire.java @@ -3,7 +3,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.net.Uri; -import android.util.Log; import com.jasonette.seed.Helper.JasonHelper; @@ -12,14 +11,15 @@ import org.json.JSONObject; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URI; import java.util.Iterator; import java.util.concurrent.CountDownLatch; -import okhttp3.Request; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; +import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -28,11 +28,22 @@ public class JasonRequire implements Runnable{ final CountDownLatch latch; final Context context; final OkHttpClient client; + private final String originalURL; JSONObject private_refs; public JasonRequire(String url, CountDownLatch latch, JSONObject refs, OkHttpClient client, Context context) { - this.URL = url.replace("\\", ""); + String resolvedUrl = ""; + try { + resolvedUrl = JasonHelper.resolveUrl(url.replace("\\", ""), context).toString(); + } catch (MalformedURLException e) { + Timber.e(e); + } + // keep the original Url in case its a relative, as we will need to use it and not the + // resolved url (from which we actually GET) as the key to add the downloaded contents + // from that url into the 'ref' JSONObject method param we are passed in + this.originalURL = url; + this.URL = resolvedUrl; this.latch = latch; this.private_refs = refs; this.context = context; @@ -56,7 +67,7 @@ public void run() try { private_refs.put(URL, json); } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); } latch.countDown(); } @@ -131,13 +142,13 @@ public void onResponse(Call call, final Response response) throws IOException { // store the res under if(res.trim().startsWith("[")) { // array - private_refs.put(URL, new JSONArray(res)); + private_refs.put(originalURL, new JSONArray(res)); } else if(res.trim().startsWith("{")){ // object - private_refs.put(URL, new JSONObject(res)); + private_refs.put(originalURL, new JSONObject(res)); } else { // string - private_refs.put(URL, res); + private_refs.put(originalURL, res); } latch.countDown(); } catch (JSONException e) { diff --git a/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java b/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java index 2f42b280..2cb6fb6b 100644 --- a/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java +++ b/app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java @@ -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; @@ -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; @@ -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; @@ -76,12 +75,14 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import timber.log.Timber; + import static com.bumptech.glide.Glide.with; public class JasonViewActivity extends AppCompatActivity { private Toolbar toolbar; private RecyclerView listView; - public String url; + protected String url; public JasonModel model; private ProgressBar loading; @@ -312,7 +313,6 @@ protected void onPause() { } catch (Exception e) { Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); } - super.onPause(); } @@ -382,13 +382,11 @@ protected void onResume() { } catch (Exception e) { Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); } - super.onResume(); if (listState != null) { listView.getLayoutManager().onRestoreInstanceState(listState); } - } @@ -746,10 +744,9 @@ private void exec(final JSONObject action, final JSONObject data, final JSONObje resolved_classname = jrjson.getString("classname"); } } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.e(e); } - if(resolved_classname != null) { fileName = "com.jasonette.seed.Action." + resolved_classname; } else { @@ -777,14 +774,10 @@ private void exec(final JSONObject action, final JSONObject data, final JSONObje model.action = action; method.invoke(module, action, model.state, event, context); } - - } } catch (Exception e){ - // Action doesn't exist yet - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.d("Action doesn't exist yet"); try { - JSONObject alert_action = new JSONObject(); alert_action.put("type", "$util.banner"); @@ -795,11 +788,9 @@ private void exec(final JSONObject action, final JSONObject data, final JSONObje alert_action.put("options", options); - call(alert_action.toString(), new JSONObject().toString(), "{}", JasonViewActivity.this); - } catch (Exception e2){ - Log.d("Warning", e2.getStackTrace()[0].getMethodName() + " : " + e2.toString()); + Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); } } } @@ -1160,7 +1151,7 @@ public void require(final JSONObject action, JSONObject data, final JSONObject e JSONArray ret = new JSONArray(); for (int i = 0; i < ((JSONArray)val).length(); i++) { String url = ((JSONArray) val).getString(i); - ret.put(refs.get(url)); + ret.put(JasonHelper.resolveUrl(refs.get(url).toString(), this).toString()); } res.put(key, ret); } else if(val instanceof String){ @@ -1219,7 +1210,7 @@ public void onFinished(JSONObject body) { JasonParser.getInstance(this).parse(type, data, template, context); } catch (Exception e){ - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); JasonHelper.next("error", action, new JSONObject(), event, context); } } @@ -1248,6 +1239,9 @@ public void href(final JSONObject action, JSONObject data, JSONObject event, Con // "view": "web" if (action.getJSONObject("options").has("view")) { String view_type = action.getJSONObject("options").getString("view"); + + url = !view_type.equalsIgnoreCase("app") ? JasonHelper.resolveUrl(url, this).toString() : url; + if(view_type.equalsIgnoreCase("web") || view_type.equalsIgnoreCase("app")){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); @@ -1832,7 +1826,7 @@ private void setup_input(JSONObject input){ } style.put("height", "25"); if(json.has("image")) { - json.put("url", json.getString("image")); + json.put("url", JasonHelper.resolveUrl(json.getString("image"), this).toString()); } json.put("type", "button"); json.put("style", style); @@ -1877,7 +1871,7 @@ private void setup_input(JSONObject input){ json.put("text", "Send"); } if(json.has("image")) { - json.put("url", json.getString("image")); + json.put("url", JasonHelper.resolveUrl(json.getString("image"), this).toString()); } style.put("height", "25"); @@ -2060,7 +2054,7 @@ public boolean onTabSelected(int position, boolean wasSelected) { call(item.get("action").toString(), "{}", "{}", JasonViewActivity.this); return false; } else if(item.has("url")) { - String url = item.getString("url"); + String url = JasonHelper.resolveUrl(item.getString("url"), JasonViewActivity.this).toString(); JSONObject action = new JSONObject(); JSONObject options = new JSONObject(); options.put("url", url); @@ -2145,7 +2139,7 @@ public boolean onPrepareOptionsMenu(Menu menu) { try { menu = toolbar.getMenu(); if (model.rendered != null) { - JSONObject header = model.rendered.getJSONObject("header"); + JSONObject header = model.rendered.optJSONObject("header"); header_height = toolbar.getHeight(); setup_title(header); @@ -2469,4 +2463,8 @@ public void removeListViewOnItemTouchListeners() { listViewOnItemTouchListeners.remove(listener); } } + + public String getUrl() { + return url; + } } diff --git a/app/src/main/java/com/jasonette/seed/Helper/JasonHelper.java b/app/src/main/java/com/jasonette/seed/Helper/JasonHelper.java index 113a2293..1a2b27cd 100644 --- a/app/src/main/java/com/jasonette/seed/Helper/JasonHelper.java +++ b/app/src/main/java/com/jasonette/seed/Helper/JasonHelper.java @@ -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; @@ -22,11 +21,15 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Iterator; 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(); @@ -45,7 +48,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 { @@ -60,7 +63,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; } @@ -76,7 +79,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(); } } @@ -100,7 +103,7 @@ 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.w(e); } } @@ -115,7 +118,7 @@ public static Object objectify(String json) { return new Object(); } } catch (Exception e) { - Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString()); + Timber.w(e); return new Object(); } } @@ -128,7 +131,7 @@ public static ArrayList 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; } @@ -278,7 +281,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; @@ -297,7 +300,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); } } @@ -355,7 +358,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) { @@ -386,10 +389,27 @@ 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, "Error", "wasn't able to preserve stack"); return callback; } } + /** + * Resolve Urls that maybe relative to a base, root url as provided by the context. + * + * @param url + * @param context + * @return + * @throws MalformedURLException + */ + public static URL resolveUrl(String url, Context context) throws MalformedURLException { + // assume relative + if (url.startsWith("/")) { + URL resolvedUrl = new URL(new URL(((JasonViewActivity)context).getUrl()), url); + return resolvedUrl; + } + // Validate the URL + return new URL(url); + } + }