Search in sources :

Example 71 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class AndroidImplementation method fixAttachmentPath.

private String fixAttachmentPath(String attachment) {
    com.codename1.io.File cn1File = new com.codename1.io.File(attachment);
    File mediaStorageDir = new File(new File(getContext().getCacheDir(), "intent_files"), "Attachment");
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(Display.getInstance().getProperty("AppName", "CodenameOne"), "failed to create directory");
            return null;
        }
    }
    File newFile = new File(mediaStorageDir.getPath() + File.separator + cn1File.getName());
    if (newFile.exists()) {
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        newFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + "_" + cn1File.getName());
    }
    // Uri fileUri = Uri.fromFile(newFile);
    newFile.getParentFile().mkdirs();
    // Uri imageUri = Uri.fromFile(newFile);
    Uri fileUri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", newFile);
    try {
        InputStream is = FileSystemStorage.getInstance().openInputStream(attachment);
        OutputStream os = new FileOutputStream(newFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) > -1) {
            os.write(buf, 0, len);
        }
        is.close();
        os.close();
    } catch (IOException ex) {
        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
    }
    return fileUri.toString();
}
Also used : Audio(com.codename1.media.Audio) java.io(java.io) com.codename1.io(com.codename1.io) BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) Date(java.util.Date) Paint(android.graphics.Paint) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 72 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class BlackBerryImplementation method init.

public void init(Object m) {
    instance = this;
    app = (CodenameOneUiApplication) m;
    if (askForPermission) {
        app.invokeAndWait(new Runnable() {

            public void run() {
                ApplicationPermissions permRequest = new ApplicationPermissions();
                permRequest.addPermission(ApplicationPermissions.PERMISSION_EMAIL);
                permRequest.addPermission(ApplicationPermissions.PERMISSION_FILE_API);
                permRequest.addPermission(ApplicationPermissions.PERMISSION_WIFI);
                try {
                    // ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION
                    permRequest.addPermission(11);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_MEDIA
                    permRequest.addPermission(21);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_INPUT_SIMULATION
                    permRequest.addPermission(6);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_LOCATION_DATA
                    permRequest.addPermission(14);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_ORGANIZER_DATA
                    permRequest.addPermission(16);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_INTERNET
                    permRequest.addPermission(7);
                } catch (Exception e) {
                }
                try {
                    // ApplicationPermissions.PERMISSION_RECORDING
                    permRequest.addPermission(17);
                } catch (Exception e) {
                }
                ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();
                if (!apm.invokePermissionsRequest(permRequest)) {
                    exitApplication();
                    return;
                }
            }
        });
    }
    app.enableKeyUpEvents(true);
    if (!app.isHandlingEvents()) {
        new Thread() {

            public void run() {
                app.enterEventDispatcher();
            }
        }.start();
    }
    Dialog.setCommandsAsButtons(true);
    UIManager.getInstance().addThemeRefreshListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            Hashtable themeProps = new Hashtable();
            themeProps.put("SoftButton.margin", "0,0,0,0");
            themeProps.put("SoftButton.padding", "0,0,0,0");
            UIManager.getInstance().addThemeProps(themeProps);
        }
    });
    RecordEnumeration e = null;
    RecordStore r = null;
    try {
        r = RecordStore.openRecordStore("FAT", true);
        if (r.getNumRecords() > 0) {
            e = r.enumerateRecords(null, null, false);
            while (e.hasNextElement()) {
                byte[] rec = e.nextRecord();
                ByteArrayInputStream bi = new ByteArrayInputStream(rec);
                DataInputStream di = new DataInputStream(bi);
                String name = di.readUTF();
                short key = di.readShort();
                di.close();
                bi.close();
                fat.put(name, new Short(key));
                if (key >= currentKey) {
                    currentKey += key;
                }
            }
            e.destroy();
            e = null;
        }
        r.closeRecordStore();
        r = null;
    } catch (Exception ex) {
        ex.printStackTrace();
        cleanup(r);
        cleanup(e);
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) Hashtable(java.util.Hashtable) RecordEnumeration(javax.microedition.rms.RecordEnumeration) ApplicationPermissions(net.rim.device.api.applicationcontrol.ApplicationPermissions) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) RecordStoreException(javax.microedition.rms.RecordStoreException) MediaException(javax.microedition.media.MediaException) ConnectionNotFoundException(javax.microedition.io.ConnectionNotFoundException) ActionListener(com.codename1.ui.events.ActionListener) ByteArrayInputStream(java.io.ByteArrayInputStream) RecordStore(javax.microedition.rms.RecordStore) ApplicationPermissionsManager(net.rim.device.api.applicationcontrol.ApplicationPermissionsManager)

Example 73 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class PropertyIndex method loadJSON.

/**
 * Loads JSON for the object from storage with the given name
 * @param name the name of the storage
 */
public void loadJSON(String name) {
    try {
        InputStream is = Storage.getInstance().createInputStream(name);
        JSONParser jp = new JSONParser();
        JSONParser.setUseBoolean(true);
        JSONParser.setUseLongs(true);
        populateFromMap(jp.parseJSON(new InputStreamReader(is, "UTF-8")), parent.getClass());
    } catch (IOException err) {
        Log.e(err);
        throw new RuntimeException(err.toString());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) JSONParser(com.codename1.io.JSONParser) IOException(java.io.IOException)

Example 74 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class CloudObject method unbindProperty.

/**
 * Releases the binding for the specific property name
 * @param cmp the component
 * @param propertyName the name of the property
 */
public void unbindProperty(Component cmp, String propertyName) {
    BindTarget t = (BindTarget) cmp.getClientProperty("CN1Bind" + propertyName);
    cmp.unbindProperty(propertyName, t);
    ;
}
Also used : BindTarget(com.codename1.cloud.BindTarget)

Example 75 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class CloudObject method bindProperty.

/**
 * Binds a property value within the given component to this cloud object, this means that
 * when the component changes the cloud object changes unless deferred. If the defer flag is
 * false all changes are stored in a temporary location and only "committed" once commitBindings()
 * is invoked.
 * @param cmp the component to bind
 * @param propertyName the name of the property in the bound component
 * @param attributeName the key within the cloud object
 * @param defer bind settings whether to defer the binding which requires developers to explicitly commit
 * the binding to perform the changes
 * @param objectLead if set to true the UI property is initialized from values in the CloudObject, if false
 * the cloud object property is initialized from the UI
 */
public void bindProperty(Component cmp, final String propertyName, final String attributeName, final int defer, boolean objectLead) {
    if (objectLead) {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (val == null) {
            if (cmpVal != null) {
                cmp.setBoundPropertyValue(propertyName, null);
            }
        } else {
            if (cmpVal == null || !(val.equals(cmpVal))) {
                cmp.setBoundPropertyValue(propertyName, val);
            }
        }
    } else {
        Object val = values.get(attributeName);
        Object cmpVal = cmp.getBoundPropertyValue(propertyName);
        if (cmpVal == null) {
            if (val != null) {
                values.remove(attributeName);
                status = STATUS_MODIFIED;
            }
        } else {
            if (val == null || !(val.equals(cmpVal))) {
                values.put(attributeName, cmpVal);
                status = STATUS_MODIFIED;
            }
        }
    }
    BindTarget target = new BindTarget() {

        public void propertyChanged(Component source, String propertyName, Object oldValue, Object newValue) {
            switch(defer) {
                case BINDING_DEFERRED:
                    if (deferedValues == null) {
                        deferedValues = new Hashtable();
                    }
                    deferedValues.put(attributeName, newValue);
                    break;
                case BINDING_IMMEDIATE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    break;
                case BINDING_AUTO_SAVE:
                    values.put(attributeName, newValue);
                    status = STATUS_MODIFIED;
                    CloudStorage.getInstance().save(CloudObject.this);
                    break;
            }
        }
    };
    cmp.bindProperty(propertyName, target);
    cmp.putClientProperty("CN1Bind" + propertyName, target);
}
Also used : Hashtable(java.util.Hashtable) BindTarget(com.codename1.cloud.BindTarget) Component(com.codename1.ui.Component)

Aggregations

IOException (java.io.IOException)36 Component (com.codename1.ui.Component)17 Hashtable (java.util.Hashtable)17 ArrayList (java.util.ArrayList)16 AnimationObject (com.codename1.ui.animations.AnimationObject)15 File (java.io.File)14 Form (com.codename1.ui.Form)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 FileInputStream (java.io.FileInputStream)13 InputStream (java.io.InputStream)12 Label (com.codename1.ui.Label)11 Button (com.codename1.ui.Button)10 Container (com.codename1.ui.Container)10 EncodedImage (com.codename1.ui.EncodedImage)10 TextArea (com.codename1.ui.TextArea)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 Point (java.awt.Point)10 FileOutputStream (java.io.FileOutputStream)10 Paint (android.graphics.Paint)8 BufferedInputStream (com.codename1.io.BufferedInputStream)7