├── .gitignore ├── README.md ├── autobundle ├── .gitignore ├── build.gradle └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── ajalt │ │ └── autobundle │ │ ├── AutoBundleTest.java │ │ ├── LargeTestArguments.java │ │ ├── TestArguments.java │ │ └── TestBundleArguments.java │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── github │ └── ajalt │ └── autobundle │ ├── AutoBundle.java │ ├── BundleArgument.java │ └── BundleArguments.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── ajalt │ │ └── autobundle │ │ └── sample │ │ ├── MainActivity.java │ │ └── SecondActivity.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── activity_second.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | .idea 9 | *.iml 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AutoBundle [![jitpack](https://img.shields.io/github/release/ajalt/AutoBundle.svg?label=JitPack)](https://jitpack.io/#ajalt/AutoBundle/1.1) 2 | 3 | Automated packing and unpacking of Android Bundles. 4 | 5 | ## Overview 6 | 7 | Android's Bundle system works, but packing and unpacking the argument for 8 | Bundles is tedious and repetitive. This library takes care of the boilerplate, 9 | removing the need to manage `putExtra`, `getExtra`, and key values yourself. 10 | 11 | ## Installation 12 | 13 | AutoBundle uses [jitpack](https://jitpack.io/) to host its builds. Add the 14 | following to your gradle file to use AutoBundle: 15 | 16 | ```groovy 17 | repositories { 18 | maven { url "https://jitpack.io" } 19 | } 20 | 21 | dependencies { 22 | compile 'com.github.ajalt:AutoBundle:1.1' 23 | } 24 | ``` 25 | 26 | ## Usage 27 | 28 | AutoBundle is set up by annotating the fields of a class with the 29 | `BundleArgument` annotation. You can also annotate a class with 30 | `BundleArguments` to pack all fields in the class. Using it is this easy: 31 | 32 | ```java 33 | // Annotate the fields of a class of any type with @BundleArgument 34 | @BundleArguments 35 | class Arguments { 36 | public String stringArg; 37 | public int intArg; 38 | 39 | // Constructors, etc... 40 | } 41 | ``` 42 | 43 | Set the extras of an intent by passing the intent and an instance of the 44 | arguments class to `AutoBundle.packIntent`: 45 | 46 | ```java 47 | // Create an instance of the class, and pass it to one of the AutoBundle pack methods 48 | public static Intent createIntent(Context context, Arguments args) { 49 | Intent intent = new Intent(context, SomeActivity.class); 50 | AutoBundle.packIntent(args, intent); 51 | return intent; 52 | } 53 | ``` 54 | 55 | Then in the onCreate of the called Activity, unpack the intent with 56 | `AutoBundle.unpackIntent`: 57 | 58 | ```java 59 | protected void onCreate(Bundle savedInstanceState) { 60 | super.onCreate(savedInstanceState); 61 | Arguments args = new Arguments(); 62 | AutoBundle.unpackIntent(getIntent(), args); 63 | // do something with `args`... 64 | } 65 | ``` 66 | 67 | That's it. The annotated fields in the `Arguments` instance will be populated 68 | with the values sent in the Intent. 69 | 70 | ### Bundles outside of intents. 71 | 72 | You can also use AutoBundle for Bundles that are not associated with an Intent 73 | by calling `AutoBundle.packBundle` and `AutoBundle.unpackBundle`. For example, 74 | with the `saveInstanceState` methods: 75 | 76 | ```java 77 | public class MyActivity extends Activity { 78 | @BundleArgument 79 | private String savedField; 80 | 81 | protected void onCreate(Bundle savedInstanceState) { 82 | super.onCreate(savedInstanceState); 83 | if (savedInstanceState != null) { 84 | AutoBundle.unpackBundle(savedInstanceState, this); 85 | } 86 | } 87 | 88 | protected void onSaveInstanceState(Bundle outState) { 89 | super.onSaveInstanceState(outState); 90 | AutoBundle.packBundle(this, outState); 91 | } 92 | } 93 | ``` 94 | 95 | ### Manually specifying keys 96 | 97 | By default, the keys for Bundle arguments are generated automatically. This 98 | works well if you have control of both packing and unpacking a Bundle. If you 99 | want to send or receive a Bundle from other sources, you can can specify a 100 | `key` parameter to `BundleArgument` to use specific key. Here's a translation 101 | of [the standard email Intent](https://developer.android.com/guide/components/intents-common.html#Email) 102 | using AutoBundle: 103 | 104 | ```java 105 | @BundleArgument(key=Intent.EXTRA_EMAIL) 106 | private String[] addresses; 107 | 108 | @BundleArgument(key=Intent.EXTRA_SUBJECT) 109 | private String subject; 110 | 111 | @BundleArgument(key=Intent.EXTRA_SUBJECT) 112 | private Uri attachment; 113 | 114 | public void composeEmail() { 115 | Intent intent = new Intent(Intent.ACTION_SEND); 116 | intent.setType("*/*"); 117 | AutoBundle.packIntent(this, intent); 118 | startActivity(intent); 119 | } 120 | ``` 121 | 122 | ### Javadoc 123 | 124 | [The full javadocs can be found here](https://jitpack.io/com/github/ajalt/AutoBundle/1.1/javadoc). 125 | 126 | ## License 127 | 128 | Copyright (c) 2015 AJ Alt 129 | 130 | 131 | Permission is hereby granted, free of charge, to any person obtaining 132 | a copy of this software and associated documentation files (the 133 | "Software"), to deal in the Software without restriction, including 134 | without limitation the rights to use, copy, modify, merge, publish, 135 | distribute, sublicense, and/or sell copies of the Software, and to 136 | permit persons to whom the Software is furnished to do so, subject to 137 | the following conditions: 138 | 139 | The above copyright notice and this permission notice shall be included 140 | in all copies or substantial portions of the Software. 141 | 142 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 143 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 144 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 145 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 146 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 147 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 148 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /autobundle/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /autobundle/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | 4 | android { 5 | compileSdkVersion 23 6 | buildToolsVersion "23.0.1" 7 | 8 | defaultConfig { 9 | minSdkVersion 9 10 | targetSdkVersion 23 11 | versionCode 2 12 | versionName "1.1" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | androidTestCompile 'com.android.support.test:rules:0.3' 25 | androidTestCompile 'com.android.support.test:runner:0.3' 26 | } 27 | 28 | // build a jar with source files 29 | task sourcesJar(type: Jar) { 30 | from android.sourceSets.main.java.srcDirs 31 | classifier = 'sources' 32 | } 33 | 34 | task javadoc(type: Javadoc) { 35 | failOnError false 36 | source = android.sourceSets.main.java.sourceFiles 37 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 38 | } 39 | 40 | // build a jar with javadoc 41 | task javadocJar(type: Jar, dependsOn: javadoc) { 42 | classifier = 'javadoc' 43 | from javadoc.destinationDir 44 | } 45 | 46 | artifacts { 47 | archives sourcesJar 48 | archives javadocJar 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /autobundle/src/androidTest/java/com/github/ajalt/autobundle/AutoBundleTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | import android.content.Intent; 4 | import android.graphics.Point; 5 | import android.graphics.PointF; 6 | import android.os.Bundle; 7 | import android.os.Parcelable; 8 | import android.support.test.runner.AndroidJUnit4; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | 13 | import java.io.Serializable; 14 | import java.util.ArrayList; 15 | import java.util.Arrays; 16 | import java.util.List; 17 | 18 | import static junit.framework.Assert.assertNotNull; 19 | import static org.hamcrest.CoreMatchers.is; 20 | import static org.junit.Assert.assertThat; 21 | 22 | @RunWith(AndroidJUnit4.class) 23 | public class AutoBundleTest { 24 | 25 | @Test 26 | public void testPackBundle() { 27 | final String arg = "Test string value"; 28 | final int arg2 = 31415; 29 | TestArguments input = new TestArguments(arg, arg2); 30 | 31 | Bundle bundle = new Bundle(); 32 | AutoBundle.packBundle(input, bundle); 33 | 34 | assertThat(bundle.getString("BUNDLE-ARG-stringArg"), is(arg)); 35 | assertThat(bundle.getInt(TestArguments.INT_ARG_KEY), is(arg2)); 36 | } 37 | 38 | @Test 39 | public void testPackBundleArguments() { 40 | final String arg = "Test string value"; 41 | final int arg2 = 31415; 42 | TestBundleArguments input = new TestBundleArguments(arg, arg2); 43 | 44 | Bundle bundle = new Bundle(); 45 | AutoBundle.packBundle(input, bundle); 46 | 47 | assertThat(bundle.getString("BUNDLE-ARG-stringArg"), is(arg)); 48 | assertThat(bundle.getInt(TestArguments.INT_ARG_KEY), is(arg2)); 49 | } 50 | 51 | @Test 52 | public void testCreateBundle() throws Exception { 53 | final String arg = "Test string value"; 54 | final int arg2 = 31415; 55 | TestArguments input = new TestArguments(arg, arg2); 56 | 57 | Bundle bundle = AutoBundle.createBundle(input); 58 | 59 | assertThat(bundle.getString("BUNDLE-ARG-stringArg"), is(arg)); 60 | assertThat(bundle.getInt(TestArguments.INT_ARG_KEY), is(arg2)); 61 | } 62 | 63 | @Test 64 | public void testUnpackBundle() throws Exception { 65 | final String arg = "Test string value"; 66 | final int arg2 = 31415; 67 | TestArguments input = new TestArguments(arg, arg2); 68 | 69 | Bundle bundle = AutoBundle.createBundle(input); 70 | 71 | TestArguments output = new TestArguments(); 72 | AutoBundle.unpackBundle(bundle, output); 73 | 74 | assertThat(output.stringArg, is(arg)); 75 | assertThat(output.intArg, is(arg2)); 76 | } 77 | 78 | @Test 79 | public void testUnpackBundleArguments() throws Exception { 80 | final String arg = "Test string value"; 81 | final int arg2 = 31415; 82 | TestBundleArguments input = new TestBundleArguments(arg, arg2); 83 | 84 | Bundle bundle = AutoBundle.createBundle(input); 85 | 86 | TestBundleArguments output = new TestBundleArguments(); 87 | AutoBundle.unpackBundle(bundle, output); 88 | 89 | assertThat(output.getStringArg(), is(arg)); 90 | assertThat(output.getIntArg(), is(arg2)); 91 | } 92 | 93 | @Test 94 | public void testPackIntent() throws Exception { 95 | final String arg = "Test string value"; 96 | final int arg2 = 31415; 97 | TestArguments input = new TestArguments(arg, arg2); 98 | 99 | Intent intent = new Intent(); 100 | AutoBundle.packIntent(input, intent); 101 | 102 | Bundle bundle = intent.getExtras().getBundle(AutoBundle.PACK_INTENT_ARGUMENT_KEY); 103 | 104 | assertNotNull(bundle); 105 | assertThat(bundle.getString("BUNDLE-ARG-stringArg"), is(arg)); 106 | assertThat(bundle.getInt(TestArguments.INT_ARG_KEY), is(arg2)); 107 | } 108 | 109 | @Test 110 | public void testUnpackIntent() throws Exception { 111 | final String arg = "Test string value"; 112 | final int arg2 = 31415; 113 | TestArguments input = new TestArguments(arg, arg2); 114 | 115 | Intent intent = new Intent(); 116 | AutoBundle.packIntent(input, intent); 117 | 118 | TestArguments output = new TestArguments(); 119 | AutoBundle.unpackIntent(intent, output); 120 | 121 | assertThat(output.stringArg, is(arg)); 122 | assertThat(output.intArg, is(arg2)); 123 | } 124 | 125 | @Test 126 | public void allArgumentTypesAreSupported() throws Exception { 127 | byte byteArg = 12; 128 | char charArg = (char) -36; 129 | short shortArg = 1234; 130 | float floatArg = 1.5f; 131 | CharSequence charSequenceArg = "charsequence"; 132 | Parcelable parcelableArg = new Point(5, 10); 133 | Parcelable[] parcelableArrayArg = new Parcelable[]{new PointF(1f, 2f), new Point(3, 4)}; 134 | List parcelableListArg = Arrays.asList((Parcelable) new Point(5, 6), new Point(7, 8)); 135 | ArrayList integerArrayListArg = new ArrayList<>(Arrays.asList(1, 2, 3)); 136 | ArrayList stringArrayListArg = new ArrayList<>(Arrays.asList("a", "b", "c")); 137 | Serializable serializableArg = "foo"; 138 | byte[] byteArrayArg = new byte[]{(byte) 0xbe, (byte) 0xef}; 139 | short[] shortArrayArg = new short[]{100, 200}; 140 | char[] charArrayArg = new char[]{'c', 'h', 'a', 'r'}; 141 | float[] floatArrayArg = new float[]{2f, 3f, 4f}; 142 | CharSequence[] charSequenceArrayArg = new CharSequence[]{"spam", "eggs"}; 143 | 144 | LargeTestArguments input = new LargeTestArguments( 145 | byteArg, charArg, 146 | shortArg, floatArg, 147 | charSequenceArg, parcelableArg, 148 | parcelableArrayArg, parcelableListArg, 149 | integerArrayListArg, stringArrayListArg, 150 | serializableArg, byteArrayArg, 151 | shortArrayArg, charArrayArg, 152 | floatArrayArg, charSequenceArrayArg); 153 | 154 | Intent intent = new Intent(); 155 | AutoBundle.packIntent(input, intent); 156 | 157 | LargeTestArguments output = new LargeTestArguments(); 158 | AutoBundle.unpackIntent(intent, output); 159 | assertThat(output.byteArg, is(byteArg)); 160 | assertThat(output.charArg, is(charArg)); 161 | assertThat(output.shortArg, is(shortArg)); 162 | assertThat(output.floatArg, is(floatArg)); 163 | assertThat(output.charSequenceArg, is(charSequenceArg)); 164 | assertThat(output.parcelableArg, is(parcelableArg)); 165 | assertThat(output.parcelableArrayArg, is(parcelableArrayArg)); 166 | assertThat(output.parcelableListArg, is(parcelableListArg)); 167 | assertThat(output.integerArrayListArg, is(integerArrayListArg)); 168 | assertThat(output.stringArrayListArg, is(stringArrayListArg)); 169 | assertThat(output.serializableArg, is(serializableArg)); 170 | assertThat(output.byteArrayArg, is(byteArrayArg)); 171 | assertThat(output.shortArrayArg, is(shortArrayArg)); 172 | assertThat(output.charArrayArg, is(charArrayArg)); 173 | assertThat(output.floatArrayArg, is(floatArrayArg)); 174 | assertThat(output.charSequenceArrayArg, is(charSequenceArrayArg)); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /autobundle/src/androidTest/java/com/github/ajalt/autobundle/LargeTestArguments.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | import android.os.Parcelable; 4 | 5 | import java.io.Serializable; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public class LargeTestArguments { 10 | @BundleArgument 11 | public byte byteArg; 12 | @BundleArgument 13 | public char charArg; 14 | @BundleArgument 15 | public short shortArg; 16 | @BundleArgument 17 | public float floatArg; 18 | @BundleArgument 19 | public CharSequence charSequenceArg; 20 | @BundleArgument 21 | public Parcelable parcelableArg; 22 | @BundleArgument 23 | public Parcelable[] parcelableArrayArg; 24 | @BundleArgument 25 | public List parcelableListArg; 26 | @BundleArgument 27 | public ArrayList integerArrayListArg; 28 | @BundleArgument 29 | public ArrayList stringArrayListArg; 30 | @BundleArgument 31 | public Serializable serializableArg; 32 | @BundleArgument 33 | public byte[] byteArrayArg; 34 | @BundleArgument 35 | public short[] shortArrayArg; 36 | @BundleArgument 37 | public char[] charArrayArg; 38 | @BundleArgument 39 | public float[] floatArrayArg; 40 | @BundleArgument 41 | public CharSequence[] charSequenceArrayArg; 42 | 43 | public LargeTestArguments() {} 44 | 45 | public LargeTestArguments(byte byteArg, 46 | char charArg, 47 | short shortArg, 48 | float floatArg, 49 | CharSequence charSequenceArg, 50 | Parcelable parcelableArg, 51 | Parcelable[] parcelableArrayArg, 52 | List parcelableListArg, 53 | ArrayList integerArrayListArg, 54 | ArrayList stringArrayListArg, 55 | Serializable serializableArg, 56 | byte[] byteArrayArg, 57 | short[] shortArrayArg, 58 | char[] charArrayArg, 59 | float[] floatArrayArg, 60 | CharSequence[] charSequenceArrayArg) { 61 | this.byteArg = byteArg; 62 | this.charArg = charArg; 63 | this.shortArg = shortArg; 64 | this.floatArg = floatArg; 65 | this.charSequenceArg = charSequenceArg; 66 | this.parcelableArg = parcelableArg; 67 | this.parcelableArrayArg = parcelableArrayArg; 68 | this.parcelableListArg = parcelableListArg; 69 | this.integerArrayListArg = integerArrayListArg; 70 | this.stringArrayListArg = stringArrayListArg; 71 | this.serializableArg = serializableArg; 72 | this.byteArrayArg = byteArrayArg; 73 | this.shortArrayArg = shortArrayArg; 74 | this.charArrayArg = charArrayArg; 75 | this.floatArrayArg = floatArrayArg; 76 | this.charSequenceArrayArg = charSequenceArrayArg; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /autobundle/src/androidTest/java/com/github/ajalt/autobundle/TestArguments.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | public class TestArguments { 4 | public static final String INT_ARG_KEY = "INT_ARG_KEY"; 5 | @BundleArgument 6 | public String stringArg; 7 | 8 | @BundleArgument(key = INT_ARG_KEY) 9 | public int intArg; 10 | 11 | public TestArguments() {} 12 | 13 | public TestArguments(String stringArg, int intArg) { 14 | this.stringArg = stringArg; 15 | this.intArg = intArg; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /autobundle/src/androidTest/java/com/github/ajalt/autobundle/TestBundleArguments.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | @BundleArguments 4 | public class TestBundleArguments { 5 | public static final String INT_ARG_KEY = "INT_ARG_KEY"; 6 | 7 | private final String stringArg; 8 | 9 | @BundleArgument(key = INT_ARG_KEY) 10 | private final int intArg; 11 | 12 | public TestBundleArguments() { 13 | stringArg = null; 14 | intArg = 0; 15 | } 16 | 17 | public TestBundleArguments(String stringArg, int intArg) { 18 | this.stringArg = stringArg; 19 | this.intArg = intArg; 20 | } 21 | 22 | public String getStringArg() { 23 | return stringArg; 24 | } 25 | 26 | public int getIntArg() { 27 | return intArg; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /autobundle/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /autobundle/src/main/java/com/github/ajalt/autobundle/AutoBundle.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.text.TextUtils; 6 | import android.util.Log; 7 | 8 | import java.lang.reflect.Field; 9 | import java.util.Map; 10 | 11 | /** 12 | * A class for automatically packing and unpacking class fields into {@link Bundle}s. 13 | *

14 | * To use, annotate primitive or {@link android.os.Parcelable} fields of an object of any type with 15 | * {@link BundleArgument}, and pass that object and any {@link Bundle} to {@link #packBundle(Object, 16 | * Bundle)}. The values of the annotated fields of the object will be added to the Bundle. 17 | *

18 | * To unpack the data, call {@link #unpackBundle(Bundle, Object)} with a bundle containing data from 19 | * a call to packBundle, and an instance of the same class that was passed to the packBundle call. 20 | * The annotated field of the class instance will be set with the unpacked values. 21 | */ 22 | public class AutoBundle { 23 | public static final String PACK_INTENT_ARGUMENT_KEY = "AUTO_BUNDLE_PACK_INTENT_ARGUMENT_KEY"; 24 | 25 | /** 26 | * Unpack the extras bundle from an intent that was previously packed with {@link 27 | * #packBundle(Object, Bundle)} or {@link #packIntent(Object, Intent)}. 28 | * 29 | * @param source The intent to unpack extras from. 30 | * @param target The object to unpack into. This method has no effect if the target does not 31 | * have any fields annotated with {@link BundleArgument} 32 | */ 33 | public static void unpackIntent(Intent source, Object target) { 34 | unpackBundle(source.getExtras(), target); 35 | } 36 | 37 | /** 38 | * Unpack the a bundle that was previously packed with {@link #packBundle(Object, Bundle)} or 39 | * {@link #packIntent(Object, Intent)}. 40 | * 41 | * @param source The intent to unpack extras from. 42 | * @param target The object to unpack into. This method has no effect if the target does not 43 | * have any fields annotated with {@link BundleArgument} 44 | */ 45 | public static void unpackBundle(Bundle source, Object target) { 46 | if (source == null || target == null) { 47 | throw new IllegalArgumentException("AutoBundle arguments cannot be null"); 48 | } 49 | 50 | boolean unpackAll = target.getClass().isAnnotationPresent(BundleArguments.class); 51 | 52 | // Check if this is coming from a packIntent call 53 | Bundle intentBundle = source.getBundle(PACK_INTENT_ARGUMENT_KEY); 54 | if (intentBundle != null) { 55 | source = intentBundle; 56 | } 57 | 58 | for (Field field : target.getClass().getDeclaredFields()) { 59 | if (!unpackAll && !field.isAnnotationPresent(BundleArgument.class)) continue; 60 | Object o = source.get(getKey(field)); 61 | field.setAccessible(true); 62 | try { 63 | field.set(target, o); 64 | } catch (IllegalAccessException e) { 65 | throw new RuntimeException(e); 66 | } 67 | } 68 | } 69 | 70 | /** 71 | * Pack the extras of an intent. 72 | * 73 | * @param source An object with fields annotated with {@link BundleArgument}. 74 | * @param target The intent to pack extras into. 75 | */ 76 | public static void packIntent(Object source, Intent target) { 77 | target.putExtra(PACK_INTENT_ARGUMENT_KEY, createBundle(source)); 78 | } 79 | 80 | /** 81 | * Create a new, packed, {@link Bundle}. 82 | * 83 | * @param source An object with fields annotated with {@link BundleArgument}. 84 | * @return A new {@link Bundle} with values from all annotated source fields added. 85 | */ 86 | public static Bundle createBundle(Object source) { 87 | Bundle bundle = new Bundle(); 88 | packBundle(source, bundle); 89 | return bundle; 90 | } 91 | 92 | /** 93 | * Pack annotated fields into a {@link Bundle}. 94 | * 95 | * @param source An object with fields annotated with {@link BundleArgument}. 96 | * @param target A {@link Bundle} into which values from all annotated source fields will be 97 | * added. 98 | */ 99 | @SuppressWarnings("unchecked") 100 | public static void packBundle(Object source, Bundle target) { 101 | if (source == null || target == null) { 102 | throw new IllegalArgumentException("AutoBundle arguments cannot be null"); 103 | } 104 | 105 | boolean packAll = source.getClass().isAnnotationPresent(BundleArguments.class); 106 | 107 | Map bundleMap = null; 108 | for (Class cls = target.getClass(); cls != null; cls = cls.getSuperclass()) { 109 | try { 110 | Field field = cls.getDeclaredField("mMap"); 111 | field.setAccessible(true); 112 | bundleMap = (Map) field.get(target); 113 | } catch (IllegalAccessException e) { 114 | throw new RuntimeException(e); 115 | } catch (NoSuchFieldException ignored) { 116 | } 117 | } 118 | 119 | if (bundleMap == null) { 120 | throw new RuntimeException("Could not access internal bundle map"); 121 | } 122 | 123 | for (Field field : source.getClass().getDeclaredFields()) { 124 | if (!packAll && !field.isAnnotationPresent(BundleArgument.class)) continue; 125 | field.setAccessible(true); 126 | try { 127 | Object o = field.get(source); 128 | bundleMap.put(getKey(field), o); 129 | } catch (IllegalAccessException e) { 130 | throw new RuntimeException(e); 131 | } 132 | } 133 | 134 | Log.d("AutoBundle", "map: " + bundleMap); 135 | } 136 | 137 | /** 138 | * Return the key for a field. 139 | *

140 | * It may be defined in the field, or it will be generated based on the name of the field. 141 | */ 142 | private static String getKey(Field field) { 143 | BundleArgument annotation = field.getAnnotation(BundleArgument.class); 144 | return annotation == null || TextUtils.isEmpty(annotation.key()) ? 145 | "BUNDLE-ARG-" + field.getName() 146 | : annotation.key(); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /autobundle/src/main/java/com/github/ajalt/autobundle/BundleArgument.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * An annotation that marks a field as an argument for {@link AutoBundle} 10 | * 11 | * @see AutoBundle 12 | */ 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target(ElementType.FIELD) 15 | public @interface BundleArgument { 16 | /** 17 | * Optionally specify the Bundle key for this field. 18 | *

19 | * If not given, a key will be generated automatically. 20 | */ 21 | String key() default ""; 22 | } 23 | -------------------------------------------------------------------------------- /autobundle/src/main/java/com/github/ajalt/autobundle/BundleArguments.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Applying this annotation to a class is equivalent to defining {@link 10 | * BundleArgument} for all fields in the class. 11 | *

12 | * If you want to specify keys for individual arguments, you can use {@link 13 | * BundleArgument#key()} on individual fields of the same class that this 14 | * annotation is applied to. 15 | */ 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.TYPE) 18 | public @interface BundleArguments { 19 | } 20 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'com.android.tools.build:gradle:1.3.0' 7 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 8 | } 9 | } 10 | 11 | allprojects { 12 | repositories { 13 | mavenCentral() 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ajalt/AutoBundle/dd2555b67593acaa75cc153cac3530d863b61641/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Sep 30 19:30:52 PDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.github.ajalt.autobundle" 9 | minSdkVersion 9 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile 'com.android.support:appcompat-v7:23.0.1' 24 | compile project(':autobundle') 25 | } 26 | -------------------------------------------------------------------------------- /sample/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 C:\Users\aj\AppData\Local\Android\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 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/ajalt/autobundle/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle.sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | } 14 | 15 | public void onButtonClick(View v) { 16 | startActivity(SecondActivity.createIntent(this, 17 | new SecondActivity.Arguments(123, "a bundle argument"))); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/ajalt/autobundle/sample/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ajalt.autobundle.sample; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.TextView; 8 | 9 | import com.github.ajalt.autobundle.AutoBundle; 10 | import com.github.ajalt.autobundle.BundleArguments; 11 | 12 | public class SecondActivity extends AppCompatActivity { 13 | @BundleArguments 14 | public static class Arguments { 15 | public Integer intArg; 16 | public String stringArg; 17 | 18 | public Arguments() {} 19 | 20 | public Arguments(Integer intArg, String stringArg) { 21 | this.intArg = intArg; 22 | this.stringArg = stringArg; 23 | } 24 | } 25 | 26 | public static Intent createIntent(Context context, Arguments args) { 27 | Intent intent = new Intent(context, SecondActivity.class); 28 | AutoBundle.packIntent(args, intent); 29 | return intent; 30 | } 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | setContentView(R.layout.activity_second); 36 | Arguments args = new Arguments(); 37 | AutoBundle.unpackIntent(getIntent(), args); 38 | 39 | ((TextView) findViewById(R.id.text)).setText(args.intArg.toString()); 40 | ((TextView) findViewById(R.id.text2)).setText(args.stringArg); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 |