Search in sources :

Example 21 with URL

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

the class RSSReader method sendRequest.

/**
 * Send the request to the server, will only work once. This is called implicitly
 * when the list is initialized
 */
public void sendRequest() {
    if (service == null) {
        service = new RSSService(url, limit);
        if (iconPlaceholder != null) {
            service.setIconPlaceholder(iconPlaceholder);
        }
        service.addResponseListener(new EventHandler());
        if (blockList) {
            Progress p = new Progress(progressTitle, service, displayProgressPercentage);
            p.setAutoShow(true);
            p.setDisposeOnCompletion(true);
        }
        setHint(progressTitle);
        NetworkManager.getInstance().addToQueue(service);
    }
}
Also used : RSSService(com.codename1.io.services.RSSService)

Example 22 with URL

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

the class RSSReader method updateComponentValues.

void updateComponentValues(Container root, Hashtable h) {
    int c = root.getComponentCount();
    for (int iter = 0; iter < c; iter++) {
        Component current = root.getComponentAt(iter);
        // subclasses
        if (current.getClass() == com.codename1.ui.Container.class || current.getClass() == com.codename1.ui.Tabs.class) {
            updateComponentValues((Container) current, h);
            continue;
        }
        String n = current.getName();
        if (n != null) {
            String val = (String) h.get(n);
            if (val != null) {
                if (current instanceof Button) {
                    final String url = (String) val;
                    ((Button) current).addActionListener(new Listener(url));
                    continue;
                }
                if (current instanceof Label) {
                    ((Label) current).setText(val);
                    continue;
                }
                if (current instanceof TextArea) {
                    ((TextArea) current).setText(val);
                    continue;
                }
                if (current instanceof WebBrowser) {
                    ((WebBrowser) current).setPage(val, null);
                    continue;
                }
            }
        }
    }
}
Also used : Container(com.codename1.ui.Container) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Component(com.codename1.ui.Component)

Example 23 with URL

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

the class ImageIO method saveAndKeepAspect.

/**
 * Scales an image on disk while maintaining an aspect ratio, the appropriate aspect size will be
 * picked based on the status of scaleToFill
 * @param imageFilePath the path to the image
 * @param preferredOutputPath the url where the image will be saved
 * @param format the format for the image either FORMAT_JPEG or FORMAT_PNG
 * @param width the desired width, either width or height will be respected based on aspect dimensions
 * @param height the desired height, either width or height will be respected based on aspect dimensions
 * @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that
 * this isn't implemented in all platforms.
 * @param onlyDownscale will not scale if the resolution to scale will be higher in this case will return the imageFilePath
 * @param scaleToFill when set to true will pick the larger value so the resulting image will be at least as big as width x height, when set to false
 * will create an image that is no bigger than width x height
 * @return the url for the scaled image or the url of the unscaled image
 * @throws IOException if the operation fails
 */
public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill) throws IOException {
    Dimension d = getImageSize(imageFilePath);
    if (onlyDownscale) {
        if (scaleToFill) {
            if (d.getHeight() <= height || d.getWidth() <= width) {
                return imageFilePath;
            }
        } else {
            if (d.getHeight() <= height && d.getWidth() <= width) {
                return imageFilePath;
            }
        }
    }
    float ratio = ((float) d.getWidth()) / ((float) d.getHeight());
    int heightBasedOnWidth = (int) (((float) width) / ratio);
    int widthBasedOnHeight = (int) (((float) height) * ratio);
    if (scaleToFill) {
        if (heightBasedOnWidth >= width) {
            height = heightBasedOnWidth;
        } else {
            width = widthBasedOnHeight;
        }
    } else {
        if (heightBasedOnWidth > width) {
            width = widthBasedOnHeight;
        } else {
            height = heightBasedOnWidth;
        }
    }
    OutputStream im = FileSystemStorage.getInstance().openOutputStream(preferredOutputPath);
    save(imageFilePath, im, format, width, height, quality);
    return preferredOutputPath;
}
Also used : OutputStream(java.io.OutputStream) Dimension(com.codename1.ui.geom.Dimension)

Example 24 with URL

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

the class JavaSEPort method downloadSkin.

private File downloadSkin(File skinDir, String url, String version, JLabel label) throws IOException {
    String fileName = url.substring(url.lastIndexOf("/"));
    File skin = new File(skinDir.getAbsolutePath() + "/" + fileName);
    HttpURLConnection.setFollowRedirects(true);
    URL u = new URL(url);
    FileOutputStream os = new FileOutputStream(skin);
    URLConnection uc = u.openConnection();
    InputStream is = uc.getInputStream();
    int length = uc.getContentLength();
    byte[] buffer = new byte[65536];
    int size = is.read(buffer);
    int offset = 0;
    int percent = 0;
    String msg = label.getText();
    if (length > 0) {
        System.out.println("Downloading " + length + " bytes");
    }
    while (size > -1) {
        offset += size;
        if (length > 0) {
            float f = ((float) offset) / ((float) length) * 100;
            if (percent != ((int) f)) {
                percent = (int) f;
                label.setText(msg + " " + percent + "%");
                label.repaint();
            }
        } else {
            if (percent < offset / 102400) {
                percent = offset / 102400;
                System.out.println("Downloaded " + percent + "00Kb");
            }
        }
        os.write(buffer, 0, size);
        size = is.read(buffer);
    }
    is.close();
    os.close();
    // store the skin version
    Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
    pref.put(fileName, version);
    return skin;
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) ZipInputStream(java.util.zip.ZipInputStream) AttributedString(java.text.AttributedString) Preferences(java.util.prefs.Preferences) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Point(java.awt.Point)

Example 25 with URL

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

the class JavaSEPort method openSkinsURL.

InputStream openSkinsURL() throws IOException {
    try {
        URL u = new URL(defaultCodenameOneComProtocol + "://www.codenameone.com/OTA/Skins.xml");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
        InputStream is = uc.getInputStream();
        return is;
    } catch (IOException err) {
        if (defaultCodenameOneComProtocol.equals("https")) {
            defaultCodenameOneComProtocol = "http";
        } else {
            throw err;
        }
        System.out.println("Failed to connect thru secure socket, trying http instead");
        URL u = new URL("http://www.codenameone.com/OTA/Skins.xml");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
        InputStream is = uc.getInputStream();
        return is;
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) ZipInputStream(java.util.zip.ZipInputStream)

Aggregations

IOException (java.io.IOException)33 InputStream (java.io.InputStream)18 ConnectionRequest (com.codename1.io.ConnectionRequest)12 ActionEvent (com.codename1.ui.events.ActionEvent)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 File (java.io.File)11 Form (com.codename1.ui.Form)10 OutputStream (java.io.OutputStream)10 Image (com.codename1.ui.Image)9 Vector (java.util.Vector)9 EncodedImage (com.codename1.ui.EncodedImage)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 URISyntaxException (java.net.URISyntaxException)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 RemoteException (android.os.RemoteException)5 BufferedInputStream (com.codename1.io.BufferedInputStream)5 FileSystemStorage (com.codename1.io.FileSystemStorage)5 MediaException (com.codename1.media.AsyncMedia.MediaException)5 ActionListener (com.codename1.ui.events.ActionListener)5 FileInputStream (java.io.FileInputStream)5