TP-80789 | file validity check and photo module null checks (#968)
This commit is contained in:
@@ -13,7 +13,15 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.Signature;
|
||||
import android.util.Base64;
|
||||
|
||||
public class ApkInstallerModule extends ReactContextBaseJavaModule {
|
||||
|
||||
@@ -32,7 +40,6 @@ public class ApkInstallerModule extends ReactContextBaseJavaModule {
|
||||
private void promptForInstall(Uri apkUri) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
Log.d("ApkInstaller", "uri: "+ apkUri);
|
||||
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
@@ -45,20 +52,37 @@ public class ApkInstallerModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidApkFile(File apkFile) {
|
||||
if (!apkFile.exists() || apkFile.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Optional: Check if it's a valid ZIP/APK file
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(apkFile))) {
|
||||
ZipEntry zipEntry = zipInputStream.getNextEntry();
|
||||
return zipEntry != null;
|
||||
} catch (Exception e) {
|
||||
Log.e("ApkInstaller", "Invalid APK file", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void installApk(String filePath, Callback callback) {
|
||||
try {
|
||||
File apkFile = new File(filePath);
|
||||
Uri apkUri;
|
||||
if (!isValidApkFile(apkFile)) {
|
||||
callback.invoke("Invalid or corrupted apk file", null);
|
||||
return;
|
||||
}
|
||||
|
||||
Uri apkUri;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
apkUri = FileProvider.getUriForFile(reactContext, BuildConfig.APPLICATION_ID + ".provider",
|
||||
apkFile);
|
||||
apkUri = FileProvider.getUriForFile(reactContext, BuildConfig.APPLICATION_ID + ".provider", apkFile);
|
||||
} else {
|
||||
apkUri = Uri.fromFile(apkFile);
|
||||
}
|
||||
promptForInstall(apkUri);
|
||||
Log.d("ApkInstaller", "App installed");
|
||||
callback.invoke(null, "Success");
|
||||
} catch (Exception e) {
|
||||
Log.e("ApkInstaller", "Error installing APK", e);
|
||||
|
||||
@@ -78,6 +78,13 @@ public class PhotoModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
private void handleError(PhotoModuleError errorCode, Promise promise, @Nullable String additionalMessage) {
|
||||
if(promise == null) {
|
||||
return;
|
||||
}
|
||||
if (errorCode == null) {
|
||||
promise.reject("UNKNOWN_ERROR", "An unknown error occurred");
|
||||
return;
|
||||
}
|
||||
promise.reject(String.valueOf(errorCode.getCode()),
|
||||
errorCode.getMessage() + (additionalMessage != null ? ": " + additionalMessage : ""));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user