/* * Copyright (c) 2014. The Trustees of Indiana University. * * This version of the code is licensed under the MPL 2.0 Open Source license with additional * healthcare disclaimer. If the user is an entity intending to commercialize any application * that uses this code in a for-profit venture, please contact the copyright holder. */ package com.muzima.view.forms; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.webkit.JavascriptInterface; import com.muzima.utils.EnDeCrypt; import com.muzima.utils.StringUtils; import com.muzima.utils.video.VideoIntent; import com.muzima.utils.video.VideoResult; import java.io.File; public class VideoComponent { private final static String TAG = "VideoComponent"; public static final int REQUEST_CODE = 0x0000c0df; public static final String FORM_UUID = "formUuid"; private final Activity activity; private String videoPathField; private String videoCaptionField; public VideoComponent(Activity activity) { this.activity = activity; } @JavascriptInterface public void openVideo(String videoUri){ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Intent startVideo = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + videoUri); startVideo.setDataAndType(uri, "video/*"); activity.startActivity(startVideo); } } @JavascriptInterface public void startVideoIntent(String sectionName, String videoPathField, String videoPath, String videoCaptionField, String videoCaption, String formUuid) { this.videoPathField = videoPathField; this.videoCaptionField = videoCaptionField; Intent videoIntent = new Intent(activity.getApplication(), VideoIntent.class); videoIntent.putExtra(FORM_UUID, formUuid); videoIntent.putExtra(VideoIntent.KEY_VIDEO_PATH, videoPath); videoIntent.putExtra(VideoIntent.KEY_VIDEO_CAPTION, videoCaption); videoIntent.putExtra(VideoIntent.KEY_SECTION_NAME, sectionName); // at this point the video is encrypted so we decrypt it if (!StringUtils.isEmpty(videoPath)) EnDeCrypt.decrypt(new File(videoPath), "this-is-supposed-to-be-a-secure-key"); activity.startActivityForResult(videoIntent, REQUEST_CODE); } public String getVideoPathField() { return videoPathField; } public String getVideoCaptionField() { return videoCaptionField; } public static VideoResult parseActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { String sectionName = intent.getStringExtra(VideoIntent.KEY_SECTION_NAME); String videoUri = intent.getStringExtra(VideoIntent.KEY_VIDEO_PATH); String videoCaption = intent.getStringExtra(VideoIntent.KEY_VIDEO_CAPTION); // now that we have video we encrypt it and keep the path if (!StringUtils.isEmpty(videoUri)) EnDeCrypt.encrypt(new File(videoUri), "this-is-supposed-to-be-a-secure-key"); return new VideoResult(sectionName, videoUri, videoCaption); } } return null; } }