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/build.gradle b/build.gradle index ea98e44..47b4a94 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 @@ -15,5 +18,6 @@ buildscript { allprojects { repositories { jcenter() + mavenLocal() } } 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..f4d6c99 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' android { compileSdkVersion 25 @@ -24,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..0ab58f9 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" @@ -28,4 +33,5 @@ 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' } 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..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,12 +3,13 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.RequiresApi; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.view.WindowManager; -import android.widget.Toast; import com.avocarrot.json2view.DynamicView; import com.avocarrot.json2view.DynamicViewId; @@ -22,6 +23,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 +41,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"); @@ -99,5 +110,4 @@ static public class SampleViewHolder { public SampleViewHolder() { } } - }