Search in sources :

Example 81 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class BoxLayout method layoutContainer.

/**
 * {@inheritDoc}
 */
public void layoutContainer(Container parent) {
    Style ps = parent.getStyle();
    int width = parent.getLayoutWidth() - parent.getSideGap() - ps.getHorizontalPadding();
    int height = parent.getLayoutHeight() - parent.getBottomGap() - ps.getVerticalPadding();
    int x = ps.getPaddingLeft(parent.isRTL());
    int y = ps.getPaddingTop();
    int numOfcomponents = parent.getComponentCount();
    boolean rtl = parent.isRTL();
    if (rtl) {
        x += parent.getSideGap();
    }
    int initX = x;
    for (int i = 0; i < numOfcomponents; i++) {
        Component cmp = parent.getComponentAt(i);
        Style stl = cmp.getStyle();
        switch(axis) {
            case Y_AXIS:
            case Y_AXIS_BOTTOM_LAST:
                int cmpBottom = height;
                cmp.setWidth(width - stl.getHorizontalMargins());
                int cmpH = cmp.getPreferredH();
                y += stl.getMarginTop();
                if (y - ps.getPaddingTop() >= cmpBottom && !parent.isScrollableY()) {
                    cmpH = 0;
                } else if (y + cmpH - ps.getPaddingTop() > cmpBottom) {
                    if (!parent.isScrollableY()) {
                        cmpH = cmpBottom - y - stl.getMarginBottom();
                    }
                }
                cmp.setHeight(cmpH);
                cmp.setX(x + stl.getMarginLeft(parent.isRTL()));
                cmp.setY(y);
                y += cmp.getHeight() + stl.getMarginBottom();
                break;
            case X_AXIS_NO_GROW:
                {
                    int cmpRight = width;
                    height = Math.min(getPreferredSize(parent).getHeight(), height);
                    int cmpW = cmp.getPreferredW();
                    x += stl.getMarginLeftNoRTL();
                    if (x >= cmpRight && !parent.isScrollableX()) {
                        cmpW = 0;
                    } else {
                        if (x + cmpW - ps.getPaddingLeftNoRTL() > cmpRight) {
                            cmpW = cmpRight - x - stl.getMarginRightNoRTL();
                        }
                    }
                    cmp.setWidth(cmpW);
                    cmp.setHeight(height - stl.getMarginTop() - stl.getMarginBottom());
                    if (rtl) {
                        cmp.setX(width + initX - (x - initX) - cmpW);
                    } else {
                        cmp.setX(x);
                    }
                    cmp.setY(y + stl.getMarginTop());
                    x += cmp.getWidth() + stl.getMarginRightNoRTL();
                    break;
                }
            default:
                int cmpRight = width;
                int cmpW = cmp.getPreferredW();
                x += stl.getMarginLeftNoRTL();
                if (x >= cmpRight && !parent.isScrollableX()) {
                    cmpW = 0;
                } else {
                    if (x + cmpW - ps.getPaddingLeftNoRTL() > cmpRight) {
                        cmpW = cmpRight - x - stl.getMarginRightNoRTL();
                    }
                }
                cmp.setWidth(cmpW);
                cmp.setHeight(height - stl.getVerticalMargins());
                if (rtl) {
                    cmp.setX(width + initX - (x - initX) - cmpW);
                } else {
                    cmp.setX(x);
                }
                cmp.setY(y + stl.getMarginTop());
                x += cmp.getWidth() + stl.getMarginRightNoRTL();
                break;
        }
    }
    if (axis == Y_AXIS_BOTTOM_LAST && numOfcomponents > 0) {
        if (parent instanceof Form) {
            parent = ((Form) parent).getContentPane();
        }
        Component cmp = parent.getComponentAt(numOfcomponents - 1);
        if (cmp.getY() + cmp.getHeight() < height) {
            cmp.setY(height - cmp.getHeight());
        }
    }
    if (axis == Y_AXIS) {
        if (numOfcomponents > 0) {
            int containerBottomInner = parent.getLayoutHeight() - parent.getStyle().getPaddingBottom();
            Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
            int lastCmpBottomOuter = lastCmp.getY() + lastCmp.getHeight() + lastCmp.getStyle().getMarginBottom();
            int dy = 0;
            switch(align) {
                case Component.CENTER:
                    {
                        dy = (containerBottomInner - lastCmpBottomOuter) / 2;
                        break;
                    }
                case Component.BOTTOM:
                    {
                        dy = (containerBottomInner - lastCmpBottomOuter);
                        break;
                    }
            }
            if (dy > 0) {
                for (int i = 0; i < numOfcomponents; i++) {
                    Component cmp = parent.getComponentAt(i);
                    cmp.setY(cmp.getY() + dy);
                }
            }
        }
    } else if (axis == X_AXIS) {
        if (numOfcomponents > 0) {
            if (rtl) {
                int containerLeftInner = parent.getStyle().getPaddingLeftNoRTL();
                Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
                int lastCmpLeftOuter = lastCmp.getX() - lastCmp.getStyle().getMarginLeftNoRTL();
                int dx = 0;
                switch(align) {
                    case Component.CENTER:
                        {
                            dx = (lastCmpLeftOuter - containerLeftInner) / 2;
                            break;
                        }
                    case Component.RIGHT:
                        {
                            dx = (lastCmpLeftOuter - containerLeftInner);
                            break;
                        }
                }
                if (dx > 0) {
                    for (int i = 0; i < numOfcomponents; i++) {
                        Component cmp = parent.getComponentAt(i);
                        cmp.setX(cmp.getX() - dx);
                    }
                }
            } else {
                int containerRightInner = parent.getLayoutWidth() - parent.getStyle().getPaddingRightNoRTL();
                Component lastCmp = parent.getComponentAt(numOfcomponents - 1);
                int lastCmpRightOuter = lastCmp.getX() + lastCmp.getWidth() + lastCmp.getStyle().getMarginRightNoRTL();
                int dx = 0;
                switch(align) {
                    case Component.CENTER:
                        {
                            dx = (containerRightInner - lastCmpRightOuter) / 2;
                            break;
                        }
                    case Component.RIGHT:
                        {
                            dx = (containerRightInner - lastCmpRightOuter);
                            break;
                        }
                }
                if (dx > 0) {
                    for (int i = 0; i < numOfcomponents; i++) {
                        Component cmp = parent.getComponentAt(i);
                        cmp.setX(cmp.getX() + dx);
                    }
                }
            }
        }
    }
}
Also used : Form(com.codename1.ui.Form) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component)

Example 82 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class AndroidImplementation method createMediaException.

public static MediaException createMediaException(int extra) {
    MediaErrorType type;
    String message;
    switch(extra) {
        case MediaPlayer.MEDIA_ERROR_IO:
            type = MediaErrorType.Network;
            message = "IO error";
            break;
        case MediaPlayer.MEDIA_ERROR_MALFORMED:
            type = MediaErrorType.Decode;
            message = "Media was malformed";
            break;
        case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            type = MediaErrorType.SrcNotSupported;
            message = "Not valie for progressive playback";
            break;
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            type = MediaErrorType.Network;
            message = "Server died";
            break;
        case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
            type = MediaErrorType.Network;
            message = "Timed out";
            break;
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            type = MediaErrorType.Network;
            message = "Unknown error";
            break;
        case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
            type = MediaErrorType.SrcNotSupported;
            message = "Unsupported media";
            break;
        default:
            type = MediaErrorType.Network;
            message = "Unknown error";
    }
    return new MediaException(type, message);
}
Also used : MediaException(com.codename1.media.AsyncMedia.MediaException) MediaErrorType(com.codename1.media.AsyncMedia.MediaErrorType)

Example 83 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class AndroidImplementation method createImage.

@Override
public Object createImage(String path) throws IOException {
    int IMAGE_MAX_SIZE = getDisplayHeight();
    if (exists(path)) {
        Bitmap b = null;
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            o.inPreferredConfig = Bitmap.Config.ARGB_8888;
            InputStream fis = createFileInputStream(path);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();
            int scale = 1;
            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
            if (sampleSizeOverride != -1) {
                o2.inSampleSize = sampleSizeOverride;
            } else {
                String sampleSize = Display.getInstance().getProperty("android.sampleSize", null);
                if (sampleSize != null) {
                    o2.inSampleSize = Integer.parseInt(sampleSize);
                } else {
                    o2.inSampleSize = scale;
                }
            }
            o2.inPurgeable = true;
            o2.inInputShareable = true;
            fis = createFileInputStream(path);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
            // fix rotation
            ExifInterface exif = new ExifInterface(removeFilePrefix(path));
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int angle = 0;
            switch(orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
            }
            if (sampleSizeOverride < 0 && angle != 0) {
                Matrix mat = new Matrix();
                mat.postRotate(angle);
                Bitmap correctBmp = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true);
                b.recycle();
                b = correctBmp;
            }
        } catch (IOException e) {
        }
        return b;
    } else {
        InputStream in = this.getResourceAsStream(getClass(), path);
        if (in == null) {
            throw new IOException("Resource not found. " + path);
        }
        try {
            return this.createImage(in);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ignored) {
                    ;
                }
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) Paint(android.graphics.Paint) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MediaException(com.codename1.media.AsyncMedia.MediaException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 84 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class AndroidImplementation method captureVideo.

@Override
public void captureVideo(VideoCaptureConstraints cnst, ActionListener response) {
    if (getActivity() == null) {
        throw new RuntimeException("Cannot capture video in background mode");
    }
    if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to take a video")) {
        return;
    }
    if (getRequestedPermissions().contains(Manifest.permission.CAMERA)) {
        // See https://github.com/codenameone/CodenameOne/issues/2409#issuecomment-391696058
        if (!checkForPermission(Manifest.permission.CAMERA, "This is required to take a video")) {
            return;
        }
    }
    callback = new EventDispatcher();
    callback.addListener(response);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
    if (cnst != null) {
        switch(cnst.getQuality()) {
            case VideoCaptureConstraints.QUALITY_LOW:
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                break;
            case VideoCaptureConstraints.QUALITY_HIGH:
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                break;
        }
        if (cnst.getMaxFileSize() > 0) {
            intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, cnst.getMaxFileSize());
        }
        if (cnst.getMaxLength() > 0) {
            intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, cnst.getMaxLength());
        }
    }
    File newFile = getOutputMediaFile(true);
    newFile.getParentFile().mkdirs();
    newFile.getParentFile().setWritable(true, false);
    Uri videoUri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", newFile);
    Storage.getInstance().writeObject("videoUri", newFile.getAbsolutePath());
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, videoUri);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if (Build.VERSION.SDK_INT < 21) {
        List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            getContext().grantUriPermission(packageName, videoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }
    this.getActivity().startActivityForResult(intent, CAPTURE_VIDEO);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Uri(android.net.Uri)

Example 85 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class CodenameOneView method onTouchEvent.

// private boolean nativePeerGrabbedPointer = false;
public boolean onTouchEvent(MotionEvent event) {
    if (this.implementation.getCurrentForm() == null) {
        /**
         * make sure a form has been set before we can send events to the
         * EDT. if we send events before the form has been set we might
         * deadlock!
         */
        return true;
    }
    int[] x = null;
    int[] y = null;
    int size = event.getPointerCount();
    if (size > 1) {
        x = new int[size];
        y = new int[size];
        for (int i = 0; i < size; i++) {
            x[i] = (int) event.getX(i);
            y[i] = (int) event.getY(i);
        }
    }
    /*
        if (!cn1GrabbedPointer) {
            
            if (x == null) {
                Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)event.getX(), (int)event.getY());
                if (componentAt != null && (componentAt instanceof PeerComponent)) {
                    
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        //nativePeerGrabbedPointer = true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        //nativePeerGrabbedPointer = false;
                    }
                    return false;
                }

            } else {
                Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)x[0], (int)y[0]);
                if (componentAt != null && (componentAt instanceof PeerComponent)) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        nativePeerGrabbedPointer = true;
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        nativePeerGrabbedPointer = false;
                    }
                    return false;
                }
            }
        }
        */
    // if (nativePeerGrabbedPointer) {
    // return false;
    // }
    Component componentAt;
    try {
        if (x == null) {
            componentAt = this.implementation.getCurrentForm().getComponentAt((int) event.getX(), (int) event.getY());
        } else {
            componentAt = this.implementation.getCurrentForm().getComponentAt((int) x[0], (int) y[0]);
        }
    } catch (Throwable t) {
        // Since this is is an EDT violation, we may get an exception
        // Just consume it
        componentAt = null;
    }
    boolean isPeer = (componentAt instanceof PeerComponent);
    boolean consumeEvent = !isPeer || cn1GrabbedPointer;
    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (x == null) {
                this.implementation.pointerPressed((int) event.getX(), (int) event.getY());
            } else {
                this.implementation.pointerPressed(x, y);
            }
            if (!isPeer)
                cn1GrabbedPointer = true;
            break;
        case MotionEvent.ACTION_UP:
            if (x == null) {
                this.implementation.pointerReleased((int) event.getX(), (int) event.getY());
            } else {
                this.implementation.pointerReleased(x, y);
            }
            cn1GrabbedPointer = false;
            break;
        case MotionEvent.ACTION_CANCEL:
            cn1GrabbedPointer = false;
            break;
        case MotionEvent.ACTION_MOVE:
            if (x == null) {
                this.implementation.pointerDragged((int) event.getX(), (int) event.getY());
            } else {
                this.implementation.pointerDragged(x, y);
            }
            break;
    }
    return consumeEvent;
}
Also used : PeerComponent(com.codename1.ui.PeerComponent) Component(com.codename1.ui.Component) PeerComponent(com.codename1.ui.PeerComponent)

Aggregations

Component (com.codename1.ui.Component)19 Font (com.codename1.ui.Font)18 Container (com.codename1.ui.Container)15 Form (com.codename1.ui.Form)14 Style (com.codename1.ui.plaf.Style)12 Button (com.codename1.ui.Button)11 Image (com.codename1.ui.Image)11 TextArea (com.codename1.ui.TextArea)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)9 Label (com.codename1.ui.Label)8 FileInputStream (java.io.FileInputStream)8 ActionListener (com.codename1.ui.events.ActionListener)7 TextField (com.codename1.ui.TextField)6 ActionEvent (com.codename1.ui.events.ActionEvent)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 EncodedImage (com.codename1.ui.EncodedImage)5