chatmidlet.java
来自「These notes are only intended as a brief」· Java 代码 · 共 405 行
JAVA
405 行
//
// Copyright 2002 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at any time,
// without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation. Java and all Java-based marks are trademarks or registered
// trademarks of Sun Microsystems, Inc. Other product and company names
// mentioned herein may be trademarks or trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
//
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ChatMIDlet
extends MIDlet
implements IncomingTextMessageListener,
OutgoingTextMessageListener,
CommandListener
{
private final static PhoneBook phoneBook = PhoneBook.getPhoneBook();
private static SmsHandler smsHandler = null;
private String port = null;
private ChatScreen chatScreen = null;
private WriteMessageScreen writeMessageScreen = null;
private PeerList peerList = null;
private PeerScreen peerScreen = null;
private String phoneNumbers[];
private String prompt;
public ChatMIDlet()
{
}
public void startApp()
{
Displayable current = Display.getDisplay(this).getCurrent();
if (current == null)
{
if (setSmsHandler())
{
InputNameScreen inputNameScreen =
new InputNameScreen(this);
Display.getDisplay(this).setCurrent(inputNameScreen);
}
else
{
Form quitScreen = new Form("Error");
quitScreen.append("Neither WMA nor " +
"Nokia SMS API is available!");
quitScreen.addCommand(new Command("Quit", Command.EXIT, 2));
quitScreen.setCommandListener(this);
Display.getDisplay(this).setCurrent(quitScreen);
}
}
else
{
Display.getDisplay(this).setCurrent(current);
}
}
private boolean setSmsHandler()
{
port = getAppProperty("Default-Port");
try
{
Class.forName("javax.wireless.messaging.Message");
smsHandler =
(SmsHandler) Class.forName("WmaSmsHandler").newInstance();
}
catch (Exception e)
{
}
if (smsHandler == null)
{
try
{
Class.forName("com.nokia.mid.messaging.Message");
smsHandler =
(SmsHandler) Class.forName("NokiaSmsHandler").newInstance();
}
catch (Exception e)
{
}
String attr = getAppProperty("Nokia-SMS-Handler-1");
if (attr != null)
{
port = attr.substring(attr.indexOf(':') + 4,
attr.indexOf(','));
}
}
return (smsHandler != null);
}
public void commandAction(Command c, Displayable d)
{
exitRequested();
}
// InputNameScreen Callbacks
void inputNameScreenDone(String chatName)
{
if (smsHandler != null)
{
ErrorScreen.init(null, Display.getDisplay(this));
smsHandler.init(this, this, port);
prompt = "<" + chatName + ": ";
chatScreen = new ChatScreen(this, prompt);
Display.getDisplay(this).setCurrent(chatScreen);
}
else
{
exitRequested();
}
}
void inputNameScreenQuit()
{
exitRequested();
}
// ChatScreen Callbacks
void chatScreenChoosePeer()
{
choosePeer();
}
void chatScreenWriteMessage()
{
writeMessage(null);
}
void chatScreenQuit()
{
exitRequested();
}
private void choosePeer()
{
String[] peerNames = phoneBook.getNames();
if (peerNames != null)
{
if (peerList == null)
{
peerList = new PeerList(this, peerNames);
}
else
{
peerList.init(peerNames);
}
Display.getDisplay(this).setCurrent(peerList);
}
else
{
updatePhoneBook(null);
}
}
private void updatePhoneBook(String name)
{
if (peerScreen == null)
{
peerScreen = new PeerScreen(this);
}
String phoneNumber = (name == null) ?
null : phoneBook.getPhoneNumber(name);
peerScreen.init(name, phoneNumber);
Display.getDisplay(this).setCurrent(peerScreen);
}
private void writeMessage(String[] peerNames)
{
if (writeMessageScreen == null)
{
writeMessageScreen = new WriteMessageScreen(this);
}
if (peerNames == null)
{
peerNames = phoneBook.getNames(phoneNumbers);
}
writeMessageScreen.setTitle(peerNames);
Display.getDisplay(this).setCurrent(writeMessageScreen);
}
// PeerList screen Callbacks
void peerListBack()
{
Display.getDisplay(this).setCurrent(chatScreen);
}
void peerListEdit(String name)
{
updatePhoneBook(name);
}
void peerListDone(String[] peerNames)
{
if (peerNames != null)
{
phoneNumbers = new String[peerNames.length];
for (int i = phoneNumbers.length - 1; i >= 0; i--)
{
phoneNumbers[i] = phoneBook.getPhoneNumber(peerNames[i]);
}
chatScreen.enableWriteCommand();
writeMessage(peerNames);
}
else
{
Display.getDisplay(this).setCurrent(chatScreen);
}
}
void peerListNew()
{
updatePhoneBook(null);
}
void peerListRemove(String[] names)
{
phoneBook.removeEntries(names);
}
// PeerScreen callbacks
void peerScreenDone(String name, String phoneNumber)
{
phoneBook.update(name, phoneNumber);
choosePeer();
}
void peerScreenBack()
{
if (peerList != null && peerList.size() > 0)
{
Display.getDisplay(this).setCurrent(peerList);
}
else
{
Display.getDisplay(this).setCurrent(chatScreen);
}
}
// WriteMessageScreen callbacks
void writeMessageScreenDone(String content)
{
Display.getDisplay(this).setCurrent(chatScreen);
if (content != null)
{
String message = prompt + content;
chatScreen.update(message);
smsHandler.sendMessage(phoneNumbers, message);
}
}
void writeMessageScreenBack()
{
Display.getDisplay(this).setCurrent(chatScreen);
}
// IncomingTextMessageListener interface methods
public void handleIncomingMessage(String phoneNumber, String content)
{
String fromName = content.substring(1, content.indexOf(':'));
fromName = phoneBook.validate(fromName, phoneNumber);
chatScreen.update(">" + content.substring(1));
chatScreen.enableWriteCommand();
if (Display.getDisplay(this).getCurrent() != writeMessageScreen)
{
phoneNumbers = new String[1];
phoneNumbers[0] = phoneNumber;
}
}
public void handleIncomingError(String errorStr)
{
ErrorScreen.showError(errorStr);
}
// OutgoingTextMessageListener interface methods
public void handleOutgoingOk(OutgoingTextMessage message)
{
}
public void handleOutgoingCancel(OutgoingTextMessage message,
String type)
{
writeMessageScreen.setTitle(type);
}
public void handleOutgoingError(OutgoingTextMessage message,
String errorStr)
{
ErrorScreen.showError(errorStr);
}
// MIDlet methods
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
// A convienence method for exiting.
void exitRequested()
{
if (smsHandler != null)
{
smsHandler.abort();
}
destroyApp(true);
notifyDestroyed();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?