PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/App.kt

100 lines
3.0 KiB
Kotlin
Raw Normal View History

2018-11-30 01:06:16 +00:00
package code.name.monkey.retromusic
import android.content.Context
import androidx.multidex.MultiDexApplication
import code.name.monkey.appthemehelper.ThemeStore
2018-12-08 03:33:02 +00:00
import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager
2018-11-30 01:06:16 +00:00
import com.anjlab.android.iab.v3.BillingProcessor
import com.anjlab.android.iab.v3.TransactionDetails
import com.bumptech.glide.Glide
class App : MultiDexApplication() {
2018-11-30 01:06:16 +00:00
lateinit var billingProcessor: BillingProcessor
override fun onCreate() {
super.onCreate()
instance = this
setupErrorHandler()
// default theme
if (!ThemeStore.isConfigured(this, 3)) {
ThemeStore.editTheme(this)
.accentColorRes(R.color.md_green_A200)
.coloredNavigationBar(true)
.commit()
}
2018-12-08 03:33:02 +00:00
if (VersionUtils.hasNougatMR())
DynamicShortcutManager(this).initDynamicShortcuts()
2018-11-30 01:06:16 +00:00
// automatically restores purchases
billingProcessor = BillingProcessor(this, BuildConfig.GOOGLE_PLAY_LICENSE_KEY,
object : BillingProcessor.IBillingHandler {
override fun onProductPurchased(productId: String, details: TransactionDetails?) {}
override fun onPurchaseHistoryRestored() {
//Toast.makeText(App.this, R.string.restored_previous_purchase_please_restart, Toast.LENGTH_LONG).show();
}
override fun onBillingError(errorCode: Int, error: Throwable?) {}
override fun onBillingInitialized() {}
})
}
private fun setupErrorHandler() {
Thread.setDefaultUncaughtExceptionHandler { _, throwable -> handleUncaughtException(throwable) }
}
private fun handleUncaughtException(throwable: Throwable) {
throwable.printStackTrace()
deleteAppData()
//Intent intent = new Intent(this, ErrorHandlerActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(intent);
}
override fun onLowMemory() {
super.onLowMemory()
Glide.with(this).onLowMemory()
}
override fun onTerminate() {
super.onTerminate()
billingProcessor.release()
}
companion object {
const val PRO_VERSION_PRODUCT_ID = "pro_version"
lateinit var instance: App
2018-11-30 01:06:16 +00:00
private set
val context: Context
get() = instance.applicationContext
val isProVersion: Boolean
get() = BuildConfig.DEBUG || instance.billingProcessor.isPurchased(PRO_VERSION_PRODUCT_ID)
fun deleteAppData() {
try {
// clearing app data
val packageName = instance.packageName
val runtime = Runtime.getRuntime()
runtime.exec("pm clear $packageName")
System.exit(0)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}