-
Notifications
You must be signed in to change notification settings - Fork 962
feat(focus-mode): add Do Not Disturb for distraction-free reading #3549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,6 +105,7 @@ import com.quran.labs.androidquran.ui.listener.AudioBarListener | |
| import com.quran.labs.androidquran.ui.util.ToastCompat.makeText | ||
| import com.quran.labs.androidquran.ui.util.TranslationsSpinnerAdapter | ||
| import com.quran.labs.androidquran.util.AudioUtils | ||
| import com.quran.labs.androidquran.util.FocusModeManager | ||
| import com.quran.labs.androidquran.util.OrientationLockUtils | ||
| import com.quran.labs.androidquran.util.QuranAppUtils | ||
| import com.quran.labs.androidquran.util.QuranFileUtils | ||
|
|
@@ -211,6 +212,8 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat | |
| private var lastSelectedTranslationAyah: QuranAyahInfo? = null | ||
| private var lastActivatedLocalTranslations: Array<LocalTranslation> = emptyArray() | ||
|
|
||
| private lateinit var focusModeManager: FocusModeManager | ||
|
|
||
| @Inject lateinit var bookmarksDao: BookmarksDao | ||
| @Inject lateinit var recentPagePresenter: RecentPagePresenter | ||
| @Inject lateinit var quranSettings: QuranSettings | ||
|
|
@@ -282,6 +285,8 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat | |
| savedInstanceState?.getBoolean(LAST_FOLDING_STATE, isFoldableDeviceOpenAndVertical) | ||
| ?: isFoldableDeviceOpenAndVertical | ||
|
|
||
| focusModeManager = FocusModeManager(this) | ||
|
|
||
| lifecycleScope.launch { | ||
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
| WindowInfoTracker.getOrCreate(this@PagerActivity) | ||
|
|
@@ -894,6 +899,10 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat | |
| audioPresenter.bind(this) | ||
| recentPagePresenter.bind(currentPageFlow) | ||
|
|
||
| if (QuranSettings.getInstance(this).isFocusModeEnabled()) { | ||
| focusModeManager.enableFocusMode() | ||
| } | ||
|
|
||
| if (shouldReconnect) { | ||
| foregroundDisposable.add( | ||
| Completable.timer(500, TimeUnit.MILLISECONDS) | ||
|
|
@@ -1091,6 +1100,8 @@ class PagerActivity : AppCompatActivity(), AudioBarListener, OnBookmarkTagsUpdat | |
| recentPagePresenter.unbind() | ||
| quranSettings.wasShowingTranslation = pagerAdapter.isShowingTranslation | ||
|
|
||
| focusModeManager.restorePreviousDndState() | ||
|
|
||
|
Comment on lines
1100
to
+1104
|
||
| super.onPause() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||
| package com.quran.labs.androidquran.ui.fragment | ||||||
|
|
||||||
| import android.content.Intent | ||||||
| import android.provider.Settings | ||||||
| import android.widget.Toast | ||||||
|
Comment on lines
+4
to
+5
|
||||||
| import android.os.Bundle | ||||||
| import android.view.LayoutInflater | ||||||
| import android.view.View | ||||||
|
|
@@ -21,6 +23,8 @@ import com.quran.labs.androidquran.R | |||||
| import com.quran.labs.androidquran.data.Constants | ||||||
| import com.quran.labs.androidquran.pageselect.PageSelectActivity | ||||||
| import com.quran.labs.androidquran.ui.TranslationManagerActivity | ||||||
| import com.quran.labs.androidquran.util.FocusModeManager | ||||||
| import com.quran.labs.androidquran.util.QuranSettings | ||||||
| import com.quran.labs.androidquran.util.OrientationLockUtils | ||||||
|
Comment on lines
+27
to
28
|
||||||
| import com.quran.labs.androidquran.util.QuranSettings | |
| import com.quran.labs.androidquran.util.OrientationLockUtils |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.quran.labs.androidquran.util | ||
|
|
||
| import android.app.Activity | ||
| import android.app.NotificationManager | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.provider.Settings | ||
|
|
||
| class FocusModeManager(private val context: Context) { | ||
|
|
||
| private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
| private var previousInterruptionFilter: Int = NotificationManager.INTERRUPTION_FILTER_ALL | ||
| private var focusModeActivatedByApp: Boolean = false | ||
|
|
||
| fun isPermissionGranted(): Boolean = notificationManager.isNotificationPolicyAccessGranted | ||
|
|
||
| fun requestPermission(activity: Activity) { | ||
| val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS) | ||
| activity.startActivity(intent) | ||
| } | ||
|
|
||
| fun saveCurrentDndState() { | ||
| previousInterruptionFilter = notificationManager.currentInterruptionFilter | ||
| } | ||
|
|
||
| fun enableFocusMode(): Boolean { | ||
| if (!isPermissionGranted()) return false | ||
| if (focusModeActivatedByApp) return true | ||
|
|
||
| previousInterruptionFilter = notificationManager.currentInterruptionFilter | ||
| notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS) | ||
| focusModeActivatedByApp = true | ||
| return true | ||
|
Comment on lines
+11
to
+33
|
||
| } | ||
|
|
||
| fun restorePreviousDndState() { | ||
| if (!isPermissionGranted()) return | ||
| if (!focusModeActivatedByApp) return | ||
|
|
||
| notificationManager.setInterruptionFilter(previousInterruptionFilter) | ||
| focusModeActivatedByApp = false | ||
| } | ||
|
|
||
| fun isFocusModeActive(): Boolean { | ||
| return focusModeActivatedByApp | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses
QuranSettings.getInstance(this)even thoughquranSettingsis already injected in the activity. Using the injected instance keeps behavior consistent and easier to test. Also,enableFocusMode()returnsfalsewhen notification policy access is missing, but the result is ignored—consider handling the failure (e.g., prompt the user or disable the setting) so Focus Mode doesn't silently do nothing.