Skip to content

Commit

Permalink
build: fix all deprecation and lint in ShareFile(s) (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy authored and jgcmarins committed Nov 8, 2018
1 parent d14d349 commit 67fb59e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
25 changes: 11 additions & 14 deletions android/src/main/java/cl/json/ShareFile.java
@@ -1,12 +1,10 @@
package cl.json;

import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.FileProvider;
import android.util.Base64;
import android.webkit.MimeTypeMap;
Expand All @@ -16,8 +14,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;

/**
* Created by disenodosbbcl on 22-07-16.
Expand All @@ -28,7 +24,6 @@ public class ShareFile {
private String url;
private Uri uri;
private String type;
private String extension = "";

public ShareFile(String url, String type, ReactApplicationContext reactContext){
this(url, reactContext);
Expand All @@ -42,7 +37,7 @@ public ShareFile(String url, ReactApplicationContext reactContext){
}
/**
* Obtain mime type from URL
* @param {@link String} url
* @param url {@link String}
* @return {@link String} mime type
*/
private String getMimeType(String url) {
Expand All @@ -60,15 +55,15 @@ private String getMimeType(String url) {
public boolean isFile() {
return this.isBase64File() || this.isLocalFile();
}
public boolean isBase64File() {
private boolean isBase64File() {
String scheme = uri.getScheme();
if((scheme != null) && uri.getScheme().equals("data")) {
this.type = this.uri.getSchemeSpecificPart().substring(0, this.uri.getSchemeSpecificPart().indexOf(";"));
return true;
}
return false;
}
public boolean isLocalFile() {
private boolean isLocalFile() {
String scheme = uri.getScheme();
if((scheme != null) && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) {
// type is already set
Expand Down Expand Up @@ -117,17 +112,17 @@ private String getRealPathFromURI(Uri contentUri) {
public Uri getURI() {

final MimeTypeMap mime = MimeTypeMap.getSingleton();
this.extension = mime.getExtensionFromMimeType(getType());
String extension = mime.getExtensionFromMimeType(getType());
final String authority = ((ShareApplication) reactContext.getApplicationContext()).getFileProviderAuthority();

if(this.isBase64File()) {
String encodedImg = this.uri.getSchemeSpecificPart().substring(this.uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists()) {
dir.mkdirs();
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("mkdirs failed on " + dir.getAbsolutePath());
}
File file = new File(dir, System.nanoTime() + "." + this.extension);
File file = new File(dir, System.nanoTime() + "." + extension);
final FileOutputStream fos = new FileOutputStream(file);
fos.write(Base64.decode(encodedImg, Base64.DEFAULT));
fos.flush();
Expand All @@ -139,7 +134,9 @@ public Uri getURI() {
}
} else if(this.isLocalFile()) {
Uri uri = Uri.parse(this.url);

if (uri.getPath() == null) {
return null;
}
return FileProvider.getUriForFile(reactContext, authority, new File(uri.getPath()));
}

Expand Down
25 changes: 14 additions & 11 deletions android/src/main/java/cl/json/ShareFiles.java
@@ -1,10 +1,10 @@
package cl.json;

import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.FileProvider;
import android.util.Base64;
import android.webkit.MimeTypeMap;
Expand All @@ -24,7 +24,6 @@
public class ShareFiles
{
private final ReactApplicationContext reactContext;
private ReadableArray urls;
private ArrayList<Uri> uris;
private String intentType;

Expand All @@ -34,8 +33,7 @@ public ShareFiles(ReadableArray urls, String type, ReactApplicationContext react
}

public ShareFiles(ReadableArray urls, ReactApplicationContext reactContext){
this.urls = urls;
this.uris = new ArrayList();
this.uris = new ArrayList<>();
for (int i = 0; i < urls.size(); i++) {
String url = urls.getString(i);
if (url != null) {
Expand All @@ -47,7 +45,7 @@ public ShareFiles(ReadableArray urls, ReactApplicationContext reactContext){
}
/**
* Obtain mime type from URL
* @param {@link String} url
* @param url {@link String}
* @return {@link String} mime type
*/
private String getMimeType(String url) {
Expand Down Expand Up @@ -79,7 +77,7 @@ private boolean isBase64File(Uri uri) {
String type = uri.getSchemeSpecificPart().substring(0, uri.getSchemeSpecificPart().indexOf(";"));
if (this.intentType == null) {
this.intentType = type;
} else if (type != null && !this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
} else if (!this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
this.intentType = (this.intentType.split("/")[0]).concat("/*");
} else if (!this.intentType.equalsIgnoreCase(type)) {
this.intentType = "*/*";
Expand All @@ -90,7 +88,7 @@ private boolean isBase64File(Uri uri) {
}
private boolean isLocalFile(Uri uri) {
String scheme = uri.getScheme();
if((scheme != null) && uri.getScheme().equals("content") || uri.getScheme().equals("file")) {
if((scheme != null) && uri.getScheme().equals("content") || "file".equals(uri.getScheme())) {
// // type is already set
// if (this.type != null) {
// return true;
Expand All @@ -109,7 +107,7 @@ private boolean isLocalFile(Uri uri) {

if (this.intentType == null) {
this.intentType = type;
} else if (type != null && !this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
} else if (!this.intentType.equalsIgnoreCase(type) && this.intentType.split("/")[0].equalsIgnoreCase((type.split("/"))[0])) {
this.intentType = (this.intentType.split("/")[0]).concat("/*");
} else if (!this.intentType.equalsIgnoreCase(type)) {
this.intentType = "*/*";
Expand All @@ -131,6 +129,9 @@ private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(this.reactContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
if (cursor == null) {
return "";
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
Expand All @@ -150,8 +151,8 @@ public ArrayList<Uri> getURI() {
String encodedImg = uri.getSchemeSpecificPart().substring(uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists()) {
dir.mkdirs();
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("mkdirs failed on " + dir.getAbsolutePath());
}
File file = new File(dir, System.currentTimeMillis() + "." + extension);
final FileOutputStream fos = new FileOutputStream(file);
Expand All @@ -163,7 +164,9 @@ public ArrayList<Uri> getURI() {
e.printStackTrace();
}
} else if(this.isLocalFile(uri)) {
finalUris.add(FileProvider.getUriForFile(reactContext, authority, new File(uri.getPath())));
if (uri.getPath() != null) {
finalUris.add(FileProvider.getUriForFile(reactContext, authority, new File(uri.getPath())));
}
}
}

Expand Down

0 comments on commit 67fb59e

Please sign in to comment.