Request permission

pull/653/merge
Varun Patil 2023-05-17 23:38:36 -07:00
parent a4d250168f
commit e1b58ae6fb
3 changed files with 41 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import android.view.KeyEvent
import android.view.View import android.view.View
import android.view.WindowInsetsController import android.view.WindowInsetsController
import android.webkit.* import android.webkit.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
@ -46,6 +47,9 @@ import gallery.memories.databinding.ActivityMainBinding
// Initialize services // Initialize services
mNativeX = NativeX(this) mNativeX = NativeX(this)
// Ensure storage permissions
ensureStoragePermissions()
// Load JavaScript // Load JavaScript
initializeWebView() initializeWebView()
@ -160,15 +164,31 @@ import gallery.memories.databinding.ActivityMainBinding
// Load accounts // Load accounts
val authHeader = mNativeX.mAccountService.authHeader val authHeader = mNativeX.mAccountService.authHeader
val memoriesUrl = mNativeX.mAccountService.memoriesUrl val memoriesUrl = mNativeX.mAccountService.memoriesUrl
// Load app interface if authenticated
if (authHeader != null && memoriesUrl != null) { if (authHeader != null && memoriesUrl != null) {
binding.webview.loadUrl(memoriesUrl, mapOf( binding.webview.loadUrl(memoriesUrl, mapOf(
"Authorization" to authHeader "Authorization" to authHeader
)) ))
return true return true
} else { }
// Load welcome page
binding.webview.loadUrl("file:///android_asset/welcome.html"); binding.webview.loadUrl("file:///android_asset/welcome.html");
return false return false
} }
fun ensureStoragePermissions() {
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted && !hasMediaPermission()) {
mNativeX.mQuery.syncFullDb()
}
setHasMediaPermission(isGranted)
}
requestPermissionLauncher.launch(android.Manifest.permission.READ_EXTERNAL_STORAGE)
} }
fun initializePlayer(uri: Uri, uid: String) { fun initializePlayer(uri: Uri, uid: String) {
@ -268,4 +288,15 @@ import gallery.memories.databinding.ActivityMainBinding
window.decorView.systemUiVisibility = if (isDark) 0 else View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR window.decorView.systemUiVisibility = if (isDark) 0 else View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
} }
} }
fun hasMediaPermission(): Boolean {
return getSharedPreferences(getString(R.string.preferences_key), 0)
.getBoolean(getString(R.string.preferences_has_media_permission), false)
}
private fun setHasMediaPermission(v: Boolean) {
getSharedPreferences(getString(R.string.preferences_key), 0).edit()
.putBoolean(getString(R.string.preferences_has_media_permission), v)
.apply()
}
} }

View File

@ -20,8 +20,8 @@ import java.net.URLDecoder
val TAG = "NativeX" val TAG = "NativeX"
private var themeStored = false private var themeStored = false
private val mImageService = ImageService(mActivity) val mImageService = ImageService(mActivity)
private val mQuery = TimelineQuery(mActivity) val mQuery = TimelineQuery(mActivity)
val mAccountService = AccountService(mActivity) val mAccountService = AccountService(mActivity)
object API { object API {
@ -41,9 +41,11 @@ import java.net.URLDecoder
init { init {
mDlService = DownloadService(mActivity) mDlService = DownloadService(mActivity)
// Synchronize the database // Synchronize the database if possible
if (mActivity.hasMediaPermission()) {
mQuery.syncDeltaDb() mQuery.syncDeltaDb()
} }
}
companion object { companion object {
var mDlService: DownloadService? = null var mDlService: DownloadService? = null

View File

@ -6,6 +6,7 @@
<string name="preferences_theme_color">themeColor</string> <string name="preferences_theme_color">themeColor</string>
<string name="preferences_theme_dark">themeDark</string> <string name="preferences_theme_dark">themeDark</string>
<string name="preferences_last_sync_time">lastDbSyncTime</string> <string name="preferences_last_sync_time">lastDbSyncTime</string>
<string name="preferences_has_media_permission">hasMediaPermission</string>
<!-- https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome --> <!-- https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome -->
<string name="ua_chrome">"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.76 Mobile Safari/537.36"</string> <string name="ua_chrome">"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.76 Mobile Safari/537.36"</string>