mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: format
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { ad } from "./native-helper";
|
||||
import { device } from "../platform";
|
||||
import { FileSystemAccess } from "../file-system/file-system-access";
|
||||
import {
|
||||
Trace
|
||||
} from "../trace";
|
||||
import { Trace } from "../trace";
|
||||
|
||||
export { ad };
|
||||
export * from "./utils-common";
|
||||
@@ -11,28 +9,35 @@ export * from "./utils-common";
|
||||
const MIN_URI_SHARE_RESTRICTED_APK_VERSION = 24;
|
||||
|
||||
export function GC() {
|
||||
gc();
|
||||
gc();
|
||||
}
|
||||
|
||||
export function releaseNativeObject(object: java.lang.Object) {
|
||||
__releaseNativeCounterpart(object);
|
||||
__releaseNativeCounterpart(object);
|
||||
}
|
||||
|
||||
export function openUrl(location: string): boolean {
|
||||
const context = ad.getApplicationContext();
|
||||
try {
|
||||
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(location.trim()));
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
const context = ad.getApplicationContext();
|
||||
try {
|
||||
const intent = new android.content.Intent(
|
||||
android.content.Intent.ACTION_VIEW,
|
||||
android.net.Uri.parse(location.trim())
|
||||
);
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
context.startActivity(intent);
|
||||
} catch (e) {
|
||||
// We Don't do anything with an error. We just output it
|
||||
Trace.write("Error in OpenURL", Trace.categories.Error, Trace.messageType.error);
|
||||
context.startActivity(intent);
|
||||
} catch (e) {
|
||||
// We Don't do anything with an error. We just output it
|
||||
Trace.write(
|
||||
"Error in OpenURL",
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,12 +46,12 @@ export function openUrl(location: string): boolean {
|
||||
* @returns {boolean} whether the external storage is read only
|
||||
*/
|
||||
function isExternalStorageReadOnly(): boolean {
|
||||
const extStorageState = android.os.Environment.getExternalStorageState();
|
||||
if (android.os.Environment.MEDIA_MOUNTED_READ_ONLY === extStorageState) {
|
||||
return true;
|
||||
}
|
||||
const extStorageState = android.os.Environment.getExternalStorageState();
|
||||
if (android.os.Environment.MEDIA_MOUNTED_READ_ONLY === extStorageState) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,12 +60,12 @@ function isExternalStorageReadOnly(): boolean {
|
||||
* @returns {boolean} whether external storage is available
|
||||
*/
|
||||
function isExternalStorageAvailable(): boolean {
|
||||
const extStorageState = android.os.Environment.getExternalStorageState();
|
||||
if (android.os.Environment.MEDIA_MOUNTED === extStorageState) {
|
||||
return true;
|
||||
}
|
||||
const extStorageState = android.os.Environment.getExternalStorageState();
|
||||
if (android.os.Environment.MEDIA_MOUNTED === extStorageState) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,13 +75,13 @@ function isExternalStorageAvailable(): boolean {
|
||||
* @returns {string} mimetype
|
||||
*/
|
||||
function getMimeTypeNameFromExtension(filePath: string): string {
|
||||
const mimeTypeMap = android.webkit.MimeTypeMap.getSingleton();
|
||||
const extension = new FileSystemAccess()
|
||||
.getFileExtension(filePath)
|
||||
.replace(".", "")
|
||||
.toLowerCase();
|
||||
const mimeTypeMap = android.webkit.MimeTypeMap.getSingleton();
|
||||
const extension = new FileSystemAccess()
|
||||
.getFileExtension(filePath)
|
||||
.replace(".", "")
|
||||
.toLowerCase();
|
||||
|
||||
return mimeTypeMap.getMimeTypeFromExtension(extension);
|
||||
return mimeTypeMap.getMimeTypeFromExtension(extension);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,101 +91,124 @@ function getMimeTypeNameFromExtension(filePath: string): string {
|
||||
* @returns {boolean} whether opening the file succeeded or not
|
||||
*/
|
||||
export function openFile(filePath: string): boolean {
|
||||
const context = ad.getApplicationContext();
|
||||
try {
|
||||
// Ensure external storage is available
|
||||
if (!isExternalStorageAvailable()) {
|
||||
Trace.write(
|
||||
`
|
||||
const context = ad.getApplicationContext();
|
||||
try {
|
||||
// Ensure external storage is available
|
||||
if (!isExternalStorageAvailable()) {
|
||||
Trace.write(
|
||||
`
|
||||
External storage is unavailable (please check app permissions).
|
||||
Applications cannot access internal storage of other application on Android (see: https://developer.android.com/guide/topics/data/data-storage).
|
||||
`,
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error,
|
||||
);
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure external storage is available
|
||||
if (isExternalStorageReadOnly()) {
|
||||
Trace.write("External storage is read only", Trace.categories.Error, Trace.messageType.error);
|
||||
// Ensure external storage is available
|
||||
if (isExternalStorageReadOnly()) {
|
||||
Trace.write(
|
||||
"External storage is read only",
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Determine file mimetype & start creating intent
|
||||
const mimeType = getMimeTypeNameFromExtension(filePath);
|
||||
const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
|
||||
const chooserIntent = android.content.Intent.createChooser(intent, "Open File...");
|
||||
// Determine file mimetype & start creating intent
|
||||
const mimeType = getMimeTypeNameFromExtension(filePath);
|
||||
const intent = new android.content.Intent(
|
||||
android.content.Intent.ACTION_VIEW
|
||||
);
|
||||
const chooserIntent = android.content.Intent.createChooser(
|
||||
intent,
|
||||
"Open File..."
|
||||
);
|
||||
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
chooserIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
chooserIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
// Android SDK <28 only requires starting the chooser Intent straight forwardly
|
||||
const sdkVersion = parseInt(device.sdkVersion, 10);
|
||||
if (sdkVersion && sdkVersion < MIN_URI_SHARE_RESTRICTED_APK_VERSION) {
|
||||
Trace.write(
|
||||
`detected sdk version ${sdkVersion} (< ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using simple openFile`,
|
||||
Trace.categories.Debug
|
||||
);
|
||||
intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), mimeType);
|
||||
context.startActivity(chooserIntent);
|
||||
// Android SDK <28 only requires starting the chooser Intent straight forwardly
|
||||
const sdkVersion = parseInt(device.sdkVersion, 10);
|
||||
if (sdkVersion && sdkVersion < MIN_URI_SHARE_RESTRICTED_APK_VERSION) {
|
||||
Trace.write(
|
||||
`detected sdk version ${sdkVersion} (< ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using simple openFile`,
|
||||
Trace.categories.Debug
|
||||
);
|
||||
intent.setDataAndType(
|
||||
android.net.Uri.fromFile(new java.io.File(filePath)),
|
||||
mimeType
|
||||
);
|
||||
context.startActivity(chooserIntent);
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Trace.write(
|
||||
`detected sdk version ${sdkVersion} (>= ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using URI openFile`,
|
||||
Trace.categories.Debug
|
||||
);
|
||||
Trace.write(
|
||||
`detected sdk version ${sdkVersion} (>= ${MIN_URI_SHARE_RESTRICTED_APK_VERSION}), using URI openFile`,
|
||||
Trace.categories.Debug
|
||||
);
|
||||
|
||||
// Android SDK 24+ introduced file system permissions changes that disallow
|
||||
// exposing URIs between applications
|
||||
//
|
||||
// see: https://developer.android.com/reference/android/os/FileUriExposedException
|
||||
// see: https://github.com/NativeScript/NativeScript/issues/5661#issuecomment-456405380
|
||||
const providerName = `${context.getPackageName()}.provider`;
|
||||
Trace.write(`fully-qualified provider name [${providerName}]`, Trace.categories.Debug);
|
||||
// Android SDK 24+ introduced file system permissions changes that disallow
|
||||
// exposing URIs between applications
|
||||
//
|
||||
// see: https://developer.android.com/reference/android/os/FileUriExposedException
|
||||
// see: https://github.com/NativeScript/NativeScript/issues/5661#issuecomment-456405380
|
||||
const providerName = `${context.getPackageName()}.provider`;
|
||||
Trace.write(
|
||||
`fully-qualified provider name [${providerName}]`,
|
||||
Trace.categories.Debug
|
||||
);
|
||||
|
||||
const apkURI = androidx.core.content.FileProvider.getUriForFile(
|
||||
context,
|
||||
providerName,
|
||||
new java.io.File(filePath),
|
||||
);
|
||||
const apkURI = androidx.core.content.FileProvider.getUriForFile(
|
||||
context,
|
||||
providerName,
|
||||
new java.io.File(filePath)
|
||||
);
|
||||
|
||||
// Set flags & URI as data type on the view action
|
||||
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
chooserIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
// Set flags & URI as data type on the view action
|
||||
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
chooserIntent.addFlags(
|
||||
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
);
|
||||
|
||||
// Finish intent setup
|
||||
intent.setDataAndType(apkURI, mimeType);
|
||||
// Finish intent setup
|
||||
intent.setDataAndType(apkURI, mimeType);
|
||||
|
||||
context.startActivity(chooserIntent);
|
||||
context.startActivity(chooserIntent);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
const msg = err.message ? `: ${err.message}` : "";
|
||||
Trace.write(`Error in openFile${msg}`, Trace.categories.Error, Trace.messageType.error);
|
||||
return true;
|
||||
} catch (err) {
|
||||
const msg = err.message ? `: ${err.message}` : "";
|
||||
Trace.write(
|
||||
`Error in openFile${msg}`,
|
||||
Trace.categories.Error,
|
||||
Trace.messageType.error
|
||||
);
|
||||
|
||||
if (msg &&
|
||||
msg.includes("Attempt to invoke virtual method") &&
|
||||
msg.includes("android.content.pm.ProviderInfo.loadXmlMetaData") &&
|
||||
msg.includes("on a null object reference")) {
|
||||
// Alert user to possible fix
|
||||
Trace.write(
|
||||
`
|
||||
if (
|
||||
msg &&
|
||||
msg.includes("Attempt to invoke virtual method") &&
|
||||
msg.includes("android.content.pm.ProviderInfo.loadXmlMetaData") &&
|
||||
msg.includes("on a null object reference")
|
||||
) {
|
||||
// Alert user to possible fix
|
||||
Trace.write(
|
||||
`
|
||||
Please ensure you have your manifest correctly configured with the FileProvider.
|
||||
(see: https://developer.android.com/reference/android/support/v4/content/FileProvider#ProviderDefinition)
|
||||
`,
|
||||
Trace.categories.Error,
|
||||
);
|
||||
}
|
||||
Trace.categories.Error
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isRealDevice(): boolean {
|
||||
return ad.isRealDevice();
|
||||
return ad.isRealDevice();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user