001: /* Copyright 2007-2008 David N. Welton - DedaSys LLC - http://www.dedasys.com
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl.android;
017:
018: import java.lang.reflect.Field;
019:
020: import java.util.Hashtable;
021:
022: import android.app.Activity;
023: import android.app.NotificationManager;
024:
025: import android.content.Intent;
026:
027: import android.database.Cursor;
028:
029: import android.net.ContentURI;
030:
031: import android.view.View;
032:
033: import android.util.Log;
034:
035: import org.hecl.DoubleThing;
036: import org.hecl.HeclException;
037: import org.hecl.Interp;
038: import org.hecl.IntThing;
039: import org.hecl.ListThing;
040: import org.hecl.LongThing;
041: import org.hecl.ObjectThing;
042: import org.hecl.Operator;
043: import org.hecl.RealThing;
044: import org.hecl.StringThing;
045: import org.hecl.Thing;
046:
047: import org.hecl.java.HeclJavaCmd;
048: import org.hecl.java.JavaCmd;
049:
050: /**
051: * The <code>AndroidCmd</code> class is where all the Android specific
052: * commands go.
053: *
054: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
055: * @version 1.0
056: */
057: public class AndroidCmd extends Operator {
058: protected static Hecl hecl = null;
059:
060: public static final int ACTIVITY = 0;
061: public static final int EXIT = 1;
062: public static final int RESLOOKUP = 2;
063: public static final int ALERT = 3;
064: public static final int FINDVIEW = 4;
065: public static final int LOG = 4;
066: public static final int NULLCMD = 5;
067: public static final int MENUSETUP = 6;
068: public static final int MENUCALLBACK = 7;
069:
070: public static final int PROVIDERQUERY = 8;
071:
072: /* These will eventually be moved elsewhere - the core most
073: * likely. */
074: public static final int TOINT = 100;
075: public static final int TOLONG = 101;
076: public static final int TODOUBLE = 102;
077: public static final int TOSTR = 103;
078:
079: protected AndroidCmd(int cmdcode, int minargs, int maxargs) {
080: super (cmdcode, minargs, maxargs);
081: }
082:
083: private void makeAlert(String msg) {
084: NotificationManager nm = (NotificationManager) hecl
085: .getSystemService(Activity.NOTIFICATION_SERVICE);
086: nm
087: .notifyWithText(1, msg,
088: NotificationManager.LENGTH_LONG, null);
089: hecl.showAlert("Hecl Alert", msg, "dismiss", false);
090: }
091:
092: /**
093: * The <code>setCurrentHecl</code> method makes AndroidCmd contain
094: * the correct Hecl class to point at. This is used for SubHecls.
095: *
096: * @param h a <code>Hecl</code> value
097: */
098: public static void setCurrentHecl(Hecl h) {
099: hecl = h;
100: }
101:
102: public Thing operate(int cmd, Interp interp, Thing[] argv)
103: throws HeclException {
104: switch (cmd) {
105: case ACTIVITY:
106: return ObjectThing.create(hecl);
107:
108: case ALERT:
109: makeAlert(argv[1].toString());
110: return new Thing(argv[1].toString());
111: case EXIT:
112: hecl.finish();
113: return null;
114: case RESLOOKUP:
115: String pieces[] = argv[1].toString().split("\\.");
116: String classname = null;
117: Thing result = null;
118: String fieldname = null;
119: if (pieces.length != 3 || !pieces[0].equals("R")) {
120: /* It's probably one of the built in ones. */
121: classname = "android.R$";
122: classname += pieces[2];
123: fieldname = pieces[3];
124: } else {
125: classname = "org.hecl.android." + "R$" + pieces[1];
126: fieldname = pieces[2];
127: }
128: try {
129: int id = 0;
130: Class c = Class.forName(classname);
131: Field f = c.getField(fieldname);
132: id = f.getInt(c);
133: return IntThing.create(id);
134: } catch (Exception e) {
135: throw new HeclException(
136: "Couldn't find a match for classname: "
137: + classname + " : " + e.toString());
138: }
139: case LOG:
140: Log.v("hecl log", argv[1].toString());
141: return null;
142:
143: case PROVIDERQUERY:
144: Cursor cur = null;
145: try {
146: ContentURI curi = new ContentURI(argv[1].toString());
147: cur = hecl.managedQuery(curi, null, null, null);
148: } catch (Exception e) {
149: Hecl.logStacktrace(e);
150: throw new HeclException(argv[0].toString() + " error: "
151: + e.toString());
152: }
153: return ObjectThing.create(cur);
154:
155: case NULLCMD:
156: return ObjectThing.create(null);
157:
158: case MENUSETUP:
159: hecl.menuvar = argv[1];
160: hecl.menucode = argv[2];
161: return null;
162: case MENUCALLBACK:
163: hecl.menucallbackvar = argv[1];
164: hecl.menucallbackcode = argv[2];
165: return null;
166:
167: case TOINT:
168: return IntThing.create(IntThing.get(argv[1]));
169: case TOLONG:
170: return LongThing.create(LongThing.get(argv[1]));
171: case TODOUBLE:
172: return DoubleThing.create(DoubleThing.get(argv[1]));
173: case TOSTR:
174: return StringThing.create(StringThing.get(argv[1]));
175:
176: default:
177: throw new HeclException("Unknown android command '"
178: + argv[0].toString() + "' with code '" + cmd + "'.");
179:
180: }
181: }
182:
183: private static Hashtable cmdtable = new Hashtable();
184:
185: static {
186: try {
187: cmdtable.put("activity", new AndroidCmd(ACTIVITY, 0, 0));
188:
189: cmdtable.put("exit", new AndroidCmd(EXIT, 0, 0));
190: cmdtable.put("reslookup", new AndroidCmd(RESLOOKUP, 1, 1));
191: cmdtable.put("alert", new AndroidCmd(ALERT, 1, 1));
192: cmdtable.put("findview", new AndroidCmd(FINDVIEW, 1, 1));
193: cmdtable.put("androidlog", new AndroidCmd(LOG, 1, 1));
194:
195: cmdtable.put("query", new AndroidCmd(PROVIDERQUERY, 1, 1));
196:
197: cmdtable.put("null", new AndroidCmd(NULLCMD, 0, 0));
198:
199: cmdtable.put("menusetup", new AndroidCmd(MENUSETUP, 2, 2));
200: cmdtable.put("menucallback", new AndroidCmd(MENUCALLBACK,
201: 2, 2));
202:
203: cmdtable.put("i", new AndroidCmd(TOINT, 1, 1));
204: cmdtable.put("l", new AndroidCmd(TOLONG, 1, 1));
205: cmdtable.put("d", new AndroidCmd(TODOUBLE, 1, 1));
206: cmdtable.put("s", new AndroidCmd(TOSTR, 1, 1));
207: } catch (Exception e) {
208: e.printStackTrace();
209: Log.v("hecl", "Can't create android commands.");
210: }
211: }
212:
213: public static void load(Interp ip, Hecl a) throws HeclException {
214: hecl = a;
215: Operator.load(ip, cmdtable);
216: HeclJavaCmd.load(ip);
217:
218: JavaCmd.load(ip, "android.app.Activity", "androidactivity");
219:
220: JavaCmd.load(ip, "android.content.Intent", "intent");
221:
222: JavaCmd.load(ip, "android.database.Cursor", "cursor");
223:
224: JavaCmd.load(ip, "android.view.Menu", "menu");
225: JavaCmd.load(ip, "android.view.Menu$Item", "menuitem");
226: JavaCmd.load(ip, "android.view.View", "view");
227:
228: JavaCmd.load(ip, "android.widget.AdapterView", "adapterview");
229:
230: JavaCmd.load(ip, "android.widget.ArrayAdapter", "arrayadapter");
231: JavaCmd.load(ip, "android.widget.Button", "button");
232: JavaCmd.load(ip, "android.widget.CheckBox", "checkbox");
233: JavaCmd.load(ip, "android.widget.EditText", "edittext");
234: JavaCmd.load(ip, "android.widget.LinearLayout", "linearlayout");
235: JavaCmd.load(ip, "android.widget.LinearLayout$LayoutParams",
236: "linearlayoutparams");
237: JavaCmd.load(ip, "android.widget.ListView", "listview");
238: JavaCmd.load(ip, "android.widget.RadioGroup$LayoutParams",
239: "radiogrouplayoutparams");
240: JavaCmd.load(ip, "android.widget.ProgressBar", "progressbar");
241: JavaCmd.load(ip, "android.widget.RadioButton", "radiobutton");
242: JavaCmd.load(ip, "android.widget.RadioGroup", "radiogroup");
243: JavaCmd.load(ip, "android.widget.ScrollView", "scrollview");
244: JavaCmd.load(ip, "android.widget.Spinner", "spinner");
245: JavaCmd.load(ip, "android.widget.TextView", "textview");
246: JavaCmd.load(ip, "android.widget.TimePicker", "timepicker");
247:
248: JavaCmd.load(ip, "org.hecl.android.HeclCallback", "callback");
249: JavaCmd.load(ip, "org.hecl.android.Hecl", "hecl");
250: JavaCmd.load(ip, "org.hecl.android.SubHecl", "subhecl");
251:
252: HeclCallback.interp = ip;
253: }
254:
255: public static void unload(Interp ip) throws HeclException {
256: Operator.unload(ip, cmdtable);
257: JavaCmd.unload(ip);
258: }
259:
260: }
|