enrecipes/app/shared/utils.ts

650 lines
20 KiB
TypeScript
Raw Normal View History

2021-05-22 08:56:31 +00:00
import {
Application,
AndroidApplication,
Utils,
Device,
Color,
path,
knownFolders,
2021-06-15 11:04:42 +00:00
ApplicationSettings,
2021-05-22 08:56:31 +00:00
} from '@nativescript/core'
2021-06-05 18:09:42 +00:00
import { localize } from '@nativescript/localize'
2021-04-07 17:18:38 +00:00
let timerOne
2021-05-25 14:32:53 +00:00
declare const global, android, androidx, com, java, Array: any
2021-06-15 11:04:42 +00:00
const View = android.view.View
2021-06-05 18:09:42 +00:00
const PowerManager = android.os.PowerManager
const pm = Utils.android
.getApplicationContext()
.getSystemService(android.content.Context.POWER_SERVICE)
const wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 'Timers')
2021-06-15 11:04:42 +00:00
const sdkv = parseInt(Device.sdkVersion)
export function restartApp() {
2021-05-22 08:56:31 +00:00
const ctx = Utils.ad.getApplicationContext()
2021-04-01 10:55:35 +00:00
let mStartActivity = new android.content.Intent(
2021-05-22 08:56:31 +00:00
ctx,
2021-02-28 17:34:27 +00:00
Application.android.startActivity.getClass()
)
2021-04-01 10:55:35 +00:00
let mPendingIntentId = Math.random() * 100000
let mPendingIntent = android.app.PendingIntent.getActivity(
2021-05-22 08:56:31 +00:00
ctx,
2021-02-28 17:34:27 +00:00
mPendingIntentId,
mStartActivity,
android.app.PendingIntent.FLAG_CANCEL_CURRENT
)
2021-05-22 08:56:31 +00:00
let mgr = ctx.getSystemService(android.content.Context.ALARM_SERVICE)
2021-02-28 17:34:27 +00:00
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())
}
2021-06-15 11:04:42 +00:00
export function 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-06-15 11:04:42 +00:00
export function 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-06-15 11:04:42 +00:00
export function vibrate(duration) {
2021-04-01 10:55:35 +00:00
let vibratorService = Application.android.context.getSystemService(
android.content.Context.VIBRATOR_SERVICE
)
if (vibratorService.hasVibrator()) vibratorService.vibrate(duration)
}
2021-06-15 11:04:42 +00:00
export function timer(dur, callback) {
callback(1)
2021-06-05 18:09:42 +00:00
clearInterval(timerOne)
2021-04-07 17:18:38 +00:00
timerOne = setInterval(() => {
dur--
2021-06-15 11:04:42 +00:00
callback(1)
2021-04-07 17:18:38 +00:00
if (dur == 0) {
clearInterval(timerOne)
2021-06-15 11:04:42 +00:00
callback(0)
2021-04-07 17:18:38 +00:00
}
}, 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
2021-06-15 11:04:42 +00:00
export function 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-05-25 14:32:53 +00:00
return callIntent(ctx, intent, 'Select photo', DIR_CODE).then(
({ resultCode, intent }: any) => {
if (resultCode === android.app.Activity.RESULT_OK)
if (intent != null && intent.getData()) return intent.getData()
}
)
2021-04-07 17:18:38 +00:00
}
2021-06-15 11:04:42 +00:00
export function copyPhotoToCache(src: string, dest: string) {
2021-04-07 17:18:38 +00:00
const ContentResolver = Application.android.nativeApp.getContentResolver()
2021-06-15 11:04:42 +00:00
const isURI = src.includes('content://')
2021-04-12 18:09:48 +00:00
return new Promise((resolve) => {
2021-06-15 11:04:42 +00:00
if (isURI) {
const input = new java.io.BufferedInputStream(
ContentResolver.openInputStream(src)
)
let size = input.available()
let buffer = Array.create('byte', size)
const output = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(dest)
)
input.read(buffer)
do {
output.write(buffer)
} while (input.read(buffer) != -1)
input.close()
output.close()
resolve(dest)
} else {
const input = new java.io.FileInputStream(src)
const output = new java.io.FileOutputStream(dest)
let buffer = Array.create('byte', 1024)
let len
while ((len = input.read(buffer)) > 0) {
output.write(buffer, 0, len)
}
resolve(dest)
}
2021-04-12 18:09:48 +00:00
})
2021-04-07 17:18:38 +00:00
}
2021-05-22 08:56:31 +00:00
// COPY DB FILE
2021-06-15 11:04:42 +00:00
export function copyDBToExport() {
2021-05-22 08:56:31 +00:00
const src = path.join(knownFolders.documents().path, 'EnRecipes.db')
const dst = path.join(
knownFolders.documents().getFolder('EnRecipes').path,
'EnRecipes.db'
)
const input = new java.io.FileInputStream(src)
try {
const output = new java.io.FileOutputStream(dst)
2021-05-25 14:32:53 +00:00
let len: number
2021-05-22 08:56:31 +00:00
let buffer = Array.create('byte', 1024)
while ((len = input.read(buffer)) > 0) output.write(buffer, 0, len)
} catch (error) {
console.log(error)
} finally {
input.close()
}
}
2021-04-12 18:09:48 +00:00
// BACKUP FOLDER PICKER
2021-06-15 11:04:42 +00:00
export function 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-05-25 14:32:53 +00:00
return callIntent(ctx, intent, 'Select folder', DIR_CODE).then(
({ resultCode, intent }: any) => {
if (resultCode === android.app.Activity.RESULT_OK)
if (intent != null && intent.getData()) return intent.getData()
}
)
2021-04-07 17:18:38 +00:00
}
2021-04-12 18:09:48 +00:00
// BACKUP FILE PICKER
2021-06-15 11:04:42 +00:00
export function 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-05-25 14:32:53 +00:00
({ resultCode, intent }: any) => {
if (resultCode === android.app.Activity.RESULT_OK) {
if (intent != null && intent.getData()) return intent.getData()
2021-04-07 17:18:38 +00:00
}
}
2021-04-12 18:09:48 +00:00
)
}
// ZIP OPERATIONS
export class Zip {
2021-06-15 11:04:42 +00:00
static getSubFiles(src: string, isRootFolder?: number) {
2021-04-12 18:09:48 +00:00
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)
2021-06-15 11:04:42 +00:00
const sourceFiles = Zip.getSubFiles(src, 1)
2021-04-18 13:28:07 +00:00
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
}
2021-06-15 11:04:42 +00:00
export function 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)
}
2021-06-15 11:04:42 +00:00
export function shareImage(image, subject, title) {
2021-04-14 09:27:40 +00:00
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)
2021-06-05 18:09:42 +00:00
const tmpFile = new java.io.File(ctx.getCacheDir(), `${title}.jpg`)
2021-04-14 09:27:40 +00:00
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
}
2021-05-22 08:56:31 +00:00
2021-06-15 11:04:42 +00:00
export function keepScreenOn(bool) {
2021-06-05 18:09:42 +00:00
let ctx =
Application.android.foregroundActivity || Application.android.startActivity
let window = ctx.getWindow()
let flag = android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
bool ? window.addFlags(flag) : window.clearFlags(flag)
}
2021-05-22 08:56:31 +00:00
// TIMER NOTIFICATION
2021-05-25 14:32:53 +00:00
export class TimerNotif {
static getIcon(ctx, icon) {
2021-05-22 08:56:31 +00:00
const packageName = ctx.getApplicationInfo().packageName
let resources = ctx.getResources()
return (
resources.getIdentifier(icon, 'drawable', packageName) ||
ctx.getApplicationInfo().icon
)
}
static clear(nID) {
let ctx = Utils.ad.getApplicationContext()
const NotifySrv = ctx.getSystemService(
android.content.Context.NOTIFICATION_SERVICE
)
// nID ? NotifySrv.cancel(nID) : NotifySrv.cancelAll()
2021-05-22 08:56:31 +00:00
NotifySrv.cancel(nID)
}
static getNotification(
{
2021-06-05 18:09:42 +00:00
multi,
actions,
bID,
cID,
cName,
description,
priority,
sound,
title,
vibrate,
}: {
2021-06-05 18:09:42 +00:00
multi?: boolean
2021-06-15 11:04:42 +00:00
actions?: number
bID: string
cID: string
cName: string
description: string
priority: number
sound: string
title: string
vibrate?: number
},
ctx
) {
2021-05-25 14:32:53 +00:00
let soundUri: any
2021-05-22 08:56:31 +00:00
if (sound) soundUri = new android.net.Uri.parse(sound)
const NotifyMgr = android.app.NotificationManager
const NotifySrv = ctx.getSystemService(
android.content.Context.NOTIFICATION_SERVICE
)
const NotificationCompat = androidx.core.app.NotificationCompat
const AudioManager = android.media.AudioManager
2021-05-22 08:56:31 +00:00
if (sdkv >= 26) {
2021-05-25 14:32:53 +00:00
const importance =
priority > 0 ? NotifyMgr.IMPORTANCE_HIGH : NotifyMgr.IMPORTANCE_MIN
2021-05-22 08:56:31 +00:00
const AudioAttributes = android.media.AudioAttributes
const audioAttributes = new AudioAttributes.Builder()
2021-05-25 14:32:53 +00:00
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
2021-05-22 08:56:31 +00:00
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
const Channel = new android.app.NotificationChannel(
cID,
cName,
importance
)
if (description) Channel.setDescription(description)
2021-06-15 11:04:42 +00:00
Channel.enableVibration(!!vibrate)
2021-05-22 08:56:31 +00:00
Channel.enableLights(false)
if (sound) Channel.setSound(soundUri, audioAttributes)
else Channel.setSound(null, null)
Channel.setShowBadge(true)
Channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC)
2021-05-22 08:56:31 +00:00
NotifySrv.createNotificationChannel(Channel)
}
const Intent = android.content.Intent
const PendingIntent = android.app.PendingIntent
const mainInt = new Intent(ctx, com.tns.NativeScriptActivity.class)
2021-06-05 18:09:42 +00:00
mainInt.putExtra('action', 'timer')
2021-05-22 08:56:31 +00:00
const mainPInt = PendingIntent.getActivity(
ctx,
1,
mainInt,
PendingIntent.FLAG_UPDATE_CURRENT
)
// Action intent
2021-06-05 18:09:42 +00:00
let actionInt1,
actionInt2,
actionInt3,
actionPInt1,
actionPInt2,
actionPInt3
2021-05-22 08:56:31 +00:00
if (actions) {
actionInt1 = new Intent(bID)
2021-06-05 18:09:42 +00:00
actionInt1.putExtra('action', 'delay')
2021-05-22 08:56:31 +00:00
actionPInt1 = PendingIntent.getBroadcast(
ctx,
2,
actionInt1,
PendingIntent.FLAG_UPDATE_CURRENT
)
actionInt2 = new Intent(bID)
2021-06-05 18:09:42 +00:00
actionInt2.putExtra('action', 'dismiss')
2021-05-22 08:56:31 +00:00
actionPInt2 = PendingIntent.getBroadcast(
ctx,
3,
actionInt2,
PendingIntent.FLAG_UPDATE_CURRENT
)
2021-06-05 18:09:42 +00:00
actionInt3 = new Intent(bID)
actionInt3.putExtra('action', 'dismissAll')
actionPInt3 = PendingIntent.getBroadcast(
ctx,
4,
actionInt3,
PendingIntent.FLAG_UPDATE_CURRENT
)
2021-05-22 08:56:31 +00:00
}
// CREATE NOTIFICATION
2021-06-15 11:04:42 +00:00
let icon = this.getIcon(ctx, 'notify')
2021-05-22 08:56:31 +00:00
let builder = new NotificationCompat.Builder(ctx, cID)
.setColor(new Color('#ff5200').android)
.setContentIntent(mainPInt)
.setContentTitle(title)
.setOngoing(true)
.setPriority(priority)
2021-06-15 11:04:42 +00:00
.setShowWhen(!!actions)
2021-05-22 08:56:31 +00:00
.setSmallIcon(icon)
.setTicker(title)
.setAutoCancel(false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
2021-05-25 14:32:53 +00:00
if (sound) builder.setSound(soundUri, AudioManager.STREAM_ALARM)
else builder.setSound(null)
2021-05-22 08:56:31 +00:00
if (description) builder.setContentText(description)
if (vibrate) builder.setVibrate([500, 1000])
if (actions) {
builder.setFullScreenIntent(mainPInt, true)
2021-06-05 18:09:42 +00:00
if (multi) builder.addAction(null, localize('dismissAll'), actionPInt3)
else {
builder.addAction(null, localize('delay'), actionPInt1)
builder.addAction(null, localize('dismiss'), actionPInt2)
}
2021-05-22 08:56:31 +00:00
}
let notification = builder.build()
2021-05-25 14:32:53 +00:00
notification.flags =
NotificationCompat.FLAG_INSISTENT | NotificationCompat.FLAG_ONGOING_EVENT
return notification
}
static show(data) {
const ctx = Utils.ad.getApplicationContext()
const NotifySrv = ctx.getSystemService(
android.content.Context.NOTIFICATION_SERVICE
)
2021-06-05 18:09:42 +00:00
NotifySrv.notify(data.nID, this.getNotification(data, ctx))
}
}
export class Printer {
2021-06-15 11:04:42 +00:00
static PrintHelper = androidx.print.PrintHelper
2021-06-05 18:09:42 +00:00
static isSupported() {
2021-06-15 11:04:42 +00:00
return this.PrintHelper.systemSupportsPrint()
2021-06-05 18:09:42 +00:00
}
2021-06-15 11:04:42 +00:00
static print(view, fileName) {
const ctx =
Application.android.foregroundActivity ||
Application.android.startActivity
const PrintAttributes = android.print.PrintAttributes
const PrintManager = ctx.getSystemService(
android.content.Context.PRINT_SERVICE
)
2021-06-05 18:09:42 +00:00
return new Promise((resolve, reject) => {
try {
2021-06-15 11:04:42 +00:00
const PrintAdapter = view.android.createPrintDocumentAdapter(fileName)
if (PrintManager != null) {
let attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.setResolution(
new PrintAttributes.Resolution('pdf', 'pdf', 300, 300)
)
PrintManager.print(fileName, PrintAdapter, attributes.build())
resolve(true)
}
2021-06-05 18:09:42 +00:00
} catch (e) {
reject(e)
}
})
2021-05-22 08:56:31 +00:00
}
}
// GET RINGTONES LIST
2021-06-15 11:04:42 +00:00
export function getTones() {
2021-05-22 08:56:31 +00:00
const RingtoneManager = android.media.RingtoneManager
2021-05-25 14:32:53 +00:00
let ctx = Utils.ad.getApplicationContext()
2021-05-22 08:56:31 +00:00
const ringtonesMgr = new RingtoneManager(ctx)
ringtonesMgr.setType(RingtoneManager.TYPE_ALARM)
const cursor = ringtonesMgr.getCursor()
2021-05-25 14:32:53 +00:00
let tones = []
2021-05-22 08:56:31 +00:00
while (cursor.moveToNext()) {
tones.push({
title: cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX),
uri:
cursor.getString(RingtoneManager.URI_COLUMN_INDEX) +
'/' +
cursor.getString(RingtoneManager.ID_COLUMN_INDEX),
})
}
2021-05-25 14:32:53 +00:00
let defaultToneUri = RingtoneManager.getActualDefaultRingtoneUri(
ctx,
RingtoneManager.TYPE_ALARM
)
let defaultTone
if (defaultToneUri) {
let uriString = defaultToneUri.toString()
let tonesAvailable = tones.filter((e) => e.uri == uriString)
let toneExist = tonesAvailable.length
defaultTone = {
title: toneExist ? tonesAvailable[0].title : tones[0].title,
uri: toneExist ? uriString : tones[0].uri,
}
}
return { tones, defaultTone: defaultToneUri ? defaultTone : tones[0] }
2021-05-22 08:56:31 +00:00
}
2021-06-05 18:09:42 +00:00
2021-06-15 11:04:42 +00:00
//WAKE LOCK
export function wakeLock(n): void {
n ? !wl.isHeld() && wl.acquire() : wl.isHeld() && wl.release()
}
//RandomId
export function getRandomID(n?: number) {
if (n) return Math.floor(Math.random() * 9000000000) + 1000000000
else {
let res = ''
let chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < 16; i++) {
res += chars.charAt(Math.floor(Math.random() * chars.length))
}
return res
}
}
//LocaleHandlers
export function updateLocale() {
let lang = ApplicationSettings.getString('appLocale', 'none').split('-')
const l = lang[0]
const c = lang[1]
2021-06-05 18:09:42 +00:00
const ctx = Utils.android.getApplicationContext()
2021-06-15 11:04:42 +00:00
const res = ctx.getResources()
const config = res.getConfiguration()
if (l !== 'none') {
const locale = c ? new java.util.Locale(l, c) : new java.util.Locale(l)
java.util.Locale.setDefault(locale)
config.setLocale(locale)
config.setLayoutDirection(locale)
ctx.createConfigurationContext(config)
res.updateConfiguration(config, res.getDisplayMetrics())
ApplicationSettings.setNumber('RTL', config.getLayoutDirection() | 0)
}
ApplicationSettings.setString('sysLocale', sysLocale())
}
export function sysRTL() {
return android.content.res.Resources.getSystem()
.getConfiguration()
.getLayoutDirection()
}
export function sysLocale() {
return android.content.res.Resources.getSystem()
.getConfiguration()
.locale.toString()
.replace('_', '-')
2021-06-05 18:09:42 +00:00
}
2021-06-15 11:04:42 +00:00
// SetBarColors
export function setBarColors(w, dv, t) {
function setColors(color) {
w.setStatusBarColor(new Color(color).android)
sdkv >= 27 && w.setNavigationBarColor(new Color(color).android)
}
switch (t) {
case 'Light':
setColors('#f1f3f5')
break
case 'Dark':
setColors('#212529')
break
default:
setColors('#000000')
break
}
if (sdkv >= 27)
dv.setSystemUiVisibility(
t == 'Light'
? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR |
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
: View.SYSTEM_UI_FLAG_DARK_STATUS_BAR |
View.SYSTEM_UI_FLAG_DARK_NAVIGATION_BAR
)
else
dv.setSystemUiVisibility(
t == 'Light'
? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
: View.SYSTEM_UI_FLAG_DARK_STATUS_BAR
)
2021-06-05 18:09:42 +00:00
}