Change requestNoBatteryOptimizations to less annoying snackbar design

This commit is contained in:
Niels van Velzen
2020-09-05 12:07:26 +02:00
parent 02f2993b54
commit b6265cb6dc
6 changed files with 30 additions and 24 deletions
+1
View File
@@ -76,6 +76,7 @@ dependencies {
implementation(Dependencies.UI.webkitX)
implementation(Dependencies.UI.exoPlayer)
implementation(Dependencies.UI.modernAndroidPreferences)
implementation(Dependencies.UI.material)
// Network
implementation(Dependencies.Network.okHttp)
@@ -11,12 +11,10 @@ import android.os.Bundle
import android.os.IBinder
import android.view.OrientationEventListener
import android.webkit.*
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.doOnNextLayout
import androidx.core.view.updatePadding
import androidx.core.view.*
import androidx.lifecycle.lifecycleScope
import androidx.webkit.WebResourceErrorCompat
import androidx.webkit.WebViewClientCompat
@@ -50,7 +48,7 @@ class MainActivity : AppCompatActivity(), WebViewController {
private val connectionHelper = ConnectionHelper(this)
private val webappFunctionChannel: Channel<String> by inject(named(WEBAPP_FUNCTION_CHANNEL))
val rootView: FrameLayout by lazyView(R.id.root_view)
val rootView: CoordinatorLayout by lazyView(R.id.root_view)
val webView: WebView by lazyView(R.id.web_view)
var serviceBinder: RemotePlayerService.ServiceBinder? = null
@@ -70,7 +68,7 @@ class MainActivity : AppCompatActivity(), WebViewController {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webapp)
setContentView(R.layout.activity_main)
// Bind player service
bindService(Intent(this, RemotePlayerService::class.java), serviceConnection, Service.BIND_AUTO_CREATE)
@@ -78,7 +76,9 @@ class MainActivity : AppCompatActivity(), WebViewController {
// Handle window insets
setStableLayoutFlags()
ViewCompat.setOnApplyWindowInsetsListener(rootView) { v, insets ->
v.updatePadding(top = insets.systemWindowInsetTop, bottom = insets.systemWindowInsetBottom)
val layoutParams = webView.layoutParams as CoordinatorLayout.LayoutParams
layoutParams.updateMargins(top = insets.systemWindowInsetTop, bottom = insets.systemWindowInsetBottom)
insets
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@@ -13,11 +13,13 @@ import android.provider.Settings
import android.provider.Settings.System.ACCELEROMETER_ROTATION
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import org.jellyfin.mobile.BuildConfig
import org.jellyfin.mobile.MainActivity
import org.jellyfin.mobile.R
import timber.log.Timber
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@@ -25,21 +27,22 @@ fun MainActivity.requestNoBatteryOptimizations() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val powerManager: PowerManager = getSystemService(AppCompatActivity.POWER_SERVICE) as PowerManager
if (!appPreferences.ignoreBatteryOptimizations && !powerManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID)) {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.battery_optimizations_title))
builder.setMessage(getString(R.string.battery_optimizations_message))
builder.setNegativeButton(android.R.string.cancel) { _, _ ->
appPreferences.ignoreBatteryOptimizations = true
}
builder.setPositiveButton(android.R.string.ok) { _, _ ->
try {
val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
Snackbar.make(rootView, R.string.battery_optimizations_message, Snackbar.LENGTH_INDEFINITE).apply {
// Add action
setAction(android.R.string.ok) {
try {
val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (e: Exception) {
Timber.e(e)
}
// Ignore after the user interacted with the snackbar at least once
appPreferences.ignoreBatteryOptimizations = true
}
show()
}
builder.show()
}
}
}
@@ -7,9 +7,9 @@ import android.view.inputmethod.InputMethodManager
import android.webkit.WebView
import android.widget.Button
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.getSystemService
import androidx.core.view.doOnNextLayout
import androidx.core.view.isVisible
@@ -42,7 +42,7 @@ class ConnectionHelper(private val activity: MainActivity) : KoinComponent {
private val appPreferences: AppPreferences get() = activity.appPreferences
private val jellyfin: Jellyfin by inject()
private val apiClient: ApiClient get() = activity.apiClient
private val rootView: FrameLayout get() = activity.rootView
private val rootView: CoordinatorLayout get() = activity.rootView
private val webView: WebView get() = activity.webView
private var cachedInstanceUrl: HttpUrl? = null
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_view"
android:layout_width="match_parent"
@@ -11,4 +11,4 @@
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
+2
View File
@@ -28,6 +28,7 @@ object Dependencies {
const val webkitX = "1.2.0"
const val coil = "0.11.0"
const val modernAndroidPreferences = "1.0-RC2"
const val material = "1.2.1"
// Cast
const val mediaRouter = "1.1.0"
@@ -79,6 +80,7 @@ object Dependencies {
val webkitX = androidx("webkit", Versions.webkitX)
val exoPlayer = exoPlayer("ui")
const val modernAndroidPreferences = "de.Maxr1998.android:modernpreferences:${Versions.modernAndroidPreferences}"
const val material = "com.google.android.material:material:${Versions.material}"
}
object Network {