Skip to content

Commit 42d3a4a

Browse files
author
Megha Joshi
committed
Adding CrossCompatibility sample app.
Change-Id: I565584ea53212f61a9fbbb44e50da59f4ba06fff
1 parent e55fc01 commit 42d3a4a

File tree

12 files changed

+390
-0
lines changed

12 files changed

+390
-0
lines changed

build/sdk.atree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ development/samples/CubeLiveWallpaper samples/${PLATFORM_NAME}/CubeLiveWa
109109
development/samples/VoiceRecognitionService samples/${PLATFORM_NAME}/VoiceRecognitionService
110110
development/samples/TicTacToeLib samples/${PLATFORM_NAME}/TicTacToeLib
111111
development/samples/TicTacToeMain samples/${PLATFORM_NAME}/TicTacToeMain
112+
development/samples/CrossCompatibility samples/${PLATFORM_NAME}/CrossCompatibility
112113

113114
# dx
114115
bin/dx platforms/${PLATFORM_NAME}/tools/dx
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.android.touchexample"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
<application android:icon="@drawable/icon" android:label="@string/app_name">
7+
<activity android:name=".TouchExampleActivity"
8+
android:label="@string/app_name">
9+
<intent-filter>
10+
<action android:name="android.intent.action.MAIN" />
11+
<category android:name="android.intent.category.LAUNCHER" />
12+
</intent-filter>
13+
</activity>
14+
15+
</application>
16+
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
17+
</manifest>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<p>This sample demonstrates how to design an application that is compatible across different Android versions. Applications
2+
should degrade gracefully on older platform versions, dropping features or providing
3+
when the platform support needed by features or functionality isn't available.</p>
4+
5+
<p>In this case, the CrossCompatibility application shows how to use APIs that are not available in all Android versions and
6+
still create a single <code>.apk</code> that runs on all Android versions.</p>
7+
8+
<ul>
9+
<li>The main application's <a
10+
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.
11+
</li>
12+
<li>
13+
<a
14+
href="src/com/example/android/touchexample/VersionedGestureDetector.html">VersionedGestureDetector.java</a>
15+
is a version independent abstract class which factors out multitouch APIs that differ between platform versions. </li>
16+
17+
</ul>
18+
19+
<p> For more information on how to make your applications cross-compatible, please check out the original
20+
blogpost <a href="http://android-developers.blogspot.com/2010/07/how-to-have-your-cupcake-and-eat-it-too.html">here</a>.</p>

samples/CrossCompatibility/build.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project name="TouchExampleActivity" default="help">
3+
4+
<!-- The local.properties file is created and updated by the 'android' tool.
5+
It contains the path to the SDK. It should *NOT* be checked in in Version
6+
Control Systems. -->
7+
<property file="local.properties" />
8+
9+
<!-- The build.properties file can be created by you and is never touched
10+
by the 'android' tool. This is the place to change some of the default property values
11+
used by the Ant rules.
12+
Here are some properties you may want to change/update:
13+
14+
application.package
15+
the name of your application package as defined in the manifest. Used by the
16+
'uninstall' rule.
17+
source.dir
18+
the name of the source directory. Default is 'src'.
19+
out.dir
20+
the name of the output directory. Default is 'bin'.
21+
22+
Properties related to the SDK location or the project target should be updated
23+
using the 'android' tool with the 'update' action.
24+
25+
This file is an integral part of the build system for your application and
26+
should be checked in in Version Control Systems.
27+
28+
-->
29+
<property file="build.properties" />
30+
31+
<!-- The default.properties file is created and updated by the 'android' tool, as well
32+
as ADT.
33+
This file is an integral part of the build system for your application and
34+
should be checked in in Version Control Systems. -->
35+
<property file="default.properties" />
36+
37+
<!-- Custom Android task to deal with the project target, and import the proper rules.
38+
This requires ant 1.6.0 or above. -->
39+
<path id="android.antlibs">
40+
<pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
41+
<pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
42+
<pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
43+
<pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
44+
<pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
45+
</path>
46+
47+
<taskdef name="setup"
48+
classname="com.android.ant.SetupTask"
49+
classpathref="android.antlibs" />
50+
51+
<!-- Execute the Android Setup task that will setup some properties specific to the target,
52+
and import the build rules files.
53+
54+
The rules file is imported from
55+
<SDK>/platforms/<target_platform>/templates/android_rules.xml
56+
57+
To customize some build steps for your project:
58+
- copy the content of the main node <project> from android_rules.xml
59+
- paste it in this build.xml below the <setup /> task.
60+
- disable the import by changing the setup task below to <setup import="false" />
61+
62+
This will ensure that the properties are setup correctly but that your customized
63+
build steps are used.
64+
-->
65+
<setup />
66+
67+
</project>
Loading
Loading
Loading
2.51 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">TouchExample</string>
4+
</resources>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.android.touchexample;
17+
18+
import android.app.Activity;
19+
import android.os.Bundle;
20+
import android.view.ViewGroup;
21+
22+
public class TouchExampleActivity extends Activity {
23+
/** Called when the activity is first created. */
24+
@Override
25+
public void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
28+
TouchExampleView view = new TouchExampleView(this);
29+
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
30+
ViewGroup.LayoutParams.MATCH_PARENT));
31+
32+
setContentView(view);
33+
}
34+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.android.touchexample;
17+
18+
import android.content.Context;
19+
import android.graphics.Canvas;
20+
import android.graphics.drawable.Drawable;
21+
import android.util.AttributeSet;
22+
import android.view.MotionEvent;
23+
import android.view.View;
24+
25+
public class TouchExampleView extends View {
26+
private Drawable mIcon;
27+
private float mPosX;
28+
private float mPosY;
29+
30+
private VersionedGestureDetector mDetector;
31+
private float mScaleFactor = 1.f;
32+
33+
public TouchExampleView(Context context) {
34+
this(context, null, 0);
35+
}
36+
37+
public TouchExampleView(Context context, AttributeSet attrs) {
38+
this(context, attrs, 0);
39+
}
40+
41+
public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
42+
super(context, attrs, defStyle);
43+
mIcon = context.getResources().getDrawable(R.drawable.icon);
44+
mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
45+
46+
mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
47+
}
48+
49+
@Override
50+
public boolean onTouchEvent(MotionEvent ev) {
51+
mDetector.onTouchEvent(ev);
52+
return true;
53+
}
54+
55+
@Override
56+
public void onDraw(Canvas canvas) {
57+
super.onDraw(canvas);
58+
59+
canvas.save();
60+
canvas.translate(mPosX, mPosY);
61+
canvas.scale(mScaleFactor, mScaleFactor);
62+
mIcon.draw(canvas);
63+
canvas.restore();
64+
}
65+
66+
private class GestureCallback implements VersionedGestureDetector.OnGestureListener {
67+
public void onDrag(float dx, float dy) {
68+
mPosX += dx;
69+
mPosY += dy;
70+
invalidate();
71+
}
72+
73+
public void onScale(float scaleFactor) {
74+
mScaleFactor *= scaleFactor;
75+
76+
// Don't let the object get too small or too large.
77+
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
78+
79+
invalidate();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)