[size=1em][size=1em]/*
[size=1em]* Copyright (C) 2011 The Rexsee Open Source Project
[size=1em]*
[size=1em]* Licensed under the Rexsee License, Version 1.0 (the "License");
[size=1em]* you may not use this file except in compliance with the License.
[size=1em]* You may obtain a copy of the License at
[size=1em]*
[size=1em]*
http://www.rexsee.com/CN/legal/license.html
[size=1em]*
[size=1em]* Unless required by applicable law or agreed to in writing, software
[size=1em]* distributed under the License is distributed on an "AS IS" BASIS,
[size=1em]* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[size=1em]* See the License for the specific language governing permissions and
[size=1em]* limitations under the License.
[size=1em]*/
[size=1em]
[size=1em]package rexsee.multimedia;
[size=1em]
[size=1em]import rexsee.core.browser.ActivityResult.ActivityResultListener;
[size=1em]import rexsee.core.browser.JavascriptInterface;
[size=1em]import rexsee.core.browser.RexseeBrowser;
[size=1em]import rexsee.core.browser.UrlListener;
[size=1em]import rexsee.core.lang.RexseeLanguage;
[size=1em]import rexsee.core.transportation.RexseeUpload;
[size=1em]import android.app.Activity;
[size=1em]import android.content.ContentResolver;
[size=1em]import android.content.Context;
[size=1em]import android.content.Intent;
[size=1em]import android.database.Cursor;
[size=1em]import android.net.Uri;
[size=1em]import android.provider.MediaStore.Audio.Media;
[size=1em]
[size=1em]public class RexseeAudioRecorder implements JavascriptInterface {
[size=1em]
[size=1em] private static final String INTERFACE_NAME = "AudioRecorder";
[size=1em] @Override
[size=1em] public String getInterfaceName() {
[size=1em] return mBrowser.application.resources.prefix + INTERFACE_NAME;
[size=1em] }
[size=1em] @Override
[size=1em] public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
[size=1em] return this;
[size=1em] }
[size=1em] @Override
[size=1em] public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
[size=1em] return new RexseeAudioRecorder(childBrowser);
[size=1em] }
[size=1em]
[size=1em] public static final String EVENT_ONRECORDAUDIOSUCCESSED = "onRecordAudioSuccessed";
[size=1em] public static final String EVENT_ONRECORDAUDIOFAILED = "onRecordAudioFailed";
[size=1em]
[size=1em] public static final String URI_MEDIA_AUDIO = "content://media/external/audio/media";
[size=1em]
[size=1em] private String mediaFile = null;
[size=1em]
[size=1em] private final RexseeBrowser mBrowser;
[size=1em]
[size=1em] public RexseeAudioRecorder(RexseeBrowser browser) {
[size=1em]
[size=1em] mBrowser = browser;
[size=1em] browser.eventList.add(EVENT_ONRECORDAUDIOSUCCESSED);
[size=1em] browser.eventList.add(EVENT_ONRECORDAUDIOFAILED);
[size=1em]
[size=1em] browser.urlListeners.add(new UrlListener(mBrowser.application.resources.prefix + ":audio") {
[size=1em] @Override
[size=1em] public void run(Context context, final RexseeBrowser browser, final String url) {
[size=1em] new Thread() {
[size=1em] @Override
[size=1em] public void run() {
[size=1em] String html = "<HTML><HEAD><TITLE>" + url + "</TITLE></HEAD>";
[size=1em] html += "<BODY style='margin:0px;background-color:black;color:white;'>";
[size=1em] ContentResolver contentResolver = mBrowser.getContext().getContentResolver();
[size=1em] browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
[size=1em] Cursor cursor = contentResolver.query(Uri.parse(URI_MEDIA_AUDIO), new String[]{"_id", "_data"}, null, null, "_id desc");
[size=1em] html += "<table width=100% cellspacing=0 style='color:white;font-size:16px;'>";
[size=1em] if (cursor.getCount() == 0) {
[size=1em] html += "<tr><td style='padding:10px;'> audio found.</td></tr>";
[size=1em] } else {
[size=1em] for (int i = 0; i < cursor.getCount(); i++) {
[size=1em] cursor.moveToPosition(i);
[size=1em] html += "<tr><td onclick=\"" + browser.function.getInterfaceName() + ".go('" + URI_MEDIA_AUDIO + "/" + cursor.getInt(0) + "');\" style='border-bottom:1px solid #222222; padding:10 5 10 5;word-break:break-all;'>" + cursor.getString(1) + "</td></tr>";
[size=1em] }
[size=1em] }
[size=1em] cursor.close();
[size=1em] html += "</table>";
[size=1em] html += "</BODY>";
[size=1em] html += "</HTML>";
[size=1em] browser.function.loadHTMLWithoutHistory(html);
[size=1em] }
[size=1em] }.start();
[size=1em] }
[size=1em] @Override
[size=1em] public boolean shouldAddToHistory(Context context, RexseeBrowser browser, String url) {
[size=1em] return true;
[size=1em] }
[size=1em] });
[size=1em]
[size=1em] }
[size=1em]
[size=1em] public String getLastMediaAudio() {
[size=1em] ContentResolver contentResolver = mBrowser.getContext().getContentResolver();
[size=1em] Cursor cursor = contentResolver.query(Uri.parse(URI_MEDIA_AUDIO), new String[]{"_data"}, null, null, "_id desc");
[size=1em] if (cursor == null || cursor.getCount() == 0) return "";
[size=1em] cursor.moveToFirst();
[size=1em] String rtn = "file://" + cursor.getString(0);
[size=1em] cursor.close();
[size=1em] return rtn;
[size=1em] }
[size=1em]
[size=1em] public void record() {
[size=1em] try {
[size=1em] Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
[size=1em] mBrowser.activityResult.start(intent, new ActivityResultListener() {
[size=1em] @Override
[size=1em] public void run(int resultCode, Intent resultIntent) {
[size=1em] if (resultCode == Activity.RESULT_OK) {
[size=1em] mediaFile = getLastMediaAudio();
[size=1em] mBrowser.eventList.run(EVENT_ONRECORDAUDIOSUCCESSED, new String[]{mediaFile});
[size=1em] } else {
[size=1em] mediaFile = null;
[size=1em] mBrowser.eventList.run(EVENT_ONRECORDAUDIOFAILED);
[size=1em] }
[size=1em] }
[size=1em] });
[size=1em] } catch (Exception e) {
[size=1em] mBrowser.exception(getInterfaceName(), e);
[size=1em] }
[size=1em] }
[size=1em]
[size=1em] public void prepareUpload() {
[size=1em] if (mediaFile == null) {
[size=1em] mBrowser.exception(getInterfaceName(), "Audio is not ready.");
[size=1em] } else {
[size=1em] RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);
[size=1em] if (uploadObject == null) {
[size=1em] mBrowser.exception(getInterfaceName(), "Upload object is not ready.");
[size=1em] } else {
[size=1em] uploadObject.selectedFiles.add(mediaFile);
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em]
[size=1em] public void upload(String action, String name) {
[size=1em] if (mediaFile == null) {
[size=1em] mBrowser.exception(getInterfaceName(), "Audio is not ready.");
[size=1em] } else {
[size=1em] RexseeUpload uploadObject = (RexseeUpload) mBrowser.interfaceList.get(mBrowser.application.resources.prefix + RexseeUpload.INTERFACE_NAME);
[size=1em] if (uploadObject == null) {
[size=1em] mBrowser.exception(getInterfaceName(), "Upload object is not ready.");
[size=1em] } else {
[size=1em] uploadObject.clear();
[size=1em] uploadObject.selectedFiles.add(mediaFile);
[size=1em] uploadObject.upload(action, name);
[size=1em] }
[size=1em] }
[size=1em] }
[size=1em]
[size=1em]}