Skip to content

Commit

Permalink
revert: Disable widget if no storage access
Browse files Browse the repository at this point in the history
* On  (<= API 30 [Android 11]), widgets cannot be enabled in code if defined as enabled=false in the manifest (Android bug #15745).
  • Loading branch information
criticalAY authored and BrayanDSO committed Mar 2, 2024
1 parent de8f902 commit 11d1c41
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 39 deletions.
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@
android:foregroundServiceType="dataSync"
/>

<!-- Don't disable/enable the widgets here until min API>=31, see issue #15747 & https://issuetracker.google.com/issues/36914010 -->
<!-- small widget -->
<receiver
android:name="com.ichi2.widget.AnkiDroidWidgetSmall"
android:label="@string/widget_small"
android:enabled="false"
android:exported="true"
>
<intent-filter>
Expand All @@ -517,7 +517,6 @@
<receiver
android:name="com.ichi2.widget.AddNoteWidget"
android:label="@string/menu_add_note"
android:enabled="false"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Expand Down
30 changes: 0 additions & 30 deletions AnkiDroid/src/main/java/com/ichi2/anki/IntentHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package com.ichi2.anki

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.Message
Expand Down Expand Up @@ -48,8 +46,6 @@ import com.ichi2.utils.Permissions
import com.ichi2.utils.Permissions.hasStorageAccessPermission
import com.ichi2.utils.copyToClipboard
import com.ichi2.utils.trimToLength
import com.ichi2.widget.AddNoteWidget
import com.ichi2.widget.AnkiDroidWidgetSmall
import timber.log.Timber
import java.io.File
import kotlin.math.max
Expand All @@ -76,12 +72,6 @@ class IntentHandler : Activity() {
// #6157 - We want to block actions that need permissions we don't have, but not the default case
// as this requires nothing
val runIfStoragePermissions = { runnable: () -> Unit -> performActionIfStorageAccessible(reloadIntent, action) { runnable() } }

runIfStoragePermissions {
enableWidgets(this)
NavigationDrawerActivity.enablePostShortcut(this)
}

when (getLaunchType(intent)) {
LaunchType.FILE_IMPORT -> runIfStoragePermissions {
handleFileImport(fileIntent, reloadIntent, action)
Expand Down Expand Up @@ -278,26 +268,6 @@ class IntentHandler : Activity() {
return !isInvalidViewIntent(intent)
}

/**
* Enables the widgets if the storage permission is granted. It enables both the small widget
* and the "Add Note" widget by setting their component enabled state to ENABLED.
**/
fun enableWidgets(context: Context) {
val smallWidgetComponentName = ComponentName(context, AnkiDroidWidgetSmall::class.java)
context.packageManager.setComponentEnabledSetting(
smallWidgetComponentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)

val addNoteWidgetComponentName = ComponentName(context, AddNoteWidget::class.java)
context.packageManager.setComponentEnabledSetting(
addNoteWidgetComponentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}

@VisibleForTesting
@CheckResult
fun getLaunchType(intent: Intent): LaunchType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ abstract class NavigationDrawerActivity :
}
drawerToggle.isDrawerSlideAnimationEnabled = animationEnabled()
drawerLayout.addDrawerListener(drawerToggle)

enablePostShortcut(this)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.IntentCompat
import androidx.fragment.app.commit
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.IntentHandler
import com.ichi2.anki.NavigationDrawerActivity
import com.ichi2.anki.PermissionSet
import com.ichi2.anki.R
import com.ichi2.annotations.NeedsTest
Expand Down Expand Up @@ -70,11 +68,6 @@ class PermissionsActivity : AnkiActivity() {

fun setContinueButtonEnabled(isEnabled: Boolean) {
findViewById<AppCompatButton>(R.id.continue_button).isEnabled = isEnabled
// in case it's not play build
if (isEnabled) {
IntentHandler.enableWidgets(this)
NavigationDrawerActivity.enablePostShortcut(this)
}
}

companion object {
Expand Down

0 comments on commit 11d1c41

Please sign in to comment.