public void openPhotoGallery( final String successCallback, final String cancelCallback, final String errorCallback, ITitaniumFile file) { if (DBG) { Log.d(LCAT, "openPhotoGallery called"); } if (!(file instanceof TitaniumBlob)) { throw new IllegalArgumentException("blob parameter must be of type TitaniumBlob"); } final TitaniumBlob blob = (TitaniumBlob) file; TitaniumActivity activity = getActivity(); TitaniumIntentWrapper galleryIntent = new TitaniumIntentWrapper(new Intent()); galleryIntent.getIntent().setAction(Intent.ACTION_PICK); galleryIntent.getIntent().setType("image/*"); galleryIntent.getIntent().addCategory(Intent.CATEGORY_DEFAULT); galleryIntent.setWindowId(TitaniumIntentWrapper.createActivityName("GALLERY")); final int code = activity.getUniqueResultCode(); activity.launchActivityForResult( galleryIntent, code, new TitaniumResultHandler() { public void onResult( TitaniumActivity activity, int requestCode, int resultCode, Intent data) { Log.e(LCAT, "OnResult called: " + resultCode); if (resultCode == Activity.RESULT_CANCELED) { invokeUserCallback(cancelCallback, null); } else { String path = data.getDataString(); String result = fillBlob(activity, blob, path); invokeUserCallback(successCallback, result); } } public void onError(TitaniumActivity activity, int requestCode, Exception e) { Log.e(LCAT, "Gallery problem: ", e); invokeUserCallback(errorCallback, createJSONError(0, e.getMessage())); } }); }
public ITitaniumVideo createVideoPlayer(final String jsonOptions) { ITitaniumVideo result = null; String errorCallback = null; try { JSONObject options = new JSONObject(jsonOptions); try { errorCallback = options.getString("error"); // callbacks will be added on JS side. to track } catch (JSONException e2) { Log.d(LCAT, "error callback not available"); } String url = null; try { url = options.getString("contentURL"); Uri uri = Uri.parse(url); String scheme = uri.getScheme(); if (scheme == null || scheme.length() == 0 || (scheme == null && !(new File(url).exists()))) { uri = Uri.parse(TitaniumUrlHelper.buildAssetUrlFromResourcesRoot(getActivity(), url)); } Intent intent = new Intent(getActivity(), TitaniumVideoActivity.class); intent.setData(uri); TitaniumIntentWrapper videoIntent = new TitaniumIntentWrapper(intent); videoIntent.setWindowId(TitaniumIntentWrapper.createActivityName("VIDEO")); result = new TitaniumVideo(this, videoIntent); } catch (JSONException e2) { String msg = "contentURL is required."; Log.e(LCAT, msg); if (errorCallback != null) { invokeUserCallback(errorCallback, createJSONError(0, msg)); } } } catch (JSONException e) { Log.e(LCAT, "Could not reconstruct options from JSON: ", e); } return result; }
public void previewImage( final String successCallback, final String errorCallback, final ITitaniumFile file) { if (DBG) { Log.d(LCAT, "previewImage"); } if (!(file instanceof TitaniumBlob)) { throw new IllegalArgumentException("blob parameter must be of type TitaniumBlob"); } final TitaniumBlob blob = (TitaniumBlob) file; TitaniumActivity activity = getActivity(); Uri uri = Uri.parse(blob.nativePath()); String type = activity.getContentResolver().getType(uri); TitaniumIntentWrapper previewIntent = new TitaniumIntentWrapper(new Intent()); previewIntent.getIntent().setAction(Intent.ACTION_VIEW); previewIntent.getIntent().setType(type); previewIntent.getIntent().setData(uri); previewIntent.setWindowId(TitaniumIntentWrapper.createActivityName("PREVIEW")); final int code = activity.getUniqueResultCode(); activity.launchActivityForResult( previewIntent, code, new TitaniumResultHandler() { public void onResult( TitaniumActivity activity, int requestCode, int resultCode, Intent data) { invokeUserCallback(successCallback, null); } public void onError(TitaniumActivity activity, int requestCode, Exception e) { Log.e(LCAT, "preview problem: ", e); invokeUserCallback(errorCallback, createJSONError(0, e.getMessage())); } }); }
public void showCamera( final String successCallback, final String cancelCallback, final String errorCallback, final String options, ITitaniumFile file) { if (DBG) { Log.d(LCAT, "showCamera called"); } if (!(file instanceof TitaniumBlob)) { throw new IllegalArgumentException("blob parameter must be of type TitaniumBlob"); } Camera camera = null; try { camera = Camera.open(); if (camera != null) { camera.release(); camera = null; } } catch (Throwable t) { if (camera != null) { camera.release(); } invokeUserCallback(errorCallback, createJSONError(0, "Camera not available.")); return; } boolean saveToPhotoGallery = false; try { JSONObject json = new JSONObject(options); try { saveToPhotoGallery = json.getBoolean("saveToPhotoGallery"); } catch (JSONException e) { if (DBG) { Log.d(LCAT, "options does not have saveToPhotoGallery option or value is not boolean"); } } // TODO allowImageEditing. Don't know if I can do this yet } catch (JSONException e) { Log.w(LCAT, "Invalid options JSON: " + options, e); } final TitaniumBlob blob = (TitaniumBlob) file; TitaniumActivity activity = getActivity(); TitaniumFileHelper tfh = new TitaniumFileHelper(getContext()); File imageDir = null; File imageFile = null; try { if (saveToPhotoGallery) { imageDir = new File(PHOTO_DCIM_CAMERA); if (!imageDir.exists()) { imageDir.mkdirs(); } } else { if (activity.getIntent() != null) { TitaniumIntentWrapper intent = new TitaniumIntentWrapper(activity.getIntent()); TitaniumAppInfo appInfo = intent.getAppInfo(activity); String name = appInfo.getAppName(); imageDir = new File(PHOTO_DCIM_CAMERA, name); if (!imageDir.exists()) { imageDir.mkdirs(); } } else { imageDir = tfh.getDataDirectory(false); } } imageFile = tfh.getTempFile(imageDir, ".jpg"); } catch (IOException e) { Log.e(LCAT, "Unable to create temp file", e); invokeUserCallback(errorCallback, createJSONError(0, e.getMessage())); return; } final File finalImageFile = imageFile; final String imageUrl = "file://" + imageFile.getAbsolutePath(); TitaniumIntentWrapper cameraIntent = new TitaniumIntentWrapper(new Intent()); cameraIntent.getIntent().setAction(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.getIntent().putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(imageUrl)); cameraIntent.getIntent().addCategory(Intent.CATEGORY_DEFAULT); cameraIntent.setWindowId(TitaniumIntentWrapper.createActivityName("CAMERA")); final int code = activity.getUniqueResultCode(); final boolean finalSaveToPhotoGallery = saveToPhotoGallery; activity.launchActivityForResult( cameraIntent, code, new TitaniumResultHandler() { public void onResult( TitaniumActivity activity, int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_CANCELED) { if (finalImageFile != null) { finalImageFile.delete(); } invokeUserCallback(cancelCallback, null); } else { ContentValues values = new ContentValues(7); values.put(Images.Media.TITLE, finalImageFile.getName()); values.put(Images.Media.DISPLAY_NAME, finalImageFile.getName()); values.put(Images.Media.DATE_TAKEN, new Date().getTime()); values.put(Images.Media.MIME_TYPE, "image/jpeg"); if (finalSaveToPhotoGallery) { values.put( Images.ImageColumns.BUCKET_ID, PHOTO_DCIM_CAMERA.toLowerCase().hashCode()); values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, "Camera"); } else { values.put( Images.ImageColumns.BUCKET_ID, finalImageFile.getPath().toLowerCase().hashCode()); values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, finalImageFile.getName()); } values.put("_data", finalImageFile.getAbsolutePath()); Uri imageUri = activity.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values); String result = fillBlob(activity, blob, imageUri.toString()); invokeUserCallback(successCallback, result); } } public void onError(TitaniumActivity activity, int requestCode, Exception e) { if (finalImageFile != null) { finalImageFile.delete(); } Log.e(LCAT, "Camera problem: ", e); invokeUserCallback(errorCallback, createJSONError(0, e.getMessage())); } }); }