2021-03-18 19:11:45 +01:00
|
|
|
/*
|
|
|
|
* * Copyright (c) 2021 Shabinder Singh
|
|
|
|
* * This program 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 3 of the License, or
|
|
|
|
* * (at your option) any later version.
|
|
|
|
* *
|
|
|
|
* * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-25 14:28:33 +01:00
|
|
|
package com.shabinder.spotiflyer
|
2021-01-26 13:54:28 +01:00
|
|
|
|
2021-02-10 18:25:36 +01:00
|
|
|
import android.annotation.SuppressLint
|
2021-08-19 22:44:56 +02:00
|
|
|
import android.content.*
|
2021-04-29 18:14:19 +02:00
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.media.MediaScannerConnection
|
|
|
|
import android.net.Uri
|
2021-02-10 18:25:36 +01:00
|
|
|
import android.os.Build
|
2021-01-26 13:54:28 +01:00
|
|
|
import android.os.Bundle
|
2021-06-22 08:13:30 +02:00
|
|
|
import android.os.IBinder
|
2021-02-10 18:25:36 +01:00
|
|
|
import android.os.PowerManager
|
2021-02-22 18:38:33 +01:00
|
|
|
import android.util.Log
|
2021-02-10 18:25:36 +01:00
|
|
|
import androidx.activity.ComponentActivity
|
2021-02-11 21:51:11 +01:00
|
|
|
import androidx.activity.compose.setContent
|
2021-07-10 07:27:18 +02:00
|
|
|
import androidx.compose.animation.ExperimentalAnimationApi
|
2021-02-25 16:23:10 +01:00
|
|
|
import androidx.compose.foundation.background
|
2021-07-10 07:27:18 +02:00
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
|
import androidx.compose.foundation.layout.Spacer
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
import androidx.compose.material.MaterialTheme
|
|
|
|
import androidx.compose.material.Surface
|
2021-08-19 22:44:56 +02:00
|
|
|
import androidx.compose.runtime.*
|
2021-02-25 16:23:10 +01:00
|
|
|
import androidx.compose.ui.Modifier
|
2021-02-24 18:02:01 +01:00
|
|
|
import androidx.compose.ui.platform.LocalView
|
2021-04-29 18:14:19 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-02-24 18:02:01 +01:00
|
|
|
import androidx.core.view.WindowCompat
|
2021-02-22 18:38:33 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-02-07 17:19:48 +01:00
|
|
|
import com.arkivanov.decompose.ComponentContext
|
2021-08-23 19:16:20 +02:00
|
|
|
import com.arkivanov.decompose.defaultComponentContext
|
2021-02-07 17:19:48 +01:00
|
|
|
import com.arkivanov.mvikotlin.logging.store.LoggingStoreFactory
|
|
|
|
import com.arkivanov.mvikotlin.main.store.DefaultStoreFactory
|
2021-04-19 20:24:48 +02:00
|
|
|
import com.codekidlabs.storagechooser.R
|
|
|
|
import com.codekidlabs.storagechooser.StorageChooser
|
2021-04-14 19:37:02 +02:00
|
|
|
import com.google.accompanist.insets.ProvideWindowInsets
|
|
|
|
import com.google.accompanist.insets.navigationBarsPadding
|
|
|
|
import com.google.accompanist.insets.statusBarsHeight
|
|
|
|
import com.google.accompanist.insets.statusBarsPadding
|
2021-08-23 19:16:20 +02:00
|
|
|
import com.shabinder.common.core_components.ConnectionLiveData
|
|
|
|
import com.shabinder.common.core_components.analytics.AnalyticsManager
|
|
|
|
import com.shabinder.common.core_components.file_manager.FileManager
|
|
|
|
import com.shabinder.common.core_components.preference_manager.PreferenceManager
|
2021-07-10 07:27:18 +02:00
|
|
|
import com.shabinder.common.di.observeAsState
|
2021-08-19 22:44:56 +02:00
|
|
|
import com.shabinder.common.models.*
|
2021-04-29 18:14:19 +02:00
|
|
|
import com.shabinder.common.models.PlatformActions.Companion.SharedPreferencesKey
|
2021-08-23 19:16:20 +02:00
|
|
|
import com.shabinder.common.providers.FetchPlatformQueryResult
|
2021-02-07 17:19:48 +01:00
|
|
|
import com.shabinder.common.root.SpotiFlyerRoot
|
2021-02-10 18:25:36 +01:00
|
|
|
import com.shabinder.common.root.callbacks.SpotiFlyerRootCallBacks
|
2021-06-25 21:58:00 +02:00
|
|
|
import com.shabinder.common.translations.Strings
|
2021-07-10 07:27:18 +02:00
|
|
|
import com.shabinder.common.uikit.configurations.SpotiFlyerTheme
|
|
|
|
import com.shabinder.common.uikit.configurations.colorOffWhite
|
|
|
|
import com.shabinder.common.uikit.screens.SpotiFlyerRootContent
|
2021-06-22 08:13:30 +02:00
|
|
|
import com.shabinder.spotiflyer.service.ForegroundService
|
2021-05-16 20:10:02 +02:00
|
|
|
import com.shabinder.spotiflyer.ui.AnalyticsDialog
|
2021-04-29 18:14:19 +02:00
|
|
|
import com.shabinder.spotiflyer.ui.NetworkDialog
|
|
|
|
import com.shabinder.spotiflyer.ui.PermissionDialog
|
2021-08-19 22:44:56 +02:00
|
|
|
import com.shabinder.spotiflyer.utils.*
|
2021-07-10 07:27:18 +02:00
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
|
|
|
import kotlinx.coroutines.flow.conflate
|
|
|
|
import kotlinx.coroutines.flow.emitAll
|
|
|
|
import kotlinx.coroutines.launch
|
2021-02-07 17:19:48 +01:00
|
|
|
import org.koin.android.ext.android.inject
|
2021-08-19 22:44:56 +02:00
|
|
|
import org.koin.core.parameter.parametersOf
|
2021-04-19 20:24:48 +02:00
|
|
|
import java.io.File
|
2021-01-26 13:54:28 +01:00
|
|
|
|
2021-02-26 10:41:25 +01:00
|
|
|
@ExperimentalAnimationApi
|
2021-05-12 00:30:52 +02:00
|
|
|
class MainActivity : ComponentActivity() {
|
2021-02-10 18:25:36 +01:00
|
|
|
|
2021-08-23 19:16:20 +02:00
|
|
|
private lateinit var root: SpotiFlyerRoot
|
2021-02-10 18:25:36 +01:00
|
|
|
private val fetcher: FetchPlatformQueryResult by inject()
|
2021-08-23 19:16:20 +02:00
|
|
|
private val fileManager: FileManager by inject()
|
2021-06-23 13:13:26 +02:00
|
|
|
private val preferenceManager: PreferenceManager by inject()
|
2021-08-19 22:44:56 +02:00
|
|
|
private val analyticsManager: AnalyticsManager by inject { parametersOf(this) }
|
2021-05-31 17:02:10 +02:00
|
|
|
private val callBacks: SpotiFlyerRootCallBacks get() = root.callBacks
|
2021-02-22 22:42:58 +01:00
|
|
|
private val trackStatusFlow = MutableSharedFlow<HashMap<String, DownloadStatus>>(1)
|
2021-02-25 16:23:10 +01:00
|
|
|
private var permissionGranted = mutableStateOf(true)
|
2021-04-29 18:14:19 +02:00
|
|
|
private val internetAvailability by lazy { ConnectionLiveData(applicationContext) }
|
2021-08-25 17:30:31 +02:00
|
|
|
private lateinit var rootComponent: SpotiFlyerRoot
|
2021-08-20 21:36:05 +02:00
|
|
|
// private val visibleChild get(): SpotiFlyerRoot.Child = root.routerState.value.activeChild.instance
|
2021-02-07 17:19:48 +01:00
|
|
|
|
2021-06-22 08:13:30 +02:00
|
|
|
// Variable for storing instance of our service class
|
|
|
|
var foregroundService: ForegroundService? = null
|
|
|
|
|
|
|
|
// Boolean to check if our activity is bound to service or not
|
|
|
|
var isServiceBound: Boolean? = null
|
|
|
|
|
2021-01-26 13:54:28 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-09-03 20:58:13 +02:00
|
|
|
preferenceManager.analyticsManager = analyticsManager
|
2021-02-24 18:02:01 +01:00
|
|
|
// This app draws behind the system bars, so we want to handle fitting system windows
|
|
|
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
2021-08-25 17:30:31 +02:00
|
|
|
rootComponent = spotiFlyerRoot(defaultComponentContext())
|
2021-01-26 13:54:28 +01:00
|
|
|
setContent {
|
2021-02-07 17:19:48 +01:00
|
|
|
SpotiFlyerTheme {
|
2021-02-12 00:17:46 +01:00
|
|
|
Surface(contentColor = colorOffWhite) {
|
2021-04-14 19:37:02 +02:00
|
|
|
ProvideWindowInsets {
|
|
|
|
permissionGranted = remember { mutableStateOf(true) }
|
|
|
|
val view = LocalView.current
|
2021-02-24 18:02:01 +01:00
|
|
|
|
2021-04-14 19:37:02 +02:00
|
|
|
Box {
|
|
|
|
root = SpotiFlyerRootContent(
|
2021-08-23 19:16:20 +02:00
|
|
|
rootComponent,
|
2021-04-14 19:37:02 +02:00
|
|
|
Modifier.statusBarsPadding().navigationBarsPadding()
|
|
|
|
)
|
|
|
|
Spacer(
|
|
|
|
Modifier
|
|
|
|
.statusBarsHeight()
|
|
|
|
.fillMaxWidth()
|
|
|
|
.background(MaterialTheme.colors.background.copy(alpha = 0.65f))
|
|
|
|
)
|
2021-02-24 18:02:01 +01:00
|
|
|
}
|
2021-02-25 16:23:10 +01:00
|
|
|
|
2021-04-29 18:14:19 +02:00
|
|
|
NetworkDialog(isInternetAvailableState())
|
2021-05-16 20:10:02 +02:00
|
|
|
|
2021-04-29 18:14:19 +02:00
|
|
|
PermissionDialog(
|
|
|
|
permissionGranted.value,
|
|
|
|
{ requestStoragePermission() },
|
2021-05-13 23:21:33 +02:00
|
|
|
{ disableDozeMode(disableDozeCode) },
|
2021-04-29 18:14:19 +02:00
|
|
|
)
|
2021-05-16 20:10:02 +02:00
|
|
|
|
|
|
|
var askForAnalyticsPermission by remember { mutableStateOf(false) }
|
|
|
|
AnalyticsDialog(
|
|
|
|
askForAnalyticsPermission,
|
2021-05-29 12:26:26 +02:00
|
|
|
enableAnalytics = {
|
2021-09-03 20:58:13 +02:00
|
|
|
preferenceManager.toggleAnalytics(true)
|
2021-06-23 13:13:26 +02:00
|
|
|
preferenceManager.firstLaunchDone()
|
2021-05-29 12:26:26 +02:00
|
|
|
},
|
2021-05-16 20:10:02 +02:00
|
|
|
dismissDialog = {
|
|
|
|
askForAnalyticsPermission = false
|
2021-06-23 13:13:26 +02:00
|
|
|
preferenceManager.firstLaunchDone()
|
2021-05-16 20:10:02 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
LaunchedEffect(view) {
|
|
|
|
permissionGranted.value = checkPermissions()
|
2021-07-13 22:45:09 +02:00
|
|
|
if (preferenceManager.isFirstLaunch) {
|
2021-05-16 20:10:02 +02:00
|
|
|
delay(2500)
|
|
|
|
// Ask For Analytics Permission on first Dialog
|
|
|
|
askForAnalyticsPermission = true
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 19:37:02 +02:00
|
|
|
}
|
2021-02-12 00:17:46 +01:00
|
|
|
}
|
2021-02-07 17:19:48 +01:00
|
|
|
}
|
2021-01-26 13:54:28 +01:00
|
|
|
}
|
2021-02-10 18:25:36 +01:00
|
|
|
initialise()
|
2021-01-26 13:54:28 +01:00
|
|
|
}
|
2021-02-07 17:19:48 +01:00
|
|
|
|
2021-02-10 18:25:36 +01:00
|
|
|
private fun initialise() {
|
2021-06-21 21:00:28 +02:00
|
|
|
val isGithubRelease = checkAppSignature(this)
|
2021-05-29 12:26:26 +02:00
|
|
|
/*
|
|
|
|
* Only Send an `Update Notification` on Github Release Builds
|
|
|
|
* and Track Downloads for all other releases like F-Droid,
|
|
|
|
* for `Github Downloads` we will track Downloads using : https://tooomm.github.io/github-release-stats/?username=Shabinder&repository=SpotiFlyer
|
|
|
|
* */
|
2021-08-19 22:44:56 +02:00
|
|
|
if (isGithubRelease) {
|
|
|
|
checkIfLatestVersion()
|
2021-05-13 23:21:33 +02:00
|
|
|
}
|
2021-08-19 22:44:56 +02:00
|
|
|
// TODO Track Download Event
|
2021-05-29 12:26:26 +02:00
|
|
|
handleIntentFromExternalActivity()
|
2021-06-22 08:13:30 +02:00
|
|
|
|
|
|
|
initForegroundService()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*START: Foreground Service Handlers*/
|
|
|
|
private fun initForegroundService() {
|
|
|
|
// Start and then Bind to the Service
|
|
|
|
ContextCompat.startForegroundService(
|
|
|
|
this@MainActivity,
|
|
|
|
Intent(this, ForegroundService::class.java)
|
|
|
|
)
|
|
|
|
bindService()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for getting the instance of binder from our service class
|
|
|
|
* So client can get instance of our service class and can directly communicate with it.
|
|
|
|
*/
|
|
|
|
private val serviceConnection = object : ServiceConnection {
|
|
|
|
val tag = "Service Connection"
|
|
|
|
|
|
|
|
override fun onServiceConnected(className: ComponentName, iBinder: IBinder) {
|
|
|
|
Log.d(tag, "connected to service.")
|
|
|
|
// We've bound to MyService, cast the IBinder and get MyBinder instance
|
|
|
|
val binder = iBinder as ForegroundService.DownloadServiceBinder
|
|
|
|
foregroundService = binder.service
|
|
|
|
isServiceBound = true
|
|
|
|
lifecycleScope.launch {
|
|
|
|
foregroundService?.trackStatusFlowMap?.statusFlow?.let {
|
|
|
|
trackStatusFlow.emitAll(it.conflate())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceDisconnected(arg0: ComponentName) {
|
|
|
|
Log.d(tag, "disconnected from service.")
|
|
|
|
isServiceBound = false
|
|
|
|
}
|
2021-04-29 18:14:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 08:13:30 +02:00
|
|
|
/*Used to bind to our service class*/
|
|
|
|
private fun bindService() {
|
|
|
|
Intent(this, ForegroundService::class.java).also { intent ->
|
|
|
|
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Used to unbind from our service class*/
|
|
|
|
private fun unbindService() {
|
|
|
|
Intent(this, ForegroundService::class.java).also {
|
|
|
|
unbindService(serviceConnection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*END: Foreground Service Handlers*/
|
|
|
|
|
2021-04-29 18:14:19 +02:00
|
|
|
@Composable
|
|
|
|
private fun isInternetAvailableState(): State<Boolean?> {
|
|
|
|
return internetAvailability.observeAsState()
|
2021-04-19 20:24:48 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:14:19 +02:00
|
|
|
private fun showPopUpMessage(string: String, long: Boolean = false) {
|
|
|
|
android.widget.Toast.makeText(
|
|
|
|
applicationContext,
|
|
|
|
string,
|
2021-07-13 22:45:09 +02:00
|
|
|
if (long) android.widget.Toast.LENGTH_LONG else android.widget.Toast.LENGTH_SHORT
|
2021-04-29 18:14:19 +02:00
|
|
|
).show()
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:16:20 +02:00
|
|
|
@Suppress("DEPRECATION")
|
2021-09-02 07:42:30 +02:00
|
|
|
override fun onRequestPermissionsResult(
|
|
|
|
requestCode: Int,
|
|
|
|
permissions: Array<out String>,
|
|
|
|
grantResults: IntArray
|
|
|
|
) {
|
2021-02-26 10:41:25 +01:00
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
|
permissionGranted.value = checkPermissions()
|
|
|
|
}
|
|
|
|
|
2021-02-07 17:19:48 +01:00
|
|
|
private fun spotiFlyerRoot(componentContext: ComponentContext): SpotiFlyerRoot =
|
|
|
|
SpotiFlyerRoot(
|
|
|
|
componentContext,
|
2021-07-13 22:45:09 +02:00
|
|
|
dependencies = object : SpotiFlyerRoot.Dependencies {
|
2021-02-07 17:19:48 +01:00
|
|
|
override val storeFactory = LoggingStoreFactory(DefaultStoreFactory)
|
2021-08-23 19:16:20 +02:00
|
|
|
override val database = this@MainActivity.fileManager.db
|
2021-06-23 13:13:26 +02:00
|
|
|
override val fetchQuery = this@MainActivity.fetcher
|
2021-08-23 19:16:20 +02:00
|
|
|
override val fileManager: FileManager = this@MainActivity.fileManager
|
2021-06-23 13:13:26 +02:00
|
|
|
override val preferenceManager = this@MainActivity.preferenceManager
|
2021-08-20 21:36:05 +02:00
|
|
|
override val analyticsManager: AnalyticsManager = this@MainActivity.analyticsManager
|
2021-09-02 07:42:30 +02:00
|
|
|
override val downloadProgressFlow: MutableSharedFlow<HashMap<String, DownloadStatus>> =
|
|
|
|
trackStatusFlow
|
2021-07-13 22:45:09 +02:00
|
|
|
override val actions = object : Actions {
|
2021-04-29 18:14:19 +02:00
|
|
|
|
|
|
|
override val platformActions = object : PlatformActions {
|
2021-09-02 07:42:30 +02:00
|
|
|
override val imageCacheDir: String =
|
|
|
|
applicationContext.cacheDir.absolutePath + File.separator
|
2021-07-13 22:45:09 +02:00
|
|
|
override val sharedPreferences = applicationContext.getSharedPreferences(
|
|
|
|
SharedPreferencesKey,
|
2021-04-29 18:14:19 +02:00
|
|
|
MODE_PRIVATE
|
|
|
|
)
|
|
|
|
|
|
|
|
override fun addToLibrary(path: String) {
|
2021-07-13 22:45:09 +02:00
|
|
|
MediaScannerConnection.scanFile(
|
2021-04-29 18:14:19 +02:00
|
|
|
applicationContext,
|
|
|
|
listOf(path).toTypedArray(), null, null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-22 08:13:30 +02:00
|
|
|
override fun sendTracksToService(array: List<TrackDetails>) {
|
2021-08-23 19:16:20 +02:00
|
|
|
for (chunk in array.chunked(25)) {
|
|
|
|
if (foregroundService == null) initForegroundService()
|
|
|
|
foregroundService?.downloadAllTracks(array)
|
|
|
|
}
|
2021-04-29 18:14:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:44:56 +02:00
|
|
|
override fun showPopUpMessage(string: String, long: Boolean) =
|
|
|
|
this@MainActivity.showPopUpMessage(string, long)
|
2021-04-29 18:14:19 +02:00
|
|
|
|
2021-08-19 22:44:56 +02:00
|
|
|
override fun setDownloadDirectoryAction(callBack: (String) -> Unit) =
|
|
|
|
setUpOnPrefClickListener(callBack)
|
2021-04-29 18:14:19 +02:00
|
|
|
|
2021-06-21 21:00:28 +02:00
|
|
|
override fun queryActiveTracks() = this@MainActivity.queryActiveTracks()
|
2021-04-29 18:14:19 +02:00
|
|
|
|
2021-05-08 17:12:09 +02:00
|
|
|
override fun giveDonation() {
|
2021-08-19 22:44:56 +02:00
|
|
|
openPlatform(
|
|
|
|
"",
|
|
|
|
platformLink = "https://razorpay.com/payment-button/pl_GnKuuDBdBu0ank/view/?utm_source=payment_button&utm_medium=button&utm_campaign=payment_button"
|
|
|
|
)
|
2021-05-08 17:12:09 +02:00
|
|
|
}
|
2021-04-29 18:14:19 +02:00
|
|
|
|
|
|
|
override fun shareApp() {
|
|
|
|
val sendIntent: Intent = Intent().apply {
|
|
|
|
action = Intent.ACTION_SEND
|
2021-06-25 21:58:00 +02:00
|
|
|
putExtra(Intent.EXTRA_TEXT, Strings.shareMessage())
|
2021-04-29 18:14:19 +02:00
|
|
|
type = "text/plain"
|
|
|
|
}
|
|
|
|
|
|
|
|
val shareIntent = Intent.createChooser(sendIntent, null)
|
|
|
|
startActivity(shareIntent)
|
|
|
|
}
|
|
|
|
|
2021-07-10 07:27:18 +02:00
|
|
|
override fun copyToClipboard(text: String) {
|
|
|
|
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("SpotiFlyer Selection", text)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
|
2021-07-10 09:29:34 +02:00
|
|
|
showPopUpMessage("Text Copied to Clipboard.")
|
2021-07-10 07:27:18 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:14:19 +02:00
|
|
|
override fun openPlatform(packageID: String, platformLink: String) {
|
|
|
|
val manager: PackageManager = applicationContext.packageManager
|
|
|
|
try {
|
|
|
|
val intent = manager.getLaunchIntentForPackage(packageID)
|
|
|
|
?: throw PackageManager.NameNotFoundException()
|
|
|
|
intent.addCategory(Intent.CATEGORY_LAUNCHER)
|
|
|
|
startActivity(intent)
|
|
|
|
} catch (e: PackageManager.NameNotFoundException) {
|
|
|
|
val uri: Uri =
|
|
|
|
Uri.parse(platformLink)
|
|
|
|
val intent = Intent(Intent.ACTION_VIEW, uri)
|
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 21:36:05 +02:00
|
|
|
override fun writeMp3Tags(trackDetails: TrackDetails) {
|
|
|
|
/*IMPLEMENTED*/
|
2021-08-19 22:44:56 +02:00
|
|
|
}
|
2021-05-04 12:44:03 +02:00
|
|
|
|
2021-07-13 22:45:09 +02:00
|
|
|
override val isInternetAvailable get() = internetAvailability.value ?: true
|
2021-04-29 18:14:19 +02:00
|
|
|
}
|
2021-02-07 17:19:48 +01:00
|
|
|
}
|
|
|
|
)
|
2021-02-10 18:25:36 +01:00
|
|
|
|
2021-06-21 21:00:28 +02:00
|
|
|
private fun queryActiveTracks() {
|
2021-06-22 08:13:30 +02:00
|
|
|
lifecycleScope.launch {
|
|
|
|
foregroundService?.trackStatusFlowMap?.let { tracksStatus ->
|
|
|
|
trackStatusFlow.emit(tracksStatus)
|
|
|
|
}
|
2021-06-21 21:00:28 +02:00
|
|
|
}
|
2021-06-22 08:13:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
queryActiveTracks()
|
2021-06-21 21:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
2021-07-13 22:45:09 +02:00
|
|
|
private fun setUpOnPrefClickListener(callBack: (String) -> Unit) {
|
2021-06-21 21:00:28 +02:00
|
|
|
// Initialize Builder
|
|
|
|
val chooser = StorageChooser.Builder()
|
|
|
|
.withActivity(this)
|
|
|
|
.withFragmentManager(fragmentManager)
|
|
|
|
.withMemoryBar(true)
|
2021-07-13 22:45:09 +02:00
|
|
|
.setTheme(
|
|
|
|
StorageChooser.Theme(applicationContext).apply {
|
|
|
|
scheme = applicationContext.resources.getIntArray(R.array.default_dark)
|
|
|
|
}
|
|
|
|
)
|
2021-06-25 21:58:00 +02:00
|
|
|
.setDialogTitle(Strings.setDownloadDirectory())
|
2021-06-21 21:00:28 +02:00
|
|
|
.allowCustomPath(true)
|
|
|
|
.setType(StorageChooser.DIRECTORY_CHOOSER)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
// get path that the user has chosen
|
|
|
|
chooser.setOnSelectListener { path ->
|
|
|
|
Log.d("Setting Base Path", path)
|
|
|
|
val f = File(path)
|
|
|
|
if (f.canWrite()) {
|
|
|
|
// hell yeah :)
|
2021-06-23 13:13:26 +02:00
|
|
|
preferenceManager.setDownloadDirectory(path)
|
2021-07-10 22:50:31 +02:00
|
|
|
callBack(path)
|
2021-08-23 19:16:20 +02:00
|
|
|
showPopUpMessage(Strings.downloadDirectorySetTo("\n${fileManager.defaultDir()}"))
|
2021-07-13 22:45:09 +02:00
|
|
|
} else {
|
2021-06-25 21:58:00 +02:00
|
|
|
showPopUpMessage(Strings.noWriteAccess("\n$path "))
|
2021-06-21 21:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show dialog whenever you want by
|
|
|
|
chooser.show()
|
|
|
|
}
|
2021-02-10 18:25:36 +01:00
|
|
|
|
2021-08-23 19:16:20 +02:00
|
|
|
@Suppress("DEPRECATION")
|
2021-02-10 18:25:36 +01:00
|
|
|
@SuppressLint("ObsoleteSdkInt")
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
if (requestCode == disableDozeCode) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
val pm =
|
|
|
|
getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
|
|
val isIgnoringBatteryOptimizations =
|
|
|
|
pm.isIgnoringBatteryOptimizations(packageName)
|
|
|
|
if (isIgnoringBatteryOptimizations) {
|
|
|
|
// Ignoring battery optimization
|
2021-02-25 16:23:10 +01:00
|
|
|
permissionGranted.value = true
|
2021-02-10 18:25:36 +01:00
|
|
|
} else {
|
2021-07-13 22:45:09 +02:00
|
|
|
disableDozeMode(disableDozeCode) // Again Ask For Permission!!
|
2021-02-10 18:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onNewIntent(intent: Intent?) {
|
|
|
|
super.onNewIntent(intent)
|
|
|
|
handleIntentFromExternalActivity(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handleIntentFromExternalActivity(intent: Intent? = getIntent()) {
|
|
|
|
if (intent?.action == Intent.ACTION_SEND) {
|
|
|
|
if ("text/plain" == intent.type) {
|
|
|
|
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
|
|
|
|
val filterLinkRegex = """http.+\w""".toRegex()
|
|
|
|
val string = it.replace("\n".toRegex(), " ")
|
|
|
|
val link = filterLinkRegex.find(string)?.value.toString()
|
2021-07-13 22:45:09 +02:00
|
|
|
Log.i("Intent", link)
|
2021-02-26 11:29:10 +01:00
|
|
|
lifecycleScope.launch {
|
2021-07-13 22:45:09 +02:00
|
|
|
while (!this@MainActivity::root.isInitialized) {
|
2021-02-26 11:29:10 +01:00
|
|
|
delay(100)
|
|
|
|
}
|
2021-08-19 22:44:56 +02:00
|
|
|
if (methods.value.isInternetAvailable) callBacks.searchLink(link)
|
2021-02-26 11:29:10 +01:00
|
|
|
}
|
2021-02-10 18:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 23:21:33 +02:00
|
|
|
|
2021-06-22 08:13:30 +02:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
unbindService()
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:44:56 +02:00
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
analyticsManager.onStart()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStop() {
|
|
|
|
super.onStop()
|
|
|
|
analyticsManager.onStop()
|
|
|
|
}
|
|
|
|
|
2021-05-13 23:21:33 +02:00
|
|
|
companion object {
|
|
|
|
const val disableDozeCode = 1223
|
|
|
|
}
|
2021-02-07 17:19:48 +01:00
|
|
|
}
|