diff --git a/build/sdk.atree b/build/sdk.atree
index 068e779ac..73bf0bcac 100644
--- a/build/sdk.atree
+++ b/build/sdk.atree
@@ -244,6 +244,7 @@ development/samples/CubeLiveWallpaper samples/${PLATFORM_NAME}/CubeLiveWa
development/samples/VoiceRecognitionService samples/${PLATFORM_NAME}/VoiceRecognitionService
development/samples/TicTacToeLib samples/${PLATFORM_NAME}/TicTacToeLib
development/samples/TicTacToeMain samples/${PLATFORM_NAME}/TicTacToeMain
+development/samples/CrossCompatibility samples/${PLATFORM_NAME}/CrossCompatibility
# NOTICE files are copied by build/core/Makefile from sdk.git
sdk/files/sdk_files_NOTICE.txt samples/${PLATFORM_NAME}/NOTICE.txt
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 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
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 @@
+
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.
+
+In this case, the CrossCompatibility application shows how to use APIs that are not available in all Android versions and
+still create a single .apk that runs on all Android versions.
+
+
+- The main application's AndroidManifest.xml file declares that it is backwards compatible with API level 3 devices with attribute
minSdkVersion in the uses-sdk tag.
+
+-
+VersionedGestureDetector.java
+is a version independent abstract class which factors out multitouch APIs that differ between platform versions.
+
+
+
+ For more information on how to make your applications cross-compatible, please check out the original
+blogpost here.
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 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/CrossCompatibility/res/drawable-hdpi/icon.png b/samples/CrossCompatibility/res/drawable-hdpi/icon.png
new file mode 100644
index 000000000..8074c4c57
Binary files /dev/null and b/samples/CrossCompatibility/res/drawable-hdpi/icon.png differ
diff --git a/samples/CrossCompatibility/res/drawable-ldpi/icon.png b/samples/CrossCompatibility/res/drawable-ldpi/icon.png
new file mode 100644
index 000000000..1095584ec
Binary files /dev/null and b/samples/CrossCompatibility/res/drawable-ldpi/icon.png differ
diff --git a/samples/CrossCompatibility/res/drawable-mdpi/icon.png b/samples/CrossCompatibility/res/drawable-mdpi/icon.png
new file mode 100644
index 000000000..a07c69fa5
Binary files /dev/null and b/samples/CrossCompatibility/res/drawable-mdpi/icon.png differ
diff --git a/samples/CrossCompatibility/res/drawable/icon.png b/samples/CrossCompatibility/res/drawable/icon.png
new file mode 100644
index 000000000..a07c69fa5
Binary files /dev/null and b/samples/CrossCompatibility/res/drawable/icon.png differ
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 @@
+
+
+ TouchExample
+
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);
+ }
+ }
+}