Search in sources :

Example 86 with Switch

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

the class CodenameOneView method setInputType.

public void setInputType(EditorInfo editorInfo) {
    /**
     * do not use the enter key to fire some kind of action!
     */
    // editorInfo.imeOptions |= EditorInfo.IME_ACTION_NONE;
    Component txtCmp = Display.getInstance().getCurrent().getFocused();
    if (txtCmp != null && txtCmp instanceof TextArea) {
        TextArea txt = (TextArea) txtCmp;
        if (txt.isSingleLineTextArea()) {
            editorInfo.imeOptions |= EditorInfo.IME_ACTION_DONE;
        } else {
            editorInfo.imeOptions |= EditorInfo.IME_ACTION_NONE;
        }
        int inputType = 0;
        int constraint = txt.getConstraint();
        if ((constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
            constraint = constraint ^ TextArea.PASSWORD;
        }
        switch(constraint) {
            case TextArea.NUMERIC:
                inputType = EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_SIGNED;
                break;
            case TextArea.DECIMAL:
                inputType = EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_DECIMAL;
                break;
            case TextArea.PHONENUMBER:
                inputType = EditorInfo.TYPE_CLASS_PHONE;
                break;
            case TextArea.EMAILADDR:
                inputType = EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
                break;
            case TextArea.URL:
                inputType = EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_URI;
                break;
            default:
                inputType = EditorInfo.TYPE_CLASS_TEXT;
                break;
        }
        editorInfo.inputType = inputType;
    }
}
Also used : TextArea(com.codename1.ui.TextArea) Component(com.codename1.ui.Component) PeerComponent(com.codename1.ui.PeerComponent)

Example 87 with Switch

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

the class ResetableTextWatcher method startEditing.

/**
 * Start editing the given text-area
 * This method is executed on the UI thread, so UI manipulation is safe here.
 * @param activity Current running activity
 * @param textArea The TextAreaData instance that wraps the CN1 TextArea that our internal EditText needs to overlap.  We use
 *                 a TextAreaData so that the text area properties can be accessed off the EDT safely.
 * @param codenameOneInputType One of the input type constants in com.codename1.ui.TextArea
 * @param initialText The text that appears in the Codename One text are before the call to startEditing
 * @param isEditedFieldSwitch if true, then special case for async edit mode - the native editing is already active, no need to show
 *                            native field, just change the connected field
 */
private synchronized void startEditing(Activity activity, TextAreaData textArea, String initialText, int codenameOneInputType, final boolean isEditedFieldSwitch) {
    int txty = lastTextAreaY = textArea.getAbsoluteY() + textArea.getScrollY();
    int txtx = lastTextAreaX = textArea.getAbsoluteX() + textArea.getScrollX();
    lastTextAreaWidth = textArea.getWidth();
    lastTextAreaHeight = textArea.getHeight();
    int paddingTop = 0;
    int paddingLeft = textArea.paddingLeft;
    int paddingRight = textArea.paddingRight;
    int paddingBottom = textArea.paddingBottom;
    // An ugly hack to smooth over an apparent race condition where
    // the lightweight textarea is not repainted after the native text field
    // becomes visible - resulting in the hint still appearing while typing.
    // https://github.com/codenameone/CodenameOne/issues/2629
    // We just blindly repaint the textfield every 50ms for half a second
    // to make sure it gets a repaint properly.
    final TextArea fTextArea = textArea.textArea;
    new Thread(new Runnable() {

        public void run() {
            for (int i = 0; i < 10; i++) {
                com.codename1.io.Util.sleep(50);
                com.codename1.ui.CN.callSerially(new Runnable() {

                    public void run() {
                        fTextArea.repaint();
                    }
                });
            }
        }
    }).start();
    if (textArea.isTextField) {
        switch(textArea.getVerticalAlignment()) {
            case Component.BOTTOM:
                paddingTop = textArea.getHeight() - textArea.paddingBottom - textArea.fontHeight;
                break;
            case Component.CENTER:
                paddingTop = textArea.getHeight() / 2 - textArea.fontHeight / 2;
                break;
            default:
                paddingTop = textArea.paddingTop;
                break;
        }
    } else {
        paddingTop = textArea.paddingTop;
    }
    int id = activity.getResources().getIdentifier("cn1Style", "attr", activity.getApplicationInfo().packageName);
    if (!isEditedFieldSwitch) {
        mEditText = new EditView(activity, textArea.textArea, this, id);
        defaultInputType = mEditText.getInputType();
        defaultIMEOptions = mEditText.getImeOptions();
    } else {
        mEditText.switchToTextArea(textArea.textArea);
    }
    if (textArea.getClientProperty("blockCopyPaste") != null || Display.getInstance().getProperty("blockCopyPaste", "false").equals("true")) {
        // The code below is taken from this stackoverflow answer: http://stackoverflow.com/a/22756538/756809
        if (android.os.Build.VERSION.SDK_INT < 11) {
            mEditText.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

                @Override
                public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                    menu.clear();
                }
            });
        } else {
            mEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                public void onDestroyActionMode(ActionMode mode) {
                }

                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
            });
        }
    } else if (isEditedFieldSwitch) {
        // reset copy-paste protection
        if (android.os.Build.VERSION.SDK_INT < 11) {
            mEditText.setOnCreateContextMenuListener(null);
        } else {
            mEditText.setCustomSelectionActionModeCallback(null);
        }
    }
    if (!isEditedFieldSwitch) {
        mEditText.addTextChangedListener(mEditText.mTextWatcher);
    }
    mEditText.setBackgroundDrawable(null);
    mEditText.setFocusableInTouchMode(true);
    mEditLayoutParams = new FrameLayout.LayoutParams(0, 0);
    // Set the appropriate gravity so that the left and top margins will be
    // taken into account
    mEditLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    mEditLayoutParams.setMargins(txtx, txty, 0, 0);
    mEditLayoutParams.width = textArea.getWidth();
    mEditLayoutParams.height = textArea.getHeight();
    mEditText.setLayoutParams(mEditLayoutParams);
    if (textArea.isRTL()) {
        mEditText.setGravity(Gravity.RIGHT | Gravity.TOP);
    } else {
        mEditText.setGravity(Gravity.LEFT | Gravity.TOP);
    }
    mEditText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
    Component nextDown = textArea.nextDown;
    boolean imeOptionTaken = true;
    int ime = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
    if (textArea.isSingleLineTextArea() || textArea.getDoneListener() != null) {
        if (textArea.getClientProperty("searchField") != null) {
            mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEARCH);
        } else {
            if (textArea.getClientProperty("sendButton") != null) {
                mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEND);
            } else {
                if (textArea.getClientProperty("goButton") != null) {
                    mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_GO);
                } else {
                    if (textArea.getDoneListener() != null) {
                        mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
                    } else if (nextDown != null) {
                        mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_NEXT);
                    } else {
                        mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
                        imeOptionTaken = false;
                    }
                }
            }
        }
    }
    mEditText.setSingleLine(textArea.isSingleLineTextArea());
    mEditText.setAdapter((ArrayAdapter<String>) null);
    mEditText.setText(initialText);
    if (!textArea.isSingleLineTextArea() && textArea.textArea.isGrowByContent() && textArea.textArea.getGrowLimit() > -1) {
        defaultMaxLines = mEditText.getMaxLines();
        mEditText.setMaxLines(textArea.textArea.getGrowLimit());
    }
    if (textArea.nativeHintBool && textArea.getHint() != null) {
        mEditText.setHint(textArea.getHint());
    }
    if (!isEditedFieldSwitch) {
        addView(mEditText, mEditLayoutParams);
    }
    invalidate();
    setVisibility(VISIBLE);
    bringToFront();
    mEditText.requestFocus();
    Object nativeFont = textArea.nativeFont;
    if (nativeFont == null) {
        nativeFont = impl.getDefaultFont();
    }
    Paint p = (Paint) ((AndroidImplementation.NativeFont) nativeFont).font;
    mEditText.setTypeface(p.getTypeface());
    mEditText.setTextScaleX(p.getTextScaleX());
    mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, p.getTextSize());
    int fgColor = textArea.fgColor;
    mEditText.setTextColor(Color.rgb(fgColor >> 16, (fgColor & 0x00ff00) >> 8, (fgColor & 0x0000ff)));
    boolean password = false;
    if ((codenameOneInputType & TextArea.PASSWORD) == TextArea.PASSWORD) {
        codenameOneInputType = codenameOneInputType ^ TextArea.PASSWORD;
        password = true;
    }
    if (textArea.isSingleLineTextArea()) {
        mEditText.setInputType(getAndroidInputType(codenameOneInputType));
        // if not ime was explicity requested and this is a single line textfield of type ANY add the emoji keyboard.
        if (!imeOptionTaken && codenameOneInputType == TextArea.ANY) {
            mEditText.setInputType(getAndroidInputType(codenameOneInputType) | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
        }
        if (Display.getInstance().getProperty("andAddComma", "false").equals("true") && (codenameOneInputType & TextArea.DECIMAL) == TextArea.DECIMAL) {
            defaultKeyListener = mEditText.getKeyListener();
            mEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
        }
    } else {
        if (textArea.getDoneListener() != null) {
            mEditText.setHorizontallyScrolling(false);
            mEditText.setMaxLines(Integer.MAX_VALUE);
            mEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
            mEditText.setMaxWidth(textArea.getWidth());
            mEditText.setMaxHeight(textArea.getHeight());
            mEditText.setHorizontalScrollBarEnabled(false);
            mEditText.getLayoutParams().width = textArea.getWidth();
            mEditText.getLayoutParams().height = textArea.getHeight();
        } else {
            mEditText.setInputType(getAndroidInputType(codenameOneInputType, true));
        }
    }
    if (password) {
        int type = mInputTypeMap.get(codenameOneInputType, InputType.TYPE_CLASS_TEXT);
        if ((type & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) == InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) {
            type = type ^ InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
        }
        // turn off suggestions for passwords
        mEditText.setInputType(type | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        mEditText.setTransformationMethod(new MyPasswordTransformationMethod());
    }
    int maxLength = textArea.maxSize;
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(maxLength);
    mEditText.setFilters(FilterArray);
    mEditText.setSelection(mEditText.getText().length());
    showVirtualKeyboard(true);
    if (Boolean.FALSE.equals(textArea.getClientProperty("android.cursorVisible"))) {
        // This provides an imperfect workaround for this issue:
        // https://github.com/codenameone/CodenameOne/issues/2317
        // Blinking cursor causes text to disappear on some versions of android
        // Can't seem to find how to detect whether device is affected, so
        // just providing a client property to disable the blinking cursor
        // on a particular text field.
        mEditText.setCursorVisible(false);
    }
/*
        // Leaving this hack here for posterity.  It seems that this manually
        // blinking cursor causes the paste menu to disappear
        // https://github.com/codenameone/CodenameOne/issues/2147
        // Removing the hack below, fixes this issue.  And in the test device
        // I'm using the blinking of text doesn't seem to occur, so perhaps
        // it was fixed via other means.  Test device:
        // Name: Samsung Galaxy S3 (T-Mobile)
        //    OS: 4.3
        //    Manufacturer: Samsung
        //    Model: 4.3
        //    Chipset: armeabi-v7a 1512MHz
        //    Memory: 16000000000
        //    Heap: 256000000
        //    Display: 720 x 1280
        //
        // UPDATE Feb. 13, 2018:
        // This issue seems to be still present in some devices, but it isn't clear even
        // how to detect it.
        // Issue reported and reproduced here https://github.com/codenameone/CodenameOne/issues/2317
        // Issue has been observed in a Virtual Box installation with 5.1.1, but
        // cannot be reproduced in a Nexus 5 running 5.1.1.
        // 
        if (Build.VERSION.SDK_INT < 21) {
            // HACK!!!  On Android 4.4, it seems that the natural blinking cursor
            // causes text to disappear when it blinks.  Manually blinking the
            // cursor seems to work around this issue, so that's what we do here.
            // This issue is described here: http://stackoverflow.com/questions/41305052/textfields-content-disappears-during-typing?noredirect=1#comment69977316_41305052
            mEditText.setCursorVisible(false);
            final boolean[] cursorVisible = new boolean[]{false};
            if (cursorTimer != null) {
                cursorTimer.cancel();
            }
            cursorTimer = new Timer();
            cursorTimerTask = new TimerTask() {
                public void run() {
                    AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            EditView v = mEditText;
                            if (v != null) {
                                cursorVisible[0] = !cursorVisible[0];
                                v.setCursorVisible(cursorVisible[0]);
                            }
                        }
                    });

                }
            };
            cursorTimer.schedule(cursorTimerTask, 100, 500);
        }
        */
}
Also used : TextArea(com.codename1.ui.TextArea) ContextMenu(android.view.ContextMenu) ContextMenu(android.view.ContextMenu) Menu(android.view.Menu) Component(com.codename1.ui.Component) InputFilter(android.text.InputFilter) MenuItem(android.view.MenuItem) Paint(android.graphics.Paint) View(android.view.View) AutoCompleteTextView(android.widget.AutoCompleteTextView) Paint(android.graphics.Paint) ActionMode(android.view.ActionMode) FrameLayout(android.widget.FrameLayout) ContextMenuInfo(android.view.ContextMenu.ContextMenuInfo)

Example 88 with Switch

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

the class AndroidGraphics method paintComponentBackground.

public void paintComponentBackground(int x, int y, int width, int height, Style s) {
    if (width <= 0 || height <= 0) {
        return;
    }
    canvas.save();
    applyTransform();
    Image bgImageOrig = s.getBgImage();
    try {
        if (bgImageOrig == null) {
            if (s.getBackgroundType() >= Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL) {
                drawGradientBackground(s, x, y, width, height);
                // canvas.restore();
                return;
            }
            setColor(s.getBgColor());
            fillRectImpl(x, y, width, height, s.getBgTransparency());
            // canvas.restore();
            return;
        } else {
            int iW = bgImageOrig.getWidth();
            int iH = bgImageOrig.getHeight();
            Object bgImage = bgImageOrig.getImage();
            switch(s.getBackgroundType()) {
                case Style.BACKGROUND_NONE:
                    if (s.getBgTransparency() != 0) {
                        setColor(s.getBgColor());
                        fillRectImpl(x, y, width, height, s.getBgTransparency());
                    }
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_SCALED:
                    drawImageImpl(bgImage, x, y, width, height);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_SCALED_FILL:
                    float r = Math.max(((float) width) / ((float) iW), ((float) height) / ((float) iH));
                    int bwidth = (int) (((float) iW) * r);
                    int bheight = (int) (((float) iH) * r);
                    drawImageImpl(bgImage, x + (width - bwidth) / 2, y + (height - bheight) / 2, bwidth, bheight);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_SCALED_FIT:
                    if (s.getBgTransparency() != 0) {
                        setColor(s.getBgColor());
                        fillRectImpl(x, y, width, height, s.getBgTransparency());
                    }
                    float r2 = Math.min(((float) width) / ((float) iW), ((float) height) / ((float) iH));
                    int awidth = (int) (((float) iW) * r2);
                    int aheight = (int) (((float) iH) * r2);
                    drawImageImpl(bgImage, x + (width - awidth) / 2, y + (height - aheight) / 2, awidth, aheight);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_BOTH:
                    tileImageImpl(bgImage, x, y, width, height);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_TOP:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    tileImageImpl(bgImage, x, y, width, iH);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_CENTER:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    tileImageImpl(bgImage, x, y + (height / 2 - iH / 2), width, iH);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_HORIZONTAL_ALIGN_BOTTOM:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    tileImageImpl(bgImage, x, y + (height - iH), width, iH);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_LEFT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    for (int yPos = 0; yPos <= height; yPos += iH) {
                        canvas.drawBitmap((Bitmap) bgImage, x, y + yPos, paint);
                    }
                    canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_CENTER:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    for (int yPos = 0; yPos <= height; yPos += iH) {
                        canvas.drawBitmap((Bitmap) bgImage, x + (width / 2 - iW / 2), y + yPos, paint);
                    }
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_TILE_VERTICAL_ALIGN_RIGHT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    for (int yPos = 0; yPos <= height; yPos += iH) {
                        canvas.drawBitmap((Bitmap) bgImage, x + width - iW, y + yPos, paint);
                    }
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_TOP:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + (width / 2 - iW / 2), y, paint);
                    canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + (width / 2 - iW / 2), y + (height - iH), paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_LEFT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x, y + (height / 2 - iH / 2), paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_RIGHT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + width - iW, y + (height / 2 - iH / 2), paint);
                    canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_CENTER:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + (width / 2 - iW / 2), y + (height / 2 - iH / 2), paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_TOP_LEFT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x, y, paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_TOP_RIGHT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + width - iW, y, paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_LEFT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x, y + (height - iH), paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_IMAGE_ALIGNED_BOTTOM_RIGHT:
                    setColor(s.getBgColor());
                    fillRectImpl(x, y, width, height, s.getBgTransparency());
                    canvas.drawBitmap((Bitmap) bgImage, x + width - iW, y + (height - iH), paint);
                    // canvas.restore();
                    return;
                case Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL:
                case Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL:
                case Style.BACKGROUND_GRADIENT_RADIAL:
                    drawGradientBackground(s, x, y, width, height);
                    // canvas.restore();
                    return;
            }
        }
    } finally {
        unapplyTransform();
        canvas.restore();
    }
}
Also used : Image(com.codename1.ui.Image)

Example 89 with Switch

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

the class AndroidVideoCaptureConstraintsCompiler method compile.

@Override
public VideoCaptureConstraints compile(VideoCaptureConstraints cnst) {
    VideoCaptureConstraints out = new VideoCaptureConstraints(cnst);
    // We can't actually support explicit width and height constraints
    // right now, so we set these to zero
    out.preferredHeight(0);
    out.preferredWidth(0);
    // But we do support low and high quality
    switch(cnst.getPreferredQuality()) {
        case VideoCaptureConstraints.QUALITY_LOW:
        case VideoCaptureConstraints.QUALITY_HIGH:
            break;
        default:
            // Smaller than 640x480 we'll call low quality.  That number is just pulled out of the air.
            if (cnst.getPreferredHeight() > 0 && cnst.getPreferredWidth() > 0) {
                if (cnst.getPreferredHeight() <= 480 || cnst.getPreferredWidth() <= 640) {
                    out.preferredQuality(VideoCaptureConstraints.QUALITY_LOW);
                } else {
                    out.preferredQuality(VideoCaptureConstraints.QUALITY_HIGH);
                }
            }
    }
    return out;
}
Also used : VideoCaptureConstraints(com.codename1.capture.VideoCaptureConstraints)

Example 90 with Switch

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

the class AudioRecorderComponentSample method recordAudio.

private AsyncResource<String> recordAudio() {
    AsyncResource<String> out = new AsyncResource<>();
    String mime = MediaManager.getAvailableRecordingMimeTypes()[0];
    String ext = mime.indexOf("mp3") != -1 ? "mp3" : mime.indexOf("wav") != -1 ? "wav" : mime.indexOf("aiff") != -1 ? "aiff" : "aac";
    MediaRecorderBuilder builder = new MediaRecorderBuilder().path(new File("myaudio." + ext).getAbsolutePath()).mimeType(mime);
    final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
    final Sheet sheet = new Sheet(null, "Record Audio");
    sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
    sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
    cmp.addActionListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            switch(cmp.getState()) {
                case Accepted:
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(builder.getPath());
                                }
                            });
                        }
                    });
                    break;
                case Canceled:
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(builder.getPath())) {
                        FileSystemStorage.getInstance().delete(builder.getPath());
                    }
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(null);
                                }
                            });
                        }
                    });
                    break;
            }
        }
    });
    sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
                FileSystemStorage fs = FileSystemStorage.getInstance();
                if (fs.exists(builder.getPath())) {
                    FileSystemStorage.getInstance().delete(builder.getPath());
                }
                CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                    public void run() {
                        out.complete(null);
                    }
                });
            }
        }
    });
    sheet.show();
    return out;
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ActionListener(com.codename1.ui.events.ActionListener) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent) AsyncResource(com.codename1.util.AsyncResource) File(com.codename1.io.File) Sheet(com.codename1.ui.Sheet)

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