Search in sources :

Example 6 with Data

use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.

the class CodenameOneImplementation method installTar.

/**
 * Installs a tar file from the build server into the file system storage so it can be used with respect for hierarchy
 */
public void installTar() throws IOException {
    String p = Preferences.get("cn1$InstallKey", null);
    String buildKey = Display.getInstance().getProperty("build_key", null);
    if (p == null || !p.equals(buildKey)) {
        FileSystemStorage fs = FileSystemStorage.getInstance();
        String tardir = fs.getAppHomePath() + "cn1html/";
        fs.mkdir(tardir);
        TarInputStream is = new TarInputStream(Display.getInstance().getResourceAsStream(getClass(), "/html.tar"));
        TarEntry t = is.getNextEntry();
        byte[] data = new byte[8192];
        while (t != null) {
            String name = t.getName();
            if (t.isDirectory()) {
                fs.mkdir(tardir + name);
            } else {
                String path = tardir + name;
                String dir = path.substring(0, path.lastIndexOf('/'));
                if (!fs.exists(dir)) {
                    mkdirs(fs, dir);
                }
                OutputStream os = fs.openOutputStream(tardir + name);
                int count;
                while ((count = is.read(data)) != -1) {
                    os.write(data, 0, count);
                }
                os.close();
            }
            t = is.getNextEntry();
        }
        Util.cleanup(is);
        Preferences.set("cn1$InstallKey", buildKey);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage) TarInputStream(com.codename1.io.tar.TarInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TarEntry(com.codename1.io.tar.TarEntry)

Example 7 with Data

use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.

the class VServAds method createAdRequest.

/**
 * {@inheritDoc}
 */
protected ConnectionRequest createAdRequest() {
    ConnectionRequest con = new ConnectionRequest() {

        protected void handleErrorResponseCode(int code, String message) {
            failed = true;
        }

        protected void handleException(Exception err) {
            failed = true;
            Log.e(err);
        }

        private String getString(Hashtable h, String n) {
            Object v = h.get(n);
            if (v == null) {
                return null;
            }
            if (v instanceof Vector) {
                return (String) ((Vector) v).elementAt(0);
            }
            return (String) v;
        }

        protected void readResponse(InputStream input) throws IOException {
            JSONParser parser = new JSONParser();
            Hashtable h = parser.parse(new InputStreamReader(input, "UTF-8"));
            if (h.size() == 0) {
                return;
            }
            backgroundColor = Integer.parseInt((String) ((Hashtable) ((Vector) h.get("style")).elementAt(0)).get("background-color"), 16);
            Hashtable actionHash = ((Hashtable) ((Vector) h.get("action")).elementAt(0));
            actionNotify = getString(actionHash, "notify");
            if (actionNotify == null) {
                actionNotify = getString(actionHash, "notify-once");
            }
            destination = (String) actionHash.get("data");
            Hashtable renderHash = ((Hashtable) ((Vector) h.get("render")).elementAt(0));
            contentType = (String) renderHash.get("type");
            renderNotify = getString(renderHash, "notify");
            if (renderNotify == null) {
                renderNotify = getString(renderHash, "notify-once");
            }
            imageURL = (String) renderHash.get("data");
        }
    };
    con.setUrl(URL);
    con.setPost(false);
    con.addArgument("zoneid", getZoneId());
    con.addArgument("ua", Display.getInstance().getProperty("User-Agent", ""));
    con.addArgument("app", "1");
    con.addArgument("aid", Display.getInstance().getProperty("androidId", ""));
    con.addArgument("uuid", Display.getInstance().getProperty("uuid", ""));
    con.addArgument("im", Display.getInstance().getProperty("imei", ""));
    con.addArgument("sw", "" + Display.getInstance().getDisplayWidth());
    con.addArgument("sh", "" + Display.getInstance().getDisplayHeight());
    con.addArgument("mn", Display.getInstance().getProperty("AppName", ""));
    con.addArgument("vs3", "1");
    con.addArgument("partnerid", "1");
    con.addArgument("zc", "" + category);
    if (countryCode != null) {
        con.addArgument("cc", countryCode);
    }
    if (networkCode != null) {
        con.addArgument("nc", networkCode);
    }
    if (locale != null) {
        con.addArgument("lc", locale);
    }
    return con;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) JSONParser(com.codename1.io.JSONParser) Vector(java.util.Vector) IOException(java.io.IOException)

Example 8 with Data

use of com.codename1.ui.util.xml.Data 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.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) 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 9 with Data

use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.

the class ImageDownloadService method createImageToFileSystem.

/**
 * Constructs an image request that will automatically populate the given list
 * when the response arrives, it will cache the file locally as a file
 * in the file storage.
 * This assumes the GenericListCellRenderer style of
 * list which relies on a map based model approach.
 *
 * @param url the image URL
 * @param targetList the list that should be updated when the data arrives
 * @param targetOffset the offset within the list to insert the image
 * @param targetKey the key for the map in the target offset
 * @param destFile local file to store the data into the given path
 */
private static void createImageToFileSystem(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String destFile, final Dimension toScale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
    if (Display.getInstance().isEdt()) {
        Display.getInstance().scheduleBackgroundTask(new Runnable() {

            public void run() {
                createImageToFileSystem(url, targetList, targetModel, targetOffset, targetKey, destFile, toScale, priority, placeholderImage, maintainAspectRatio);
            }
        });
        return;
    }
    // image not found on cache go and download from the url
    ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
    i.targetModel = targetModel;
    i.maintainAspectRatio = maintainAspectRatio;
    Image im = cacheImage(null, false, destFile, toScale, placeholderImage, maintainAspectRatio);
    if (im != null) {
        i.setEntryInListModel(targetOffset, im);
        targetList.repaint();
        return;
    }
    i.cacheImages = true;
    i.destinationFile = destFile;
    i.toScale = toScale;
    i.placeholder = placeholderImage;
    i.setPriority(priority);
    i.setFailSilently(true);
    NetworkManager.getInstance().addToQueue(i);
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) FileEncodedImage(com.codename1.components.FileEncodedImage) StorageImage(com.codename1.components.StorageImage)

Example 10 with Data

use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.

the class FaceBookAccess method getUser.

/**
 * Gets a user from a user id
 *
 * @param userId the user id or null to get detaild on the authenticated user
 * @param user an object to fill with the user details
 * @param callback the callback that should be updated when the data arrives
 */
public void getUser(String userId, final User user, final ActionListener callback) throws IOException {
    String id = userId;
    if (id == null) {
        id = "me";
    }
    getFaceBookObject(id, new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Vector v = (Vector) ((NetworkEvent) evt).getMetaData();
            Hashtable t = (Hashtable) v.elementAt(0);
            if (user != null) {
                user.copy(t);
            }
            if (callback != null) {
                callback.actionPerformed(evt);
            }
        }
    });
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) NetworkEvent(com.codename1.io.NetworkEvent) Vector(java.util.Vector)

Aggregations

IOException (java.io.IOException)18 EncodedImage (com.codename1.ui.EncodedImage)12 Hashtable (java.util.Hashtable)12 Image (com.codename1.ui.Image)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 FileInputStream (java.io.FileInputStream)8 ActionEvent (com.codename1.ui.events.ActionEvent)7 ActionListener (com.codename1.ui.events.ActionListener)7 FileEncodedImage (com.codename1.components.FileEncodedImage)6 ConnectionRequest (com.codename1.io.ConnectionRequest)6 File (java.io.File)6 Intent (android.content.Intent)5 StorageImage (com.codename1.components.StorageImage)5 IntentResultListener (com.codename1.impl.android.IntentResultListener)5 EditableResources (com.codename1.ui.util.EditableResources)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DataInputStream (java.io.DataInputStream)5 Vector (java.util.Vector)5 CodenameOneActivity (com.codename1.impl.android.CodenameOneActivity)4