zamojski / TowerCollector

The OpenCellID and MLS contributor's app.
Mozilla Public License 2.0
217 stars 27 forks source link

add Package setting to be able to broadcast data to other (inactive) applications #208

Open se-ti opened 1 year ago

se-ti commented 1 year ago

I'd like to use your app to collect data to build a mobile network coverage map.

TowerCollectors' MEASUREMENTS_COLLECTED notifications are great for it, but according to contemporary Android Policies (prohibited implicit broadcast receivers) they won't wake up inactive application. https://developer.android.com/about/versions/oreo/background#broadcasts

Could you please add externPackageID setting and a couple of lines

intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
if (!TextUtils.isEmpty(externPackageID))
    intent.setPackage(externPackageID);

here https://github.com/zamojski/TowerCollector/blob/7315e1f3c04e80867712467e052f08c6e29b4c01/app/src/main/java/info/zamojski/soft/towercollector/broadcast/ExternalBroadcastSender.java#L42C66-L42C66

zamojski commented 11 months ago

@se-ti What's an externPackageId?

se-ti commented 11 months ago

I thought this could be a setting string (for example, somwhere by the existing switch Collector / Notify about collected measurements), where the user can add package id (like com.example.datagatherer) of the application, he wants to notify.

Btw, i've read some more specs since july, and i'm not sure even intent.setPackage would help :( Probably there exists one more limit: implicit broadcast receivers work just between apps, signed with the same certificate. But this should be tested.

zamojski commented 11 months ago

Can you share some articles? From what I found there shouldn't be any issues except for missing intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES).

se-ti commented 11 months ago

As far as i understand,

  1. to be activated (launched if it's not yet launched) by extern intent, the app should declare broadcast receicer in it's manifest.
  2. since Android 8 applications can no more subscribe for implicit intents, except for closed (and irreleveant) white-list
  3. Dev android.com: An explicit intent is one that you use to launch a specific app component...

https://stackoverflow.com/a/46121548/7867704 If the Intent has a ComponentName, the Intent is explicit. Otherwise, it is implicit. ComponentName... ...can be derived by the system from the action string, etc. plus the package defined via setPackage()

That's says me, you need setPackage to wake up another app with a specific intent.

P.S. The idea of common certificate goes from https://developer.android.com/about/versions/oreo/background#services Broadcasts that require a [signature permission](https://developer.android.com/guide/topics/manifest/permission-element#plevel) are exempted from this restriction, since these broadcasts are only sent to apps that are signed with the same certificate, not to all the apps on the device. and seems to be irrelevant.

zamojski commented 11 months ago

I don't declare any <permission> in the app. My broadcasts are not protected this way so I don't think it's the case.

It may be truth that some configuration is missing but instead of setPackage I think we need to set the exact class and package name - https://stackoverflow.com/a/62891524

Can you share the code you use to receive the intent in your app?

zamojski commented 11 months ago

I've made a test in couple of scenarios:

  1. Two apps each signed differently on Android 10 phone - broadcast works fine, even without Intent.FLAG_INCLUDE_STOPPED_PACKAGES.
  2. Two apps signed with the same key on Android 12 phone - broadcast works fine.
  3. Automate app from Play Store and one my own that received fake broadcast on Android 13 emulator - broadcast works. In Automate I just set the action and extras fields for Broadcast send action.
  4. Listen for broadcast from Tower Collector in Automate on Android 13 emulator - broadcast was never received.
  5. Listen for broadcast from Tower Collector in my own app on Android 13 emulator - broadcast was never received.

This brings the conclusion that something is indeed wrong but no idea what...