Search in sources :

Example 11 with ConnectionRequest

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

the class AnalyticsService method sendCrashReport.

/**
 * In apps mode we can send information about an exception to the analytics server
 * @param t the exception
 * @param message up to 150 character message,
 * @param fatal is the exception fatal
 */
public static void sendCrashReport(Throwable t, String message, boolean fatal) {
    // https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#exception
    ConnectionRequest req = GetGARequest();
    req.addArgument("t", "exception");
    System.out.println(message);
    req.addArgument("exd", message.substring(0, Math.min(message.length(), 150) - 1));
    if (fatal) {
        req.addArgument("exf", "1");
    } else {
        req.addArgument("exf", "0");
    }
    NetworkManager.getInstance().addToQueue(req);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 12 with ConnectionRequest

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

the class CloudImageProperty method propertyValue.

/**
 * {@inheritDoc}
 */
public Object propertyValue(CloudObject obj, String propertyName) {
    final String key = (String) obj.getObject(idProperty);
    if (key == null) {
        return placeholderImage;
    }
    Image image = (Image) getCache().get(key);
    if (image == null) {
        ReplaceableImage r = inProgress.get(key);
        if (r != null) {
            return r;
        }
        final ReplaceableImage rp = ReplaceableImage.create(placeholderImage);
        inProgress.put(key, rp);
        ConnectionRequest cr = new ConnectionRequest() {

            private EncodedImage e;

            protected void readResponse(InputStream input) throws IOException {
                e = EncodedImage.create(input);
                ;
                if (e.getWidth() != placeholderImage.getWidth() || e.getHeight() != placeholderImage.getHeight()) {
                    ImageIO io = ImageIO.getImageIO();
                    if (io != null) {
                        ByteArrayOutputStream bo = new ByteArrayOutputStream();
                        io.save(new ByteArrayInputStream(e.getImageData()), bo, ImageIO.FORMAT_JPEG, placeholderImage.getWidth(), placeholderImage.getHeight(), 0.9f);
                        e = EncodedImage.create(bo.toByteArray());
                    }
                }
            }

            protected void postResponse() {
                rp.replace(e);
                getCache().put(key, e);
                inProgress.remove(key);
            }
        };
        cr.setPost(false);
        cr.setUrl(CloudStorage.getInstance().getUrlForCloudFileId(key));
        NetworkManager.getInstance().addToQueue(cr);
        return rp;
    }
    return image;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ReplaceableImage(com.codename1.components.ReplaceableImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(com.codename1.ui.Image) ReplaceableImage(com.codename1.components.ReplaceableImage) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage) ImageIO(com.codename1.ui.util.ImageIO)

Example 13 with ConnectionRequest

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

the class CloudPersona method createOrLogin.

/**
 * Creates a new user if a user isn't occupying the given login already,
 * if the user exists performs a login operation.
 *
 * @param login a user name
 * @param password a password
 * @return true if the login is successful false otherwise
 */
public static boolean createOrLogin(String login, String password) {
    if (instance == null) {
        getCurrentPersona();
        if (instance.persona != null) {
            return true;
        }
    }
    ConnectionRequest loginRequest = new ConnectionRequest();
    loginRequest.setPost(true);
    loginRequest.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
    loginRequest.addArgument("l", login);
    loginRequest.addArgument("p", password);
    loginRequest.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    loginRequest.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(loginRequest);
    if (loginRequest.getResposeCode() != 200) {
        return false;
    }
    ByteArrayInputStream bi = new ByteArrayInputStream(loginRequest.getResponseData());
    DataInputStream di = new DataInputStream(bi);
    try {
        if (di.readBoolean()) {
            if (instance == null) {
                instance = new CloudPersona();
            }
            instance.persona = di.readUTF();
            Preferences.set("CN1Persona", instance.persona);
            Util.cleanup(di);
        } else {
            Util.cleanup(di);
            return false;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return true;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 14 with ConnectionRequest

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

the class CloudPersona method createAnonymous.

/**
 * Creates an anonymous persona that will be unique in the cloud, NEVER logout an anonymous user!
 * @return false in case login failed e.g. due to bad network connection
 */
public static boolean createAnonymous() {
    if (instance == null) {
        getCurrentPersona();
    }
    ConnectionRequest login = new ConnectionRequest();
    login.setPost(true);
    login.setUrl(CloudStorage.SERVER_URL + "/objStoreUser");
    login.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    login.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(login);
    if (login.getResposeCode() != 200) {
        return false;
    }
    ByteArrayInputStream bi = new ByteArrayInputStream(login.getResponseData());
    DataInputStream di = new DataInputStream(bi);
    if (instance == null) {
        instance = new CloudPersona();
    }
    try {
        instance.persona = di.readUTF();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    Preferences.set("CN1Persona", instance.persona);
    Preferences.set("CN1PersonaAnonymous", true);
    Util.cleanup(di);
    return true;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 15 with ConnectionRequest

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

the class CloudStorage method deleteAllCloudFilesForUser.

/**
 * Deletes all the cloud files under this user, notice that this method
 * is asynchronous and a background server process performs the actual deletion
 * @deprecated this API is currently deprecated due to Googles cloud storage deprection
 */
public void deleteAllCloudFilesForUser() {
    if (CloudPersona.getCurrentPersona().getToken() == null) {
        return;
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setFailSilently(true);
    req.setUrl(SERVER_URL + "/purgeCloudFiles");
    req.addArgument("own", CloudPersona.getCurrentPersona().getToken());
    req.addArgument("u", Display.getInstance().getProperty("built_by_user", ""));
    NetworkManager.getInstance().addToQueue(req);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)41 IOException (java.io.IOException)11 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Map (java.util.Map)6 JSONParser (com.codename1.io.JSONParser)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 InputStreamReader (java.io.InputStreamReader)5 InfiniteProgress (com.codename1.components.InfiniteProgress)4 NetworkEvent (com.codename1.io.NetworkEvent)4 Dialog (com.codename1.ui.Dialog)4 Form (com.codename1.ui.Form)4 ActionListener (com.codename1.ui.events.ActionListener)3 DataInputStream (java.io.DataInputStream)3 Hashtable (java.util.Hashtable)3 Component (com.codename1.ui.Component)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2