Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: mount receivers #16714

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Arthur-Milchior
Copy link
Member

@Arthur-Milchior Arthur-Milchior commented Jul 10, 2024

This ensure there is no change in behaviour.

I checked whether we could try to not export the download of decks, but it does not works.
The app does not crash on api 24 on simulator.

There was a slight problem, with variables that could potentially be null. To ensure the type checker agreed with the code, without using !!, I had to rewrite some of the logic.

I also ensured that some of the code that was copy/pasted in various activity now entirely belong to AnkiActivity. As a side effect, all activities that used to listen to the "unmount" notification and finished, will now also show a toast when the card gets unmounted. Currently it was only the deck picker.

david-allison

This comment was marked as resolved.

@david-allison david-allison added Needs Author Reply Waiting for a reply from the original author Review High Priority Request for high priority review labels Jul 10, 2024
@Arthur-Milchior
Copy link
Member Author

RECEIVER_EXPORTED is not available before API 33. If I could use it outside of the CompatV33 file, there would be no reason to have a compat file.
The registerReceiver with flag appeared at API 26

If you really want the function to accept a flag, I could still accept a flag, and do a xor with the EXPORTED flag

@david-allison
Copy link
Member

The patch above uses ContextCompat.RECEIVER_EXPORTED which is available on lower APIs

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works locally on an API23 emulator even (have to do some manual clicking because of the outdated-webview-check popup, during test)

this picks cleanly to release-2.18 in case we need it

@mikehardy
Copy link
Member

@david-allison with limited time and having already reviewed it I'm not sure on the value of splitting the refactor to a commit. It appears to work in local testing, I'd merge as is but you've got a pending reject, so back to you

@mikehardy mikehardy added Needs Second Approval Has one approval, one more approval to merge and removed Needs Author Reply Waiting for a reply from the original author Needs Review labels Jul 10, 2024
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, still appears to be incomplete, when I change the targetSdk in versions lib and attempt to run ./gradlew jacocoTestReport with an API34 emulator running, I see this:

Process: com.ichi2.anki.debug, PID: 7566
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ichi2.anki.debug/com.ichi2.anki.FilteredDeckOptions}: java.lang.SecurityException: com.ichi2.anki.debug: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:205)
        at android.os.Looper.loop(Looper.java:294)
        at android.app.ActivityThread.main(ActivityThread.java:8177)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.SecurityException: com.ichi2.anki.debug: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
        at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
        at android.os.Parcel.createException(Parcel.java:3041)
        at android.os.Parcel.readException(Parcel.java:3024)
        at android.os.Parcel.readException(Parcel.java:2966)
        at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5684)
        at java.lang.reflect.Method.invoke(Native Method)
        at leakcanary.ServiceWatcher$install$4$2.invoke(ServiceWatcher.kt:93)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy4.registerReceiverWithFeature(Unknown Source)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1852)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1780)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
        at com.ichi2.ui.AppCompatPreferenceActivity.registerExternalStorageListener(AppCompatPreferenceActivity.kt:288)
        at com.ichi2.anki.FilteredDeckOptions.onCreate(FilteredDeckOptions.kt:211)
        at android.app.Activity.performCreate(Activity.java:8595)
        at android.app.Activity.performCreate(Activity.java:8573)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
        at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:766)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
        ... 12 more
Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13927)
        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
        at android.os.Binder.execTransactInternal(Binder.java:1339)
        at android.os.Binder.execTransact(Binder.java:1275)

@mikehardy mikehardy added Needs Author Reply Waiting for a reply from the original author Needs Review and removed Needs Second Approval Has one approval, one more approval to merge labels Jul 10, 2024
@david-allison
Copy link
Member

Agreed - refactor would be better as a PR

@david-allison
Copy link
Member

david-allison commented Jul 10, 2024

The force push didn't seem to have applied the patch. I'll move the 'snipe' into another PR, so this PR can focus on the refactoring

@Arthur-Milchior
Copy link
Member Author

Yeah, I did the force push after seeing your request for import and before seeing that actually, your code worked on all supported version.

@Arthur-Milchior
Copy link
Member Author

I'll wait until the urgent PR is merged to solve the merge conflict and do the refactor

@david-allison
Copy link
Member

For posterity: d90bdc8

This comment was marked as outdated.

@github-actions github-actions bot added the Stale label Aug 17, 2024
@Arthur-Milchior Arthur-Milchior changed the title NF: Mark receivers are exported Refactor mount receivers to AnkiActivity Aug 18, 2024
@david-allison
Copy link
Member

One comment, from memory the rest of the code was good and I'll approve once onDestroy is a little cleaner

@github-actions github-actions bot removed the Stale label Aug 18, 2024
@david-allison david-allison changed the title Refactor mount receivers to AnkiActivity refactor: mount receivers Aug 19, 2024
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you test this:

  • I want to confirm that moving from one receiver to two won't cause problems and that mounting/unmounting are handled in the DeckPicker

Other than testing, LGTM

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt Outdated Show resolved Hide resolved
unmountReceiver = object : BroadcastReceiver() {
override fun registerExternalStorageListener() {
super.registerExternalStorageListener()
if (mountReceiver == null) {
Copy link
Member

@david-allison david-allison Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: invert if and return early

Copy link
Contributor

github-actions bot commented Sep 3, 2024

Hello 👋, this PR has had no activity for more than 2 weeks and needs a reply from the author. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

@Arthur-Milchior
Copy link
Member Author

I rewrote it so that to refactor the code. There is a map from action name to the actual action to execute , and that's the only part that is open

@github-actions github-actions bot removed the Stale label Sep 7, 2024
Copy link
Contributor

Hello 👋, this PR has had no activity for more than 2 weeks and needs a reply from the author. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

This ensure that the same receiver appears in all activities and not
just the few one where the code was essentially copy pasted.

The Deck Picker also listen for mounting, so I renamed its receiver as
a `mountReceiver` instead
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

State whether register are registered
3 participants