enrecipes/app/shared/utils.js

275 lines
9.1 KiB
JavaScript
Raw Permalink Normal View History

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 = () => {
2021-04-14 09:27:40 +00:00
let ctx = Utils.ad.getApplicationContext()
let sensorManager = ctx.getSystemService(
2021-02-28 17:34:27 +00:00
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)
}
2021-04-14 09:27:40 +00:00
function callIntent(ctx, intent, msg, pickerType) {
return new Promise((resolve) => {
2021-04-07 17:18:38 +00:00
const onEvent = function(e) {
if (e.requestCode === pickerType) {
resolve(e)
Application.android.off(AndroidApplication.activityResultEvent, onEvent)
}
}
Application.android.once(AndroidApplication.activityResultEvent, onEvent)
2021-04-14 09:27:40 +00:00
ctx.startActivityForResult(
2021-04-12 18:09:48 +00:00
android.content.Intent.createChooser(intent, msg),
pickerType
)
2021-04-07 17:18:38 +00:00
})
}
2021-04-12 18:09:48 +00:00
// IMAGE PICKER
export const getRecipePhoto = () => {
2021-04-14 09:27:40 +00:00
const ctx =
2021-04-12 18:09:48 +00:00
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.setType('image/*')
2021-04-14 09:27:40 +00:00
return callIntent(ctx, intent, 'Select photo', DIR_CODE).then((res) => {
2021-04-12 18:09:48 +00:00
if (res.resultCode === android.app.Activity.RESULT_OK)
if (res.intent != null && res.intent.getData())
return res.intent.getData()
})
2021-04-07 17:18:38 +00:00
}
2021-04-12 18:09:48 +00:00
export const copyPhotoToCache = (uri, filepath) => {
2021-04-07 17:18:38 +00:00
const ContentResolver = Application.android.nativeApp.getContentResolver()
2021-04-12 18:09:48 +00:00
return new Promise((resolve) => {
const inputStream = ContentResolver.openInputStream(uri)
const input = new java.io.BufferedInputStream(inputStream)
let size = input.available()
let buffer = Array.create('byte', size)
const output = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(filepath)
)
input.read(buffer)
do {
output.write(buffer)
} while (input.read(buffer) != -1)
input.close()
output.close()
resolve(filepath)
})
2021-04-07 17:18:38 +00:00
}
2021-04-12 18:09:48 +00:00
// BACKUP FOLDER PICKER
export const getBackupFolder = () => {
2021-04-14 09:27:40 +00:00
const ctx =
2021-04-07 17:18:38 +00:00
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
)
2021-04-14 09:27:40 +00:00
return callIntent(ctx, intent, 'Select folder', DIR_CODE).then((res) => {
2021-04-07 17:18:38 +00:00
if (res.resultCode === android.app.Activity.RESULT_OK)
if (res.intent != null && res.intent.getData())
2021-04-12 18:09:48 +00:00
return res.intent.getData()
2021-04-07 17:18:38 +00:00
})
}
2021-04-12 18:09:48 +00:00
// BACKUP FILE PICKER
2021-04-07 17:18:38 +00:00
export const getBackupFile = () => {
2021-04-14 09:27:40 +00:00
const ctx =
2021-04-07 17:18:38 +00:00
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')
2021-04-14 09:27:40 +00:00
return callIntent(ctx, intent, 'Select file to import', DIR_CODE).then(
2021-04-12 18:09:48 +00:00
(res) => {
if (res.resultCode === android.app.Activity.RESULT_OK) {
if (res.intent != null && res.intent.getData())
return res.intent.getData()
2021-04-07 17:18:38 +00:00
}
}
2021-04-12 18:09:48 +00:00
)
}
// ZIP OPERATIONS
export class Zip {
static getSubFiles(src, isRootFolder) {
const fileList = new java.util.ArrayList()
const sourceFile = new java.io.File(src)
let tempList = sourceFile.listFiles()
for (let i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
fileList.add(tempList[i])
}
if (tempList[i].isDirectory()) {
if (isRootFolder) {
fileList.add(tempList[i])
}
fileList.addAll(Zip.getSubFiles(tempList[i].getAbsolutePath()))
}
}
return fileList
}
static zip(src, destUri, filename) {
const ContentResolver = Application.android.nativeApp.getContentResolver()
const parsedUri = new android.net.Uri.parse(destUri)
const uri = new androidx.documentfile.provider.DocumentFile.fromTreeUri(
Application.android.context,
parsedUri
)
2021-04-18 13:28:07 +00:00
return new Promise((resolve, reject) => {
if (uri.exists()) {
let destFile = uri.createFile('application/zip', filename).getUri()
const outputStream = ContentResolver.openOutputStream(destFile)
const zipOutputStream = new java.util.zip.ZipOutputStream(outputStream)
const sourceFiles = Zip.getSubFiles(src, true)
for (let i = 0; i < sourceFiles.size(); i++) {
let len
let buffer = Array.create('byte', 4096)
let file = sourceFiles.get(i)
let entry = new java.util.zip.ZipEntry(
'EnRecipes/' +
new java.io.File(src)
.toURI()
.relativize(file.toURI())
.getPath()
)
zipOutputStream.putNextEntry(entry)
if (!file.isDirectory()) {
let inputStream = new java.io.FileInputStream(file)
while ((len = inputStream.read(buffer)) != -1) {
zipOutputStream.write(buffer, 0, len)
}
inputStream.close()
2021-04-12 18:09:48 +00:00
}
2021-04-18 13:28:07 +00:00
zipOutputStream.closeEntry()
2021-04-12 18:09:48 +00:00
}
2021-04-18 13:28:07 +00:00
zipOutputStream.close()
resolve(destFile)
} else {
reject('Destination file cannot be created: Path does not exist')
2021-04-12 18:09:48 +00:00
}
})
}
static unzip(uri, dest) {
const ContentResolver = Application.android.nativeApp.getContentResolver()
return new Promise((resolve) => {
const inputStream = ContentResolver.openInputStream(uri)
const zipInputStream = new java.util.zip.ZipInputStream(inputStream)
2021-04-18 13:28:07 +00:00
let len, filepath, entry
while ((entry = zipInputStream.getNextEntry()) != null) {
2021-04-12 18:09:48 +00:00
filepath = dest + '/' + entry.getName()
2021-04-18 13:28:07 +00:00
let output = new java.io.File(filepath)
if (entry.isDirectory()) {
output.mkdirs()
continue
2021-04-12 18:09:48 +00:00
}
2021-04-18 13:28:07 +00:00
if (!output.getParentFile().exists()) output.getParentFile().mkdirs()
const outputStream = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(output)
)
let buffer = Array.create('byte', 4096)
while ((len = zipInputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, len)
outputStream.close()
2021-04-12 18:09:48 +00:00
zipInputStream.closeEntry()
}
zipInputStream.close()
resolve(dest)
})
}
}
// SHARE OPERATIONS
function share(intent, subject) {
2021-04-14 09:27:40 +00:00
const ctx = Application.android.context
2021-04-12 18:09:48 +00:00
const shareIntent = android.content.Intent.createChooser(intent, subject)
shareIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
2021-04-14 09:27:40 +00:00
ctx.startActivity(shareIntent)
}
function getSendIntent(type) {
const intent = new android.content.Intent(android.content.Intent.ACTION_SEND)
intent.setType(type)
return intent
2021-04-12 18:09:48 +00:00
}
export const shareText = (text, subject) => {
2021-04-14 09:27:40 +00:00
const intent = getSendIntent('text/plain')
intent.putExtra(android.content.Intent.EXTRA_TEXT, text)
share(intent, subject)
}
export const shareImage = (image, subject) => {
let ctx = Application.android.context
const intent = getSendIntent('image/jpeg')
const baos = new java.io.ByteArrayOutputStream()
image.android.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, baos)
const tmpFile = new java.io.File(ctx.getExternalCacheDir(), 'EnRecipes.jpg')
const fos = new java.io.FileOutputStream(tmpFile)
fos.write(baos.toByteArray())
fos.flush()
fos.close()
2021-04-18 16:08:58 +00:00
let shareUri = global.androidx.core.content.FileProvider.getUriForFile(
ctx,
Application.android.nativeApp.getPackageName() + '.provider',
tmpFile
2021-04-14 09:27:40 +00:00
)
2021-04-18 16:08:58 +00:00
intent.putExtra(android.content.Intent.EXTRA_STREAM, shareUri)
2021-04-12 18:09:48 +00:00
share(intent, subject)
2021-04-07 17:18:38 +00:00
}