ホーム > デベロッパ > J2ME / MIDP プログラミング > 実機対応と携帯電話の将来展望

第6回: 実機対応と携帯電話の将来展望


001: package com.s_cradle.improve.client; 
002: 
003: import java.io.*; 
004: import com.s_cradle.improve.Constants; 
005: import java.util.*; 
006: import javax.microedition.lcdui.Canvas; 
007: import javax.microedition.lcdui.Displayable; 
008: 
009: /** 
010:  * Copyright (C) 2003 Sophia Cradle, Inc. 
011:  * 
012:  * オペレーション polling を使ったタスクを行うクラスです。 
013:  * 
014:  * @author Masaru Yoshimura 
015:  */ 
016: public class PollingTask extends Task{ 
017:     private static final String TICKERMESSAGE_ATTACKED = "から攻撃されました!"; 
018:     private static final String MESSAGE_ATTACKED = "攻撃されました。"; 
019: 
020:     private Vector newList; 
021:     private Vector messages; 
022: 
023:     private static final String[] ITEMS = new String[]{ 
024:         Constants.PARAMETER_ACCOUNT, Constants.PARAMETER_PASSWORD}; 
025: 
026:     /** 
027:      * コンストラクタ。タスクで使う値(アカウント,パスワード)を引数にとる。 
028:      */ 
029:     public PollingTask(String[] strs) { 
030:         super(TASK_POLLING,ITEMS,new String[]{account,password}); 
031:         newList = new Vector(); 
032:         messages = new Vector(); 
033:     } 
034: 
035:     /** 
036:      * 失敗時の処理 
037:      */ 
038:     protected void processFailed() { 
039:     } 
040: 
041:     /** 
042:      * 返値を読んだ後の処理 
043:      */ 
044:     protected void processAfterReturnValue(
                          int result, DataInputStream input) throws java.io.IOException { 
045:         Vector oldList = ImproveMIDlet.buddyList; 
046: 
047:         short userNumber = (short)(result >> 16); 
048:         short messageNumber = (short)result; 
049: 
050:         for (short i=0; i<userNumber; i++) { 
051:             if( i == 0 ){ 
052:                 Buddy buddy = new Buddy(input); 
053:                 buddy.setAttribute(Buddy.ATTRIBUTE_HIM); 
054:                 newList.addElement(buddy); 
055:             } else{ 
056:                 newList.addElement(new Buddy(input)); 
057:             } 
058:         } 
059: 
060:         for (short i=0; i<messageNumber; i++) { 
061:             messages.addElement(new Message(input)); 
062:         } 
063: 
064:         ImproveMIDlet.callSerially(new Runnable(){ 
065:             public void run(){ 
066:                 Vector oldList = ImproveMIDlet.buddyList; 
067:                 Vector buddyList = new Vector(); 
068:                 Hashtable hush = new Hashtable(); 
069: 
070:                 for(Enumeration e=newList.elements();e.hasMoreElements();){ 
071:                     Buddy newBuddy = (Buddy)e.nextElement(); 
072:                     if( oldList.indexOf(newBuddy) >= 0 ){ 
073:                         Buddy oldBuddy = (Buddy)oldList.elementAt(
                                                             oldList.indexOf(newBuddy)); 
074:                         oldBuddy.update(newBuddy); 
075:                         buddyList.addElement(oldBuddy); 
076:                         hush.put(oldBuddy.getAccount(),oldBuddy); 
077:                    } else{ 
078:                         buddyList.addElement(newBuddy); 
079:                         hush.put(newBuddy.getAccount(),newBuddy); 
080:                     } 
081:                 } 
082: 
083:                 for(Enumeration e=messages.elements();e.hasMoreElements();){ 
084:                     Message m = (Message)e.nextElement(); 
085:                     Buddy buddy = (Buddy)hush.get(m.getAccount()); 
086:                     if( m.getFrom() == m.FROM_ME){ 
087:                         if(buddy != null){ 
088:                             if(buddy.getMessageBox() == null){ 
089:                                 new MessagingCanvas(buddy); 
090:                             } 
091:                             buddy.getMessageBox().addMessage(
                                                m.getContent(),MessageBox.MESSAGE_MINE); 
092:                         } 
093:                     } else if(m.getFrom() == m.FROM_OTHER){ 
094:                         if( m.getType() == Constants.MESSAGE_TYPE_NORMAL){ 
095:                             if( buddy != null){ 
096:                                 if(buddy.getMessageBox() == null){ 
097:                                     new MessagingCanvas(buddy); 
098:                                 } 
099:                                 buddy.getMessageBox().addMessage(
                                               m.getContent(),MessageBox.MESSAGE_OTHER); 
100:                             } 
101:                         } else if(m.getType() == Constants.MESSAGE_TYPE_ATTACK){ 
102:                             ImproveMIDlet.listCanvas.ticker.insert(
                              Ticker.LEVEL_NORMAL,m.getAccount()+TICKERMESSAGE_ATTACKED); 
103:                             if( buddy != null){ 
104:                                 if(buddy.getMessageBox() == null){ 
105:                                     new MessagingCanvas(buddy); 
106:                                 } 
107:                                 buddy.getMessageBox().addMessage(
                                             MESSAGE_ATTACKED,MessageBox.MESSAGE_OTHER); 
108:                             } 
109:                         } 
110:                     } 
111:                 } 
112: 
113:                 ImproveMIDlet.buddyList = buddyList; 
114:                 Displayable d = ImproveMIDlet.getCurrent(); 
115:                 if( d instanceof Canvas ){ 
116:                     ((Canvas)d).repaint(); 
117:                 } 
118:                 newList = new Vector(); 
119:                 messages = new Vector(); 
120:             } 
121:         }); 
122:     } 
123: 
124:     /** 
125:      * 自分の投げたメッセージを追加する。 
126:      */ 
127:     public void addOwnMessage(String target,String content){ 
128:         messages.addElement(new Message(target,content)); 
129:     } 
130: }