Search in sources :

Example 81 with ActionListener

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

the class AndroidImplementation method openImageGallery.

/**
 * Opens the device image gallery
 *
 * @param response callback for the resulting image
 */
public void openImageGallery(ActionListener response) {
    if (getActivity() == null) {
        throw new RuntimeException("Cannot open image gallery in background mode");
    }
    if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to browse the photos")) {
        return;
    }
    if (editInProgress()) {
        stopEditing(true);
    }
    callback = new EventDispatcher();
    callback.addListener(response);
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    this.getActivity().startActivityForResult(galleryIntent, OPEN_GALLERY);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 82 with ActionListener

use of com.codename1.ui.events.ActionListener 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 83 with ActionListener

use of com.codename1.ui.events.ActionListener 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 84 with ActionListener

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

the class AndroidImplementation method execute.

public void execute(String url, ActionListener response) {
    if (response != null) {
        callback = new EventDispatcher();
        callback.addListener(response);
    }
    try {
        Intent intent = createIntentForURL(url);
        if (intent == null) {
            return;
        }
        if (response != null && getActivity() != null) {
            getActivity().startActivityForResult(intent, IntentResultListener.URI_SCHEME);
        } else {
            getContext().startActivity(intent);
        }
        return;
    } catch (Exception ex) {
        com.codename1.io.Log.e(ex);
    }
    try {
        if (editInProgress()) {
            stopEditing(true);
        }
        getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(java.text.ParseException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 85 with ActionListener

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

the class AndroidImplementation method captureVideo.

@Override
public void captureVideo(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;
    }
    callback = new EventDispatcher();
    callback.addListener(response);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
    File newFile = getOutputMediaFile(true);
    Uri videoUri = Uri.fromFile(newFile);
    Storage.getInstance().writeObject("videoUri", newFile.getAbsolutePath());
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, videoUri);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    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)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)61 ActionListener (com.codename1.ui.events.ActionListener)61 IOException (java.io.IOException)25 BorderLayout (com.codename1.ui.layouts.BorderLayout)20 EventDispatcher (com.codename1.ui.util.EventDispatcher)16 Hashtable (java.util.Hashtable)14 NetworkEvent (com.codename1.io.NetworkEvent)13 Form (com.codename1.ui.Form)13 Vector (java.util.Vector)11 Container (com.codename1.ui.Container)10 Button (com.codename1.ui.Button)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 Dialog (com.codename1.ui.Dialog)7 EncodedImage (com.codename1.ui.EncodedImage)6 Image (com.codename1.ui.Image)6 Label (com.codename1.ui.Label)6 ConnectionNotFoundException (javax.microedition.io.ConnectionNotFoundException)6 MediaException (javax.microedition.media.MediaException)6 RecordStoreException (javax.microedition.rms.RecordStoreException)6