forked from saschpe/android-exoplayer2-ext-icy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaxi80Application.kt
More file actions
200 lines (150 loc) · 6.8 KB
/
Maxi80Application.kt
File metadata and controls
200 lines (150 loc) · 6.8 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.stormacq.android.maxi80
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.util.Log
import com.amazonaws.amplify.generated.graphql.StationQuery
import com.amazonaws.auth.CognitoCachingCredentialsProvider
import com.amazonaws.mobile.config.AWSConfiguration
import com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient
import com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers
import com.amazonaws.regions.Regions
import com.apollographql.apollo.GraphQLCall
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.exception.ApolloException
import okhttp3.OkHttpClient
import java.security.KeyStore
import java.util.*
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
class Maxi80Application : Application() {
lateinit var station : StationQuery.Station
lateinit var appSyncClient: AWSAppSyncClient
var currentArtist : String = ""
private set
var currentTrack : String = ""
private set
// I know, I know, this should be a list and I should implement register(), unregister() methods
var metaDataChangedListener : MetaDataListener? = null
var isPlaying = false
/**************************************************************************
*
* Lifecycle
*
*************************************************************************/
override fun onCreate() {
Log.d(TAG, "onCreate")
super.onCreate()
val primaryLocale: Locale
//check locale for debugging
if (Build.VERSION.SDK_INT >= 24) {
primaryLocale = applicationContext.resources.configuration.locales[0]
} else {
@Suppress("DEPRECATION")
primaryLocale = applicationContext.resources.configuration.locale
}
Log.d(TAG, "onCreate, locale is $primaryLocale")
// default value for Station
station = StationQuery.Station("Station",
resources.getString(R.string.app_name),
resources.getString(R.string.app_url),
"",
resources.getString(R.string.app_description),
resources.getString(R.string.app_description),
resources.getString(R.string.website_url),
resources.getString(R.string.donation_url))
prepareAppSync()
queryRadioData()
prepareNotificationChannel()
}
private fun prepareAppSync() {
// Initialize the Amazon Cognito credentials provider
val credentialsProvider = CognitoCachingCredentialsProvider(
applicationContext,
"eu-west-1:74b938b1-4a81-43ed-a4de-86b37001110a", // Identity pool ID
Regions.EU_WEST_1 // Region
)
// initialize the AppSync client
val builder = AWSAppSyncClient.builder()
.context(applicationContext)
.awsConfiguration(AWSConfiguration(applicationContext))
.credentialsProvider(credentialsProvider)
if (Build.VERSION.SDK_INT <= MINIMUM_SDK_FEATURES) {
// The below is required on Android API <= 20 to enable TLSv1.1 and TLSv1.2 (TLSv1.0 and SSLv3 are not supported by AppSync)
// https://stackoverflow.com/questions/29249630/android-enable-tlsv1-2-in-okhttp
// https://github.com/square/okhttp/issues/1934
// https://stackoverflow.com/questions/31002159/now-that-sslsocketfactory-is-deprecated-on-android-what-would-be-the-best-way-t
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
tmf.init(null as KeyStore?)
val tms = tmf.trustManagers
if (tms.size != 1 || tms[0] !is X509TrustManager) {
throw IllegalStateException("Unexpected default trust managers: $tms")
}
val tm = tms[0] as X509TrustManager
val okHTTPClient = OkHttpClient.Builder().sslSocketFactory(TLSSocketFactory(), tm).build()
// use our specific OKHTTP Client for Android <= 20
builder.okHttpClient(okHTTPClient) // for android <= 20
}
appSyncClient = builder.build()
}
private fun prepareNotificationChannel() {
if (Build.VERSION.SDK_INT >= 26) {
val serviceChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
resources.getString(R.string.app_name),
NotificationManager.IMPORTANCE_LOW)
val manager : NotificationManager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(serviceChannel)
}
}
private fun queryRadioData() {
// query radio data
appSyncClient.query(StationQuery.builder().build())
.responseFetcher(AppSyncResponseFetchers.NETWORK_ONLY)
.enqueue(object : GraphQLCall.Callback<StationQuery.Data>() {
override fun onResponse(response: Response<StationQuery.Data>) {
Log.d(TAG, "StationQuery returned : " + response.data().toString())
station = response.data()?.station() as StationQuery.Station
setTrack(station.name(), station.desc())
}
override fun onFailure(e: ApolloException) {
Log.e(TAG, "Failed to perform StationQuery", e)
// default value is set already, let's use that one
// inform customer we had a failure
metaDataChangedListener?.onError(e)
}
})
}
/**************************************************************************
*
* Meta Data
*
*************************************************************************/
fun setTrack(artist: String?, track: String?) {
currentArtist = artist ?: station.name()
currentTrack = track ?: station.desc()
metaDataChangedListener?.onCurrentTrackChanged(currentArtist, currentTrack)
}
// callback to receive metadata
fun handleiCyMetaData(metadata : String) {
var data = metadata.split(" - ")
Log.d(TAG, data.toString())
if (data.size == 2) {
setTrack(data[0], data[1])
} else {
data = metadata.split("-") // try without space
Log.d(TAG, data.toString())
if (data.size == 2) {
setTrack(data[0], data[1])
} else {
setTrack(null, metadata)
}
}
}
companion object {
private const val TAG = "Maxi80_Application"
const val NOTIFICATION_CHANNEL_ID = "com.stormacq.android.maxi80"
const val MINIMUM_SDK_FEATURES = 20
}
}