Search in sources :

Example 81 with com.codename1.io

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

the class ResetableTextWatcher method edit.

/**
 * Entry point for using this class
 * @param impl The current running activity
 * @param component Any subclass of com.codename1.ui.TextArea
 * @param inputType One of the TextArea's input-type constants
 */
public static void edit(final AndroidImplementation impl, final Component component, final int inputType) {
    if (impl.getActivity() == null) {
        throw new IllegalArgumentException("activity is null");
    }
    if (component == null) {
        throw new IllegalArgumentException("component is null");
    }
    if (!(component instanceof TextArea)) {
        throw new IllegalArgumentException("component must be instance of TextArea");
    }
    final TextArea textArea = (TextArea) component;
    textArea.registerAsInputDevice();
    final String initialText = textArea.getText();
    textArea.putClientProperty("InPlaceEditView.initialText", initialText);
    // The very first time we try to edit a string, let's determine if the
    // system default is to do async editing.  If the system default
    // is not yet set, we set it here, and it will be used as the default from now on
    // We do this because the nativeInstance.isAsyncEditMode() value changes
    // to reflect the currently edited field so it isn't a good way to keep a
    // system default.
    String defaultAsyncEditingSetting = Display.getInstance().getProperty("android.VKBAlwaysOpen", null);
    if (defaultAsyncEditingSetting == null) {
        defaultAsyncEditingSetting = impl.isAsyncEditMode() ? "true" : "false";
        Display.getInstance().setProperty("android.VKBAlwaysOpen", defaultAsyncEditingSetting);
    }
    boolean asyncEdit = "true".equals(defaultAsyncEditingSetting) ? true : false;
    // Check if the form has any setting for asyncEditing that should override
    // the application defaults.
    final Form parentForm = component.getComponentForm();
    if (parentForm == null) {
        com.codename1.io.Log.p("Attempt to edit text area that is not on a form.  This is not supported");
        return;
    }
    if (parentForm.getClientProperty("asyncEditing") != null) {
        Object async = parentForm.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.getClientProperty("android.asyncEditing") != null) {
        Object async = parentForm.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to ios.asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.isFormBottomPaddingEditingMode()) {
        asyncEdit = true;
    }
    // then this will override all other settings.
    if (component.getClientProperty("asyncEditing") != null) {
        Object async = component.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field asyncEditing client property: "+asyncEdit);
        }
    }
    if (component.getClientProperty("android.asyncEditing") != null) {
        Object async = component.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field ios.asyncEditing client property: "+asyncEdit);
        }
    }
    final boolean resizeEditMode = "resize".equalsIgnoreCase(String.valueOf(component.getClientProperty("android.editMode")));
    final boolean panEditMode = "pan".equalsIgnoreCase(String.valueOf(component.getClientProperty("android.editMode")));
    // if true, then in async mode we are currently editing and are switching to another field
    final boolean isEditedFieldSwitch;
    // If we are already editing, we need to finish that up before we proceed to edit the next field.
    synchronized (editingLock) {
        if (mIsEditing) {
            if (impl.isAsyncEditMode()) {
                // Using isEditedFieldSwitch was causing issues with cursors not showing up.
                // https://github.com/codenameone/CodenameOne/issues/2353
                // https://stackoverflow.com/questions/49004370/focus-behaviour-in-textarea-in-cn1
                // Disabling this feature by default now, but can be re-enabled by setting
                // Display.getInstance().setProperty("android.reuseTextEditorOnSwitch", "true");
                // This editedFieldSwitch feature was added a while back to improve experience on older
                // Android devices where the field switching was going too slow.
                // https://github.com/codenameone/CodenameOne/issues/2012
                // This issue was resolved in this commit (https://github.com/jaanushansen/CodenameOne/commit/f3e53a80704149e4d7cde276d01c1368bcdcfe2c)
                // which was submitted as part of a pull request.  This fix has been the source of several
                // regressions, mostly related to properties not being propagated properly when a text field is changed
                // However, this issue (with the cursor not showing up), doesn't appear to have a simple solution
                // so, I'm disabling this feature for now.
                isEditedFieldSwitch = "true".equals(Display.getInstance().getProperty("android.reuseTextEditorOnSwitch", "false"));
                final String[] out = new String[1];
                TextArea prevTextArea = null;
                if (sInstance != null && sInstance.mLastEditText != null) {
                    prevTextArea = sInstance.mLastEditText.getTextArea();
                }
                if (prevTextArea != null) {
                    final TextArea fPrevTextArea = prevTextArea;
                    final String retVal = sInstance.mLastEditText.getText().toString();
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            Display.getInstance().onEditingComplete(fPrevTextArea, retVal);
                            textArea.requestFocus();
                        }
                    });
                }
                InPlaceEditView.setEditedTextField(textArea);
                nextTextArea = null;
            } else {
                isEditedFieldSwitch = false;
                final InPlaceEditView instance = sInstance;
                if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) {
                    instance.showTextEditorAgain();
                    return;
                }
                if (!isClosing && sInstance != null && sInstance.mEditText != null) {
                    isClosing = true;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            instance.endEditing(REASON_UNDEFINED, true, 0);
                        }
                    });
                }
                afterClose = new Runnable() {

                    @Override
                    public void run() {
                        impl.callHideTextEditor();
                        Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText());
                    }
                };
                return;
            }
        } else {
            isEditedFieldSwitch = false;
        }
        mIsEditing = true;
        isClosing = false;
        afterClose = null;
    }
    impl.setAsyncEditMode(asyncEdit);
    // textArea.setPreferredSize(prefSize);
    if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
        ((TextField) textArea).setEditable(false);
    }
    final boolean scrollableParent = isScrollableParent(textArea);
    // We wrap the text area so that we can safely pass data across to the
    // android UI thread.
    final TextAreaData textAreaData = new TextAreaData(textArea);
    impl.getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (!isEditedFieldSwitch) {
                releaseEdit();
                if (sInstance == null) {
                    sInstance = new InPlaceEditView(impl);
                    impl.relativeLayout.addView(sInstance);
                }
            // Let's try something new here
            // We'll ALWAYS try resize edit mode (since it just works better)
            // But we'll detect whether the field is still covered by the keyboard
            // and switch to pan mode if necessary.
            }
            if (panEditMode) {
                setEditMode(false);
            } else if (resizeEditMode) {
                setEditMode(true);
            } else if (parentForm.isFormBottomPaddingEditingMode()) {
                setEditMode(true);
            } else if (scrollableParent) {
                setEditMode(false);
            } else {
                trySetEditMode(true);
            }
            sInstance.startEditing(impl.getActivity(), textAreaData, initialText, inputType, isEditedFieldSwitch);
        }
    });
    final String[] out = new String[1];
    // In case the contents of the text area are changed while editing is in progress
    // we should propagate the changes to the native text field.
    final DataChangedListener textAreaDataChanged = new DataChangedListener() {

        @Override
        public void dataChanged(int type, int index) {
            if (suppressDataChangedEvent) {
                // https://github.com/codenameone/CodenameOne/issues/3343
                return;
            }
            TextArea currTextArea = getCurrentTextArea();
            if (currTextArea != textArea) {
                // This is not the active text area anymore
                textArea.removeDataChangedListener(this);
                return;
            }
            final String newText = textArea.getText();
            EditView currEditView = getCurrentEditView();
            if (currEditView == null || currEditView.mTextArea != textArea) {
                textArea.removeDataChangedListener(this);
                return;
            }
            String existingText = currEditView.getText().toString();
            // because Objects.equals was not available until API 19
            if (!com.codename1.compat.java.util.Objects.equals(newText, existingText)) {
                impl.getActivity().runOnUiThread(new Runnable() {

                    public void run() {
                        TextArea currTextArea = getCurrentTextArea();
                        EditView currEditView = getCurrentEditView();
                        if (currTextArea != textArea || currEditView == null || currEditView.mTextArea != textArea) {
                            return;
                        }
                        String existingText = currEditView.getText().toString();
                        // because Objects.equals was not available until API 19
                        if (!com.codename1.compat.java.util.Objects.equals(newText, existingText)) {
                            // We need to suppress the Android text change events
                            // to prevent weird things from happening.  E.g. https://github.com/codenameone/CodenameOne/issues/3349
                            suppressTextChangeEvent = true;
                            currEditView.setText(newText);
                            suppressTextChangeEvent = false;
                        }
                    }
                });
            }
        }
    };
    textArea.addDataChangedListener(textAreaDataChanged);
    // In order to reuse the code the runs after edit completion, we will wrap it in a runnable
    // For sync edit mode, we will just run onComplete.run() at the end of this method.  For
    // Async mode we add the Runnable to the textarea as a client property, then run it
    // when editing eventually completes.
    Runnable onComplete = new Runnable() {

        public void run() {
            textArea.removeDataChangedListener(textAreaDataChanged);
            if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
                ((TextField) textArea).setEditable(true);
            }
            textArea.setPreferredSize(null);
            if (sInstance != null && sInstance.mLastEditText != null && sInstance.mLastEditText.mTextArea == textArea) {
                String retVal = sInstance.mLastEditText.getText().toString();
                if (!impl.isAsyncEditMode()) {
                    sInstance.mLastEditText = null;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        public void run() {
                            releaseEdit();
                        }
                    });
                }
                out[0] = retVal;
            } else {
                out[0] = initialText;
            }
            Display.getInstance().onEditingComplete(component, out[0]);
            if (impl.isAsyncEditMode()) {
                impl.callHideTextEditor();
            } else {
                // lock.
                if (sInstance != null) {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (sInstance != null) {
                                com.codename1.io.Util.sleep(5);
                            }
                        }
                    });
                }
            }
            // Release the editing flag
            synchronized (editingLock) {
                mIsEditing = false;
            }
            // as a runnable ... this should take priority over the "nextTextArea" setting
            if (afterClose != null) {
                Display.getInstance().callSerially(afterClose);
            } else if (nextTextArea != null) {
                final TextArea next = nextTextArea;
                nextTextArea = null;
                next.requestFocus();
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        Display.getInstance().editString(next, next.getMaxSize(), next.getConstraint(), next.getText());
                    }
                });
            }
        }
    };
    textArea.requestFocus();
    textArea.repaint();
    if (impl.isAsyncEditMode()) {
        component.putClientProperty("android.onAsyncEditingComplete", onComplete);
        return;
    }
    // Make this call synchronous
    // We set this flag so that waitForEditCompletion can block on it.
    // The flag will be released inside the endEditing method which will
    // allow the method to proceed.
    waitingForSynchronousEditingCompletion = true;
    waitForEditCompletion();
    onComplete.run();
}
Also used : TextArea(com.codename1.ui.TextArea) Form(com.codename1.ui.Form) DataChangedListener(com.codename1.ui.events.DataChangedListener) TextField(com.codename1.ui.TextField)

Example 82 with com.codename1.io

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

the class BillingSupport method handlePurchase.

private void handlePurchase(final Purchase purchase) {
    if (handlingPurchase.contains(purchase.getPurchaseToken())) {
        return;
    }
    handlingPurchase.add(purchase.getPurchaseToken());
    final PurchaseCallback pc = getPurchaseCallback();
    if (!verifyDeveloperPayload(purchase)) {
        if (pc != null && pc instanceof PendingPurchaseCallback) {
            final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
            CN.callSerially(new Runnable() {

                public void run() {
                    for (String sku : purchase.getSkus()) {
                        ppc.itemPurchaseError(sku, "Invalid developer payload");
                    }
                }
            });
        }
        handlingPurchase.remove(purchase.getPurchaseToken());
        return;
    }
    if (purchase.getPurchaseState() != Purchase.PurchaseState.PURCHASED) {
        // This will be called again when the purchase completes.
        if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING && pc != null && pc instanceof PendingPurchaseCallback) {
            final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
            CN.callSerially(new Runnable() {

                @Override
                public void run() {
                    ppc.itemPurchasePending(purchase.getSkus().iterator().next());
                }
            });
        }
        handlingPurchase.remove(purchase.getPurchaseToken());
        return;
    }
    final String sku = purchase.getSkus().iterator().next();
    final Runnable onPurchaseAcknowledged = new Runnable() {

        public void run() {
            if (pc != null) {
                Display.getInstance().callSerially(new Runnable() {

                    @Override
                    public void run() {
                        // Sandbox transactions have no order ID, so we'll make a dummy transaction ID
                        // in this case.
                        String transactionId = (purchase.getOrderId() == null || purchase.getOrderId().isEmpty()) ? "play-sandbox-" + UUID.randomUUID().toString() : purchase.getOrderId();
                        String purchaseJsonStr = purchase.getOriginalJson();
                        try {
                            // In order to verify receipts, we'll need both the order data and the signature
                            // so we'll pack it all into a single JSON string.
                            JSONObject purchaseJson = new JSONObject(purchaseJsonStr);
                            JSONObject rootJson = new JSONObject();
                            rootJson.put("data", purchaseJson);
                            rootJson.put("signature", purchase.getSignature());
                            purchaseJsonStr = rootJson.toString();
                        } catch (JSONException ex) {
                            Logger.getLogger(CodenameOneActivity.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_PLAY, sku, transactionId, purchase.getPurchaseTime(), purchaseJsonStr);
                        pc.itemPurchased(sku);
                    }
                });
                inventory.add(sku, purchase);
                // This is a temp hack to get the last purchase raw data
                // The IAP API needs to be modified to support this on all platforms
                Display.getInstance().setProperty("lastPurchaseData", purchase.getOriginalJson());
            }
        }
    };
    if (!isConsumable(sku)) {
        if (!purchase.isAcknowledged()) {
            runWithConnection(new Runnable() {

                public void run() {
                    billingClient.acknowledgePurchase(AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build(), new AcknowledgePurchaseResponseListener() {

                        @Override
                        public void onAcknowledgePurchaseResponse(final BillingResult billingResult) {
                            handlingPurchase.remove(purchase.getPurchaseToken());
                            if (isFailure(billingResult)) {
                                final PurchaseCallback pc = getPurchaseCallback();
                                if (pc != null) {
                                    Display.getInstance().callSerially(new Runnable() {

                                        @Override
                                        public void run() {
                                            pc.itemPurchaseError(sku, billingResult.getDebugMessage());
                                        }
                                    });
                                }
                            } else {
                                onPurchaseAcknowledged.run();
                            }
                        }
                    });
                }
            });
        } else {
            handlingPurchase.remove(purchase.getPurchaseToken());
        }
        return;
    }
    final ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
    final ConsumeResponseListener listener = new ConsumeResponseListener() {

        @Override
        public void onConsumeResponse(final BillingResult billingResult, String purchaseToken) {
            if (purchase != null)
                handlingPurchase.remove(purchase.getPurchaseToken());
            if (isFailure(billingResult)) {
                final PurchaseCallback pc = getPurchaseCallback();
                if (pc != null) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            pc.itemPurchaseError(sku, billingResult.getDebugMessage());
                        }
                    });
                }
            } else {
                onPurchaseAcknowledged.run();
            }
            if (purchase != null) {
                inventory.erasePurchase(sku);
            }
        }
    };
    if (!purchase.isAcknowledged()) {
        runWithConnection(new Runnable() {

            public void run() {
                billingClient.consumeAsync(consumeParams, listener);
            }
        }).except(new SuccessCallback<Throwable>() {

            public void onSucess(final Throwable t) {
                if (purchase != null)
                    handlingPurchase.remove(purchase.getPurchaseToken());
                final PurchaseCallback pc = getPurchaseCallback();
                if (pc != null) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            pc.itemPurchaseError(sku, t.getMessage());
                        }
                    });
                }
                if (purchase != null) {
                    inventory.erasePurchase(sku);
                }
            }
        });
    } else {
        handlingPurchase.remove(purchase.getPurchaseToken());
    }
}
Also used : ConsumeParams(com.android.billingclient.api.ConsumeParams) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) JSONException(org.json.JSONException) PendingPurchaseCallback(com.codename1.payment.PendingPurchaseCallback) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) JSONObject(org.json.JSONObject) PurchaseCallback(com.codename1.payment.PurchaseCallback) PendingPurchaseCallback(com.codename1.payment.PendingPurchaseCallback) BillingResult(com.android.billingclient.api.BillingResult)

Example 83 with com.codename1.io

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

the class JavaSEPort method getTransform.

@Override
public com.codename1.ui.Transform getTransform(Object graphics) {
    checkEDT();
    com.codename1.ui.Transform t = getNativeScreenGraphicsTransform(graphics);
    if (t == null) {
        return Transform.makeIdentity();
    }
    return t.copy();
}
Also used : Transform(com.codename1.ui.Transform)

Example 84 with com.codename1.io

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

the class Tabs method addTab.

/**
 * Adds a <code>component</code>
 * represented by a <code>title</code> and/or <code>icon</code>,
 * either of which can be <code>null</code>.
 * Cover method for <code>insertTab</code>.
 *
 * @param title the title to be displayed in this tab
 * @param materialIcon one of the material design icon constants from {@link com.codename1.ui.FontImage}
 * @param iconSize icon size in millimeters
 * @param component the component to be displayed when this tab is clicked
 * @return this so these calls can be chained
 *
 * @see #insertTab
 * @see #removeTabAt
 */
public Tabs addTab(String title, char materialIcon, float iconSize, Component component) {
    int index = tabsContainer.getComponentCount();
    FontImage i = FontImage.createMaterial(materialIcon, "Tab", iconSize);
    insertTab(title, i, component, index);
    Style sel = getUIManager().getComponentSelectedStyle("Tab");
    i = FontImage.createMaterial(materialIcon, sel, iconSize);
    setTabSelectedIcon(index, i);
    return this;
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 85 with com.codename1.io

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

the class AutoCompleteTextField method addPopup.

private void addPopup() {
    final Form f = getComponentForm();
    popup.removeAll();
    popup.setVisible(false);
    popup.setEnabled(false);
    filter(getText());
    final com.codename1.ui.List l = new com.codename1.ui.List(getSuggestionModel());
    if (getMinimumElementsShownInPopup() > 0) {
        l.setMinElementHeight(getMinimumElementsShownInPopup());
    }
    l.setScrollToSelected(false);
    l.setItemGap(0);
    for (ActionListener al : listeners) {
        l.addActionListener(al);
    }
    if (completionRenderer == null) {
        ((DefaultListCellRenderer<String>) l.getRenderer()).setShowNumbers(false);
    } else {
        l.setRenderer(completionRenderer);
    }
    l.setUIID("AutoCompleteList");
    l.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            pickedText = (String) l.getSelectedItem();
            setParentText(pickedText);
            fireActionEvent();
            // relaunch text editing if we are still editing
            if (Display.getInstance().isTextEditing(AutoCompleteTextField.this)) {
                Display.getInstance().editString(AutoCompleteTextField.this, getMaxSize(), getConstraint(), (String) l.getSelectedItem());
            }
            popup.setVisible(false);
            popup.setEnabled(false);
            f.repaint();
        }
    });
    byte[] units = popup.getStyle().getMarginUnit();
    if (units != null) {
        units[Component.LEFT] = Style.UNIT_TYPE_PIXELS;
        units[Component.TOP] = Style.UNIT_TYPE_PIXELS;
        popup.getAllStyles().setMarginUnit(units);
    }
    popup.getAllStyles().setMargin(LEFT, Math.max(0, getAbsoluteX()));
    int popupHeight = calcPopuupHeight(l);
    popup.setPreferredW(getWidth());
    popup.setHeight(popupHeight);
    popup.setWidth(getWidth());
    popup.addComponent(l);
    popup.layoutContainer();
    // block the reflow of this popup, which can cause painting problems
    dontCalcSize = true;
    if (f != null) {
        if (popup.getParent() == null) {
            Container lay = f.getLayeredPane(AutoCompleteTextField.this.getClass(), true);
            lay.setLayout(new LayeredLayout());
            Container wrapper = new Container();
            wrapper.add(popup);
            lay.addComponent(wrapper);
        }
        f.revalidate();
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ArrayList(java.util.ArrayList) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Aggregations

IOException (java.io.IOException)39 EncodedImage (com.codename1.ui.EncodedImage)29 ArrayList (java.util.ArrayList)27 Point (java.awt.Point)25 File (java.io.File)24 BufferedImage (java.awt.image.BufferedImage)23 AnimationObject (com.codename1.ui.animations.AnimationObject)22 Form (com.codename1.ui.Form)19 Hashtable (java.util.Hashtable)19 Component (com.codename1.ui.Component)18 Image (com.codename1.ui.Image)18 EditableResources (com.codename1.ui.util.EditableResources)16 FileInputStream (java.io.FileInputStream)16 Timeline (com.codename1.ui.animations.Timeline)15 BorderLayout (com.codename1.ui.layouts.BorderLayout)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)11 FileOutputStream (java.io.FileOutputStream)10 EditorFont (com.codename1.ui.EditorFont)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9