├── .github ├── ISSUE_TEMPLATE └── PULL_REQUEST_TEMPLATE ├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── yahoo │ │ └── android │ │ └── dlayout │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── yahoo │ │ │ └── android │ │ │ └── dlayout │ │ │ └── demo │ │ │ ├── activity │ │ │ └── LauncherActivity.java │ │ │ ├── app │ │ │ └── DynamicLayoutApplication.java │ │ │ ├── cache │ │ │ └── LayoutCache.java │ │ │ └── http │ │ │ └── Httpd.java │ └── res │ │ ├── layout │ │ ├── activity_launcher.xml │ │ ├── builtin_layout.xml │ │ ├── dynamic_choice_layout.xml │ │ └── fragment_launcher.xml │ │ ├── menu │ │ └── menu_launcher.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ ├── dynamic_layout_01.bin │ │ └── dynamic_layout_02.bin │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── yahoo │ └── android │ └── dlayout │ └── ExampleUnitTest.java ├── build.gradle ├── dynamic-layout-loader ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yahoo │ │ └── android │ │ └── dlayout │ │ └── LayoutLoader.java │ └── res │ └── values │ └── strings.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── layout-designer ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yahoo │ │ └── android │ │ └── designer │ │ └── MainActivity.java │ └── res │ ├── layout │ ├── activity_main.xml │ ├── dynamic_layout_01.xml │ └── dynamic_layout_02.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.github/ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | ### Expected Behaviour 2 | 3 | 4 | ### Actual Behaviour 5 | 6 | 7 | ### Steps to Reproduce 8 | 9 | 10 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | For Issue # . 2 | 3 | #### Changes 4 | 5 | . 6 | . 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/ 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 10 | 11 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 12 | 13 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 14 | 15 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 16 | 17 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 18 | 19 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 20 | 21 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 22 | 23 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 24 | 25 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 26 | 27 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 28 | 29 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 30 | 31 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 32 | 33 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 34 | 35 | (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and 36 | 37 | (b) You must cause any modified files to carry prominent notices stating that You changed the files; and 38 | 39 | (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | 41 | (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 42 | 43 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 44 | 45 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 46 | 47 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 | 49 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, eitherf any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS 54 | 55 | APPENDIX: How to apply the Apache License to your work. 56 | 57 | To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 58 | 59 | Copyright [yyyy] [name of copyright owner] 60 | 61 | Licensed under the Apache License, Version 2.0 (the "License"); 62 | you may not use this file except in compliance with the License. 63 | You may obtain a copy of the License at 64 | 65 | http://www.apache.org/licenses/LICENSE-2.0 66 | 67 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 68 | 69 | See the License for the specific language governing permissions and limitations under the License. 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Dynamic Layout Loader 2 | 3 | `LayoutInflater` cannot load layouts that are plain XML. It can also not load layouts that are not loaded from resources (`R.layout.{blah}`). 4 | 5 | Dynamic Layout Loader uses a non-public API (available and unchanged since API Level 1) to help load compiled layouts from any source - local filesystem, network etc. All it needs is `InputStream` to load the data or raw data as `byte[]`. 6 | 7 | ## Repository Structure 8 | 9 | * `dynamic-layout-loader`: Provides the loader. See [`LayoutLoader`](dynamic-layout-loader/src/main/java/com/yahoo/android/dlayout/LayoutLoader.java) class. 10 | * `app`: Sample app demonstrating the use of the loader. 11 | * `layout-designer`: Accompanying app to create layouts independent of the target app - to demonstrate full capabilities. 12 | 13 | ## Constraints 14 | 15 | 1. The resource references do not work. For example, you cannot use `@drawable/icon` or `@string/name` etc in the layout. 16 | 1. As a corollary, the ids do not work. You cannot use `@id/another_element` in layout or `findViewById` in code. Use `android:tag` and `findViewWithTag`. 17 | 1. The current implementation does not support styles and theme. You cannot use `style` attribute in the layout. 18 | 19 | 20 | ## License 21 | 22 | The use and distribution terms for this software are covered by the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html). 23 | 24 | See file [LICENSE](LICENSE) accompanying the code. 25 | 26 | ## Related Links 27 | 28 | * Presentation at SF Droidcon 2016: [Original - Keynote](http://bit.ly/1o01Znz) and [PDF](http://bit.ly/1Uv6MLY) 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | allprojects { 4 | repositories { 5 | jcenter() 6 | mavenCentral() 7 | } 8 | } 9 | 10 | android { 11 | compileSdkVersion 23 12 | buildToolsVersion "23.0.2" 13 | 14 | defaultConfig { 15 | applicationId "com.yahoo.android.dlayout.demo" 16 | minSdkVersion 16 17 | targetSdkVersion 23 18 | versionCode 1 19 | versionName "1.0" 20 | } 21 | buildTypes { 22 | release { 23 | minifyEnabled false 24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 | } 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | compile project(':dynamic-layout-loader') 32 | 33 | testCompile 'junit:junit:4.12' 34 | compile 'com.android.support:appcompat-v7:23.2.1' 35 | compile 'com.android.support:design:23.2.1' 36 | compile 'org.nanohttpd:nanohttpd:2.3.0' 37 | compile 'com.squareup.okhttp3:okhttp:3.2.0' 38 | compile 'com.squareup.picasso:picasso:2.5.2' 39 | compile 'com.google.android.gms:play-services-appindexing:8.1.0' 40 | } 41 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/vgaurav/Desktop/all/downloads/adt/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yahoo/android/dlayout/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.yahoo.android.dlayout; 2 | 3 | import android.test.ApplicationTestCase; 4 | 5 | import com.yahoo.android.dlayout.demo.app.DynamicLayoutApplication; 6 | 7 | /** 8 | * Testing Fundamentals 9 | */ 10 | public class ApplicationTest extends ApplicationTestCase { 11 | public ApplicationTest() { 12 | super(DynamicLayoutApplication.class); 13 | } 14 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/yahoo/android/dlayout/demo/activity/LauncherActivity.java: -------------------------------------------------------------------------------- 1 | package com.yahoo.android.dlayout.demo.activity; 2 | 3 | import android.app.AlertDialog; 4 | import android.content.Context; 5 | import android.content.DialogInterface; 6 | import android.support.design.widget.TabLayout; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import android.support.v4.app.Fragment; 11 | import android.support.v4.app.FragmentManager; 12 | import android.support.v4.app.FragmentPagerAdapter; 13 | import android.support.v4.view.ViewPager; 14 | import android.os.Bundle; 15 | import android.view.LayoutInflater; 16 | import android.view.Menu; 17 | import android.view.View; 18 | import android.view.ViewGroup; 19 | 20 | import android.widget.ImageView; 21 | import android.widget.LinearLayout; 22 | import android.widget.RadioGroup; 23 | 24 | import com.google.android.gms.appindexing.Action; 25 | import com.google.android.gms.appindexing.AppIndex; 26 | import com.google.android.gms.common.api.GoogleApiClient; 27 | import com.yahoo.android.dlayout.demo.R; 28 | import com.yahoo.android.dlayout.demo.app.DynamicLayoutApplication; 29 | import com.squareup.picasso.Picasso; 30 | 31 | public class LauncherActivity extends AppCompatActivity { 32 | 33 | private SectionsPagerAdapter mSectionsPagerAdapter; 34 | private ViewPager mViewPager; 35 | 36 | private static String[] imageURLs = { 37 | "https://raw.githubusercontent.com/gvaish/images/master/01.jpg", 38 | "https://raw.githubusercontent.com/gvaish/images/master/02.png", 39 | "https://raw.githubusercontent.com/gvaish/images/master/03.jpg" 40 | }; 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_launcher); 46 | 47 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 48 | setSupportActionBar(toolbar); 49 | mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 50 | 51 | mViewPager = (ViewPager) findViewById(R.id.container); 52 | mViewPager.setAdapter(mSectionsPagerAdapter); 53 | 54 | TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 55 | tabLayout.setupWithViewPager(mViewPager); 56 | } 57 | 58 | 59 | @Override 60 | public boolean onCreateOptionsMenu(Menu menu) { 61 | return false; 62 | } 63 | 64 | public static class PlaceholderFragment extends Fragment { 65 | private static final String ARG_SECTION_NUMBER = "section_number"; 66 | private DynamicLayoutApplication app; 67 | 68 | public static PlaceholderFragment newInstance(int sectionNumber) { 69 | PlaceholderFragment fragment = new PlaceholderFragment(); 70 | Bundle args = new Bundle(); 71 | args.putInt(ARG_SECTION_NUMBER, sectionNumber); 72 | fragment.setArguments(args); 73 | return fragment; 74 | } 75 | 76 | @Override 77 | public void onAttach(Context context) { 78 | super.onAttach(context); 79 | this.app = (DynamicLayoutApplication) context.getApplicationContext(); 80 | } 81 | 82 | @Override 83 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 84 | Bundle savedInstanceState) { 85 | int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER); 86 | switch(sectionNumber) { 87 | case 1: 88 | return populate(inflater.inflate(R.layout.builtin_layout, container, false), sectionNumber, "1"); 89 | case 2: 90 | case 3: 91 | return populate(loadViewFromNetwork(sectionNumber, container), sectionNumber, String.valueOf(sectionNumber)); 92 | case 4: 93 | return populate(loadDynamically(inflater, container, sectionNumber), sectionNumber, String.valueOf(sectionNumber)); 94 | } 95 | return null; 96 | } 97 | 98 | protected View loadDynamically(final LayoutInflater inflater, final ViewGroup container, int sectionNumber) { 99 | View root = inflater.inflate(R.layout.dynamic_choice_layout, container, false); 100 | RadioGroup rg = (RadioGroup) root.findViewById(R.id.choice); 101 | final ViewGroup fl = (ViewGroup) root.findViewById(R.id.container); 102 | rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 103 | @Override 104 | public void onCheckedChanged(RadioGroup group, int checkedId) { 105 | View v = null; 106 | switch (checkedId) { 107 | case R.id.choice1: 108 | v = createDynamicView(0, inflater, fl, "4.1"); 109 | break; 110 | case R.id.choice2: 111 | v = createDynamicView(1, inflater, fl, "4.2"); 112 | break; 113 | case R.id.choice3: 114 | v = createDynamicView(2, inflater, fl, "4.3"); 115 | break; 116 | } 117 | fl.removeAllViews();; 118 | if(v != null) { 119 | fl.addView(v); 120 | } 121 | } 122 | }); 123 | return root; 124 | } 125 | 126 | protected View createDynamicView(int index, LayoutInflater inflater, ViewGroup container, String msgSuffix) { 127 | View rv = null; 128 | switch (index) { 129 | case 0: 130 | rv = inflater.inflate(R.layout.builtin_layout, container, false); 131 | break; 132 | case 1: 133 | case 2: 134 | rv = loadViewFromNetwork(index + 1, container); 135 | break; 136 | } 137 | if(rv != null) { 138 | populate(rv, index + 1, msgSuffix); 139 | } 140 | return rv; 141 | } 142 | 143 | private View loadViewFromNetwork(int sectionNumber, ViewGroup container) { 144 | byte[] data = app.getLayoutCache().get("http://127.0.0.1:8888/0" + (sectionNumber - 1)); 145 | return app.getLayoutLoader().load(data, getActivity(), container, false); 146 | } 147 | 148 | private View populate(View view, final int imageNo, final String msgSuffix) { 149 | ImageView iv = (ImageView) view.findViewWithTag("image"); 150 | if (iv != null) { 151 | Picasso.with(getActivity()).load(imageURLs[imageNo - 1]).into(iv); 152 | } 153 | 154 | View action = view.findViewWithTag("action"); 155 | if (action != null) { 156 | action.setOnClickListener(new View.OnClickListener() { 157 | @Override 158 | public void onClick(View v) { 159 | new AlertDialog.Builder(getActivity()) 160 | .setTitle("Action Performed at " + msgSuffix) 161 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 162 | @Override 163 | public void onClick(DialogInterface dialog, int which) { 164 | dialog.dismiss(); 165 | } 166 | }) 167 | .create().show(); 168 | } 169 | }); 170 | } 171 | 172 | return view; 173 | } 174 | } 175 | 176 | public class SectionsPagerAdapter extends FragmentPagerAdapter { 177 | 178 | public SectionsPagerAdapter(FragmentManager fm) { 179 | super(fm); 180 | } 181 | 182 | @Override 183 | public Fragment getItem(int position) { 184 | return PlaceholderFragment.newInstance(position + 1); 185 | } 186 | 187 | @Override 188 | public int getCount() { 189 | return 4; 190 | } 191 | 192 | @Override 193 | public CharSequence getPageTitle(int position) { 194 | switch (position) { 195 | case 0: 196 | return "App"; 197 | case 1: 198 | return "Remote 1"; 199 | case 2: 200 | return "Remote 2"; 201 | case 3: 202 | return "Choice"; 203 | } 204 | return null; 205 | } 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /app/src/main/java/com/yahoo/android/dlayout/demo/app/DynamicLayoutApplication.java: -------------------------------------------------------------------------------- 1 | package com.yahoo.android.dlayout.demo.app; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | import com.yahoo.android.dlayout.LayoutLoader; 7 | import com.yahoo.android.dlayout.demo.cache.LayoutCache; 8 | import com.yahoo.android.dlayout.demo.http.Httpd; 9 | 10 | import java.io.IOException; 11 | 12 | import okhttp3.Call; 13 | import okhttp3.Callback; 14 | import okhttp3.Interceptor; 15 | import okhttp3.OkHttpClient; 16 | import okhttp3.Request; 17 | import okhttp3.Response; 18 | 19 | /** 20 | * Created by vgaurav on 3/15/16. 21 | */ 22 | public class DynamicLayoutApplication extends Application { 23 | 24 | private LayoutLoader layoutLoader; 25 | private Httpd httpd; 26 | private OkHttpClient httpClient; 27 | private LayoutCache layoutCache; 28 | 29 | @Override 30 | public void onCreate() { 31 | super.onCreate(); 32 | layoutLoader = new LayoutLoader().initialize(); 33 | startServer(); 34 | layoutCache = new LayoutCache(); 35 | initializeLayoutCache(getLayoutCache()); 36 | } 37 | 38 | private void initializeLayoutCache(LayoutCache layoutCache) { 39 | 40 | for(int i = 1; i <= 2; i++) { 41 | String url = "http://127.0.0.1:8888/0" + i; 42 | 43 | Request req = new Request.Builder().url(url).get().build(); 44 | OkHttpClient client = getHttpClient(); 45 | 46 | client.newCall(req).enqueue(new Callback() { 47 | @Override 48 | public void onFailure(Call call, IOException e) { 49 | Log.w("DLayout", "[FAILED] Loaded Layout: " + call.request().url(), e); 50 | } 51 | 52 | @Override 53 | public void onResponse(Call call, Response response) throws IOException { 54 | byte[] data = response.body().bytes(); 55 | String url = response.request().url().toString(); 56 | Log.w("DLayout", "Loaded Layout: " + url); 57 | getLayoutCache().put(url, data); 58 | } 59 | }); 60 | } 61 | 62 | } 63 | 64 | public LayoutLoader getLayoutLoader() { 65 | return layoutLoader; 66 | } 67 | 68 | public void startServer() { 69 | if(httpd == null) { 70 | httpd = new Httpd(this, 8888); 71 | try { 72 | httpd.start(); 73 | Log.d("DLayout", "Server started..."); 74 | } catch (IOException e) { 75 | Log.d("DLayout", "Server failed to start", e); 76 | } 77 | } 78 | } 79 | 80 | public void stopServer() { 81 | if(httpd != null) { 82 | httpd.stop(); 83 | httpd = null; 84 | } 85 | } 86 | 87 | public OkHttpClient getHttpClient() { 88 | if(httpClient == null) { 89 | httpClient = new OkHttpClient.Builder() 90 | .addInterceptor(new Interceptor() { 91 | @Override 92 | public Response intercept(Interceptor.Chain chain) throws IOException { 93 | Request req = chain.request(); 94 | Log.d("DLayout", "[http-request] url=" + req.url()); 95 | return chain.proceed(req); 96 | } 97 | }).build(); 98 | } 99 | return httpClient; 100 | } 101 | 102 | public LayoutCache getLayoutCache() { 103 | return layoutCache; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/com/yahoo/android/dlayout/demo/cache/LayoutCache.java: -------------------------------------------------------------------------------- 1 | package com.yahoo.android.dlayout.demo.cache; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by vgaurav on 3/15/16. 8 | */ 9 | public class LayoutCache { 10 | 11 | private Map cache; 12 | 13 | public LayoutCache() { 14 | cache = new HashMap<>(); 15 | } 16 | 17 | public void put(String url, byte[] data) { 18 | cache.put(url, data); 19 | } 20 | 21 | public byte[] get(String url) { 22 | return cache.get(url); 23 | } 24 | 25 | public void remove(String url) { 26 | cache.remove(url); 27 | } 28 | 29 | public void clear() { 30 | cache.clear(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/yahoo/android/dlayout/demo/http/Httpd.java: -------------------------------------------------------------------------------- 1 | package com.yahoo.android.dlayout.demo.http; 2 | 3 | import android.content.Context; 4 | import android.content.res.Resources; 5 | import android.util.Log; 6 | 7 | import com.yahoo.android.dlayout.demo.R; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | 12 | import fi.iki.elonen.NanoHTTPD; 13 | 14 | /** 15 | * Created by vgaurav on 3/15/16. 16 | */ 17 | public class Httpd extends NanoHTTPD { 18 | private final Context context; 19 | 20 | public Httpd(Context ctx, int port) { 21 | super("127.0.0.1", port); 22 | this.context = ctx; 23 | } 24 | 25 | @Override 26 | public void start() throws IOException { 27 | super.start(); 28 | Log.d("DLayout", "server started"); 29 | } 30 | 31 | @Override 32 | public Response serve(IHTTPSession session) { 33 | String uri = session.getUri(); 34 | 35 | Log.d("DLayout", "[Server::serve] uri=" + uri); 36 | 37 | Resources res = context.getResources(); 38 | InputStream input = null; 39 | 40 | if("/01".equals(uri)) { 41 | input = res.openRawResource(R.raw.dynamic_layout_01); 42 | } else { 43 | input = res.openRawResource(R.raw.dynamic_layout_02); 44 | } 45 | return newChunkedResponse(Response.Status.OK, "application/octet-stream", input); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 17 | 18 | 25 | 26 | 27 | 28 | 32 | 33 | 34 | 35 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/res/layout/builtin_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 14 | 15 |