From e1b58ae6fbcdd982e99579ad9846e8d69d0564bb Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 17 May 2023 23:38:36 -0700 Subject: [PATCH] Request permission --- .../java/gallery/memories/MainActivity.kt | 37 +++++++++++++++++-- app/src/main/java/gallery/memories/NativeX.kt | 10 +++-- app/src/main/res/values/strings.xml | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/gallery/memories/MainActivity.kt b/app/src/main/java/gallery/memories/MainActivity.kt index cf6ecbf0..d78a5401 100644 --- a/app/src/main/java/gallery/memories/MainActivity.kt +++ b/app/src/main/java/gallery/memories/MainActivity.kt @@ -10,6 +10,7 @@ import android.view.KeyEvent import android.view.View import android.view.WindowInsetsController import android.webkit.* +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.media3.common.MediaItem import androidx.media3.common.util.UnstableApi @@ -46,6 +47,9 @@ import gallery.memories.databinding.ActivityMainBinding // Initialize services mNativeX = NativeX(this) + // Ensure storage permissions + ensureStoragePermissions() + // Load JavaScript initializeWebView() @@ -160,15 +164,31 @@ import gallery.memories.databinding.ActivityMainBinding // Load accounts val authHeader = mNativeX.mAccountService.authHeader val memoriesUrl = mNativeX.mAccountService.memoriesUrl + + // Load app interface if authenticated if (authHeader != null && memoriesUrl != null) { binding.webview.loadUrl(memoriesUrl, mapOf( "Authorization" to authHeader )) return true - } else { - binding.webview.loadUrl("file:///android_asset/welcome.html"); - return false } + + // Load welcome page + binding.webview.loadUrl("file:///android_asset/welcome.html"); + 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) { @@ -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 } } + + 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() + } } \ No newline at end of file diff --git a/app/src/main/java/gallery/memories/NativeX.kt b/app/src/main/java/gallery/memories/NativeX.kt index e64e76cc..13102cbf 100644 --- a/app/src/main/java/gallery/memories/NativeX.kt +++ b/app/src/main/java/gallery/memories/NativeX.kt @@ -20,8 +20,8 @@ import java.net.URLDecoder val TAG = "NativeX" private var themeStored = false - private val mImageService = ImageService(mActivity) - private val mQuery = TimelineQuery(mActivity) + val mImageService = ImageService(mActivity) + val mQuery = TimelineQuery(mActivity) val mAccountService = AccountService(mActivity) object API { @@ -41,8 +41,10 @@ import java.net.URLDecoder init { mDlService = DownloadService(mActivity) - // Synchronize the database - mQuery.syncDeltaDb() + // Synchronize the database if possible + if (mActivity.hasMediaPermission()) { + mQuery.syncDeltaDb() + } } companion object { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b7f0897..eab26231 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ themeColor themeDark lastDbSyncTime + hasMediaPermission "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.76 Mobile Safari/537.36"