diff options
author | Roman Nurik <romannurik@google.com> | 2012-06-08 12:34:55 -0700 |
---|---|---|
committer | Roman Nurik <romannurik@google.com> | 2012-06-08 12:34:55 -0700 |
commit | 5f7b1388e1d8fb1214236ca2557f484717db9199 (patch) | |
tree | fdf544fe3d4bed35886f318af2cb64cf8143bba3 /samples/roboguice | |
parent | 7f69c53e8d475924ec81e059975cf817e65809f9 (diff) | |
download | actionbarsherlock-5f7b1388e1d8fb1214236ca2557f484717db9199.tar.gz |
Add ActionBarSherlock 4.1.0 to master branch of actionbarsherlock project
Change-Id: I4f557ca50a210d66b6c6f37f1edc44c08f7e31c6
Diffstat (limited to 'samples/roboguice')
21 files changed, 544 insertions, 0 deletions
diff --git a/samples/roboguice/AndroidManifest.xml b/samples/roboguice/AndroidManifest.xml new file mode 100644 index 0000000..b671f20 --- /dev/null +++ b/samples/roboguice/AndroidManifest.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="90" android:versionName="4.1.0" package="com.actionbarsherlock.sample.roboguice"> + + <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15"/> + + <uses-permission android:name="android.permission.VIBRATE"/> + + <application android:icon="@drawable/icon" android:label="ActionBarSherlock RoboGuice" android:theme="@style/Theme.Sherlock"> + + <activity android:label="ABS: RoboGuice" android:name=".activity.AstroboyMasterConsole"> + <intent-filter> + <action android:name="android.intent.action.MAIN"/> + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + </activity> + + <activity android:name=".activity.FightForcesOfEvilActivity"/> + + </application> +</manifest> diff --git a/samples/roboguice/README.md b/samples/roboguice/README.md new file mode 100644 index 0000000..ec09b5f --- /dev/null +++ b/samples/roboguice/README.md @@ -0,0 +1,16 @@ +ActionBarSherlock Sample: RoboGuice +=================================== + +See [actionbarsherlock.com/samples.html][1] for information on the sample +contained in this folder. + +This sample uses the [roboguice-sherlock][2] plugin from Roberto Tyley. + + + + + + + + [1]: http://actionbarsherlock.com/samples.html + [2]: https://github.com/rtyley/roboguice-sherlock diff --git a/samples/roboguice/libs/android-support-v4.jar b/samples/roboguice/libs/android-support-v4.jar Binary files differnew file mode 100644 index 0000000..99e063b --- /dev/null +++ b/samples/roboguice/libs/android-support-v4.jar diff --git a/samples/roboguice/libs/guice-3.0-no_aop.jar b/samples/roboguice/libs/guice-3.0-no_aop.jar Binary files differnew file mode 100644 index 0000000..ef3a55b --- /dev/null +++ b/samples/roboguice/libs/guice-3.0-no_aop.jar diff --git a/samples/roboguice/libs/javax.inject-1.jar b/samples/roboguice/libs/javax.inject-1.jar Binary files differnew file mode 100644 index 0000000..b2a9d0b --- /dev/null +++ b/samples/roboguice/libs/javax.inject-1.jar diff --git a/samples/roboguice/libs/roboguice-2.0b4.jar b/samples/roboguice/libs/roboguice-2.0b4.jar Binary files differnew file mode 100644 index 0000000..4704414 --- /dev/null +++ b/samples/roboguice/libs/roboguice-2.0b4.jar diff --git a/samples/roboguice/libs/roboguice-sherlock-1.0.jar b/samples/roboguice/libs/roboguice-sherlock-1.0.jar Binary files differnew file mode 100644 index 0000000..604b007 --- /dev/null +++ b/samples/roboguice/libs/roboguice-sherlock-1.0.jar diff --git a/samples/roboguice/pom.xml b/samples/roboguice/pom.xml new file mode 100644 index 0000000..385847b --- /dev/null +++ b/samples/roboguice/pom.xml @@ -0,0 +1,130 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.actionbarsherlock</groupId> + <artifactId>sample-roboguice</artifactId> + <name>Sample: RoboGuice</name> + <packaging>apk</packaging> + + <parent> + <groupId>com.actionbarsherlock</groupId> + <artifactId>parent-sample</artifactId> + <version>4.1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>com.google.android</groupId> + <artifactId>android</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>com.actionbarsherlock</groupId> + <artifactId>library</artifactId> + <version>${project.version}</version> + <type>apklib</type> + </dependency> + + <!-- https://github.com/rtyley/roboguice-sherlock --> + <dependency> + <groupId>com.github.rtyley</groupId> + <artifactId>roboguice-sherlock</artifactId> + <version>1.4</version> + </dependency> + </dependencies> + + <repositories> + <repository> + <id>oss-sonatype</id> + <name>oss-sonatype</name> + <url>https://oss.sonatype.org/content/repositories/snapshots/</url> + <snapshots> + <enabled>true</enabled> + </snapshots> + </repository> + </repositories> + + <build> + <sourceDirectory>src</sourceDirectory> + <finalName>${apk.prefix}-${project.artifactId}-${project.version}-unaligned</finalName> + + <plugins> + <plugin> + <groupId>com.jayway.maven.plugins.android.generation2</groupId> + <artifactId>android-maven-plugin</artifactId> + <extensions>true</extensions> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>zipalign</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jarsigner-plugin</artifactId> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>sign</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <configuration> + <configLocation>../../checkstyle.xml</configLocation> + </configuration> + <executions> + <execution> + <phase>verify</phase> + <goals> + <goal>checkstyle</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>abs-release</id> + <build> + <plugins> + <plugin> + <groupId>com.github.github</groupId> + <artifactId>downloads-maven-plugin</artifactId> + <executions> + <execution> + <phase>deploy</phase> + <goals> + <goal>upload</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> diff --git a/samples/roboguice/project.properties b/samples/roboguice/project.properties new file mode 100644 index 0000000..602c3af --- /dev/null +++ b/samples/roboguice/project.properties @@ -0,0 +1,15 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "ant.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-14 +android.library.reference.1=../../library + + + diff --git a/samples/roboguice/res/anim/expletive_animation.xml b/samples/roboguice/res/anim/expletive_animation.xml new file mode 100644 index 0000000..9d11d8d --- /dev/null +++ b/samples/roboguice/res/anim/expletive_animation.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rotate + xmlns:android="http://schemas.android.com/apk/res/android" + android:fromDegrees="0" + android:toDegrees="360" + android:pivotX="50%" + android:pivotY="50%" + android:repeatCount="infinite" + android:duration="500" /> diff --git a/samples/roboguice/res/drawable-hdpi/icon.png b/samples/roboguice/res/drawable-hdpi/icon.png Binary files differnew file mode 100644 index 0000000..723fbf0 --- /dev/null +++ b/samples/roboguice/res/drawable-hdpi/icon.png diff --git a/samples/roboguice/res/drawable-ldpi/icon.png b/samples/roboguice/res/drawable-ldpi/icon.png Binary files differnew file mode 100644 index 0000000..181f679 --- /dev/null +++ b/samples/roboguice/res/drawable-ldpi/icon.png diff --git a/samples/roboguice/res/drawable-mdpi/icon.png b/samples/roboguice/res/drawable-mdpi/icon.png Binary files differnew file mode 100644 index 0000000..79f50d8 --- /dev/null +++ b/samples/roboguice/res/drawable-mdpi/icon.png diff --git a/samples/roboguice/res/drawable-xhdpi/icon.png b/samples/roboguice/res/drawable-xhdpi/icon.png Binary files differnew file mode 100644 index 0000000..34ca9a4 --- /dev/null +++ b/samples/roboguice/res/drawable-xhdpi/icon.png diff --git a/samples/roboguice/res/layout/fight_evil.xml b/samples/roboguice/res/layout/fight_evil.xml new file mode 100644 index 0000000..a44ecce --- /dev/null +++ b/samples/roboguice/res/layout/fight_evil.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > + + <TextView + android:id="@+id/expletive" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:gravity="center" + /> + +</LinearLayout> diff --git a/samples/roboguice/res/layout/main.xml b/samples/roboguice/res/layout/main.xml new file mode 100644 index 0000000..c608c7a --- /dev/null +++ b/samples/roboguice/res/layout/main.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > + + <TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Welcome back, Doctor Tenma." + android:layout_marginBottom="20dp" + /> + + <TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Send a remote command to Astroboy:" + /> + + <EditText + android:id="@+id/say_text" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:hint="Say Something" + /> + + <Button + android:id="@+id/brush_teeth" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Brush Teeth" + /> + + <Button + android:id="@+id/fight_evil" + android:tag="fightevil" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="Fight Forces of Evil" + /> + + + <Button + android:id="@+id/self_destruct" + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:text="Self Destruct"/> + +</LinearLayout> diff --git a/samples/roboguice/res/values/strings.xml b/samples/roboguice/res/values/strings.xml new file mode 100644 index 0000000..5025c0f --- /dev/null +++ b/samples/roboguice/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">Astroboy</string> +</resources> diff --git a/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/AstroboyMasterConsole.java b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/AstroboyMasterConsole.java new file mode 100644 index 0000000..6625e5e --- /dev/null +++ b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/AstroboyMasterConsole.java @@ -0,0 +1,97 @@ +package com.actionbarsherlock.sample.roboguice.activity; + +import android.content.Intent; +import android.os.Bundle; +import android.os.Vibrator; +import android.view.KeyEvent; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.TextView.OnEditorActionListener; +import com.actionbarsherlock.sample.roboguice.R; +import com.actionbarsherlock.sample.roboguice.controller.AstroboyRemoteControl; +import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockActivity; +import com.google.inject.Inject; +import roboguice.inject.ContentView; +import roboguice.inject.InjectView; + + +/** + * This activity uses an AstroboyRemoteControl to control Astroboy remotely! + * + * What you'll learn in this class: + * - How to use @InjectView as a typesafe version of findViewById() + * - How to inject plain old java objects as well (POJOs) + * - When injection happens + * - Some basics about injection, including when injection results in a call to + * an object's default constructor, versus when it does something "special" + * like call getSystemService() + */ +@ContentView(R.layout.main) +public class AstroboyMasterConsole extends RoboSherlockActivity { + + // Various views that we inject into the activity. + // Equivalent to calling findViewById() in your onCreate(), except more succinct + @InjectView(R.id.self_destruct) Button selfDestructButton; + @InjectView(R.id.say_text) EditText sayText; + @InjectView(R.id.brush_teeth) Button brushTeethButton; + @InjectView(tag="fightevil") Button fightEvilButton; // we can also use tags if we want + + + // Standard Guice injection of Plain Old Java Objects (POJOs) + // Guice will find or create the appropriate instance of AstroboyRemoteControl for us + // Since we haven't specified a special binding for AstroboyRemoteControl, Guice + // will create a new instance for us using AstroboyRemoteControl's default constructor. + // Contrast this with Vibrator, which is an Android service that is pre-bound by RoboGuice. + // Injecting a Vibrator will return a new instance of a Vibrator obtained by calling + // context.getSystemService(VIBRATOR_SERVICE). This is configured in DefaultRoboModule, which is + // used by default to configure every RoboGuice injector. + @Inject AstroboyRemoteControl remoteControl; + @Inject Vibrator vibrator; + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); // @Inject, @InjectResource, and @InjectExtra injection happens during super.onCreate() + + sayText.setOnEditorActionListener(new OnEditorActionListener() { + public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { + + // Have the remoteControl tell Astroboy to say something + remoteControl.say(textView.getText().toString()); + textView.setText(null); + return true; + } + }); + + brushTeethButton.setOnClickListener( new OnClickListener() { + public void onClick(View view) { + remoteControl.brushTeeth(); + } + }); + + selfDestructButton.setOnClickListener( new OnClickListener() { + public void onClick(View view) { + + // Self destruct the remoteControl + vibrator.vibrate(2000); + remoteControl.selfDestruct(); + } + }); + + // Fighting the forces of evil deserves its own activity + fightEvilButton.setOnClickListener( new OnClickListener() { + public void onClick(View view) { + startActivity(new Intent(AstroboyMasterConsole.this, FightForcesOfEvilActivity.class)); + } + }); + + } + +} + + + + diff --git a/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/FightForcesOfEvilActivity.java b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/FightForcesOfEvilActivity.java new file mode 100644 index 0000000..f28748a --- /dev/null +++ b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/activity/FightForcesOfEvilActivity.java @@ -0,0 +1,78 @@ +package com.actionbarsherlock.sample.roboguice.activity; + +import android.content.Context; +import android.os.Bundle; +import android.view.animation.Animation; +import android.widget.TextView; +import com.actionbarsherlock.sample.roboguice.R; +import com.actionbarsherlock.sample.roboguice.controller.Astroboy; +import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockActivity; +import com.google.inject.Inject; +import roboguice.inject.InjectResource; +import roboguice.inject.InjectView; +import roboguice.util.RoboAsyncTask; + +import java.util.Random; + +/** + * Things you'll learn in this class: + * - How to inject Resources + * - How to use RoboAsyncTask to do background tasks with injection + * - What it means to be a @Singleton + */ +public class FightForcesOfEvilActivity extends RoboSherlockActivity { + + @InjectView(R.id.expletive) TextView expletiveText; + + // You can also inject resources such as Strings, Drawables, and Animations + @InjectResource(R.anim.expletive_animation) Animation expletiveAnimation; + + // AstroboyRemoteControl is annotated as @ContextSingleton, so the instance + // we get in FightForcesOfEvilActivity will be a different instance than + // the one we got in AstroboyMasterConsole + //@Inject AstroboyRemoteControl remoteControl; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fight_evil); + + expletiveText.setAnimation(expletiveAnimation); + expletiveAnimation.start(); + + // Throw some punches + for( int i=0; i<10; ++i ) + new AsyncPunch(this) { + @Override + protected void onSuccess(String expletive) throws Exception { + expletiveText.setText(expletive); + } + + // We could also override onException() and onFinally() if we wanted + + }.execute(); + + } + + + + // This class will call Astroboy.punch() in the background + public static class AsyncPunch extends RoboAsyncTask<String> { + + // Because Astroboy is a @Singleton, this will be the same + // instance that we inject elsewhere in our app. + // Random of course will be a new instance of java.util.Random, since + // we haven't specified any special binding instructions anywhere + @Inject Astroboy astroboy; + @Inject Random random; + + public AsyncPunch(Context context) { + super(context); + } + + public String call() throws Exception { + Thread.sleep(random.nextInt(5*1000)); + return astroboy.punch(); + } + } +} diff --git a/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/Astroboy.java b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/Astroboy.java new file mode 100644 index 0000000..be3211e --- /dev/null +++ b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/Astroboy.java @@ -0,0 +1,52 @@ +package com.actionbarsherlock.sample.roboguice.controller; + +import android.app.Application; +import android.os.Vibrator; +import android.widget.Toast; + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import java.util.Random; + + +/** + * What you'll learn in this class: + * - What it means to be a @Singleton + * - That Singletons must use Provider<Context> instead of Context to get + * the current context + * - Some basics about injection, including when injection results in a call to + * an object's default constructor, versus when it does something "special" like + * call getSystemService() + */ + +// There's only one Astroboy, so make it a @Singleton. +// This means that there will be only one instance of Astroboy in the entire app. +// Any class that requires an instance of Astroboy will get the same instance. +// This also means this class needs to be thread safe, of course +@Singleton +public class Astroboy { + + // Because Astroboy is a Singleton, we can't directly inject the current Context + // since the current context may change depending on what activity is using Astroboy + // at the time. Instead we use the application context. + // Vibrator is bound to context.getSystemService(VIBRATOR_SERVICE) in DefaultRoboModule. + // Random has no special bindings, so Guice will create a new instance for us. + @Inject Application application; + @Inject Vibrator vibrator; + @Inject Random random; + + public void say(String something) { + // Make a Toast, using the current context as returned by the Context Provider + Toast.makeText(application, "Astroboy says, \"" + something + "\"", Toast.LENGTH_LONG).show(); + } + + public void brushTeeth() { + vibrator.vibrate(new long[]{0, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, 200, 50, }, -1); + } + + public String punch() { + final String expletives[] = new String[]{"POW!", "BANG!", "KERPOW!", "OOF!"}; + return expletives[random.nextInt(expletives.length)]; + } +} diff --git a/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/AstroboyRemoteControl.java b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/AstroboyRemoteControl.java new file mode 100644 index 0000000..fc9b67c --- /dev/null +++ b/samples/roboguice/src/com/actionbarsherlock/sample/roboguice/controller/AstroboyRemoteControl.java @@ -0,0 +1,57 @@ +package com.actionbarsherlock.sample.roboguice.controller; + +import roboguice.inject.ContextSingleton; +import roboguice.util.Ln; + +import android.app.Activity; +import android.widget.Toast; + +import com.google.inject.Inject; + +/** + * A class to control Astroboy remotely. + * + * This class uses the current context, so we must make it @ContextSingleton. + * This means that there will be one AstroboyRemoteControl for every activity or + * service that requires one. + * Note that we actually ask for the Activity, rather than the Context (which is + * the same thing), because we need access to some activity-related methods and this + * saves us from having to downcast to an Activity manually. + * + * It also asks RoboGuice to inject the Astroboy instance so we can control him. + * + * What you'll learn in this class + * - What @ContextScope means and when to use it + * - How to inject an Activity instead of a Context (which is really the same thing) + * - How to use RoboGuice's convenient and flexible logging facility, Ln. + */ +@ContextSingleton +public class AstroboyRemoteControl { + + + // The Astroboy class has been decorated with @Singleton, so this instance of + // Astroboy will be the same instance used elsewhere in our app. + // Injecting an Activity is basically equivalent to "@Inject Context context", + // and thus also requires @ContextScope. If you wanted, you could also + // @Inject Application, Service, etc. wherever appropriate. + @Inject Astroboy astroboy; + @Inject Activity activity; + + + + public void brushTeeth() { + // More info about logging available here: http://code.google.com/p/roboguice/wiki/Logging + Ln.d("Sent brushTeeth command to Astroboy"); + astroboy.brushTeeth(); + } + + public void say( String something ) { + Ln.d("Sent say(%s) command to Astroboy",something); + astroboy.say(something); + } + + public void selfDestruct() { + Toast.makeText(activity, "Your evil remote control has exploded! Now Astroboy is FREEEEEEEEEE!", Toast.LENGTH_LONG).show(); + activity.finish(); + } +} |