Search in sources :

Example 61 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class BlackBerryImplementation method sendMessage.

public void sendMessage(String[] recipients, String subject, Message msg) {
    Folder[] folders = Session.getDefaultInstance().getStore().list(Folder.SENT);
    net.rim.blackberry.api.mail.Message message = new net.rim.blackberry.api.mail.Message(folders[0]);
    try {
        Address[] toAdds = new Address[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            Address address = new Address(recipients[i], "");
            toAdds[i] = address;
        }
        message.addRecipients(net.rim.blackberry.api.mail.Message.RecipientType.TO, toAdds);
        message.setSubject(subject);
    } catch (Exception e) {
        EventLog.getInstance().logErrorEvent("err " + e.getMessage());
    }
    try {
        if (msg.getAttachment() != null && msg.getAttachment().length() > 0) {
            Multipart content = new Multipart();
            TextBodyPart tbp = new TextBodyPart(content, msg.getContent());
            content.addBodyPart(tbp);
            InputStream stream = com.codename1.io.FileSystemStorage.getInstance().openInputStream(msg.getAttachment());
            byte[] buf;
            buf = IOUtilities.streamToBytes(stream);
            stream.close();
            String name = msg.getAttachment();
            name = name.substring(name.lastIndexOf(getFileSystemSeparator()) + 1, name.length());
            SupportedAttachmentPart sap = new SupportedAttachmentPart(content, msg.getAttachmentMimeType(), name, buf);
            content.addBodyPart(sap);
            message.setContent(content);
        } else {
            message.setContent(msg.getContent());
        }
        app.setWaitingForReply(true);
        Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(message));
    } catch (Exception ex) {
        EventLog.getInstance().logErrorEvent("err " + ex.getMessage());
    }
}
Also used : Multipart(net.rim.blackberry.api.mail.Multipart) TextMessage(javax.wireless.messaging.TextMessage) Message(com.codename1.messaging.Message) Address(net.rim.blackberry.api.mail.Address) BufferedInputStream(com.codename1.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) TextBodyPart(net.rim.blackberry.api.mail.TextBodyPart) Folder(net.rim.blackberry.api.mail.Folder) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) MessageArguments(net.rim.blackberry.api.invoke.MessageArguments) SupportedAttachmentPart(net.rim.blackberry.api.mail.SupportedAttachmentPart)

Example 62 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class GameCanvasImplementation method capturePhoto.

public void capturePhoto(ActionListener response) {
    captureResponse = response;
    final Form current = Display.getInstance().getCurrent();
    final Form cam = new Form();
    cam.setScrollable(false);
    cam.setTransitionInAnimator(CommonTransitions.createEmpty());
    cam.setTransitionOutAnimator(CommonTransitions.createEmpty());
    cam.setLayout(new BorderLayout());
    cam.show();
    String platform = System.getProperty("microedition.platform");
    MMAPIPlayer p = null;
    if (platform != null && platform.indexOf("Nokia") >= 0) {
        try {
            p = MMAPIPlayer.createPlayer("capture://image", null);
        } catch (Throwable e) {
        // Ignore all exceptions for image capture, continue with video capture...
        }
    }
    if (p == null) {
        try {
            p = MMAPIPlayer.createPlayer("capture://video", null);
        } catch (Exception e) {
            // The Nokia 2630 throws this if image/video capture is not supported
            throw new RuntimeException("Image/video capture not supported on this phone");
        }
    }
    final MMAPIPlayer player = p;
    MIDPVideoComponent video = new MIDPVideoComponent(player, canvas);
    video.play();
    video.setVisible(true);
    cam.addCommand(new com.codename1.ui.Command("Cancel") {

        public void actionPerformed(ActionEvent evt) {
            if (player != null) {
                player.cleanup();
            }
            captureResponse.actionPerformed(null);
            current.showBack();
        }
    });
    final ActionListener l = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            try {
                cam.removeAll();
                VideoControl cnt = (VideoControl) player.nativePlayer.getControl("VideoControl");
                byte[] pic = cnt.getSnapshot("encoding=jpeg&width=" + current.getWidth() + "&height=" + current.getHeight());
                String imagePath = getOutputMediaFile() + ".jpg";
                OutputStream out = null;
                try {
                    if (pic != null) {
                        out = FileSystemStorage.getInstance().openOutputStream(imagePath);
                        out.write(pic);
                    }
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    System.out.println("failed to store picture to " + imagePath);
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                        player.cleanup();
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
                captureResponse.actionPerformed(new ActionEvent(imagePath));
                current.showBack();
            } catch (Throwable ex) {
                ex.printStackTrace();
                System.out.println("failed to take picture");
                current.showBack();
            }
        }
    };
    cam.addGameKeyListener(Display.GAME_FIRE, l);
    Container cn = new Container(new BorderLayout()) {

        public void pointerReleased(int x, int y) {
            l.actionPerformed(null);
        }
    };
    cn.setFocusable(true);
    cn.addComponent(BorderLayout.CENTER, video);
    cam.addComponent(BorderLayout.CENTER, cn);
    cam.revalidate();
// cam.addPointerReleasedListener(l);
}
Also used : Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) IOException(java.io.IOException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) ActionListener(com.codename1.ui.events.ActionListener) VideoControl(javax.microedition.media.control.VideoControl)

Example 63 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class BlackBerryOS5Implementation method openOutputStream.

/**
 * (non-Javadoc)
 *
 * @see
 * com.codename1.impl.blackberry.BlackBerryImplementation#openOutputStream(java.lang.Object)
 */
public OutputStream openOutputStream(Object connection) throws IOException {
    if (connection instanceof String) {
        return super.openOutputStream(connection);
    }
    OutputStream os = ((HttpConnection) connection).openOutputStream();
    // getSoftwareVersion() not available in legacy port,introduced at API 4.3.0
    int majorVersion = DeviceInfo.getSoftwareVersion().charAt(0) - '0';
    // in version 7, BBOS started supporting HTTP 1.1, so facade not required.
    if (majorVersion < 7) {
        os = new BlackBerryOutputStream(os);
    }
    return new BufferedOutputStream(os, ((HttpConnection) connection).getURL());
}
Also used : HttpConnection(javax.microedition.io.HttpConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream)

Example 64 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class Util method readObject.

/**
 * <p>Reads an object from the stream, notice that this is the inverse of the
 * {@link #writeObject(java.lang.Object, java.io.DataOutputStream)}.</p>
 *
 * <p>
 * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
 * </p>
 * <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
 *
 * @param input the source input stream
 * @throws IOException thrown by the stream
 */
public static Object readObject(DataInputStream input) throws IOException {
    try {
        if (!input.readBoolean()) {
            return null;
        }
        String type = input.readUTF();
        if ("int".equals(type)) {
            return new Integer(input.readInt());
        }
        if ("byte".equals(type)) {
            return new Byte(input.readByte());
        }
        if ("short".equals(type)) {
            return new Short(input.readShort());
        }
        if ("long".equals(type)) {
            return new Long(input.readLong());
        }
        if ("float".equals(type)) {
            return new Float(input.readFloat());
        }
        if ("double".equals(type)) {
            return new Double(input.readDouble());
        }
        if ("bool".equals(type)) {
            return new Boolean(input.readBoolean());
        }
        if ("String".equals(type)) {
            return input.readUTF();
        }
        if ("Date".equals(type)) {
            return new Date(input.readLong());
        }
        if ("ObjectArray".equals(type)) {
            Object[] v = new Object[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = readObject(input);
            }
            return v;
        }
        if ("ByteArray".equals(type)) {
            byte[] v = new byte[input.readInt()];
            input.readFully(v);
            return v;
        }
        if ("LongArray".equals(type)) {
            long[] v = new long[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readLong();
            }
            return v;
        }
        if ("ShortArray".equals(type)) {
            short[] v = new short[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readShort();
            }
            return v;
        }
        if ("DoubleArray".equals(type)) {
            double[] v = new double[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readDouble();
            }
            return v;
        }
        if ("FloatArray".equals(type)) {
            float[] v = new float[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readFloat();
            }
            return v;
        }
        if ("IntArray".equals(type)) {
            int[] v = new int[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readInt();
            }
            return v;
        }
        if ("java.util.Vector".equals(type)) {
            Vector v = new Vector();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.addElement(readObject(input));
            }
            return v;
        }
        if ("java.util.Hashtable".equals(type)) {
            Hashtable v = new Hashtable();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("java.util.Set".equals(type)) {
            Collection v = new HashSet();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.add(readObject(input));
            }
            return v;
        }
        if ("java.util.Collection".equals(type)) {
            Collection v = new ArrayList();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.add(readObject(input));
            }
            return v;
        }
        if ("java.util.Map".equals(type)) {
            Map v = new HashMap();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("EncodedImage".equals(type)) {
            int width = input.readInt();
            int height = input.readInt();
            boolean op = input.readBoolean();
            byte[] data = new byte[input.readInt()];
            input.readFully(data);
            return EncodedImage.create(data, width, height, op);
        }
        Class cls = (Class) externalizables.get(type);
        if (cls != null) {
            Object o = cls.newInstance();
            if (o instanceof Externalizable) {
                Externalizable ex = (Externalizable) o;
                ex.internalize(input.readInt(), input);
                return ex;
            } else {
                PropertyBusinessObject pb = (PropertyBusinessObject) o;
                pb.getPropertyIndex().asExternalizable().internalize(input.readInt(), input);
                return pb;
            }
        }
        throw new IOException("Object type not supported: " + type);
    } catch (InstantiationException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    } catch (IllegalAccessException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Vector(java.util.Vector) HashSet(java.util.HashSet) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Date(java.util.Date) Collection(java.util.Collection) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Externalizable(com.codename1.io.Externalizable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with com.codename1.io

use of com.codename1.io in project CodenameOne by codenameone.

the class CodenameOneImplementation method downloadImageToCache.

/**
 * Downloads an image from a URL to the cache. Platforms
 * that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
 * platform's handling of cached images.  Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
 * will use that directory to cache the image.  Other platforms will just download to storage.
 *
 * @param url The URL of the image to download.
 * @param onSuccess Callback on success.
 * @param onFail Callback on fail.
 *
 * @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
 */
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
    FileSystemStorage fs = FileSystemStorage.getInstance();
    if (fs.hasCachesDir()) {
        String name = "cn1_image_cache[" + url + "]";
        name = StringUtil.replaceAll(name, "/", "_");
        name = StringUtil.replaceAll(name, "\\", "_");
        name = StringUtil.replaceAll(name, "%", "_");
        name = StringUtil.replaceAll(name, "?", "_");
        name = StringUtil.replaceAll(name, "*", "_");
        name = StringUtil.replaceAll(name, ":", "_");
        name = StringUtil.replaceAll(name, "=", "_");
        String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
        // We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
        // because we want it to try to load from file system first.
        Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
    } else {
        // We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
        // because we want it to try to load from storage first.
        Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Aggregations

IOException (java.io.IOException)39 EncodedImage (com.codename1.ui.EncodedImage)29 ArrayList (java.util.ArrayList)27 Point (java.awt.Point)25 File (java.io.File)24 BufferedImage (java.awt.image.BufferedImage)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 Form (com.codename1.ui.Form)19 Hashtable (java.util.Hashtable)19 Component (com.codename1.ui.Component)18 Image (com.codename1.ui.Image)18 EditableResources (com.codename1.ui.util.EditableResources)16 FileInputStream (java.io.FileInputStream)16 Timeline (com.codename1.ui.animations.Timeline)15 BorderLayout (com.codename1.ui.layouts.BorderLayout)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)11 FileOutputStream (java.io.FileOutputStream)10 EditorFont (com.codename1.ui.EditorFont)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9