Browse Source

Merge pull request #161 from jackpal/android-example

An Android Amber example
Nicolas Petton 12 years ago
parent
commit
a02a205ec8

+ 4 - 0
examples/android/helloamber/.gitignore

@@ -0,0 +1,4 @@
+*.js
+!assets/jquery-1.7.2.min.js
+bin/
+gen/

+ 19 - 0
examples/android/helloamber/AndroidManifest.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="org.amberlang.android.helloamber"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <uses-sdk android:minSdkVersion="8" 
+        android:targetSdkVersion="15" />
+    <uses-permission android:name="android.permission.INTERNET" />
+    <application android:label="@string/app_name" >
+        <activity android:name="HelloAmber"
+                  android:label="@string/app_name"
+                  android:theme="@android:style/Theme.NoTitleBar">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest> 

+ 13 - 0
examples/android/helloamber/HelloAmber.st

@@ -0,0 +1,13 @@
+Object subclass: #HelloAmber
+    instanceVariableNames: ''
+    package: 'HelloAmber'!
+
+!HelloAmber methodsFor: 'accessing'!
+begin
+    "Makes me say hello to the user."
+
+    | msg button |
+    msg := 'Hello world.'.
+    button := window jQuery: '#sayHello'.
+    button click: [button after: '<p>' , msg , '</p>'].
+! !

+ 56 - 0
examples/android/helloamber/Makefile

@@ -0,0 +1,56 @@
+#
+# If you copy this file for an Android Amber project, just
+# modify these first three lines
+# and then add .st files as you please. This Makefile
+# should pick them all up and compile into Program.js.
+#
+
+NAME  := HelloAmber
+PACKAGE := org.amberlang.android.helloamber
+ACTIVITY := HelloAmber
+
+# debug or release
+
+VERSION  := debug
+
+# -O for Closure optimization of js code.
+FLAGS    := 
+APK      := bin/$(NAME)-$(VERSION).apk
+FILE     := Program
+SOURCES  := $(wildcard *.st)
+OBJECTS  := $(patsubst %.st,%.js,$(wildcard *.st))
+FILEJS   := assets/$(FILE).js
+
+# Find files that match a wildcard pattern recursively
+
+rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
+
+JAVA_SOURCES := $(call rwildcard,src/,*.java)
+
+ASSETS := $(call rwildcard,assets/,*)
+
+all: $(APK)
+
+$(FILEJS): $(SOURCES)
+	../../../bin/amberc $(FLAGS) $(SOURCES) $(FILE)
+	mv $(FILE).js $(FILEJS)
+
+$(APK): $(FILEJS) $(ASSETS) $(JAVA_SOURCES) AndroidManifest.xml build.xml
+	ant $(VERSION)
+
+clean:
+	rm -f $(FILEJS) $(OBJECTS) $(APK)
+	rm -rf gen bin
+
+install: $(APK)
+	adb install $(APK)
+
+# If you want to run it in the emulator, start the emulator first with the
+# android command.
+run:    $(APK)
+	adb install -r $(APK)
+	adb shell am start -n $(PACKAGE)/$(PACKAGE).$(ACTIVITY)
+
+# This should fire it up in Chromium (at least under Ubuntu)
+chromium: $(FILEJS)
+	chromium-browser --allow-file-access-from-files assets/index.html

+ 80 - 0
examples/android/helloamber/README.md

@@ -0,0 +1,80 @@
+HelloAmber Android Amber Smalltalk Example
+
+Introduction
+============
+
+This example shows how to create an Android application that is partially
+written in Amber Smalltalk. The Android Java code uses a WebView to load
+and execute the JavaScript code produced by the Amber Smalltalk amberc
+compiler.
+
+You could modify this example to create your own Android Amber Smalltalk
+examples.
+
+Prerequisites
+=============
+
+Install the Android SDK from http://developer.android.com/sdk/index.html
+
+(Tested with Android SDK version 17, should work with any SDK that
+supports Android SDK 8 or above.)
+
+Make sure your PATH includes the android sdk tools and platform-tools
+directories.
+
+If you want to run on a real Android device you will need a phone running
+Android 2.2 (Froyo) or newer. (With some work this restriction could
+be relaxed to support back to Android 1.6, although JavaScript performance
+is not good on earlier Android phones.)
+
+Building
+========
+
+    cd helloamber
+    make
+
+Testing on a Desktop Browser
+============================
+
+If the build is successful, you should be able to use your desktop computer's
+web browser to run the HTML5 part of the Android application. Simply open
+assets/index.html
+
+Installing on an Android Device
+===============================
+
+Check that ADB can see your android device or emulator:
+
+    adb devices
+
+If your device is visible:
+
+    cd helloamber
+    make install
+
+Building, Installing and Running on an Android Device
+=====================================================
+
+    make run
+
+Tips
+====
+
+Javascript errors and console log messages are sent to the Android log.
+Use the adb logcat command to view the Android log.
+
+
+Known Issues
+============
+
+The amberc compiler does not stop the "make" process when a smalltalk
+error is detected. You have to manually scan the build log to see if there
+are any smalltalk errors.
+
+The makefile only supports making "debug" versions of Android applications.
+A release version would be the same, except that it is "signed", which is
+needed in order to publish on Android Market.
+
+The app would start running more quickly if a lighter-weight version of
+JQuery was used.
+

+ 17 - 0
examples/android/helloamber/ant.properties

@@ -0,0 +1,17 @@
+# This file is used to override default values used by the Ant build system.
+#
+# This file must be checked into Version Control Systems, as it is
+# integral to the build system of your project.
+
+# This file is only used by the Ant script.
+
+# You can use this to override default values such as
+#  'source.dir' for the location of your java source folder and
+#  'out.dir' for the location of your output folder.
+
+# You can also use it define how the release builds are signed by declaring
+# the following properties:
+#  'key.store' for the location of your keystore and
+#  'key.alias' for the name of the key to use.
+# The password will be asked during the build when you use the 'release' target.
+

+ 15 - 0
examples/android/helloamber/assets/index.html

@@ -0,0 +1,15 @@
+<!doctype html>
+<html>
+<head>
+    <title>Android Amber HelloWorld</title>
+    <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
+    <script src="Program.js" type="text/javascript"></script>
+    <script type="text/javascript">
+        $(document).ready(function(){smalltalk.HelloAmber._new()._begin()});
+    </script>
+</head>
+<body>
+<h1>HelloAmber</h1>
+      <button id="sayHello">say hello</button>
+</body>
+</html>

File diff suppressed because it is too large
+ 1 - 0
examples/android/helloamber/assets/jquery-1.7.2.min.js


+ 83 - 0
examples/android/helloamber/build.xml

@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project name="HelloAmber" 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 into
+         Version Control Systems. -->
+    <property file="local.properties" />
+
+    <!-- The ant.properties file can be created by you. It is only edited by the
+         'android' tool to add properties to it.
+         This is the place to change some Ant specific build properties.
+         Here are some properties you may want to change/update:
+
+         source.dir
+             The name of the source directory. Default is 'src'.
+         out.dir
+             The name of the output directory. Default is 'bin'.
+
+         For other overridable properties, look at the beginning of the rules
+         files in the SDK, at tools/ant/build.xml
+
+         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 into Version Control Systems.
+
+         -->
+    <property file="ant.properties" />
+
+    <!-- The project.properties file is created and updated by the 'android'
+         tool, as well as ADT.
+
+         This contains project specific properties such as project target, and library
+         dependencies. Lower level build properties are stored in ant.properties
+         (or in .classpath for Eclipse projects).
+
+         This file is an integral part of the build system for your
+         application and should be checked into Version Control Systems. -->
+    <loadproperties srcFile="project.properties" />
+
+    <!-- quick check on sdk.dir -->
+    <fail
+            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
+            unless="sdk.dir"
+    />
+
+    <!--
+        Import per project custom build rules if present at the root of the project.
+        This is the place to put custom intermediary targets such as:
+            -pre-build
+            -pre-compile
+            -post-compile (This is typically used for code obfuscation.
+                           Compiled code location: ${out.classes.absolute.dir}
+                           If this is not done in place, override ${out.dex.input.absolute.dir})
+            -post-package
+            -post-build
+            -pre-clean
+    -->
+    <import file="custom_rules.xml" optional="true" />
+
+    <!-- Import the actual build file.
+
+         To customize existing targets, there are two options:
+         - Customize only one target:
+             - copy/paste the target into this file, *before* the
+               <import> task.
+             - customize it to your needs.
+         - Customize the whole content of build.xml
+             - copy/paste the content of the rules files (minus the top node)
+               into this file, replacing the <import> task.
+             - customize to your needs.
+
+         ***********************
+         ****** IMPORTANT ******
+         ***********************
+         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
+         in order to avoid having your file be overridden by tools such as "android update project"
+    -->
+    <!-- version-tag: 1 -->
+    <import file="${sdk.dir}/tools/ant/build.xml" />
+
+</project>

+ 10 - 0
examples/android/helloamber/local.properties

@@ -0,0 +1,10 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must *NOT* be checked into Version Control Systems,
+# as it contains information specific to your local configuration.
+
+# location of the SDK. This is only used by Ant
+# For customization when using a Version Control System, please read the
+# header note.
+sdk.dir=/Users/jackpal/code/android-sdk-macosx

+ 20 - 0
examples/android/helloamber/proguard-project.txt

@@ -0,0 +1,20 @@
+# To enable ProGuard in your project, edit project.properties
+# to define the proguard.config property as described in that file.
+#
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the ProGuard
+# include property in project.properties.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}

+ 14 - 0
examples/android/helloamber/project.properties

@@ -0,0 +1,14 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-8

+ 6 - 0
examples/android/helloamber/res/layout/main.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/webview"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+/>

+ 4 - 0
examples/android/helloamber/res/values/strings.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="app_name">HelloAmber</string>
+</resources>

+ 59 - 0
examples/android/helloamber/src/org/amberlang/android/helloamber/HelloAmber.java

@@ -0,0 +1,59 @@
+package org.amberlang.android.helloamber;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.webkit.ConsoleMessage;
+import android.webkit.WebChromeClient;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+
+
+public class HelloAmber extends Activity
+{
+    private static final String TAG = "HelloAmber";
+
+    private WebView mWebView;
+
+    private class HelloWebViewClient extends WebViewClient {
+        @Override
+        public boolean shouldOverrideUrlLoading(WebView view, String url) {
+            view.loadUrl(url);
+            return true;
+        }
+    }
+
+    private class HelloWebChromeClient extends WebChromeClient {
+        @Override
+        public boolean onConsoleMessage(ConsoleMessage cm) {
+            Log.d(TAG, cm.message()
+                    + " -- " + cm.messageLevel()
+                    + " From line " + cm.lineNumber()
+                    + " of " + cm.sourceId() );
+            return true;
+        }
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+
+        mWebView = (WebView) findViewById(R.id.webview);
+        mWebView.getSettings().setJavaScriptEnabled(true);
+        mWebView.setWebViewClient(new HelloWebViewClient());
+        mWebView.setWebChromeClient(new HelloWebChromeClient());
+        mWebView.loadUrl("file:///android_asset/index.html");
+    } 
+
+    @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event) {
+        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
+            mWebView.goBack();
+            return true;
+        }
+        return super.onKeyDown(keyCode, event);
+    }
+}

Some files were not shown because too many files changed in this diff