From c13dc19171aa3dff8f6be256b74d9fc796acd252 Mon Sep 17 00:00:00 2001 From: frankiesardo Date: Thu, 13 Mar 2014 21:28:05 +0100 Subject: [PATCH] Add a decent sample application --- sample/src/main/AndroidManifest.xml | 2 + .../auto/value/sample/DetailActivity.java | 24 +++++++++ .../auto/value/sample/MainActivity.java | 12 +++-- .../android/auto/value/sample/Person.java | 13 ----- .../android/auto/value/sample/SampleData.java | 22 +++++++++ sample/src/main/java/model1/HeightBucket.java | 5 ++ sample/src/main/java/model2/Address.java | 14 ++++++ sample/src/main/java/model3/Person.java | 22 +++++++++ .../src/main/res/layout/activity_detail.xml | 49 +++++++++++++++++++ sample/src/main/res/layout/activity_main.xml | 9 ++-- sample/src/main/res/menu/main.xml | 9 ---- sample/src/main/res/values/strings.xml | 6 +-- 12 files changed, 153 insertions(+), 34 deletions(-) create mode 100644 sample/src/main/java/android/auto/value/sample/DetailActivity.java delete mode 100644 sample/src/main/java/android/auto/value/sample/Person.java create mode 100644 sample/src/main/java/android/auto/value/sample/SampleData.java create mode 100644 sample/src/main/java/model1/HeightBucket.java create mode 100644 sample/src/main/java/model2/Address.java create mode 100644 sample/src/main/java/model3/Person.java create mode 100644 sample/src/main/res/layout/activity_detail.xml delete mode 100644 sample/src/main/res/menu/main.xml diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 3110274..d9ac398 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -16,6 +16,8 @@ + diff --git a/sample/src/main/java/android/auto/value/sample/DetailActivity.java b/sample/src/main/java/android/auto/value/sample/DetailActivity.java new file mode 100644 index 0000000..725da87 --- /dev/null +++ b/sample/src/main/java/android/auto/value/sample/DetailActivity.java @@ -0,0 +1,24 @@ +package android.auto.value.sample; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; +import model3.Person; + +public class DetailActivity extends Activity { + + @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_detail); + Person person = getIntent().getParcelableExtra("Person"); + textView(R.id.name).setText("Name:" + person.name()); + textView(R.id.id).setText("Id:" + person.id()); + textView(R.id.height).setText("Height:" + person.heightType()); + textView(R.id.addresses).setText("Addresses:" + person.addresses()); + textView(R.id.friends).setText("Friends:" + person.friends()); + } + + private TextView textView(int id) { + return (TextView) findViewById(id); + } +} diff --git a/sample/src/main/java/android/auto/value/sample/MainActivity.java b/sample/src/main/java/android/auto/value/sample/MainActivity.java index 3a8430f..9502c24 100644 --- a/sample/src/main/java/android/auto/value/sample/MainActivity.java +++ b/sample/src/main/java/android/auto/value/sample/MainActivity.java @@ -1,8 +1,9 @@ package android.auto.value.sample; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; -import android.widget.Toast; +import android.view.View; public class MainActivity extends Activity { @@ -10,7 +11,12 @@ public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Person person = Person.create("Frankie", 123); - Toast.makeText(this, person.name(), Toast.LENGTH_SHORT).show(); + findViewById(R.id.click_me).setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { + Intent detailIntent = new Intent(MainActivity.this, DetailActivity.class); + detailIntent.putExtra("Person", SampleData.BOB); + startActivity(detailIntent); + } + }); } } diff --git a/sample/src/main/java/android/auto/value/sample/Person.java b/sample/src/main/java/android/auto/value/sample/Person.java deleted file mode 100644 index 8528996..0000000 --- a/sample/src/main/java/android/auto/value/sample/Person.java +++ /dev/null @@ -1,13 +0,0 @@ -package android.auto.value.sample; - -import android.auto.value.AutoValue; - -@AutoValue -public abstract class Person { - public static Person create(String name, int id) { - return new AutoValue_Person(name, id); - } - - public abstract String name(); - public abstract int id(); -} diff --git a/sample/src/main/java/android/auto/value/sample/SampleData.java b/sample/src/main/java/android/auto/value/sample/SampleData.java new file mode 100644 index 0000000..5f0a714 --- /dev/null +++ b/sample/src/main/java/android/auto/value/sample/SampleData.java @@ -0,0 +1,22 @@ +package android.auto.value.sample; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import model1.HeightBucket; +import model2.Address; +import model3.Person; + +public interface SampleData { + + static final Person ALICE = Person.create("Alice", 1L, HeightBucket.AVERAGE, + new HashMap() {{ + put("home", Address.create(new double[] { 0.3, 0.7 }, "Rome")); + }}, Collections.emptyList()); + + static final Person BOB = Person.create("Bob", 2L, HeightBucket.TALL, + new HashMap() {{ + put("home", Address.create(new double[] { 3.2, 143.2 }, "Turin")); + put("work", Address.create(new double[] { 5.9, 156.1 }, "Genoa")); + }}, Arrays.asList(ALICE)); +} diff --git a/sample/src/main/java/model1/HeightBucket.java b/sample/src/main/java/model1/HeightBucket.java new file mode 100644 index 0000000..9ebac30 --- /dev/null +++ b/sample/src/main/java/model1/HeightBucket.java @@ -0,0 +1,5 @@ +package model1; + +public enum HeightBucket { + SHORT, AVERAGE, TALL +} diff --git a/sample/src/main/java/model2/Address.java b/sample/src/main/java/model2/Address.java new file mode 100644 index 0000000..77e24c1 --- /dev/null +++ b/sample/src/main/java/model2/Address.java @@ -0,0 +1,14 @@ +package model2; + +import android.auto.value.AutoValue; +import android.os.Parcelable; + +@AutoValue +public abstract class Address implements Parcelable { + public abstract double[] coordinates(); + public abstract String cityName(); + + public static Address create(double[] coordinates, String cityName) { + return new AutoValue_Address(coordinates, cityName); + } +} diff --git a/sample/src/main/java/model3/Person.java b/sample/src/main/java/model3/Person.java new file mode 100644 index 0000000..ed3b99e --- /dev/null +++ b/sample/src/main/java/model3/Person.java @@ -0,0 +1,22 @@ +package model3; + +import android.auto.value.AutoValue; +import android.os.Parcelable; +import java.util.List; +import java.util.Map; +import model1.HeightBucket; +import model2.Address; + +@AutoValue +public abstract class Person implements Parcelable { + public static Person create(String name, long id, HeightBucket heightType, Map addresses, + List friends) { + return new AutoValue_Person(name, id, heightType, addresses, friends); + } + + public abstract String name(); + public abstract long id(); + public abstract HeightBucket heightType(); + public abstract Map addresses(); + public abstract List friends(); +} diff --git a/sample/src/main/res/layout/activity_detail.xml b/sample/src/main/res/layout/activity_detail.xml new file mode 100644 index 0000000..65aff3e --- /dev/null +++ b/sample/src/main/res/layout/activity_detail.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 90f8dbf..19ba9bb 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -8,9 +8,10 @@ android:paddingBottom="@dimen/activity_vertical_margin" tools:context="android.auto.value.sample.MainActivity"> - +