diff options
author | Adam Koch <akoch@android.com> | 2013-01-14 15:56:51 -0500 |
---|---|---|
committer | Adam Koch <akoch@android.com> | 2013-01-24 15:44:07 -0500 |
commit | 2956ff812e9c0eb3c8937d8d1cd71d4181484285 (patch) | |
tree | e80d55c1ea724bc224c6b5dffdd877c24c1bf3ea /templates/other | |
parent | a737845cdcf443843a3bc112a66f0d560b1e007a (diff) | |
download | sdk-2956ff812e9c0eb3c8937d8d1cd71d4181484285.zip sdk-2956ff812e9c0eb3c8937d8d1cd71d4181484285.tar.gz sdk-2956ff812e9c0eb3c8937d8d1cd71d4181484285.tar.bz2 |
New "IntentService" ADT template.
This change adds a new IntentService template in the 'other' category,
that creates a basic IntentService class, some default actions and
optionally some static helper methods to start the service.
Change-Id: I86d656507706c71c914a5ec3f13668b47d6358e0
Diffstat (limited to 'templates/other')
5 files changed, 155 insertions, 0 deletions
diff --git a/templates/other/IntentService/globals.xml.ftl b/templates/other/IntentService/globals.xml.ftl new file mode 100644 index 0000000..bfc27eb --- /dev/null +++ b/templates/other/IntentService/globals.xml.ftl @@ -0,0 +1,4 @@ +<?xml version="1.0"?> +<globals> + <global id="srcOut" value="src/${slashedPackageName(packageName)}" /> +</globals> diff --git a/templates/other/IntentService/recipe.xml.ftl b/templates/other/IntentService/recipe.xml.ftl new file mode 100644 index 0000000..958f6fd --- /dev/null +++ b/templates/other/IntentService/recipe.xml.ftl @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<recipe> + <merge from="AndroidManifest.xml.ftl" /> + <instantiate from="src/app_package/IntentService.java.ftl" + to="${srcOut}/${className}.java" /> + <open file="${srcOut}/${className}.java" /> +</recipe> diff --git a/templates/other/IntentService/root/AndroidManifest.xml.ftl b/templates/other/IntentService/root/AndroidManifest.xml.ftl new file mode 100644 index 0000000..fcc0b66 --- /dev/null +++ b/templates/other/IntentService/root/AndroidManifest.xml.ftl @@ -0,0 +1,9 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" > + + <application> + <service android:name=".${className}" + android:exported="false" > + </service> + </application> + +</manifest> diff --git a/templates/other/IntentService/root/src/app_package/IntentService.java.ftl b/templates/other/IntentService/root/src/app_package/IntentService.java.ftl new file mode 100644 index 0000000..c6848ef --- /dev/null +++ b/templates/other/IntentService/root/src/app_package/IntentService.java.ftl @@ -0,0 +1,106 @@ +package ${packageName}; + +import android.app.IntentService; +import android.content.Intent; +<#if includeHelper>import android.content.Context;</#if> + +/** + * An {@link IntentService} subclass for handling asynchronous task requests in + * a service on a separate handler thread. + * <p> +<#if includeHelper> + * TODO: Customize class - update intent actions, extra parameters and static + * helper methods. +<#else> + * TODO: Customize class - update intent actions and extra parameters. +</#if> + */ +public class ${className} extends IntentService { +<#if !includeHelper> + // TODO: Rename actions, choose action names that describe tasks that this + // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS + public static final String ACTION_FOO = "${packageName}.action.FOO"; + public static final String ACTION_BAZ = "${packageName}.action.BAZ"; + + // TODO: Rename parameters + public static final String EXTRA_PARAM1 = "${packageName}.extra.PARAM1"; + public static final String EXTRA_PARAM2 = "${packageName}.extra.PARAM2"; +<#else> + // TODO: Rename actions, choose action names that describe tasks that this + // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS + private static final String ACTION_FOO = "${packageName}.action.FOO"; + private static final String ACTION_BAZ = "${packageName}.action.BAZ"; + + // TODO: Rename parameters + private static final String EXTRA_PARAM1 = "${packageName}.extra.PARAM1"; + private static final String EXTRA_PARAM2 = "${packageName}.extra.PARAM2"; + + /** + * Starts this service to perform action Foo with the given parameters. If + * the service is already performing a task this action will be queued. + * + * @see IntentService + */ + // TODO: Customize helper method + public static void startActionFoo(Context context, String param1, String param2) { + Intent intent = new Intent(context, ${className}.class); + intent.setAction(ACTION_FOO); + intent.putExtra(EXTRA_PARAM1, param1); + intent.putExtra(EXTRA_PARAM2, param2); + context.startService(intent); + } + + /** + * Starts this service to perform action Baz with the given parameters. If + * the service is already performing a task this action will be queued. + * + * @see IntentService + */ + // TODO: Customize helper method + public static void startActionBaz(Context context, String param1, String param2) { + Intent intent = new Intent(context, ${className}.class); + intent.setAction(ACTION_BAZ); + intent.putExtra(EXTRA_PARAM1, param1); + intent.putExtra(EXTRA_PARAM2, param2); + context.startService(intent); + } +</#if> + + public ${className}() { + super("${className}"); + } + + @Override + protected void onHandleIntent(Intent intent) { + if (intent != null) { + final String action = intent.getAction(); + if (ACTION_FOO.equals(action)) { + final String param1 = intent.getStringExtra(EXTRA_PARAM1); + final String param2 = intent.getStringExtra(EXTRA_PARAM2); + handleActionFoo(param1, param2); + } else if (ACTION_BAZ.equals(action)) { + final String param1 = intent.getStringExtra(EXTRA_PARAM1); + final String param2 = intent.getStringExtra(EXTRA_PARAM2); + handleActionBaz(param1, param2); + } + } + } + + /** + * Handle action Foo in the provided background thread with the provided + * parameters. + */ + private void handleActionFoo(String param1, String param2) { + // TODO: Handle action Foo + throw new UnsupportedOperationException("Not yet implemented"); + } + + /** + * Handle action Baz in the provided background thread with the provided + * parameters. + */ + private void handleActionBaz(String param1, String param2) { + // TODO: Handle action Baz + throw new UnsupportedOperationException("Not yet implemented"); + } +} diff --git a/templates/other/IntentService/template.xml b/templates/other/IntentService/template.xml new file mode 100644 index 0000000..ffe6cd5 --- /dev/null +++ b/templates/other/IntentService/template.xml @@ -0,0 +1,29 @@ +<?xml version="1.0"?> +<template + format="3" + revision="1" + name="New IntentService" + description="Creates a new intent service class." + minApi="3" + minBuildApi="3"> + + <category value="Other" /> + + <parameter + id="className" + name="Class Name" + type="string" + constraints="class|unique|nonempty" + default="MyIntentService" /> + + <parameter + id="includeHelper" + name="Include helper start methods?" + type="boolean" + default="true" + help="Generate static helper methods to start the service e.g. MyIntentService.startAction()" /> + + <globals file="globals.xml.ftl" /> + <execute file="recipe.xml.ftl" /> + +</template> |