diff options
| author | Megha Joshi <mjoshi@google.com> | 2010-08-17 13:56:59 -0700 |
|---|---|---|
| committer | Megha Joshi <mjoshi@google.com> | 2010-09-13 17:00:10 -0700 |
| commit | 42d3a4ae1431fb3f02ac212000e8cdfb6f21e226 (patch) | |
| tree | 11f88083ab2e31d4fceeaf501967bd717ea493dc /samples/CrossCompatibility | |
| parent | e55fc016a6e40edd456c0d240d06689d84a7127b (diff) | |
| download | android_development-42d3a4ae1431fb3f02ac212000e8cdfb6f21e226.tar.gz android_development-42d3a4ae1431fb3f02ac212000e8cdfb6f21e226.tar.bz2 android_development-42d3a4ae1431fb3f02ac212000e8cdfb6f21e226.zip | |
Adding CrossCompatibility sample app.
Change-Id: I565584ea53212f61a9fbbb44e50da59f4ba06fff
Diffstat (limited to 'samples/CrossCompatibility')
| -rw-r--r-- | samples/CrossCompatibility/AndroidManifest.xml | 17 | ||||
| -rw-r--r-- | samples/CrossCompatibility/_index.html | 20 | ||||
| -rw-r--r-- | samples/CrossCompatibility/build.xml | 67 | ||||
| -rw-r--r-- | samples/CrossCompatibility/res/drawable-hdpi/icon.png | bin | 0 -> 4147 bytes | |||
| -rw-r--r-- | samples/CrossCompatibility/res/drawable-ldpi/icon.png | bin | 0 -> 1723 bytes | |||
| -rw-r--r-- | samples/CrossCompatibility/res/drawable-mdpi/icon.png | bin | 0 -> 2574 bytes | |||
| -rw-r--r-- | samples/CrossCompatibility/res/drawable/icon.png | bin | 0 -> 2574 bytes | |||
| -rw-r--r-- | samples/CrossCompatibility/res/values/strings.xml | 4 | ||||
| -rw-r--r-- | samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleActivity.java | 34 | ||||
| -rw-r--r-- | samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleView.java | 82 | ||||
| -rw-r--r-- | samples/CrossCompatibility/src/com/example/android/touchexample/VersionedGestureDetector.java | 165 |
11 files changed, 389 insertions, 0 deletions
diff --git a/samples/CrossCompatibility/AndroidManifest.xml b/samples/CrossCompatibility/AndroidManifest.xml new file mode 100644 index 000000000..1097a4da8 --- /dev/null +++ b/samples/CrossCompatibility/AndroidManifest.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.touchexample" + android:versionCode="1" + android:versionName="1.0"> + <application android:icon="@drawable/icon" android:label="@string/app_name"> + <activity android:name=".TouchExampleActivity" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + + </application> + <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> +</manifest> diff --git a/samples/CrossCompatibility/_index.html b/samples/CrossCompatibility/_index.html new file mode 100644 index 000000000..42683b0a1 --- /dev/null +++ b/samples/CrossCompatibility/_index.html @@ -0,0 +1,20 @@ +<p>This sample demonstrates how to design an application that is compatible across different Android versions. Applications +should degrade gracefully on older platform versions, dropping features or providing +when the platform support needed by features or functionality isn't available.</p> + +<p>In this case, the CrossCompatibility application shows how to use APIs that are not available in all Android versions and +still create a single <code>.apk</code> that runs on all Android versions.</p> + +<ul> +<li>The main application's <a +href="AndroidManifest.html">AndroidManifest.xml</a> file declares that it is backwards compatible with API level 3 devices with attribute <code>minSdkVersion</code> in the <code>uses-sdk</code> tag. + </li> +<li> +<a +href="src/com/example/android/touchexample/VersionedGestureDetector.html">VersionedGestureDetector.java</a> +is a version independent abstract class which factors out multitouch APIs that differ between platform versions. </li> + +</ul> + +<p> For more information on how to make your applications cross-compatible, please check out the original +blogpost <a href="http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html">here</a>.</p> diff --git a/samples/CrossCompatibility/build.xml b/samples/CrossCompatibility/build.xml new file mode 100644 index 000000000..805e30cbe --- /dev/null +++ b/samples/CrossCompatibility/build.xml @@ -0,0 +1,67 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project name="TouchExampleActivity" default="help"> + + <!-- The local.properties file is created and updated by the 'android' tool. + It contains the path to the SDK. It should *NOT* be checked in in Version + Control Systems. --> + <property file="local.properties" /> + + <!-- The build.properties file can be created by you and is never touched + by the 'android' tool. This is the place to change some of the default property values + used by the Ant rules. + Here are some properties you may want to change/update: + + application.package + the name of your application package as defined in the manifest. Used by the + 'uninstall' rule. + source.dir + the name of the source directory. Default is 'src'. + out.dir + the name of the output directory. Default is 'bin'. + + Properties related to the SDK location or the project target should be updated + using the 'android' tool with the 'update' action. + + This file is an integral part of the build system for your application and + should be checked in in Version Control Systems. + + --> + <property file="build.properties" /> + + <!-- The default.properties file is created and updated by the 'android' tool, as well + as ADT. + This file is an integral part of the build system for your application and + should be checked in in Version Control Systems. --> + <property file="default.properties" /> + + <!-- Custom Android task to deal with the project target, and import the proper rules. + This requires ant 1.6.0 or above. --> + <path id="android.antlibs"> + <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" /> + <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" /> + <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" /> + <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" /> + <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" /> + </path> + + <taskdef name="setup" + classname="com.android.ant.SetupTask" + classpathref="android.antlibs" /> + + <!-- Execute the Android Setup task that will setup some properties specific to the target, + and import the build rules files. + + The rules file is imported from + <SDK>/platforms/<target_platform>/templates/android_rules.xml + + To customize some build steps for your project: + - copy the content of the main node <project> from android_rules.xml + - paste it in this build.xml below the <setup /> task. + - disable the import by changing the setup task below to <setup import="false" /> + + This will ensure that the properties are setup correctly but that your customized + build steps are used. + --> + <setup /> + +</project> diff --git a/samples/CrossCompatibility/res/drawable-hdpi/icon.png b/samples/CrossCompatibility/res/drawable-hdpi/icon.png Binary files differnew file mode 100644 index 000000000..8074c4c57 --- /dev/null +++ b/samples/CrossCompatibility/res/drawable-hdpi/icon.png diff --git a/samples/CrossCompatibility/res/drawable-ldpi/icon.png b/samples/CrossCompatibility/res/drawable-ldpi/icon.png Binary files differnew file mode 100644 index 000000000..1095584ec --- /dev/null +++ b/samples/CrossCompatibility/res/drawable-ldpi/icon.png diff --git a/samples/CrossCompatibility/res/drawable-mdpi/icon.png b/samples/CrossCompatibility/res/drawable-mdpi/icon.png Binary files differnew file mode 100644 index 000000000..a07c69fa5 --- /dev/null +++ b/samples/CrossCompatibility/res/drawable-mdpi/icon.png diff --git a/samples/CrossCompatibility/res/drawable/icon.png b/samples/CrossCompatibility/res/drawable/icon.png Binary files differnew file mode 100644 index 000000000..a07c69fa5 --- /dev/null +++ b/samples/CrossCompatibility/res/drawable/icon.png diff --git a/samples/CrossCompatibility/res/values/strings.xml b/samples/CrossCompatibility/res/values/strings.xml new file mode 100644 index 000000000..a1502425d --- /dev/null +++ b/samples/CrossCompatibility/res/values/strings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">TouchExample</string> +</resources> diff --git a/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleActivity.java b/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleActivity.java new file mode 100644 index 000000000..aac998767 --- /dev/null +++ b/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleActivity.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.touchexample; + +import android.app.Activity; +import android.os.Bundle; +import android.view.ViewGroup; + +public class TouchExampleActivity extends Activity { + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + TouchExampleView view = new TouchExampleView(this); + view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT)); + + setContentView(view); + } +} diff --git a/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleView.java b/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleView.java new file mode 100644 index 000000000..c567b0f62 --- /dev/null +++ b/samples/CrossCompatibility/src/com/example/android/touchexample/TouchExampleView.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.touchexample; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; + +public class TouchExampleView extends View { + private Drawable mIcon; + private float mPosX; + private float mPosY; + + private VersionedGestureDetector mDetector; + private float mScaleFactor = 1.f; + + public TouchExampleView(Context context) { + this(context, null, 0); + } + + public TouchExampleView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public TouchExampleView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + mIcon = context.getResources().getDrawable(R.drawable.icon); + mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight()); + + mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback()); + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + mDetector.onTouchEvent(ev); + return true; + } + + @Override + public void onDraw(Canvas canvas) { + super.onDraw(canvas); + + canvas.save(); + canvas.translate(mPosX, mPosY); + canvas.scale(mScaleFactor, mScaleFactor); + mIcon.draw(canvas); + canvas.restore(); + } + + private class GestureCallback implements VersionedGestureDetector.OnGestureListener { + public void onDrag(float dx, float dy) { + mPosX += dx; + mPosY += dy; + invalidate(); + } + + public void onScale(float scaleFactor) { + mScaleFactor *= scaleFactor; + + // Don't let the object get too small or too large. + mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f)); + + invalidate(); + } + } +} diff --git a/samples/CrossCompatibility/src/com/example/android/touchexample/VersionedGestureDetector.java b/samples/CrossCompatibility/src/com/example/android/touchexample/VersionedGestureDetector.java new file mode 100644 index 000000000..75d4ee213 --- /dev/null +++ b/samples/CrossCompatibility/src/com/example/android/touchexample/VersionedGestureDetector.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.touchexample; + +import android.content.Context; +import android.os.Build; +import android.util.Log; +import android.view.MotionEvent; +import android.view.ScaleGestureDetector; + +public abstract class VersionedGestureDetector { + private static final String TAG = "VersionedGestureDetector"; + + OnGestureListener mListener; + + public static VersionedGestureDetector newInstance(Context context, + OnGestureListener listener) { + final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); + VersionedGestureDetector detector = null; + if (sdkVersion < Build.VERSION_CODES.ECLAIR) { + detector = new CupcakeDetector(); + } else if (sdkVersion < Build.VERSION_CODES.FROYO) { + detector = new EclairDetector(); + } else { + detector = new FroyoDetector(context); + } + + Log.d(TAG, "Created new " + detector.getClass()); + detector.mListener = listener; + + return detector; + } + + public abstract boolean onTouchEvent(MotionEvent ev); + + public interface OnGestureListener { + public void onDrag(float dx, float dy); + public void onScale(float scaleFactor); + } + + private static class CupcakeDetector extends VersionedGestureDetector { + float mLastTouchX; + float mLastTouchY; + + float getActiveX(MotionEvent ev) { + return ev.getX(); + } + + float getActiveY(MotionEvent ev) { + return ev.getY(); + } + + boolean shouldDrag() { + return true; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + switch (ev.getAction()) { + case MotionEvent.ACTION_DOWN: { + mLastTouchX = getActiveX(ev); + mLastTouchY = getActiveY(ev); + break; + } + case MotionEvent.ACTION_MOVE: { + final float x = getActiveX(ev); + final float y = getActiveY(ev); + + if (shouldDrag()) { + mListener.onDrag(x - mLastTouchX, y - mLastTouchY); + } + + mLastTouchX = x; + mLastTouchY = y; + break; + } + } + return true; + } + } + + private static class EclairDetector extends CupcakeDetector { + private static final int INVALID_POINTER_ID = -1; + private int mActivePointerId = INVALID_POINTER_ID; + private int mActivePointerIndex = 0; + + @Override + float getActiveX(MotionEvent ev) { + return ev.getX(mActivePointerIndex); + } + + @Override + float getActiveY(MotionEvent ev) { + return ev.getY(mActivePointerIndex); + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + final int action = ev.getAction(); + switch (action & MotionEvent.ACTION_MASK) { + case MotionEvent.ACTION_DOWN: + mActivePointerId = ev.getPointerId(0); + break; + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_UP: + mActivePointerId = INVALID_POINTER_ID; + break; + case MotionEvent.ACTION_POINTER_UP: + final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) + >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; + final int pointerId = ev.getPointerId(pointerIndex); + if (pointerId == mActivePointerId) { + // This was our active pointer going up. Choose a new + // active pointer and adjust accordingly. + final int newPointerIndex = pointerIndex == 0 ? 1 : 0; + mActivePointerId = ev.getPointerId(newPointerIndex); + mLastTouchX = ev.getX(newPointerIndex); + mLastTouchY = ev.getY(newPointerIndex); + } + break; + } + + mActivePointerIndex = ev.findPointerIndex(mActivePointerId); + return super.onTouchEvent(ev); + } + } + + private static class FroyoDetector extends EclairDetector { + private ScaleGestureDetector mDetector; + + public FroyoDetector(Context context) { + mDetector = new ScaleGestureDetector(context, + new ScaleGestureDetector.SimpleOnScaleGestureListener() { + @Override public boolean onScale(ScaleGestureDetector detector) { + mListener.onScale(detector.getScaleFactor()); + return true; + } + }); + } + + @Override + boolean shouldDrag() { + return !mDetector.isInProgress(); + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + mDetector.onTouchEvent(ev); + return super.onTouchEvent(ev); + } + } +} |
