-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathApp.kt
More file actions
287 lines (257 loc) · 11.4 KB
/
Copy pathApp.kt
File metadata and controls
287 lines (257 loc) · 11.4 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package org.nekomanga
import android.Manifest
import android.annotation.SuppressLint
import android.app.Application
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import android.os.Looper
import android.webkit.CookieManager
import android.webkit.WebView
import android.widget.Toast
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.multidex.MultiDex
import coil3.SingletonImageLoader
import eu.kanade.tachiyomi.AppModule
import eu.kanade.tachiyomi.PreferenceModule
import eu.kanade.tachiyomi.crash.CrashActivity
import eu.kanade.tachiyomi.crash.GlobalExceptionHandler
import eu.kanade.tachiyomi.data.coil.coilImageLoader
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.library.LibraryViewModel
import eu.kanade.tachiyomi.ui.main.DeepLinks
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate
import eu.kanade.tachiyomi.util.manga.MangaCoverMetadata
import eu.kanade.tachiyomi.util.system.AuthenticatorUtil
import eu.kanade.tachiyomi.util.system.notification
import java.security.Security
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.conscrypt.Conscrypt
import org.nekomanga.core.network.NetworkPreferences
import org.nekomanga.core.security.SecurityPreferences
import org.nekomanga.domain.site.MangaDexPreferences
import org.nekomanga.logging.CrashReportingTree
import org.nekomanga.logging.DebugReportingTree
import org.nekomanga.logging.TimberKt
import org.nekomanga.presentation.screens.feed.FeedViewModel
import tachiyomi.core.util.system.WebViewUtil
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.injectLazy
private const val ACTION_DISABLE_INCOGNITO_MODE = "tachi.action.DISABLE_INCOGNITO_MODE"
open class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.Factory {
val preferences: PreferencesHelper by injectLazy()
val networkPreferences: NetworkPreferences by injectLazy()
val securityPreferences: SecurityPreferences by injectLazy()
val mangaDexPreferences: MangaDexPreferences by injectLazy()
private val disableIncognitoReceiver = DisableIncognitoReceiver()
@SuppressLint("LaunchActivityFromNotification")
override fun onCreate() {
super<Application>.onCreate()
GlobalExceptionHandler.initialize(applicationContext, CrashActivity::class.java)
// TLS 1.3 support for Android < 10
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Security.insertProviderAt(Conscrypt.newProvider(), 1)
}
// Avoid potential crashes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val process = getProcessName()
if (packageName != process) {
runCatching { WebView.setDataDirectorySuffix(process) }
}
}
Injekt.importModule(PreferenceModule(this))
Injekt.importModule(AppModule(this))
if (!BuildConfig.DEBUG) {
TimberKt.plant(CrashReportingTree())
}
// also plant a debug tree in prod if enabled
if (BuildConfig.DEBUG || networkPreferences.verboseLogging().get()) {
TimberKt.plant(DebugReportingTree())
}
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
ProcessLifecycleOwner.get().lifecycleScope.launch(Dispatchers.Default) {
runCatching { CookieManager.getInstance() }
.onFailure {
withContext(Dispatchers.Main) {
Toast.makeText(
applicationContext,
getString(R.string.information_webview_required),
Toast.LENGTH_LONG,
)
.show()
}
}
setupNotificationChannels()
MangaCoverMetadata.load()
}
preferences
.nightMode()
.changes()
.onEach { AppCompatDelegate.setDefaultNightMode(it) }
.launchIn((ProcessLifecycleOwner.get().lifecycleScope))
// Show notification to disable Incognito Mode when it's enabled
securityPreferences
.incognitoMode()
.changes()
.onEach { enabled ->
val notificationManager = NotificationManagerCompat.from(this)
if (enabled) {
disableIncognitoReceiver.register()
val notification =
notification(Notifications.CHANNEL_INCOGNITO_MODE) {
val incogText = getString(R.string.incognito_mode)
setContentTitle(incogText)
setContentText(getString(R.string.turn_off_, incogText))
setSmallIcon(R.drawable.ic_incognito_24dp)
setOngoing(true)
val pendingIntent =
PendingIntent.getBroadcast(
this@App,
0,
Intent(ACTION_DISABLE_INCOGNITO_MODE),
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE,
)
setContentIntent(pendingIntent)
}
if (
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS,
) != PackageManager.PERMISSION_GRANTED
) {
return@onEach
}
notificationManager.notify(Notifications.ID_INCOGNITO_MODE, notification)
} else {
disableIncognitoReceiver.unregister()
notificationManager.cancel(Notifications.ID_INCOGNITO_MODE)
}
}
.launchIn(ProcessLifecycleOwner.get().lifecycleScope)
// Show notification when the user has been unexpectedly signed out of MangaDex
// (auth refresh rejected). The flag is set in MangaDexLoginHelper and cleared on
// successful login or manual logout.
mangaDexPreferences
.unexpectedLogout()
.changes()
.distinctUntilChanged()
.onEach { unexpected ->
val notificationManager = NotificationManagerCompat.from(this)
if (unexpected) {
if (
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS,
) != PackageManager.PERMISSION_GRANTED
) {
return@onEach
}
val tapIntent =
Intent(this, MainActivity::class.java).apply {
action = DeepLinks.Actions.MangaDexSettings
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntent =
PendingIntent.getActivity(
this,
Notifications.Id.Authentication.SessionExpired,
tapIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
val body = getString(R.string.mangadex_session_expired_body)
val notification =
notification(Notifications.Channel.Authentication) {
setContentTitle(getString(R.string.mangadex_session_expired_title))
setContentText(body)
setStyle(NotificationCompat.BigTextStyle().bigText(body))
setSmallIcon(R.drawable.ic_neko_notification)
setContentIntent(pendingIntent)
setAutoCancel(true)
priority = NotificationCompat.PRIORITY_HIGH
}
notificationManager.notify(
Notifications.Id.Authentication.SessionExpired,
notification,
)
} else {
notificationManager.cancel(Notifications.Id.Authentication.SessionExpired)
}
}
.launchIn(ProcessLifecycleOwner.get().lifecycleScope)
}
override fun onPause(owner: LifecycleOwner) {
if (!AuthenticatorUtil.isAuthenticating && securityPreferences.lockAfter().get() >= 0) {
SecureActivityDelegate.locked = true
}
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
override fun onLowMemory() {
super.onLowMemory()
LibraryViewModel.onLowMemory()
FeedViewModel.onLowMemory()
}
override fun getPackageName(): String {
try {
// Override the value passed as X-Requested-With in WebView requests
val stackTrace = Looper.getMainLooper().thread.stackTrace
val isChromiumCall = stackTrace.any { trace ->
trace.className.lowercase() in
setOf("org.chromium.base.buildinfo", "org.chromium.base.apkinfo") &&
trace.methodName.lowercase() in setOf("getall", "getpackagename", "<init>")
}
if (isChromiumCall) return WebViewUtil.spoofedPackageName(applicationContext)
} catch (e: Exception) {
TimberKt.e(e) { "Failed to spoof package name" }
}
return super.getPackageName()
}
override fun newImageLoader(context: Context) = coilImageLoader(context)
protected open fun setupNotificationChannels() {
Notifications.createChannels(this)
}
private inner class DisableIncognitoReceiver : BroadcastReceiver() {
private var registered = false
override fun onReceive(context: Context, intent: Intent) {
securityPreferences.incognitoMode().set(false)
}
fun register() {
if (!registered) {
ContextCompat.registerReceiver(
this@App,
this,
IntentFilter(ACTION_DISABLE_INCOGNITO_MODE),
ContextCompat.RECEIVER_EXPORTED,
)
registered = true
}
}
fun unregister() {
if (registered) {
unregisterReceiver(this)
registered = false
}
}
}
}