-
Notifications
You must be signed in to change notification settings - Fork 3
/
NaviToTeslaService.kt
273 lines (256 loc) · 10.4 KB
/
NaviToTeslaService.kt
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package me.zipi.navitotesla.service
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.google.firebase.perf.metrics.AddTrace
import me.zipi.navitotesla.AppRepository
import me.zipi.navitotesla.R
import me.zipi.navitotesla.db.AppDatabase
import me.zipi.navitotesla.exception.DuplicatePoiException
import me.zipi.navitotesla.exception.ForbiddenException
import me.zipi.navitotesla.exception.IgnorePoiException
import me.zipi.navitotesla.exception.NotSupportedNaviException
import me.zipi.navitotesla.model.TeslaApiResponse
import me.zipi.navitotesla.model.TeslaRefreshTokenRequest
import me.zipi.navitotesla.model.Token
import me.zipi.navitotesla.model.Vehicle
import me.zipi.navitotesla.service.poifinder.PoiFinderFactory
import me.zipi.navitotesla.service.share.TeslaShareByApi
import me.zipi.navitotesla.service.share.TeslaShareByApp
import me.zipi.navitotesla.util.AnalysisUtil
import me.zipi.navitotesla.util.EnablerUtil
import me.zipi.navitotesla.util.PreferencesUtil
import me.zipi.navitotesla.util.ResponseCloser
import retrofit2.Response
import java.io.IOException
import java.util.regex.Pattern
class NaviToTeslaService(context: Context) {
private val context: Context
private val pattern =
Pattern.compile("^(?:[가-힣]+\\s[가-힣]+[시군구]|(?:세종시|세종특별시|세종특별자치시)\\s[가-힣\\d]+[읍면동로])\\s")
private val appRepository: AppRepository
init {
this.context = context.applicationContext
appRepository = AppRepository.getInstance(
this.context,
AppDatabase.getInstance(this.context)
)
}
fun isAddress(text: String): Boolean {
return pattern.matcher(text).find()
}
private fun makeToast(text: String) {
try {
Log.i(this.javaClass.name, text)
AnalysisUtil.log(text)
Handler(Looper.getMainLooper()).post {
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
}
} catch (e: Exception) {
AnalysisUtil.recordException(e)
e.printStackTrace()
}
}
/**
* 안내 종료
*/
fun notificationClear() {
PreferencesUtil.put(context, "lastAddress", "")
}
@AddTrace(name = "share")
fun share(packageName: String, notificationTitle: String?, notificationText: String?) {
if (!EnablerUtil.isSendingCheck(context)) {
AnalysisUtil.log("skip send share because condition")
return
}
AnalysisUtil.setCustomKey("packageName", packageName)
AnalysisUtil.setCustomKey("notificationTitle", notificationTitle ?: "")
AnalysisUtil.setCustomKey("notificationText", notificationText ?: "")
val eventParam = Bundle()
eventParam.putString("package", packageName)
try {
val address = getAddress(packageName, notificationTitle, notificationText)
val lastAddress = PreferencesUtil.getString(context, "lastAddress", "")
if (lastAddress != address) {
try {
share(address)
} catch (e: ForbiddenException) {
AnalysisUtil.log("force expire token and retry...")
expireToken()
share(address)
}
} else {
// 마지막 전송 주소와 동일
// makeToast("목적지 전송 무시\n이전에 전송 요청한 주소와 동일함.");
AnalysisUtil.logEvent("previous_request_address", eventParam)
}
appRepository.clearExpiredPoi()
} catch (e: DuplicatePoiException) {
AnalysisUtil.logEvent("duplicated_address", eventParam)
AnalysisUtil.log("duplicate poi name: " + e.poiName)
makeToast(context.getString(R.string.sendDestinationFail) + "\n" + context.getString(R.string.duplicatedPoiName))
} catch (e: NotSupportedNaviException) {
AnalysisUtil.logEvent("unsupported_navi", eventParam)
AnalysisUtil.recordException(e)
makeToast(context.getString(R.string.sendDestinationFail) + "\n" + context.getString(R.string.unsupportedNavi))
} catch (e: IgnorePoiException) {
AnalysisUtil.logEvent("ignore_address", eventParam)
} catch (e: ForbiddenException) {
makeToast(context.getString(R.string.sendDestinationFail) + "\n" + context.getString(R.string.authFail))
AnalysisUtil.logEvent("error_share", eventParam)
AnalysisUtil.recordException(e)
} catch (e: Exception) {
Log.e(NaviToTeslaService::class.java.name, "thread inside error", e)
makeToast(context.getString(R.string.sendDestinationFail) + "\n" + context.getString(R.string.apiError))
AnalysisUtil.logEvent("error_share", eventParam)
AnalysisUtil.recordException(e)
AnalysisUtil.sendUnsentReports()
}
}
@Throws(IOException::class, ForbiddenException::class)
fun share(address: String) {
PreferencesUtil.put(context, "lastAddress", address)
if (address.isNotEmpty()) {
makeToast(context.getString(R.string.requestSend) + "\n" + address)
val shareMode = PreferencesUtil.getString(context, "shareMode", "app")
if (shareMode == "api" && PreferencesUtil.loadToken(context) != null) {
if (refreshToken() == null) {
return
}
val id = loadVehicleId()
TeslaShareByApi(context, id).share(address)
} else {
TeslaShareByApp(context).share(address)
}
}
}
@AddTrace(name = "getAddress")
@Throws(
NotSupportedNaviException::class,
DuplicatePoiException::class,
IgnorePoiException::class,
IOException::class
)
private fun getAddress(
packageName: String,
notificationTitle: String?,
notificationText: String?
): String {
val eventParam = Bundle()
eventParam.putString("package", packageName)
val poiFinder = PoiFinderFactory.getPoiFinder(packageName)
val poiName = poiFinder.parseDestination(notificationText ?: "")
if (poiName.isEmpty() || poiFinder.isIgnore(
notificationTitle ?: "",
notificationText ?: ""
)
) {
AnalysisUtil.logEvent("address_ignore_or_not_found", eventParam)
throw IgnorePoiException(packageName)
}
val poiAddressEntity = appRepository.getPoiSync(poiName)
// 10 days cache
val address: String?
if (poiAddressEntity != null && !poiAddressEntity.isExpire) {
address = poiAddressEntity.address
AnalysisUtil.logEvent("address_parse_cache", eventParam)
} else if (isAddress(poiName)) {
address = poiName
AnalysisUtil.logEvent("address_direct", eventParam)
} else {
address = poiFinder.findPoiAddress(poiName)
AnalysisUtil.logEvent("address_parse_api", eventParam)
appRepository.savePoi(poiName, address, false)
}
return address
}
val token: Token?
get() = PreferencesUtil.loadToken(context)
fun expireToken() {
PreferencesUtil.expireToken(context)
}
@AddTrace(name = "refreshToken")
fun refreshToken(): Token? {
return this.refreshToken(null)
}
fun refreshToken(refreshToken: String?): Token? {
var actualRefreshToken = refreshToken
if (refreshToken == null) {
val token = PreferencesUtil.loadToken(context)
if (token == null) {
makeToast(context.getString(R.string.notExistsToken))
return null
} else if (!token.isExpire()) {
return token
} else {
actualRefreshToken = token.refreshToken
}
}
AnalysisUtil.log("Start refresh access token")
var token: Token? = null
try {
val newToken: Response<Token?> = appRepository.teslaAuthApi
.refreshAccessToken(TeslaRefreshTokenRequest(actualRefreshToken!!)).execute()
if (newToken.isSuccessful && newToken.body() != null) {
PreferencesUtil.saveToken(context, newToken.body()!!)
token = newToken.body()
AnalysisUtil.log("Success refresh access token")
}
ResponseCloser.closeAll(newToken)
} catch (e: Exception) {
Log.w(this.javaClass.name, "refresh token fail", e)
makeToast(context.getString(R.string.refreshTokenError))
AnalysisUtil.recordException(e)
}
if (token == null) {
AnalysisUtil.log("fail refresh access token. token is null")
}
return token
}
@AddTrace(name = "getVehicles")
fun getVehicles(token: Token?): List<Vehicle> {
var actualToken = token
if (token == null) {
actualToken = PreferencesUtil.loadToken(context)
}
if (actualToken?.refreshToken == null) {
makeToast(context.getString(R.string.requireToken))
return ArrayList()
}
var vehicles: List<Vehicle> = ArrayList()
try {
if (refreshToken() == null) {
return vehicles
}
val response: Response<TeslaApiResponse.ListType<Vehicle>?> =
appRepository.teslaApi.vehicles().execute()
if (response.code() == 401) {
makeToast(context.getString(R.string.invalidToken))
} else if (response.isSuccessful && response.body() != null) {
vehicles = response.body()!!.response
} else {
Log.w(this.javaClass.name, "get vehicle error: $response")
}
ResponseCloser.closeAll(response)
} catch (e: Exception) {
Log.w(this.javaClass.name, "get vehicle error", e)
AnalysisUtil.recordException(e)
}
if (vehicles.isEmpty()) {
makeToast(context.getString(R.string.noVehicle))
}
return vehicles
}
fun saveVehicleId(id: Long) {
PreferencesUtil.put(context, "vehicleId", id)
}
fun loadVehicleId(): Long {
return PreferencesUtil.getLong(context, "vehicleId", 0L)
}
fun clearPoiCache() {
appRepository.clearAllPoi()
}
}