Package org.blync.client

Source Code of org.blync.client.BlyncClient

/*
* Copyright 2009-2010 Marcel Zumstein, Oxinia GmbH
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.  Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/

package org.blync.client;

import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import org.blync.client.mail.MailMainScreen;
import org.blync.client.calendar.appointments.CalendarMonthScreen;
import org.blync.client.calendar.appointments.Scheduler;
import org.blync.client.mail.MailListCustomScreen;
import org.blync.client.mail.MoveMailScreen;

public class BlyncClient extends MIDlet implements CommandListener, ItemCommandListener, TerminatableSession {

    private LoginScreen loginScreen;
    private MainScreen mainScreen;
    private LogScreen logScreen;
    private static BlyncClient instance;

    private DataAccess dataAccess;

    public BlyncClient() {
        Commands.setMainCommandListener(this);
        Commands.setMainItemCommandListener(this);

        instance = this;

        DisplayController.setDisplay(Display.getDisplay(this));
        mainScreen = new MainScreen();
       
        loginScreen = new LoginScreen(mainScreen);
        DisplayController.setCurrentScreen(loginScreen);

        logScreen = LogScreen.getInstance();
        logScreen.addCommand(Commands.getBackCommand());
        logScreen.addCommand(Commands.getExitCommand());
        logScreen.addCommand(Commands.getClearLogCommand());
        logScreen.setCommandListener(this);

        new SessionTimer(this);

        dataAccess = DataAccess.getInstance();
    }

    public void startApp() {
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void terminateSession() {
        if (DisplayController.getCurrentScreen() != loginScreen) {
            dataAccess.deleteTempDir();
            loginScreen.clear();
            DisplayController.setCurrentScreen(loginScreen);
        }
    }

    public void exit() {
        dataAccess.deleteTempDir();
        destroyApp(false);
        notifyDestroyed();
    }

    public static BlyncClient getMIDlet() {
        return instance;
    }

    public void commandAction(Command c, Displayable s) {
        SessionTimer.reset();

        if (s instanceof YesNoScreen) {
            if (c == Commands.getOkCommand()) {
                exit();
            }
            return;
        }

        if (c == Commands.getExitCommand()) {
            YesNoScreen yesNoScreen = new YesNoScreen(Resources.get(Resources.CLOSE), Resources.get(Resources.REALLY_CLOSE), this, s);
            DisplayController.setCurrentScreen(yesNoScreen);
        }
        else if (c == Commands.getOkCommand()) {

            if (s instanceof DetailScreen) {
                DetailScreen detailScreen = (DetailScreen)s;
                if (detailScreen.ok()) {
                    DisplayController.setCurrentScreen(detailScreen.getParentScreen());
                }
            }
        }
        else if (c == Commands.getBackCommand() || c == Commands.getCancelCommand()) {
            Displayable parentScreen = null;
            if (s instanceof HierarchyScreen) {
                parentScreen = ((HierarchyScreen) s).getParentScreen();
            }

            if (parentScreen == null) {
                parentScreen = mainScreen;
            }
            DisplayController.setCurrentScreen(parentScreen);
        }
        else if (c == Commands.getShowItemCommand()) {
            if (s instanceof ListScreenInterface) {
                ListScreenInterface listScreen = (ListScreenInterface)s;
                listScreen.showItem();
                DisplayController.setCurrentScreen(listScreen.getShowItemScreen());
            }
        }
        else if (c == Commands.getEditItemCommand()) {
            ListScreenInterface listScreen = null;
            if (s instanceof ListScreenInterface) {
                listScreen = (ListScreenInterface)s;
            }
            else if (s instanceof DetailScreen) {
                Displayable parentScreen = ((DetailScreen)s).getParentScreen();
                if (parentScreen instanceof ListScreenInterface) {
                    listScreen = (ListScreenInterface)parentScreen;
                }
            }
            if (listScreen != null) {
                listScreen.editItem();
                DisplayController.setCurrentScreen(listScreen.getEditScreen());
            }
        }
        else if (c == Commands.getMoveCommand()) {
            MailListCustomScreen listScreen = null;
            if (s instanceof MailListCustomScreen) {
                listScreen = (MailListCustomScreen)s;
            }
            else if (s instanceof DetailScreen) {
                Displayable parentScreen = ((DetailScreen)s).getParentScreen();
                if (parentScreen instanceof MailListCustomScreen) {
                    listScreen = (MailListCustomScreen)parentScreen;
                }
            }
            if (listScreen != null) {
                MoveMailScreen moveMailScreen = new MoveMailScreen(listScreen.getFolder(), listScreen);
                DisplayController.setCurrentScreen(moveMailScreen);
            }
        }
        else if (c == Commands.getAddCommand()) {
            if (s instanceof ListScreenInterface) {
                ListScreenInterface listScreen = (ListScreenInterface)s;
                listScreen.newItem();
                DisplayController.setCurrentScreen(listScreen.getEditScreen());
            }
        }
        else if (c == Commands.getFindCommand()) {
            if (s instanceof ListScreenInterface) {
                ListScreenInterface listScreen = (ListScreenInterface)s;
                DisplayController.setCurrentScreen(listScreen.getFindScreen());
            }
        }
        else if (c == Commands.getDeleteCommand()) {
            ListScreenInterface listScreen = null;
            if (s instanceof ListScreenInterface) {
                listScreen = (ListScreenInterface)s;
            }
            else if (s instanceof DetailScreen) {
                Displayable parentScreen = ((DetailScreen)s).getParentScreen();
                if (parentScreen instanceof ListScreenInterface) {
                    listScreen = (ListScreenInterface)parentScreen;
                }
            }

            if (listScreen != null) {
                listScreen.deleteSelectedItem();
                DisplayController.setCurrentScreen(listScreen.getDisplayScreen());
            }
        }
        else if (c == Commands.getSyncCommand() || c == Commands.getRecoverCommand()) {
            boolean recovery = (c == Commands.getRecoverCommand());
            if (s instanceof Syncable) {
                Synchronizer synchronizer = new Synchronizer((Syncable)s);
                synchronizer.syncItems(recovery);
            }
            else if (s instanceof MailListCustomScreen) {
                mainScreen.getMailScreen().synchronizeMail(recovery);
            }
            else if (s instanceof CalendarMonthScreen) {
                Synchronizer synchronizer = new Synchronizer(Scheduler.getInstance());
                synchronizer.syncItems(recovery);
            }
            else {
                mainScreen.getMailScreen().synchronizeMail(recovery);
                Synchronizer synchronizer = new Synchronizer(Scheduler.getInstance());
                synchronizer.syncItems(recovery);
                synchronizer = new Synchronizer(mainScreen.getContactsScreen());
                synchronizer.syncItems(recovery);
            }
        }
        else if (c == Commands.getReceiveCommand()) {
            Synchronizer synchronizer = new Synchronizer(mainScreen.getMailScreen().getInboxScreen());
            synchronizer.downloadItems();
        }
        else if (c == Commands.getSendCommand()) {
            MailListCustomScreen outboxScreen = mainScreen.getMailScreen().getOutboxScreen();
            Synchronizer synchronizer = new Synchronizer(outboxScreen);
            if (synchronizer.uploadItems()) {
                outboxScreen.moveMails(MailMainScreen.SENT_NAME);
            }
        }
        else if (c == Commands.getReplyCommand()) {
            MailListCustomScreen listScreen = null;
            if (s instanceof MailListCustomScreen) {
                listScreen = (MailListCustomScreen)s;
            }
            else if (s instanceof DetailScreen) {
                Displayable parentScreen = ((DetailScreen)s).getParentScreen();
                if (parentScreen instanceof MailListCustomScreen) {
                    listScreen = (MailListCustomScreen)parentScreen;
                }
            }
            if (listScreen != null) {
                listScreen.reply();
            }
        }
        else if (c == Commands.getShowLogCommand()) {
            logScreen.setParentScreen(DisplayController.getCurrentScreen());
            DisplayController.setCurrentScreen(logScreen);
        }
        else if (c == Commands.getClearLogCommand()) {
            logScreen.clearLog();
        }
    }

    public void commandAction(Command c, Item item) {
        SessionTimer.reset();
        if (c == Commands.getCallCommand()) {
            if (item instanceof StringItem) {
                StringItem stringItem = (StringItem) item;
                makeCall(stringItem.getText());
            }
        }
        else if (c == Commands.getSmsCommand()) {
            if (item instanceof StringItem) {
                StringItem stringItem = (StringItem) item;
                sendSms(stringItem.getText());
            }
        }
        else if (c == Commands.getMailCommand()) {
            if (item instanceof StringItem) {
                StringItem stringItem = (StringItem) item;
                writeMail(stringItem.getText());
            }
        }
        else if (c == Commands.getUrlCommand()) {
            if (item instanceof StringItem) {
                StringItem stringItem = (StringItem) item;
                openUrl(stringItem.getText());
            }
        }
    }

    private void makeCall(String phoneNo) {
        try {
            platformRequest("tel:" + phoneNo);
        }
        catch (ConnectionNotFoundException e) {
            logScreen.log("makeCall", e.toString());
        }
    }

    private void sendSms(String phoneNo) {
        try {
            platformRequest("sms:" + phoneNo);
        }
        catch (ConnectionNotFoundException e) {
            logScreen.log("sendSms", e.toString());
        }
    }

    /*
//   Method using import javax.wireless.messaging.*;
    private boolean sendSms(String number, String message) {
        boolean result = true;
        try {
          //sets address to send message
          String addr = "sms://"+number;
          // opens connection
          MessageConnection conn = (MessageConnection) Connector.open(addr);
          // prepares text message
          TextMessage msg =
          (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
          //set text
          msg.setPayloadText(message);
          // send message
          conn.send(msg);
          conn.close();
        } catch (SecurityException e) {
            // probably the user has not allowed to send sms
            logScreen.log("sendSms", e.toString());
            result = false;
        } catch (IOException e) {
            logScreen.log("sendSms", e.toString());
            result = false;
        }
        return result;
    }
*/


    private void writeMail(String emailAddress) {
        MailMainScreen.getInstance().writeMail(emailAddress);
    }

    private void openUrl(String url) {
        try {
            if (url.indexOf("://") == -1) {
                url = "http://" + url;
            }
            p("Opening URL " + url);
            platformRequest(url);
        }
        catch (ConnectionNotFoundException e) {
            logScreen.log("openUrl", e.toString());
        }
    }

    private static void p(String msg) {
        System.out.println(msg);
    }
/*
    private static void p(String label, String msg) {
        System.out.println(label + ": " + msg);
    }

    private static void p(byte[] data, int length) {
        System.out.print(">");
        for (int i = 0; i < length; i++) {
            System.out.print((char) data[i]);
        }
        System.out.println("<");
    } */
TOP

Related Classes of org.blync.client.BlyncClient

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.