Fix share from SwiftKey Keyboard and clear temp files (#1585)

* Fix share from SwiftKey Keyboard and clear temp files

* feedback review
This commit is contained in:
Elias Nahum 2018-04-13 12:26:29 -03:00 committed by GitHub
parent 006c5a08d7
commit e31fc00377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View file

@ -70,10 +70,14 @@ public class RealPathUtil {
return uri.getLastPathSegment();
}
String path = getDataColumn(context, uri, null, null);
try {
String path = getDataColumn(context, uri, null, null);
if (path != null) {
return path;
if (path != null) {
return path;
}
} catch (Exception e) {
// do nothing and try to get a temp file
}
// Try save to tmp file, and return tmp file path
@ -89,7 +93,12 @@ public class RealPathUtil {
File tmpFile;
try {
String fileName = uri.getLastPathSegment();
tmpFile = File.createTempFile("tmp", fileName, context.getCacheDir());
File cacheDir = new File(context.getCacheDir(), "mmShare");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
tmpFile = File.createTempFile("tmp", fileName, cacheDir);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
@ -172,4 +181,22 @@ public class RealPathUtil {
File file = new File(filePath);
return getMimeType(file);
}
public static void deleteTempFiles(final File dir) {
try {
if (dir.isDirectory()) {
deleteRecursive(dir);
}
} catch (Exception e) {
// do nothing
}
}
private static void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
}

View file

@ -40,6 +40,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
public ShareModule(ReactApplicationContext reactContext) {
super(reactContext);
}
private File tempFolder;
@Override
public String getName() {
@ -60,6 +61,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
@ReactMethod
public void close(ReadableMap data) {
this.clear();
getCurrentActivity().finish();
if (data != null) {
@ -78,6 +80,8 @@ public class ShareModule extends ReactContextBaseJavaModule {
}
}
}
RealPathUtil.deleteTempFiles(this.tempFolder);
}
@ReactMethod
@ -96,6 +100,7 @@ public class ShareModule extends ReactContextBaseJavaModule {
Activity currentActivity = getCurrentActivity();
if (currentActivity != null) {
this.tempFolder = new File(currentActivity.getCacheDir(), "mmShare");
Intent intent = currentActivity.getIntent();
action = intent.getAction();
type = intent.getType();