📄 message.java
字号:
/**
* Class : short description
* @author Michele 'Miccar' Cardinale miccar@gmail.com
* @author Natale Vinto ebballon@interfree.it
* @version 1.0
*/
/*
* goText : text messaging over GPRS
* Copyright (C) 2006 Natale Vinto <ebballon@interfree.it>
* OpenJLab Group www.openjlab.org
* Copyright (C) 2006 Michele Cardinale 'Miccar' <miccar@gmail.com>
* www.miccar.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
import javax.microedition.rms.*;
import java.util.*;
public class Message{
//public static final String DEFAULT_PREFIX="+39";
static String RN="_messages";
public final int MAX_MEM_MESSAGES=10;
private String token=";";
private String text;
private String recipients;
private String messageString;
/** Creates a new instance of Message */
public Message() {
this.text="";
this.recipients="";
this.messageString="";
}
public Message(Message m) {
this.text=m.text;
this.recipients=m.recipients;
this.messageString=m.messageString;
}
public Message(String stringa) {
this.text=utils.getTag(stringa, "t");
this.recipients=utils.getTag(stringa, "r");
//this.compression=utils.getTag(stringa, "c");
this.messageString=stringa;
}
public int setText(String text){
this.text=text;
return this.text.length();
}
private int setRecipients(String recipients){
this.recipients=recipients;
return this.recipients.length();
}
public void resetRecipients(){
this.recipients="";
}
public int addNumericRecipient(String recipient){
/*
if(recipient.length()<10)
return -1;
if(recipient.length()==10)
recipient=DEFAULT_PREFIX+recipient;
if(recipient.length()==12){
recipient="+"+recipient;
}
if(recipient.length()!=13)
return -1;
*/
if(recipient.length()<1)
return -1;
if(this.getRecipients().equals(""))
return setRecipients(recipient);
else
return setRecipients(this.getRecipients()+token+recipient);
}
public int addEmailRecipient(String recipient){
if(recipient.indexOf("@")<1)
return -1;
if(recipient.indexOf(".")<2)
return -1;
if(this.getRecipients().equals(""))
return setRecipients(recipient);
else
return setRecipients(this.getRecipients()+token+recipient);
}
public String[] getRecipientsArray(){
String[] risultato=utils.splitString(this.getRecipients(), token);
return risultato;
}
public String getText(){
return this.text;
}
public String getRecipients(){
return this.recipients;
}
public boolean isReadyToSend(Service s){
if((this.text.length()>0)&&(this.text.length()<=s.getMaxCharsWithSign())){
if(this.recipients.length()>0)
return true;
if(s.getMaxRecipients()==0)
return true;
}
return false;
}
public void setForService(Service s){
String[] temp;
if(this.getRecipientsArray().length>s.getMaxRecipients()){
temp=new String[s.getMaxRecipients()];
}else {
temp=new String[this.getRecipientsArray().length];
}
System.arraycopy(this.getRecipientsArray(), 0, temp, 0, temp.length);
this.resetRecipients();
for (int i=0;i<temp.length;i++){
if(s.getIsNumericRecipient()==1)
this.addNumericRecipient(temp[i]);
else
this.addEmailRecipient(temp[i]);
}
}
public int saveMessage(boolean only_text){
/*
* Memorizza il messaggio sull'RMS.
* Ritorna:
* -2 se sono gia' stati memorizzati MAX_MEM_MESSAGES.
* Altrimenti ritorna il risultato della chiamata a insMessage();
*/
if(Message.countMessages()<MAX_MEM_MESSAGES){
String stringa;
if(only_text){
stringa="<t>"+this.text+"</t><r></r>";
}else{
stringa="<t>"+this.text+"</t><r>"+this.recipients+"</r>";
}
return Message.insMessage(stringa);
} else {
return -2;
}
}
public int getRmsId(){
RecordStore rs=null;
try{
rs=RecordStore.openRecordStore(RN, true);
RecordEnumeration e=rs.enumerateRecords(new RSUtils().new FilterByString(this.messageString),null,true);
if(e.hasNextElement()){
return e.nextRecordId();
} else {
return -1;
}
} catch (Exception e){
return -2;
} finally {
try{
rs.closeRecordStore();
} catch (Exception e){
}
}
}
private static Message[] messagesArray() {
if(Message.messagesList()==null) {
return null;
} else {
Message[] messages_array=new Message[Message.messagesList().size()];
Message.messagesList().copyInto(messages_array);
return messages_array;
}
}
private static Vector messagesList() {
Vector lista_messaggi=new Vector();
RecordStore rs=null;
try{
rs=RecordStore.openRecordStore(RN, true);
RecordEnumeration e=rs.enumerateRecords(null,null,true);
while(e.hasNextElement()){
lista_messaggi.addElement(new Message(new String(e.nextRecord())));
}
} catch (Exception e){
} finally {
try{
rs.closeRecordStore();
} catch (Exception e){
}
}
return lista_messaggi;
}
public static int countMessages() {
RecordStore rs=null;
int risultato;
try{
rs=RecordStore.openRecordStore(RN, true);
risultato=rs.getNumRecords();
} catch (Exception e){
risultato=-1;
} finally {
try{
rs.closeRecordStore();
} catch (Exception e){
}
}
return risultato;
}
private static boolean isValidMessageString(String stringa){ //DA IMPLEMENTARE
if((stringa.indexOf("<t>")<0)||(stringa.indexOf("</t>")<0)||(stringa.indexOf("<r>")<0)||(stringa.indexOf("</r>")<0))
return false;
else
return true;
}
public static int insMessage(String stringa){
/*
* 0 : la stringa passata non pu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -