Package net.sf.log.mobile

Source Code of net.sf.log.mobile.Log

package net.sf.log.mobile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Date;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
public class Log
//#ifdef LOGGING
  extends Form implements CommandListener
//#endif
{
  //#ifdef LOGGING
  private static final String LOG = "log";
  private static final String EXPORT_PATH =  System.getProperty("fileconn.dir.photos") + "nfp-log.txt";
  private Command BACK = new Command("Back", Command.BACK, 1);
  private Command CLEAR = new Command("Clear", Command.OK, 2);
  private Command EXPORT = new Command("Export", Command.SCREEN, 3);
  private Displayable prev;
  private MIDlet midlet;

  public Log() {
    super("Log");
    addCommand(BACK);
    addCommand(CLEAR);
    //#ifdef FILE_API
    addCommand(EXPORT);
    //#endif
    super.setCommandListener(this);
  }

  private static RecordStore rms;
  private static Log instance;
  private static int MAX = 30;

  static {
    try {
      open();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static void open() throws RecordStoreException {
    rms = RecordStore.openRecordStore(LOG, true);
  }

  private void doShow(MIDlet midlet) throws Exception {
    prev = Display.getDisplay(midlet).getCurrent();
    Display.getDisplay(midlet).setCurrent(this);
    deleteAll();
    Font[] fonts = {
        Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL),
        Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED,  Font.SIZE_SMALL),
    };
    int i = 0;
    RecordEnumeration enumeration = rms.enumerateRecords(null, null, false);
    while (enumeration.hasNextElement()) {
      String data = decode(enumeration.nextRecord());
      StringItem item = new StringItem(null, data);
      //#ifndef midp1.0
      item.setFont(fonts[++i % fonts.length]);
      //#endif
      append(item);
      if(i > MAX)
        break;
    }
    this.midlet = midlet;
  }

  public void deleteAll() {
    //#ifndef midp1.0
    super.deleteAll();
    //#else
    for(int i=0;i< this.size(); ++i)
      delete(i);
    //#endif
  }
 
  private String decode(byte[] bytes) throws Exception{
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    try {
      Calendar timestamp = Calendar.getInstance();
      timestamp.setTime(new Date(in.readLong()));
 
      StringBuffer str = new StringBuffer();
      str.append(timestamp.get(Calendar.HOUR_OF_DAY)).append(':')
        .append(timestamp.get(Calendar.MINUTE)).append(':')
        .append(timestamp.get(Calendar.SECOND)).append('.')
        .append(timestamp.get(Calendar.MILLISECOND));
      str.append(": ").append(in.readUTF()).append("\n");
      return str.toString();
    }finally{
      in.close();
    }
  }
   

  public void commandAction(Command c, Displayable d) {
    try {
      if (c == BACK)
        Display.getDisplay(midlet).setCurrent(prev);
      else if (c == CLEAR)
        clear();
      else if (c == EXPORT)
        export();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void export() throws Exception {
    //#ifdef FILE_API
    javax.microedition.io.file.FileConnection fc =
      (javax.microedition.io.file.FileConnection) Connector.open(EXPORT_PATH,
        Connector.READ_WRITE);
    try {
      if (fc.exists())
        fc.delete();
      fc.create();
      PrintStream out = new PrintStream(fc.openOutputStream());
      RecordEnumeration enumeration = rms.enumerateRecords(null, null, false);
      while (enumeration.hasNextElement()) {
        String data = decode(enumeration.nextRecord());
        out.print(data);
      }
      out.close();
    } finally {
      fc.close();
    }
    //#endif
  }

  private void clear() throws Exception {
    rms.closeRecordStore();
    RecordStore.deleteRecordStore(LOG);
    open();
    deleteAll();
  }
  //#endif
 
  public static void log(Object msg) {
    //#ifdef LOGGING
    System.out.println(msg);
    try {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      DataOutputStream out = new DataOutputStream(bytes);
      out.writeLong(new Date().getTime()); // <- timestamp
      out.writeUTF(""+msg); // <- null safe
      out.close();
      byte[] bs = bytes.toByteArray();
      rms.addRecord(bs, 0, bs.length);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    //#endif
  }
 
  public static void show(MIDlet midlet) {
    //#ifdef LOGGING
    try {
      if (instance == null)
        instance = new Log();
      instance.doShow(midlet);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    //#endif
  }
}
TOP

Related Classes of net.sf.log.mobile.Log

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.