From e0a9bc09b7bf01594092539cec15978eeba450ec Mon Sep 17 00:00:00 2001 From: Fco Pardo Date: Sat, 5 Aug 2017 01:49:40 -0400 Subject: [PATCH 1/4] added plugin for artifact publishing --- build.gradle | 3 +++ gradle/wrapper/gradle-wrapper.properties | 4 ++-- json2view/build.gradle | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index ea98e44..ec1e3bb 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,12 @@ buildscript { repositories { jcenter() + mavenCentral() + mavenLocal() } dependencies { classpath 'com.android.tools.build:gradle:2.3.2' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 919ed0b..81950ec 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 06 01:23:45 PDT 2016 +#Sat Aug 05 01:28:17 CLT 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/json2view/build.gradle b/json2view/build.gradle index 82be098..63d5874 100644 --- a/json2view/build.gradle +++ b/json2view/build.gradle @@ -1,4 +1,8 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' + +version '1.0' +group 'com.avocarrot.json2view' android { compileSdkVersion 25 From e2526cf83c2d7e59054b92f186b70aa900e95954 Mon Sep 17 00:00:00 2001 From: Fco Pardo Date: Sun, 13 Aug 2017 22:30:44 -0300 Subject: [PATCH 2/4] Added dynamic parsing method --- build.gradle | 1 + json2view/build.gradle | 3 +- .../com/avocarrot/json2view/DynamicView.java | 49 + .../avocarrot/json2view/ParserExecutor.java | 56 + projectFilesBackup/.idea/workspace.xml | 3922 +++++++++++++++++ sample/build.gradle | 10 +- .../json2view/sample/MainActivity.java | 79 +- 7 files changed, 4109 insertions(+), 11 deletions(-) create mode 100644 json2view/src/main/java/com/avocarrot/json2view/ParserExecutor.java create mode 100644 projectFilesBackup/.idea/workspace.xml diff --git a/build.gradle b/build.gradle index ec1e3bb..47b4a94 100644 --- a/build.gradle +++ b/build.gradle @@ -18,5 +18,6 @@ buildscript { allprojects { repositories { jcenter() + mavenLocal() } } diff --git a/json2view/build.gradle b/json2view/build.gradle index 63d5874..f4d6c99 100644 --- a/json2view/build.gradle +++ b/json2view/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' version '1.0' -group 'com.avocarrot.json2view' +group 'com.avocarrot' android { compileSdkVersion 25 @@ -28,4 +28,5 @@ android { dependencies { androidTestCompile 'junit:junit:4.12' compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:support-annotations:25.3.1' } diff --git a/json2view/src/main/java/com/avocarrot/json2view/DynamicView.java b/json2view/src/main/java/com/avocarrot/json2view/DynamicView.java index 46b5231..ee0d880 100644 --- a/json2view/src/main/java/com/avocarrot/json2view/DynamicView.java +++ b/json2view/src/main/java/com/avocarrot/json2view/DynamicView.java @@ -1,6 +1,7 @@ package com.avocarrot.json2view; import android.content.Context; +import android.support.annotation.UiThread; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; @@ -20,6 +21,38 @@ */ public class DynamicView { + public interface OnJsonParsedAsView{ + void onBackgroundChanges(View parsedView); + void onSuccess(View parsedView); + void onFailure(); + } + + static class AsynchronousParser implements Runnable{ + public Context context; + public JSONObject jsonObject; + public Class viewHolderClass; + public OnJsonParsedAsView onJsonParsedAsView; + View sampleView; + + @Override + public void run() { + sampleView = DynamicView.createView(context, jsonObject, viewHolderClass); + if(onJsonParsedAsView != null) onJsonParsedAsView.onBackgroundChanges(sampleView); + finished(); + } + + @UiThread + private void finished(){ + if(onJsonParsedAsView != null){ + if(sampleView != null){ + onJsonParsedAsView.onSuccess(sampleView); + }else{ + onJsonParsedAsView.onFailure(); + } + } + } + } + static int mCurrentId = 13; static int INTERNAL_TAG_ID = 0x7f020000; @@ -32,6 +65,22 @@ public static View createView (Context context, JSONObject jsonObject, Class hol return createView(context, jsonObject, null, holderClass); } + /** + * @param jsonObject : json object + * @param holderClass : class that will be created as an holder and attached as a tag in the View + * @return the view that created + */ + public static void createViewAsync (Context context, JSONObject jsonObject, Class holderClass, OnJsonParsedAsView onJsonParsedAsView) { + + AsynchronousParser parser = new AsynchronousParser(); + parser.context = context; + parser.jsonObject = jsonObject; + parser.viewHolderClass = holderClass; + parser.onJsonParsedAsView = onJsonParsedAsView; + + ParserExecutor.getExecutor().execute(parser); + } + /** * @param jsonObject : json object * @param parent : parent viewGroup diff --git a/json2view/src/main/java/com/avocarrot/json2view/ParserExecutor.java b/json2view/src/main/java/com/avocarrot/json2view/ParserExecutor.java new file mode 100644 index 0000000..b4ea857 --- /dev/null +++ b/json2view/src/main/java/com/avocarrot/json2view/ParserExecutor.java @@ -0,0 +1,56 @@ +package com.avocarrot.json2view; + +import android.content.Context; +import android.support.annotation.UiThread; +import android.view.View; + +import org.json.JSONObject; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Created by fco on 13-08-17. + */ + +public class ParserExecutor { + + class AsynchronousParser implements Runnable{ + public Context context; + public JSONObject jsonObject; + public Class viewHolderClass; + public DynamicView.OnJsonParsedAsView onJsonParsedAsView; + View sampleView; + + @Override + public void run() { + sampleView = DynamicView.createView(context, jsonObject, viewHolderClass); + if(onJsonParsedAsView != null) onJsonParsedAsView.onBackgroundChanges(sampleView); + finished(); + } + + @UiThread + private void finished(){ + if(onJsonParsedAsView != null){ + if(sampleView != null){ + onJsonParsedAsView.onSuccess(sampleView); + }else{ + onJsonParsedAsView.onFailure(); + } + } + } + } + + private static BlockingQueue workQueue = new LinkedBlockingQueue<>(); + private static ThreadPoolExecutor executor; + + public static ThreadPoolExecutor getExecutor(){ + if(executor == null){ + int processors = 1; + executor = new ThreadPoolExecutor(processors, processors, 1, TimeUnit.SECONDS, workQueue); + } + return executor; + } +} diff --git a/projectFilesBackup/.idea/workspace.xml b/projectFilesBackup/.idea/workspace.xml new file mode 100644 index 0000000..115f60f --- /dev/null +++ b/projectFilesBackup/.idea/workspace.xml @@ -0,0 +1,3922 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1501910835797 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Android + + + + + + + + + + + + + + + 1.8 + + + + + + + + json2view + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/build.gradle b/sample/build.gradle index 8bd972d..c13a4d6 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -1,3 +1,8 @@ +repositories { + jcenter() + mavenLocal() +} + apply plugin: 'com.android.application' android { @@ -6,7 +11,7 @@ android { defaultConfig { applicationId "com.avocarrot.json2view" - minSdkVersion 9 + minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" @@ -27,5 +32,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.3.1' - compile project(':json2view') + //compile project(':json2view') + compile 'com.avocarrot:json2view:1.0@aar' } diff --git a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java index b5230db..1a725be 100644 --- a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java +++ b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java @@ -3,12 +3,16 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.RequiresApi; +import android.support.annotation.UiThread; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; -import android.widget.Toast; +import android.widget.LinearLayout; import com.avocarrot.json2view.DynamicView; import com.avocarrot.json2view.DynamicViewId; @@ -22,6 +26,7 @@ public class MainActivity extends ActionBarActivity implements View.OnClickListener { + @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -39,14 +44,23 @@ protected void onCreate(Bundle savedInstanceState) { if (jsonObject != null) { - /* create dynamic view and return the view with the holder class attached as tag */ - View sampleView = DynamicView.createView(this, jsonObject, SampleViewHolder.class); - /* get the view with id "testClick" and attach the onClickListener */ - ((SampleViewHolder) sampleView.getTag()).clickableView.setOnClickListener(this); + DynamicView.createViewAsync(this, jsonObject, SampleViewHolder.class, new DynamicView.OnJsonParsedAsView() { + @Override + public void onBackgroundChanges(View view) { + ((SampleViewHolder) view.getTag()).clickableView.setOnClickListener(MainActivity.this); + view.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); + } - /* add Layout Parameters in just created view and set as the contentView of the activity */ - sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); - setContentView(sampleView); + @Override + public void onSuccess(View view) { + setContentView(view); + } + + @Override + public void onFailure() { + Log.e("Json2View", "View parsing was no possible"); + } + }); } else { Log.e("Json2View", "Could not load valid json file"); @@ -100,4 +114,53 @@ public SampleViewHolder() { } } + class exa implements Runnable{ + public Context context; + public JSONObject jsonObject; + public View.OnClickListener clickListener; + View sampleView; + LinearLayout linearLayout; + + @Override + public void run() { + linearLayout = new LinearLayout(context); + sampleView = DynamicView.createView(context, jsonObject, SampleViewHolder.class); + sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); + linearLayout.addView(sampleView); + finished(); + } + + @UiThread + private void finished(){ + ((SampleViewHolder) sampleView.getTag()).clickableView.setOnClickListener(clickListener); + /* add Layout Parameters in just created view and set as the contentView of the activity */ + //sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); + setContentView(linearLayout); + } + } + + class exa2 extends AsyncTask{ + public Context context; + public JSONObject jsonObject; + public View.OnClickListener clickListener; + View sampleView; + + @Override + protected Boolean doInBackground(Void... params) { + /* create dynamic view and return the view with the holder class attached as tag */ + //LinearLayout linearLayout = new LinearLayout(context); + sampleView = DynamicView.createView(context, jsonObject, SampleViewHolder.class); + /* get the view with id "testClick" and attach the onClickListener */ + return true; + } + + @Override + protected void onPostExecute(Boolean result) { + ((SampleViewHolder) sampleView.getTag()).clickableView.setOnClickListener(clickListener); + /* add Layout Parameters in just created view and set as the contentView of the activity */ + sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); + setContentView(sampleView); + } + } + } From a2d2564fdf9eb07bb16b50aa7990a39e89cadc7d Mon Sep 17 00:00:00 2001 From: Fco Pardo Date: Sun, 13 Aug 2017 22:31:18 -0300 Subject: [PATCH 3/4] clean up --- .../json2view/sample/MainActivity.java | 50 ------------------- 1 file changed, 50 deletions(-) diff --git a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java index 1a725be..8fee624 100644 --- a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java +++ b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java @@ -113,54 +113,4 @@ static public class SampleViewHolder { public SampleViewHolder() { } } - - class exa implements Runnable{ - public Context context; - public JSONObject jsonObject; - public View.OnClickListener clickListener; - View sampleView; - LinearLayout linearLayout; - - @Override - public void run() { - linearLayout = new LinearLayout(context); - sampleView = DynamicView.createView(context, jsonObject, SampleViewHolder.class); - sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); - linearLayout.addView(sampleView); - finished(); - } - - @UiThread - private void finished(){ - ((SampleViewHolder) sampleView.getTag()).clickableView.setOnClickListener(clickListener); - /* add Layout Parameters in just created view and set as the contentView of the activity */ - //sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); - setContentView(linearLayout); - } - } - - class exa2 extends AsyncTask{ - public Context context; - public JSONObject jsonObject; - public View.OnClickListener clickListener; - View sampleView; - - @Override - protected Boolean doInBackground(Void... params) { - /* create dynamic view and return the view with the holder class attached as tag */ - //LinearLayout linearLayout = new LinearLayout(context); - sampleView = DynamicView.createView(context, jsonObject, SampleViewHolder.class); - /* get the view with id "testClick" and attach the onClickListener */ - return true; - } - - @Override - protected void onPostExecute(Boolean result) { - ((SampleViewHolder) sampleView.getTag()).clickableView.setOnClickListener(clickListener); - /* add Layout Parameters in just created view and set as the contentView of the activity */ - sampleView.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)); - setContentView(sampleView); - } - } - } From a90d45adf794445bb1ff5465761c05b3bf9933a1 Mon Sep 17 00:00:00 2001 From: Fco Pardo Date: Mon, 14 Aug 2017 01:08:27 -0300 Subject: [PATCH 4/4] Updated readme --- README.md | 2 +- sample/build.gradle | 4 ++-- .../java/com/avocarrot/json2view/sample/MainActivity.java | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a40810b..b9c3633 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ You can parse any xml through the json2view library to create a JSON that will b how it works schematic -Note: Runtime creation of a view without the precompiled version of xml in apk (res/layout), especially for highly complex layouts, can be a potential latency issue. +Note: Runtime creation of a view without the precompiled version of xml in apk (res/layout), especially for highly complex layouts, can be a potential latency issue. In order to reduce the workload in the UI thread, use the createViewAsync methods. ## Examples diff --git a/sample/build.gradle b/sample/build.gradle index c13a4d6..0ab58f9 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -32,6 +32,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:25.3.1' - //compile project(':json2view') - compile 'com.avocarrot:json2view:1.0@aar' + compile project(':json2view') + //compile 'com.avocarrot:json2view:1.0@aar' } diff --git a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java index 8fee624..3ee24a7 100644 --- a/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java +++ b/sample/src/main/java/com/avocarrot/json2view/sample/MainActivity.java @@ -3,16 +3,13 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; -import android.support.annotation.UiThread; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; -import android.widget.LinearLayout; import com.avocarrot.json2view.DynamicView; import com.avocarrot.json2view.DynamicViewId;