breaking(android): stop using CordovaUri helper class (#617)

This commit is contained in:
jcesarmobile 2020-07-14 16:26:09 +02:00 committed by GitHub
parent 2bf5b9347e
commit fd155d9705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 155 deletions

View File

@ -69,7 +69,6 @@
</config-file>
<source-file src="src/android/CameraLauncher.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/CordovaUri.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/ExifHelper.java" target-dir="src/org/apache/cordova/camera" />
<source-file src="src/android/FileProvider.java" target-dir="src/org/apache/cordova/camera" />

View File

@ -109,7 +109,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
private int targetWidth; // desired width of the image
private int targetHeight; // desired height of the image
private CordovaUri imageUri; // Uri of captured image
private Uri imageUri; // Uri of captured image
private String imageFilePath; // File where the image is stored
private int encodingType; // Type of encoding to use
private int mediaType; // What type of media to retrieve
private int destType; // Source type (needs to be saved for the permission handling)
@ -127,6 +128,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private MediaScannerConnection conn; // Used to update gallery app with newly-written files
private Uri scanMe; // Uri of image to be added to content store
private Uri croppedUri;
private String croppedFilePath;
private ExifHelper exifData; // Exif data from source
private String applicationId;
@ -290,10 +292,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Specify file so that large image is captured and returned
File photo = createCaptureFile(encodingType);
this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
this.imageFilePath = photo.getAbsolutePath();
this.imageUri = FileProvider.getUriForFile(cordova.getActivity(),
applicationId + ".provider",
photo));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//We can write to this URI, this will hopefully allow us to write files to get to the next step
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
@ -360,6 +363,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
Intent intent = new Intent();
String title = GET_PICTURE;
croppedUri = null;
croppedFilePath = null;
if (this.mediaType == PICTURE) {
intent.setType("image/*");
if (this.allowEdit) {
@ -375,8 +379,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
}
File photo = createCaptureFile(JPEG);
croppedUri = Uri.fromFile(photo);
File croppedFile = createCaptureFile(JPEG);
croppedFilePath = croppedFile.getAbsolutePath();
croppedUri = Uri.fromFile(croppedFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
} else {
intent.setAction(Intent.ACTION_GET_CONTENT);
@ -428,7 +433,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
cropIntent.putExtra("aspectY", 1);
}
// create new file handle to get full resolution crop
croppedUri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
croppedFilePath = createCaptureFile(this.encodingType, System.currentTimeMillis() + "").getAbsolutePath();
croppedUri = Uri.parse(croppedFilePath);
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
cropIntent.putExtra("output", croppedUri);
@ -466,8 +472,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
ExifHelper exif = new ExifHelper();
String sourcePath = (this.allowEdit && this.croppedUri != null) ?
FileHelper.stripFileProtocol(this.croppedUri.toString()) :
this.imageUri.getFilePath();
this.croppedFilePath :
this.imageFilePath;
if (this.encodingType == JPEG) {
@ -494,7 +500,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
if (this.allowEdit && this.croppedUri != null) {
writeUncompressedImage(croppedUri, galleryUri);
} else {
Uri imageUri = this.imageUri.getFileUri();
Uri imageUri = this.imageUri;
writeUncompressedImage(imageUri, galleryUri);
}
@ -539,10 +545,10 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
if (this.allowEdit && this.croppedUri != null) {
Uri croppedUri = Uri.fromFile(new File(getFileNameFromUri(this.croppedUri)));
Uri croppedUri = Uri.parse(croppedFilePath);
writeUncompressedImage(croppedUri, uri);
} else {
Uri imageUri = this.imageUri.getFileUri();
Uri imageUri = this.imageUri;
writeUncompressedImage(imageUri, uri);
}
@ -588,7 +594,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
throw new IllegalStateException();
}
this.cleanup(FILE_URI, this.imageUri.getFileUri(), galleryUri, bitmap);
this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap);
bitmap = null;
}
@ -1336,11 +1342,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum);
if (this.croppedUri != null) {
state.putString(CROPPED_URI_KEY, this.croppedUri.toString());
state.putString(CROPPED_URI_KEY, this.croppedFilePath);
}
if (this.imageUri != null) {
state.putString(IMAGE_URI_KEY, this.imageUri.getFileUri().toString());
state.putString(IMAGE_URI_KEY, this.imageFilePath);
}
return state;
@ -1365,28 +1371,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
if (state.containsKey(IMAGE_URI_KEY)) {
//I have no idea what type of URI is being passed in
this.imageUri = new CordovaUri(Uri.parse(state.getString(IMAGE_URI_KEY)));
this.imageUri = Uri.parse(state.getString(IMAGE_URI_KEY));
}
this.callbackContext = callbackContext;
}
/*
* This is dirty, but it does the job.
*
* Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
* and since we actually need the Camera to create the file for us most of the time, we don't
* actually write the file, just generate the location based on a timestamp, we need to get it
* back from the Intent.
*
* However, the FilesProvider preserves the path, so we can at least write to it from here, since
* we own the context in this case.
*/
private String getFileNameFromUri(Uri uri) {
String fullUri = uri.toString();
String partial_path = fullUri.split("external_files")[1];
File external_storage = Environment.getExternalStorageDirectory();
String path = external_storage.getAbsolutePath() + partial_path;
return path;
}
}

View File

@ -1,104 +0,0 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.cordova.camera;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import java.io.File;
/*
* This class exists because Andorid FilesProvider doesn't work on Android 4.4.4 and below and throws
* weird errors. I'm not sure why writing to shared cache directories is somehow verboten, but it is
* and this error is irritating for a Compatibility library to have.
*
*/
public class CordovaUri {
private Uri androidUri;
private String fileName;
private Uri fileUri;
/*
* We always expect a FileProvider string to be passed in for the file that we create
*
*/
CordovaUri (Uri inputUri)
{
//Determine whether the file is a content or file URI
if(inputUri.getScheme().equals("content"))
{
androidUri = inputUri;
fileName = getFileNameFromUri(androidUri);
fileUri = Uri.parse("file://" + fileName);
}
else
{
fileUri = inputUri;
fileName = FileHelper.stripFileProtocol(inputUri.toString());
}
}
public Uri getFileUri()
{
return fileUri;
}
public String getFilePath()
{
return fileName;
}
/*
* This only gets called by takePicture
*/
public Uri getCorrectUri()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return androidUri;
else
return fileUri;
}
/*
* This is dirty, but it does the job.
*
* Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
* and since we actually need the Camera to create the file for us most of the time, we don't
* actually write the file, just generate the location based on a timestamp, we need to get it
* back from the Intent.
*
* However, the FilesProvider preserves the path, so we can at least write to it from here, since
* we own the context in this case.
*/
private String getFileNameFromUri(Uri uri) {
String fullUri = uri.toString();
String partial_path = fullUri.split("external_files")[1];
File external_storage = Environment.getExternalStorageDirectory();
String path = external_storage.getAbsolutePath() + partial_path;
return path;
}
}

View File

@ -50,16 +50,7 @@ public class FileHelper {
*/
@SuppressWarnings("deprecation")
public static String getRealPath(Uri uri, CordovaInterface cordova) {
String realPath = null;
if (Build.VERSION.SDK_INT < 11)
realPath = FileHelper.getRealPathFromURI_BelowAPI11(cordova.getActivity(), uri);
// SDK >= 11
else
realPath = FileHelper.getRealPathFromURI_API11_And_Above(cordova.getActivity(), uri);
return realPath;
return FileHelper.getRealPathFromURI(cordova.getActivity(), uri);
}
/**
@ -75,11 +66,9 @@ public class FileHelper {
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11_And_Above(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
public static String getRealPathFromURI(final Context context, final Uri uri) {
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {

View File

@ -17,5 +17,5 @@
-->
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<cache-path name="cache_files" path="." />
</paths>