-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMainActivity.kt
More file actions
128 lines (101 loc) · 4.03 KB
/
MainActivity.kt
File metadata and controls
128 lines (101 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.simplecityapps.shuttle.ui
import android.Manifest
import android.app.ForegroundServiceStartNotAllowedException
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.navigation.fragment.NavHostFragment
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.simplecityapps.playback.PlaybackService
import com.simplecityapps.shuttle.R
import com.simplecityapps.shuttle.di.AppCoroutineScope
import com.simplecityapps.shuttle.persistence.GeneralPreferenceManager
import com.simplecityapps.shuttle.ui.common.view.SnowfallView
import com.simplecityapps.trial.BillingManager
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withTimeout
import timber.log.Timber
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var preferenceManager: GeneralPreferenceManager
@Inject
lateinit var themeManager: ThemeManager
@Inject
lateinit var billingManager: BillingManager
@Inject
lateinit var remoteConfig: FirebaseRemoteConfig
@Inject
@AppCoroutineScope
lateinit var scope: CoroutineScope
var snowfallView: SnowfallView? = null
// Lifecycle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
themeManager.setTheme(this)
setContentView(R.layout.activity_main)
val navHost = supportFragmentManager.findFragmentById(R.id.onboardingNavHostFragment) as NavHostFragment
val navController = navHost.navController
val navInflater = navController.navInflater
val graph = navInflater.inflate(R.navigation.launch)
if (!preferenceManager.hasOnboarded || !hasStoragePermission()) {
graph.setStartDestination(R.id.onboardingFragment)
} else {
graph.setStartDestination(R.id.mainFragment)
}
navController.graph = graph
handleSearchQuery(intent)
billingManager.queryPurchases()
snowfallView = findViewById(R.id.snowfallView)
scope.launch {
withTimeout(5000) {
remoteConfig.fetchAndActivate().await()
}
snowfallView?.setForecast(remoteConfig.getDouble("snow_forecast"))
}
}
override fun onResume() {
super.onResume()
billingManager.queryPurchases()
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleSearchQuery(intent)
}
// Private
private fun hasStoragePermission(): Boolean = (ContextCompat.checkSelfPermission(this, getStoragePermission()) == PackageManager.PERMISSION_GRANTED)
private fun getStoragePermission(): String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Manifest.permission.READ_MEDIA_AUDIO
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
}
private fun handleSearchQuery(intent: Intent?) {
if (intent?.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) {
try {
ContextCompat.startForegroundService(
this,
Intent(this, PlaybackService::class.java).apply {
action = PlaybackService.ACTION_SEARCH
intent.extras?.let { extras ->
putExtras(extras)
}
}
)
} catch (e: IllegalStateException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && e is ForegroundServiceStartNotAllowedException) {
Timber.w(e, "Cannot start foreground service from search query - app may be in restricted state")
} else {
throw e
}
}
}
}
}