Files
APKDownloader/app/src/main/java/com/aurora/extensions/Context.kt
2021-02-24 17:41:20 +05:30

131 lines
3.8 KiB
Kotlin

/*
* Aurora Store
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
*
* Aurora Store is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Aurora Store is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.aurora.extensions
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.core.app.ShareCompat
import com.aurora.Constants
import com.aurora.gplayapi.data.models.App
import com.aurora.store.MainActivity
import com.aurora.store.R
import com.aurora.store.util.Log
import kotlin.system.exitProcess
val Context.inflater: LayoutInflater
get() = LayoutInflater.from(this)
val Context.displayMetrics: DisplayMetrics
get() = resources.displayMetrics
fun Context.browse(url: String) {
try {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
)
)
} catch (e: Exception) {
Log.e(e.message)
}
}
fun Context.share(app: App) {
try {
ShareCompat.IntentBuilder.from(this as AppCompatActivity)
.setType("text/plain")
.setChooserTitle(getString(R.string.action_share))
.setSubject(app.displayName)
.setText(Constants.SHARE_URL + app.packageName)
.startChooser()
} catch (e: Exception) {
}
}
fun Context.openInfo(packageName: String) {
try {
val intent = Intent(
"android.settings.APPLICATION_DETAILS_SETTINGS",
Uri.parse("package:$packageName")
)
startActivity(intent)
} catch (e: Exception) {
}
}
fun <T> Context.open(className: Class<T>, newTask: Boolean = false) {
val intent = Intent(this, className)
if (newTask)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(
intent,
getEmptyActivityBundle()
)
}
fun Context.restartApp() {
val pendingIntent = PendingIntent.getActivity(
this,
1337,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_CANCEL_CURRENT
)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent)
exitProcess(0)
}
fun Context.getEmptyActivityBundle(): Bundle? {
return ActivityOptionsCompat.makeCustomAnimation(
this,
android.R.anim.fade_in,
android.R.anim.fade_out
).toBundle()
}
fun Context.copyToClipBoard(data: String?) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Download Url", data)
clipboard.setPrimaryClip(clip)
}
fun Context.getStyledAttributeColor(id: Int): Int {
val arr = obtainStyledAttributes(TypedValue().data, intArrayOf(id))
val styledAttr = arr.getColor(0, Color.WHITE)
arr.recycle()
return styledAttr
}