2021-04-07 17:18:38 +00:00
|
|
|
import { Application, AndroidApplication, Utils } from '@nativescript/core'
|
|
|
|
let timerOne
|
2020-11-23 09:49:58 +00:00
|
|
|
export const restartApp = () => {
|
2021-04-01 10:55:35 +00:00
|
|
|
let mStartActivity = new android.content.Intent(
|
2021-02-28 17:34:27 +00:00
|
|
|
Application.android.context,
|
|
|
|
Application.android.startActivity.getClass()
|
|
|
|
)
|
2021-04-01 10:55:35 +00:00
|
|
|
let mPendingIntentId = Math.random() * 100000
|
|
|
|
let mPendingIntent = android.app.PendingIntent.getActivity(
|
2021-02-28 17:34:27 +00:00
|
|
|
Application.android.context,
|
|
|
|
mPendingIntentId,
|
|
|
|
mStartActivity,
|
|
|
|
android.app.PendingIntent.FLAG_CANCEL_CURRENT
|
|
|
|
)
|
2021-04-01 10:55:35 +00:00
|
|
|
let mgr = Application.android.context.getSystemService(
|
2021-02-28 17:34:27 +00:00
|
|
|
android.content.Context.ALARM_SERVICE
|
|
|
|
)
|
|
|
|
mgr.set(
|
|
|
|
android.app.AlarmManager.RTC,
|
|
|
|
java.lang.System.currentTimeMillis() + 100,
|
|
|
|
mPendingIntent
|
|
|
|
)
|
2020-11-23 09:49:58 +00:00
|
|
|
android.os.Process.killProcess(android.os.Process.myPid())
|
|
|
|
}
|
|
|
|
export const openAppSettingsPage = () => {
|
2021-04-01 10:55:35 +00:00
|
|
|
let intent = new android.content.Intent(
|
2021-02-28 17:34:27 +00:00
|
|
|
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
|
|
)
|
2020-11-23 09:49:58 +00:00
|
|
|
intent.addCategory(android.content.Intent.CATEGORY_DEFAULT)
|
2021-02-28 17:34:27 +00:00
|
|
|
intent.setData(
|
|
|
|
android.net.Uri.parse(
|
|
|
|
'package:' + Application.android.context.getPackageName()
|
|
|
|
)
|
|
|
|
)
|
2020-11-23 09:49:58 +00:00
|
|
|
Application.android.foregroundActivity.startActivity(intent)
|
|
|
|
}
|
2021-02-28 17:34:27 +00:00
|
|
|
export const hasAccelerometer = () => {
|
|
|
|
let context = Utils.ad.getApplicationContext()
|
|
|
|
let sensorManager = context.getSystemService(
|
|
|
|
android.content.Context.SENSOR_SERVICE
|
|
|
|
)
|
|
|
|
return sensorManager.getDefaultSensor(
|
|
|
|
android.hardware.Sensor.TYPE_ACCELEROMETER
|
|
|
|
)
|
|
|
|
}
|
2021-04-01 10:55:35 +00:00
|
|
|
export const vibrate = (duration) => {
|
|
|
|
let vibratorService = Application.android.context.getSystemService(
|
|
|
|
android.content.Context.VIBRATOR_SERVICE
|
|
|
|
)
|
|
|
|
if (vibratorService.hasVibrator()) vibratorService.vibrate(duration)
|
|
|
|
}
|
2021-04-07 17:18:38 +00:00
|
|
|
export const timer = (dur, callback) => {
|
|
|
|
clearInterval(timerOne)
|
|
|
|
callback(true)
|
|
|
|
timerOne = setInterval(() => {
|
|
|
|
dur--
|
|
|
|
callback(true)
|
|
|
|
if (dur == 0) {
|
|
|
|
callback(false)
|
|
|
|
clearInterval(timerOne)
|
|
|
|
}
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FOLDER PICKER
|
|
|
|
function callIntent(context, intent, pickerType) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const onEvent = function(e) {
|
|
|
|
if (e.requestCode === pickerType) {
|
|
|
|
resolve(e)
|
|
|
|
Application.android.off(AndroidApplication.activityResultEvent, onEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Application.android.once(AndroidApplication.activityResultEvent, onEvent)
|
|
|
|
context.startActivityForResult(intent, pickerType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//SOURCE: https://github.com/NativeScript/nativescript-imagepicker/blob/bc9209dd7d773eb437ff812f07873c72c789d572/src/imagepicker.android.ts
|
|
|
|
|
|
|
|
function getDataColumn(uri, selection, selectionArgs, isDownload) {
|
|
|
|
const ContentResolver = Application.android.nativeApp.getContentResolver()
|
|
|
|
let cursor = null
|
|
|
|
let filePath
|
|
|
|
if (isDownload) {
|
|
|
|
let columns = ['_display_name']
|
|
|
|
try {
|
|
|
|
cursor = ContentResolver.query(
|
|
|
|
uri,
|
|
|
|
columns,
|
|
|
|
selection,
|
|
|
|
selectionArgs,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
let column_index = cursor.getColumnIndexOrThrow(columns[0])
|
|
|
|
filePath = cursor.getString(column_index)
|
|
|
|
if (filePath) {
|
|
|
|
const dl = android.os.Environment.getExternalStoragePublicDirectory(
|
|
|
|
android.os.Environment.DIRECTORY_DOWNLOADS
|
|
|
|
)
|
|
|
|
filePath = `${dl}/${filePath}`
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
} finally {
|
|
|
|
if (cursor) {
|
|
|
|
cursor.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let columns = [android.provider.MediaStore.MediaColumns.DATA]
|
|
|
|
let filePath
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = ContentResolver.query(
|
|
|
|
uri,
|
|
|
|
columns,
|
|
|
|
selection,
|
|
|
|
selectionArgs,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
let column_index = cursor.getColumnIndexOrThrow(columns[0])
|
|
|
|
filePath = cursor.getString(column_index)
|
|
|
|
if (filePath) {
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
} finally {
|
|
|
|
if (cursor) {
|
|
|
|
cursor.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
function getTreeUri(uri) {
|
|
|
|
const DocumentsContract = android.provider.DocumentsContract
|
|
|
|
const ContentResolver = Application.android.nativeApp.getContentResolver()
|
|
|
|
if (DocumentsContract.isTreeUri(uri)) {
|
|
|
|
let docId, id, type
|
|
|
|
let contentUri = null
|
|
|
|
if ('com.android.externalstorage.documents' === uri.getAuthority()) {
|
|
|
|
docId = DocumentsContract.getTreeDocumentId(uri).split(':')
|
|
|
|
type = docId[0]
|
|
|
|
id = docId[1]
|
|
|
|
|
|
|
|
if ('primary' === type.toLowerCase())
|
|
|
|
return android.os.Environment.getExternalStorageDirectory() + '/' + id
|
|
|
|
else {
|
|
|
|
ContentResolver.takePersistableUriPermission(
|
|
|
|
uri,
|
|
|
|
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
|
|
|
|
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
)
|
|
|
|
const externalMediaDirs = Application.android.context.getExternalMediaDirs()
|
|
|
|
if (externalMediaDirs.length > 1) {
|
|
|
|
let filePath = externalMediaDirs[1].getAbsolutePath()
|
|
|
|
filePath = filePath.substring(0, filePath.indexOf('Android')) + id
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// DownloadsProvider
|
|
|
|
else if (
|
|
|
|
'com.android.providers.downloads.documents' === uri.getAuthority()
|
|
|
|
) {
|
|
|
|
return getDataColumn(uri, null, null, true)
|
|
|
|
}
|
|
|
|
// MediaProvider
|
|
|
|
else if ('com.android.providers.media.documents' === uri.getAuthority()) {
|
|
|
|
docId = DocumentsContract.getTreeDocumentId(uri).split(':')
|
|
|
|
type = docId[0]
|
|
|
|
id = docId[1]
|
|
|
|
|
|
|
|
if ('image' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
|
|
|
else if ('video' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
|
|
|
else if ('audio' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
|
|
|
|
|
|
|
|
let selection = '_id=?'
|
|
|
|
let selectionArgs = [id]
|
|
|
|
|
|
|
|
return getDataColumn(contentUri, selection, selectionArgs, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
function getFileUri(uri) {
|
|
|
|
const DocumentsContract = android.provider.DocumentsContract
|
|
|
|
const ContentResolver = Application.android.nativeApp.getContentResolver()
|
|
|
|
if (DocumentsContract.isDocumentUri(Application.android.context, uri)) {
|
|
|
|
let docId, id, type
|
|
|
|
let contentUri = null
|
|
|
|
if ('com.android.externalstorage.documents' === uri.getAuthority()) {
|
|
|
|
docId = DocumentsContract.getDocumentId(uri).split(':')
|
|
|
|
type = docId[0]
|
|
|
|
id = docId[1]
|
|
|
|
|
|
|
|
if ('primary' === type.toLowerCase())
|
|
|
|
return android.os.Environment.getExternalStorageDirectory() + '/' + id
|
|
|
|
else {
|
|
|
|
ContentResolver.takePersistableUriPermission(
|
|
|
|
uri,
|
|
|
|
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
|
|
|
|
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
)
|
|
|
|
const externalMediaDirs = Application.android.context.getExternalMediaDirs()
|
|
|
|
if (externalMediaDirs.length > 1) {
|
|
|
|
let filePath = externalMediaDirs[1].getAbsolutePath()
|
|
|
|
filePath = filePath.substring(0, filePath.indexOf('Android')) + id
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// DownloadsProvider
|
|
|
|
else if (
|
|
|
|
'com.android.providers.downloads.documents' === uri.getAuthority()
|
|
|
|
) {
|
|
|
|
return getDataColumn(uri, null, null, true)
|
|
|
|
}
|
|
|
|
// MediaProvider
|
|
|
|
else if ('com.android.providers.media.documents' === uri.getAuthority()) {
|
|
|
|
docId = DocumentsContract.getDocumentId(uri).split(':')
|
|
|
|
type = docId[0]
|
|
|
|
id = docId[1]
|
|
|
|
|
|
|
|
if ('image' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
|
|
|
else if ('video' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
|
|
|
else if ('audio' === type)
|
|
|
|
contentUri =
|
|
|
|
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
|
|
|
|
|
|
|
|
let selection = '_id=?'
|
|
|
|
let selectionArgs = [id]
|
|
|
|
|
|
|
|
return getDataColumn(contentUri, selection, selectionArgs, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
export const pickFolder = () => {
|
|
|
|
const context =
|
|
|
|
Application.android.foregroundActivity || Application.android.startActivity
|
|
|
|
const DIR_CODE = Math.round(Math.random() * 10000)
|
|
|
|
const intent = new android.content.Intent(
|
|
|
|
android.content.Intent.ACTION_OPEN_DOCUMENT_TREE
|
|
|
|
)
|
|
|
|
intent.addCategory(android.content.Intent.CATEGORY_DEFAULT)
|
|
|
|
return callIntent(context, intent, DIR_CODE).then((res) => {
|
|
|
|
if (res.resultCode === android.app.Activity.RESULT_OK)
|
|
|
|
if (res.intent != null && res.intent.getData())
|
|
|
|
return getTreeUri(res.intent.getData())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function callBackupIntent(context, intent, pickerType) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const onEvent = function(e) {
|
|
|
|
if (e.requestCode === pickerType) {
|
|
|
|
resolve(e)
|
|
|
|
Application.android.off(AndroidApplication.activityResultEvent, onEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Application.android.once(AndroidApplication.activityResultEvent, onEvent)
|
|
|
|
context.startActivityForResult(intent, pickerType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export const getBackupFile = () => {
|
|
|
|
const context =
|
|
|
|
Application.android.foregroundActivity || Application.android.startActivity
|
|
|
|
const DIR_CODE = Math.round(Math.random() * 10000)
|
|
|
|
const intent = new android.content.Intent(
|
|
|
|
android.content.Intent.ACTION_GET_CONTENT
|
|
|
|
)
|
|
|
|
intent.addCategory(android.content.Intent.CATEGORY_OPENABLE)
|
|
|
|
intent.setType('application/zip')
|
|
|
|
// intent.setFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
return callBackupIntent(context, intent, DIR_CODE).then((res) => {
|
|
|
|
if (res.resultCode === android.app.Activity.RESULT_OK) {
|
|
|
|
if (res.intent != null && res.intent.getData()) {
|
|
|
|
// return getFileUri(res.intent.getData())
|
|
|
|
return res.intent.getData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|