Adding CrossCompatibility sample app.
Change-Id: I565584ea53212f61a9fbbb44e50da59f4ba06fff
This commit is contained in:
@@ -109,6 +109,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
|
||||
|
||||
# dx
|
||||
bin/dx platforms/${PLATFORM_NAME}/tools/dx
|
||||
|
||||
17
samples/CrossCompatibility/AndroidManifest.xml
Normal file
17
samples/CrossCompatibility/AndroidManifest.xml
Normal file
@@ -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>
|
||||
20
samples/CrossCompatibility/_index.html
Normal file
20
samples/CrossCompatibility/_index.html
Normal file
@@ -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>
|
||||
67
samples/CrossCompatibility/build.xml
Normal file
67
samples/CrossCompatibility/build.xml
Normal file
@@ -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>
|
||||
BIN
samples/CrossCompatibility/res/drawable-hdpi/icon.png
Normal file
BIN
samples/CrossCompatibility/res/drawable-hdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
BIN
samples/CrossCompatibility/res/drawable-ldpi/icon.png
Normal file
BIN
samples/CrossCompatibility/res/drawable-ldpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
BIN
samples/CrossCompatibility/res/drawable-mdpi/icon.png
Normal file
BIN
samples/CrossCompatibility/res/drawable-mdpi/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
BIN
samples/CrossCompatibility/res/drawable/icon.png
Normal file
BIN
samples/CrossCompatibility/res/drawable/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
4
samples/CrossCompatibility/res/values/strings.xml
Normal file
4
samples/CrossCompatibility/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">TouchExample</string>
|
||||
</resources>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user