ホーム > デベロッパ > J2ME / MIDP プログラミング > 実機に載せよう

第4回:実機に載せよう(ダウンサイズ)

001: //package com.s_cradle.improve.client;
002: 
003: //import com.s_cradle.improve.Constants;
004: import java.util.Timer;
005: import java.util.TimerTask;
006: import java.io.*;
007: import java.util.*;
008: import javax.microedition.io.*;
009: import javax.microedition.lcdui.TextBox;
010: import javax.microedition.lcdui.TextField;
011: import javax.microedition.lcdui.Graphics;
012: import javax.microedition.lcdui.Canvas;
013: import javax.microedition.lcdui.Font;
014: import javax.microedition.lcdui.Command;
015: 
016: /**
017:  * Copyright (C) 2003 Sophia Cradle, Inc.
018:  *
019:  * Timer を持ち、TimerTask を継承するクラスです。
020:  *
021:  * @author Masaru Yoshimura
022:  */
023: public abstract class Z/*Task*/ extends TimerTask {
024:     //downsize 定数展開
025:     /*
026:     // タスクの状況
027:     public static final int STATUS_NOT_YET    = 0;
028:     public static final int STATUS_PROCESSING = 1;
029:     public static final int STATUS_SUCCEEDED  = 2;
030:     public static final int STATUS_FAILED     = 3;
031: 
032:     // タスクID
033:     public static final int TASK_CREATE  = 0;
034:     public static final int TASK_DELETE  = 1;
035:     public static final int TASK_ADD     = 2;
036:     public static final int TASK_REMOVE  = 3;
037:     public static final int TASK_SET     = 4;
038:     public static final int TASK_SEND    = 5;
039:     public static final int TASK_POLLING = 6;
040:     public static final int TASK_TEST    = 7;
041:     public static final int TASK_CHECK   = 8;
042: 
043:     // ポーリング間隔
044:     public static final int POLLING_PERIOD = 5000;
045:     */
046: 
047:     //downsize メソッドインライン化
048:     protected static String[] D/*strs*/ = new String[2];
049:     //downsize 配列化
050:     //protected static String account;
051:     //protected static String password;
052: 
053:     private static Timer OK/*timer*/ = new Timer();
054:     private String[] SJIS/*operation*/ = new String[]{
055:         "/create"/*Constants.OPERATION_CREATE*/,"/delete"/*
                                                            Constants.OPERATION_DELETE*/,
056:         "/add"/*Constants.OPERATION_ADD*/,"/remove"/*Constants.OPERATION_REMOVE*/,
057:         "/set"/*Constants.OPERATION_SET*/, "/send"/*Constants.OPERATION_SEND*/,
058:         "/polling"/*Constants.OPERATION_POLLING*/, "/test"/*
                                                              Constants.OPERATION_TEST*/,
059:         "/check"/*Constants.OPERATION_CHECK*/
060:     };
061: 
062:     protected String[] B/*parameterValues*/;
063:     protected String[] A/*parameterNames*/;
064: 
065:     //downsize 未使用field削除
066:     //protected volatile int status;
067:     //private volatile Object returnValue = null;
068: 
069:     protected int E/*task*/;
070: 
071:     protected Z/*Task*/(){ //初期化は子でしましょう。
072:     }
073: 
074:     //protected Task(int task, String[] names, String[] values) {
075:     protected void StackMap/*initializeTask*/(
                                             int task, String[] names, String[] values) {
076:         this.E/*task*/ = task;
077:         this.A/*parameterNames*/ = names;
078:         this.B/*parameterValues*/ = values;
079:         //status = 0/*STATUS_NOT_YET*/;
080: 
081:         if (names.length != values.length) {
082:             throw new IllegalArgumentException(
                                       "number of argument names and values not match!");
083:         }
084:     }
085: 
086:     //downsize メソッドインライン化
087:     /*
088:     public static void setAccount(String account_){
089:         account = account_;
090:     }
091: 
092:     public static String getAccount(){
093:         return account;
094:     }
095: 
096:     public static void setPassword(String password_){
097:         password = password_;
098:     }
099: 
100:     public static String getPassword(){
101:         return password;
102:     }
103:     */
104: 
105:     public static Z/*Task*/ Code/*getFirstTask*/(){
106:         I/*ImproveMIDlet*//*ResourceManager*/.I/*
                                                getAccountAndPasswordFromRecordStore*/();
107: 
108:         if(D/*strs*/[0]/*account*/ != null && D/*
                                                    strs*/[0]/*account*/.length() != 0 &&
109:            D/*strs*/[1]/*password*/ != null && D/*
                                                    strs*/[1]/*password*/.length() != 0){
110:             I/*ImproveMIDlet*/.Code/*setCurrent*/(
                                              I/*ImproveMIDlet*/.StackMap/*listCanvas*/);
111:             Z/*getTask*/(4/*Task.TASK_SET*/,new String[]{
112:                     String.valueOf(4/*Constants.ITEM_INDEX_STATUS*/),
113:                     String.valueOf(3/*Constants.STATUS_BLUE*/)})
114:                 .StackMap/*schedule*/(0);
115:             return Z/*getTask*/(6/*TASK_POLLING*/,null);
116:         }
117:         else{
118:             TextBox textBox = new TextBox(
                      "ログイン画面","",10/*Constants.ACCOUNT_MAX*/,TextField.EMAILADDR);
119:             textBox.addCommand(I/*ImproveMIDlet*/.A/*okCommand*/);
120:             textBox.setCommandListener(I/*ImproveMIDlet*/.I/*midlet*/);
121: 
122:             I/*ImproveMIDlet*/.Code/*setCurrent*/(textBox);
123:             return null;
124:         }
125:     }
126: 
127:     public static Z/*Task*/ Z/*getTask*/(int task, String[] str){
128:         switch(task){
129:             case 0/*TASK_CREATE*/:
130:                 D/*CreateTask*/ createTask = new D/*CreateTask*/();
131:                 createTask.D/*initializeCreateTask*/(str);
132:                 return createTask;
133:             case 1/*TASK_DELETE*/:
134:                 E/*DeleteTask*/ delete = new E/*DeleteTask*/();
135:                 delete.E/*initializeDeleteTask*/(str);
136:                 return delete;
137:             case 2/*TASK_ADD*/:
138:                 return new F/*FollowedByPollingTask*/(
                                               0/*FollowedByPollingTask.FBPT_ADD*/,str);
139:             case 3/*TASK_REMOVE*/:
140:                 return new F/*FollowedByPollingTask*/(
                                            1/*FollowedByPollingTask.FBPT_REMOVE*/,str);
141:             case 4/*TASK_SET*/:
142:                 return new F/*FollowedByPollingTask*/(
                                               2/*FollowedByPollingTask.FBPT_SET*/,str);
143:             case 5/*TASK_SEND*/:
144:                 return new F/*FollowedByPollingTask*/(
                                              3/*FollowedByPollingTask.FBPT_SEND*/,str);
145:             case 6/*TASK_POLLING*/:
146:                 return new N/*PollingTask*/(str);
147:         }
148: 
149:         return null;
150:     }
151: 
152:     public void run(){
153:         try{
154:             System.out.println(">>run:"+E/*task*/);
155:             HttpConnection http = null;
156:             DataInputStream input = null;
157:             int result;
158:             //status = 1/*STATUS_PROCESSING*/;
159: 
160:             char sep = '?';
161:             StringBuffer buf = new StringBuffer();
162:             String temp = I/*ImproveMIDlet*/.I/*
               midlet*/.getAppProperty("MIDlet-X-AllowURL-1"/*ImproveMIDlet.ALLOW_URL*/);
163:             buf.append(temp.substring(0, temp.length()-1));
164:             buf.append(SJIS/*operation*/[E/*task*/]);
165:             for (int i=0; i<B/*parameterValues*/.length; i++) {
166:                 buf.append(sep).append(A/*parameterNames*/[i]).append('=').append(
                                                   D/*encode*/(B/*parameterValues*/[i]));
167:                 if (i==0) {
168:                     sep = '&';
169:                 }
170:             }
171:
172:             try {
173:                 http = (HttpConnection)Connector.open(buf.toString());
174:                 input = http.openDataInputStream();
175:                 result = input.readInt();
176:                 if (result > 0){
177:                     Code/*processAfterReturnValue*/(result, input);
178:                 }
179:             } catch (IOException ioe) {
180:                 ioe.printStackTrace();
181:                 result = -1;
182:             }
183: 
184:             try {
185:                 if (input != null) {
186:                     input.close();
187:                 }
188:                 if (http != null) {
189:                     http.close();
190:                 }
191: 
192:             } catch (IOException ioe) {
193:                 ioe.printStackTrace();
194:             }
195: 
196:             //status = (result >=0) ? 2/*STATUS_SUCCEEDED*/ : 3/*STATUS_FAILED*/;
197:             if(result < 0){
198:                 Z/*processFailed*/();
199:             }
200: 
201:             System.out.println("<<run");
202:         } catch(Throwable a){
203:             I/*processThrowable*/();
204:             a.printStackTrace();
205:         }
206:         return;
207:     }
208: 
209:     /**
210:      * URLエンコードします。エンコード時にShift-JISを用います。
211:      *
212:      * @param in エンコードして欲しい文字列
213:      * @return エンコードされた文字列
214:      */
215:     public static String D/*encode*/(String in) {
216:         StringBuffer inBuf = new StringBuffer(in);;
217:         StringBuffer outBuf = new StringBuffer();
218:         for (int i=0;i<inBuf.length(); i++) {
219:             char temp = inBuf.charAt(i);
220:             if (('a'<=temp && temp <='z')
221:                 || ('A'<=temp && temp <='Z')
222:                 || ('0'<=temp && temp <='9')
223:                 || temp == '.' || temp == '-' || temp == '*' || temp == '_') {
224:                 outBuf.append(temp);
225:             } else if (temp == ' ') {
226:                 outBuf.append('+');
227:             } else {
228:                 byte[] bytes;
229:                 try {
230:                     bytes = new String(new char[]{temp}).getBytes("SJIS");
231:                     for(int j=0;j<bytes.length;j++) {
232:                         int high = (bytes[j]>>>4)&0x0F;
233:                         int low  = (bytes[j]&0x0F);
234:                         outBuf.append('%');
235:                         outBuf.append(Integer.toString(high, 16).toUpperCase());
236:                         outBuf.append(Integer.toString(low , 16).toUpperCase());
237:                     }
238:                 } catch (Exception e) {
239:                 }
240:             }
241:         }
242:         return outBuf.toString();
243:     }
244: 
245:     protected void I/*processThrowable*/(){
246:         System.out.println("Task.processThrowable");
247:         Z/*processFailed*/();
248:     }
249: 
250:     /** 失敗時の処理 */
251:     protected void Z/*processFailed*/(){
252:     }
253: 
254:     /** 返値を読んだ後の処理 */
255:     protected void Code/*processAfterReturnValue*/(
                                   int result, DataInputStream input) throws IOException{
256:     }
257: 
258:     /** 返り値の取得 */
259:     //downsize 未使用メソッド
260:     /*
261:     public Object getReturnValue() {
262:         return returnValue;
263:     }*/
264: 
265:     /** スケジュール */
266:     public void StackMap/*schedule*/(long delay) {
267:         OK/*timer*/.schedule(this, delay);
268:     }
269: 
270:     /** スケジュール */
271:     public void StackMap/*schedule*/(long delay, long period) {
272:         OK/*timer*/.schedule(this, delay, period);
273:     }
274: 
275:     /** タイマのキャンセル */
276:     //downsize 未使用メソッド
277:     /*
278:     public static void cancelTimer() {
279:         timer.cancel();
280:     }
281:     */
282: 
283:     /* タスクの処理状況の取得 */
284:     //downsize 未使用メソッド
285:     /*
286:     public int getProcessorStatus() {
287:         return status;
288:     }
289:     */
290: 
291:    //downsize クラス融合 以下はMenu
292: 
293:    //downsize 定数展開
294:    /*
295:    protected static final int LIST_BG_COLOR = 0x00DDDDFF;
296:    private static final int COLOR_FRAME     = 0x000000;
297:    private static final int COLOR_SHADOW    = 0x444444;
298:    private static final int LEFT_INTERVAL = 5;
299:    private static final int RIGHT_INTERVAL = 5;
300:    protected static final int COLUMN_HEIGHT = 17;
301:    protected static final int STRING_MARGIN_HEIGHT = 3;
302:    */
303: 
304:    protected static final Command StackMap/*okCommand*/ = new Command(
                                                                     "OK",Command.OK,1);
305:    protected static final Command Code/*cancelCommand*/ = new Command(
                                                              "キャンセル",Command.CANCEL,1);
306: 
307:    private String setColor/*title*/;
308:    //downsize クラス融合 items>strs。
309:    private String[] ログイン画面/*items*/;
310:    //downsize 配列化
311:    protected int[] Z/*idata*/ = new int[3];
312:    /*
313:    protected int cursor;
314:    private int width;
315:    private int height;
316:    */
317:   private boolean キャンセル/*containsTitleWidth*/;
318: 
319:    /**
320:     * コンストラクタ
321:     */
322:    //public Menu(String title_, String[] items_, boolean containsTitleWidth_) {
323:    public void E/*initializeMenu*/(
                          String title_, String[] items_, boolean containsTitleWidth_) {
324:        Z/*idata*/[0]/*cursor*/ = 0;
325:        キャンセル/*containsTitleWidth*/ = containsTitleWidth_;
326:        if( title_ != null && items_ != null ){
327:            OK/*setTitleAndItems*/(title_, items_);
328:        }
329:    }
330: 
331:    /**
332:     * コンストラクタ
333:     */
334:    //public Menu(String title_, String[] items_) {
335:    public void Z/*initializeMenu*/(String title_, String[] items_) {
336:        E/*initializeMenu*/(title_, items_, false);
337:    }
338: 
339:    /**
340:     * タイトルと項目の設定
341:     */
342:    public void OK/*setTitleAndItems*/(String title_, String[] items_){
343:        setColor/*title*/ = title_;
344:        ログイン画面/*items*/ = items_;
345: 
346:        Font font = Font.getDefaultFont();
347:        Z/*idata*/[1]/*width*/ = (
                キャンセル/*containsTitleWidth*/) ? font.stringWidth(setColor/*title*/)+2 : 0;
348:        for(int i=0;i<ログイン画面/*items*/.length;i++){
349:            int temp = 5/*LEFT_INTERVAL*/+font.stringWidth(
                                           ログイン画面/*items*/[i])+5/*RIGHT_INTERVAL*/;
350:            if(Z/*idata*/[1]/*width*/< temp){
351:                Z/*idata*/[1]/*width*/ = temp;
352:            }
353:        }
354:        Z/*idata*/[2]/*height*/ = 17/*COLUMN_HEIGHT*/ *(
                                                       1+ログイン画面/*items*/.length)+1;
355:    }
356: 
357:    /**
358:     * キーハンドラ
359:     */
360:    public Z/*Task*//*Menu*/ A/*processKeyRelease*/(int gameAction, int keyCode){
361:        switch( gameAction ){
362:           case Canvas.FIRE:
363:                return StackMap/*processFireKeyRelease*/();
364:            case Canvas.DOWN:
365:                Z/*idata*/[0]/*cursor*/++;
366:                if( Z/*idata*/[0]/*cursor*/ >= ログイン画面/*items*/.length ){
367:                    Z/*idata*/[0]/*cursor*/ --;
368:                }
369:                break;
370:            case Canvas.UP:
371:                Z/*idata*/[0]/*cursor*/--;
372:                if( Z/*idata*/[0]/*cursor*/ < 0 ){
373:                    Z/*idata*/[0]/*cursor*/ ++;
374:                }
375:                break;
376:            default:
377:                break;
378:        }
379: 
380:        return this;
381:    }
382: 
383:    /**
384:     * キーハンドラ
385:     */
386:    protected Z/*Task*//*Menu*/ StackMap/*processFireKeyRelease*/(){
387:        return null;
388:    }
389: 
390:    /**
391:     * 描画
392:     */
393:    public void I/*draw*/(Graphics g, int width_, int height_){
394:        g.translate((width_-Z/*idata*/[1]/*width*/)/2, (
                                                     height_-Z/*idata*/[2]/*height*/)/2);
395: 
396:        StackMap/*draw3DRect*/(
                          g,0,0,Z/*idata*/[1]/*width*/,Z/*idata*/[2]/*
                             height*/,0x00DDDDFF/*LIST_BG_COLOR*/,0x00EEEEFF,0x00000080);
397:        Code/*drawFrameShadow*/(
                          g,0,0,Z/*idata*/[1]/*width*/,Z/*idata*/[2]/*height*/,0x000000/*
                                                COLOR_FRAME*/, 0x444444/*COLOR_SHADOW*/);
398: 
399:        g.setClip(1,1,Z/*idata*/[1]/*width*/-2,Z/*idata*/[2]/*height*/-2);
400: 
401:        g.setColor(0x00646464);
402:        g.fillRect(1, 1, Z/*idata*/[1]/*width*/-2, 17/*COLUMN_HEIGHT*/);
403:        g.setColor(0x00CCCCCC);
404:        g.drawString(setColor/*title*/,2,4, Graphics.LEFT | Graphics.TOP);
405: 
406:        for(int i=0;i<ログイン画面/*items*/.length;i++){
407:            if(Z/*idata*/[0]/*cursor*/ == i){
408:                g.setColor(0x0000FF);
409:                g.fillRect(1, 17/*COLUMN_HEIGHT*/ *(i+1), Z/*
                                             idata*/[1]/*width*/-2, 17/*COLUMN_HEIGHT*/);
410:                g.setColor(0xFFFFFF);
411:            } else{
412:                g.setColor(0x00000000);
413:            }
414:            g.drawString(ログイン画面/*items*/[i],5/*LEFT_INTERVAL*/,17/*
         COLUMN_HEIGHT*/ *(i+1)+3/*STRING_MARGIN_HEIGHT*/, Graphics.LEFT | Graphics.TOP);
415:        }
416:        g.translate(-g.getTranslateX(), -g.getTranslateY());
417:        g.setClip(0,0,width_,height_);
418:    }
419: 
420:    /**
421:     * 明暗付の矩形の描画
422:     */
423:    protected static void StackMap/*draw3DRect*/(
                                             Graphics graph, int x,int y, int dx,int dy,
424:                                    int base,int light, int shadow){
425:        graph.setColor(base);
426:        graph.fillRect(x,y,dx,dy);
427:        graph.setColor(light);
428:        graph.drawLine(x,y,x + dx - 2,y);
429:        graph.drawLine(x,y,x,y + dy - 2);
430:        graph.setColor(shadow);
431:        graph.drawLine(x + dx - 1,y + 1,x + dx - 1,y + dy - 1);
432:        graph.drawLine(x + 1,y + dy - 1,x + dx - 1,y + dy - 1);
433:        return;
434:    }
435: 
436:    /**
437:     * 影の描画
438:     */
439:    protected static void Code/*drawFrameShadow*/(
                                                    Graphics g,int x, int y, int width,
440:            int height, int frame, int shadow)
441:    {
442:        g.setColor(frame);
443:        g.drawRect(x-1,y-1,width+1,height+1);
444:        g.setColor(shadow);
445:        g.drawLine(x,y + height + 1,x + width + 1,y + height + 1);
446:        g.drawLine(x + width + 1,y,x + width + 1,y + height + 1);
447:     }
448: }