Search in sources :

Example 91 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class AndroidImplementation method captureAudio.

public void captureAudio(final ActionListener response) {
    if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record the audio")) {
        return;
    }
    try {
        final Form current = Display.getInstance().getCurrent();
        final File temp = File.createTempFile("mtmp", ".3gpp");
        temp.deleteOnExit();
        if (recorder != null) {
            recorder.release();
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(temp.getAbsolutePath());
        final Form recording = new Form("Recording");
        recording.setTransitionInAnimator(CommonTransitions.createEmpty());
        recording.setTransitionOutAnimator(CommonTransitions.createEmpty());
        recording.setLayout(new BorderLayout());
        recorder.prepare();
        recorder.start();
        final Label time = new Label("00:00");
        time.getAllStyles().setAlignment(Component.CENTER);
        Font f = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
        f = f.derive(getDisplayHeight() / 10, Font.STYLE_PLAIN);
        time.getAllStyles().setFont(f);
        recording.addComponent(BorderLayout.CENTER, time);
        recording.registerAnimated(new Animation() {

            long current = System.currentTimeMillis();

            long zero = current;

            int sec = 0;

            public boolean animate() {
                long now = System.currentTimeMillis();
                if (now - current > 1000) {
                    current = now;
                    sec++;
                    return true;
                }
                return false;
            }

            public void paint(Graphics g) {
                int seconds = sec % 60;
                int minutes = sec / 60;
                String secStr = seconds < 10 ? "0" + seconds : "" + seconds;
                String minStr = minutes < 10 ? "0" + minutes : "" + minutes;
                String txt = minStr + ":" + secStr;
                time.setText(txt);
            }
        });
        Container south = new Container(new com.codename1.ui.layouts.GridLayout(1, 2));
        Command cancel = new Command("Cancel") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(null);
            }
        };
        recording.setBackCommand(cancel);
        south.add(new com.codename1.ui.Button(cancel));
        south.add(new com.codename1.ui.Button(new Command("Save") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                if (recorder != null) {
                    recorder.stop();
                    recorder.release();
                    recorder = null;
                }
                current.showBack();
                response.actionPerformed(new ActionEvent(temp.getAbsolutePath()));
            }
        }));
        recording.addComponent(BorderLayout.SOUTH, south);
        recording.show();
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new RuntimeException("failed to start audio recording");
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) IOException(java.io.IOException) Font(com.codename1.ui.Font) Paint(android.graphics.Paint) BorderLayout(com.codename1.ui.layouts.BorderLayout) com.codename1.ui(com.codename1.ui) Animation(com.codename1.ui.animations.Animation) MediaRecorder(android.media.MediaRecorder) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 92 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class AndroidImplementation method stopTextEditing.

@Override
public void stopTextEditing(final Runnable onFinish) {
    final Form f = Display.getInstance().getCurrent();
    f.addSizeChangedListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            f.removeSizeChangedListener(this);
            Display.getInstance().callSerially(new Runnable() {

                @Override
                public void run() {
                    onFinish.run();
                }
            });
        }
    });
    stopEditing(true);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 93 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class AndroidImplementation method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == ZOOZ_PAYMENT) {
        ((IntentResultListener) pur).onActivityResult(requestCode, resultCode, intent);
        return;
    }
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE) {
            try {
                String imageUri = (String) Storage.getInstance().readObject("imageUri");
                Vector pathandId = StringUtil.tokenizeString(imageUri, ";");
                String path = (String) pathandId.get(0);
                String lastId = (String) pathandId.get(1);
                Storage.getInstance().deleteStorageFile("imageUri");
                clearMediaDB(lastId, path);
                callback.fireActionEvent(new ActionEvent(path));
                return;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == CAPTURE_VIDEO) {
            String path = (String) Storage.getInstance().readObject("videoUri");
            Storage.getInstance().deleteStorageFile("videoUri");
            callback.fireActionEvent(new ActionEvent(path));
            return;
        } else if (requestCode == CAPTURE_AUDIO) {
            Uri data = intent.getData();
            String path = convertImageUriToFilePath(data, getContext());
            callback.fireActionEvent(new ActionEvent(path));
            return;
        } else if (requestCode == OPEN_GALLERY) {
            Uri selectedImage = intent.getData();
            String scheme = intent.getScheme();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            // this happens on Android devices, not exactly sure what the use case is
            if (cursor == null) {
                callback.fireActionEvent(null);
                return;
            }
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            if (filePath == null && "content".equals(scheme)) {
                // locally
                try {
                    InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
                    if (inputStream != null) {
                        String name = getContentName(getContext().getContentResolver(), selectedImage);
                        if (name != null) {
                            filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            File f = new File(filePath);
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = inputStream.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            inputStream.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            callback.fireActionEvent(new ActionEvent(filePath));
            return;
        } else {
            if (callback != null) {
                callback.fireActionEvent(new ActionEvent("ok"));
            }
            return;
        }
    }
    // clean imageUri
    String imageUri = (String) Storage.getInstance().readObject("imageUri");
    if (imageUri != null) {
        Storage.getInstance().deleteStorageFile("imageUri");
    }
    if (callback != null) {
        callback.fireActionEvent(null);
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) 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) Cursor(android.database.Cursor) Vector(java.util.Vector) Uri(android.net.Uri) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 94 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class CodenameOneView method handleSizeChange.

public void handleSizeChange(int w, int h) {
    if (!drawing) {
        if ((this.width != w && (this.width < w || this.height < h)) || (bitmap.getHeight() < h)) {
            this.initBitmaps(w, h);
        }
    }
    if (this.width == w && this.height == h) {
        return;
    }
    this.width = w;
    this.height = h;
    Log.d("Codename One", "sizechanged: " + width + " " + height + " " + this);
    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;
    }
    if (InPlaceEditView.isEditing()) {
        final Form f = this.implementation.getCurrentForm();
        ActionListener sizeChanged = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                CodenameOneView.this.implementation.getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        InPlaceEditView.reLayoutEdit();
                        InPlaceEditView.scrollActiveTextfieldToVisible();
                    }
                });
                f.removeSizeChangedListener(this);
            }
        };
        f.addSizeChangedListener(sizeChanged);
    }
    Display.getInstance().sizeChanged(w, h);
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) Form(com.codename1.ui.Form) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 95 with ActionEvent

use of com.codename1.ui.events.ActionEvent in project CodenameOne by codenameone.

the class AdsService method readResponse.

/**
 * {@inheritDoc}
 */
protected void readResponse(InputStream input) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    int len;
    while ((len = input.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    int size = out.toByteArray().length;
    if (size > 0) {
        String s = new String(out.toByteArray(), 0, size, "UTF-8");
        currentAd = s;
        fireResponseListener(new ActionEvent(currentAd, ActionEvent.Type.Response));
    }
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)98 ActionListener (com.codename1.ui.events.ActionListener)55 IOException (java.io.IOException)30 BorderLayout (com.codename1.ui.layouts.BorderLayout)28 Form (com.codename1.ui.Form)18 Hashtable (java.util.Hashtable)14 Vector (java.util.Vector)11 NetworkEvent (com.codename1.io.NetworkEvent)10 Container (com.codename1.ui.Container)9 ActionEvent (java.awt.event.ActionEvent)9 Command (com.codename1.ui.Command)8 ActionListener (java.awt.event.ActionListener)8 Button (com.codename1.ui.Button)7 Style (com.codename1.ui.plaf.Style)7 Rectangle (com.codename1.ui.geom.Rectangle)6 File (java.io.File)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6 BufferedOutputStream (com.codename1.io.BufferedOutputStream)5