Skip to content

Commit

Permalink
fix Android 14 local broadcast
Browse files Browse the repository at this point in the history
Intent must have package set or else receiver with RECEIVER_NOT_EXPORTED will not receive
  • Loading branch information
TBog committed May 8, 2024
1 parent c0a024b commit 54e7b2a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/rocks/tbog/tblauncher/TBApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ public void initDataHandler() {
}
if (dataHandler.fullLoadOverSent()) {
// Already loaded! We still need to fire the FULL_LOAD event
Intent i = new Intent(TBLauncherActivity.FULL_LOAD_OVER);
sendBroadcast(i);
DataHandler.sendBroadcast(this, TBLauncherActivity.FULL_LOAD_OVER, TAG);
}
}

Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/rocks/tbog/tblauncher/TBLauncherActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@
public class TBLauncherActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final String TAG = "TBL";
public static final String START_LOAD = "fr.neamar.summon.START_LOAD";
public static final String LOAD_OVER = "fr.neamar.summon.LOAD_OVER";
public static final String FULL_LOAD_OVER = "fr.neamar.summon.FULL_LOAD_OVER";
public static final String START_LOAD = "rocks.tbog.provider.START_LOAD";
public static final String LOAD_OVER = "rocks.tbog.provider.LOAD_OVER";
public static final String FULL_LOAD_OVER = "rocks.tbog.provider.FULL_LOAD_OVER";
public static final String INTENT_DATA = "rocks.tbog.provider.INTENT_DATA";

/**
* Receive events from providers
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BroadcastReceiver action=`" + intent.getAction() + "` extra=`" + intent.getStringExtra(INTENT_DATA) + "`");
if (START_LOAD.equalsIgnoreCase(intent.getAction())) {
behaviour.displayLoader(true);
} else if (LOAD_OVER.equalsIgnoreCase(intent.getAction())) {
Expand Down Expand Up @@ -120,13 +122,12 @@ protected void onCreate(Bundle savedInstanceState) {
/*
* Initialize data handler and start loading providers
*/
IntentFilter intentFilterLoad = new IntentFilter(START_LOAD);
IntentFilter intentFilterLoadOver = new IntentFilter(LOAD_OVER);
IntentFilter intentFilterFullLoadOver = new IntentFilter(FULL_LOAD_OVER);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(START_LOAD);
intentFilter.addAction(LOAD_OVER);
intentFilter.addAction(FULL_LOAD_OVER);

ActivityCompat.registerReceiver(this, mReceiver, intentFilterLoad, ContextCompat.RECEIVER_NOT_EXPORTED);
ActivityCompat.registerReceiver(this, mReceiver, intentFilterLoadOver, ContextCompat.RECEIVER_NOT_EXPORTED);
ActivityCompat.registerReceiver(this, mReceiver, intentFilterFullLoadOver, ContextCompat.RECEIVER_NOT_EXPORTED);
ActivityCompat.registerReceiver(this, mReceiver, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);

// init DataHandler after we register the receiver
app.initDataHandler();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rocks.tbog.tblauncher.dataprovider;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.MainThread;
Expand All @@ -15,12 +14,12 @@
import java.util.List;

import rocks.tbog.tblauncher.BuildConfig;
import rocks.tbog.tblauncher.handler.DataHandler;
import rocks.tbog.tblauncher.TBApplication;
import rocks.tbog.tblauncher.TBLauncherActivity;
import rocks.tbog.tblauncher.WorkAsync.AsyncTask;
import rocks.tbog.tblauncher.WorkAsync.TaskRunner;
import rocks.tbog.tblauncher.entry.EntryItem;
import rocks.tbog.tblauncher.handler.DataHandler;
import rocks.tbog.tblauncher.searcher.ISearcher;
import rocks.tbog.tblauncher.utils.Timer;

Expand Down Expand Up @@ -162,8 +161,7 @@ protected void onPostExecute(List<T> entryItems) {
provider.mTimer.stop();
Log.i("time", "Time to load " + provider.getClass().getSimpleName() + ": " + provider.mTimer);

Intent i = new Intent(TBLauncherActivity.LOAD_OVER);
provider.context.sendBroadcast(i);
DataHandler.sendBroadcast(provider.context, TBLauncherActivity.LOAD_OVER, provider.getClass().getSimpleName());
}

public void execute() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IProvider<T extends EntryItem> {
/**
* Reload the data stored in this provider
* <p>
* `"fr.neamar.summon.LOAD_OVER"` will be emitted once the reload is complete. The data provider
* `LOAD_OVER` will be emitted once the reload is complete. The data provider
* will stay usable (using it's old data) during the reload.
* @param cancelCurrentLoadTask pass true to stop current loading task and start another;
* pass false to do nothing if already loading
Expand All @@ -41,7 +41,7 @@ public interface IProvider<T extends EntryItem> {
* Indicate whether this provider has already loaded it's data
* <p>
* If this method returns `false` then the client may listen for the
* `"fr.neamar.summon.LOAD_OVER"` intent for notification of when the provider is ready.
* `LOAD_OVER` intent for notification of when the provider is ready.
*
* @return Is the provider ready to process search results?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import rocks.tbog.tblauncher.TBApplication;
import rocks.tbog.tblauncher.TBLauncherActivity;
import rocks.tbog.tblauncher.entry.EntryItem;
import rocks.tbog.tblauncher.handler.DataHandler;
import rocks.tbog.tblauncher.loader.LoadEntryItem;
import rocks.tbog.tblauncher.utils.Timer;

Expand Down Expand Up @@ -113,8 +114,7 @@ public void loadOver(ArrayList<T> results) {
this.loader = null;

// Broadcast this event
Intent i = new Intent(TBLauncherActivity.LOAD_OVER);
this.sendBroadcast(i);
DataHandler.sendBroadcast(this, TBLauncherActivity.LOAD_OVER, getClass().getSimpleName());
}

@NonNull
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/rocks/tbog/tblauncher/handler/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public DataHandler(@NonNull Application app) {

ActivityCompat.registerReceiver(ctx, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);

Intent i = new Intent(TBLauncherActivity.START_LOAD);
ctx.sendBroadcast(i);
sendBroadcast(ctx, TBLauncherActivity.START_LOAD, TAG);

// Monitor changes for service preferences (to automatically start and stop services)
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
Expand Down Expand Up @@ -161,6 +160,13 @@ public DataHandler(@NonNull Application app) {
}
}

public static void sendBroadcast(@NonNull Context context, @NonNull String action, @Nullable String data) {
Intent msg = new Intent(action)
.setPackage(context.getPackageName())
.putExtra(TBLauncherActivity.INTENT_DATA, data);
context.sendBroadcast(msg);
}

@NonNull
public Context getContext() {
return mApplication.getApplicationContext();
Expand Down Expand Up @@ -486,8 +492,7 @@ private void handleProviderLoaded() {
// Broadcast the fact that the new providers list is ready
try {
context.unregisterReceiver(this);
Intent i = new Intent(TBLauncherActivity.FULL_LOAD_OVER);
context.sendBroadcast(i);
sendBroadcast(context, TBLauncherActivity.FULL_LOAD_OVER, TAG);
} catch (IllegalArgumentException e) {
Log.e(TAG, "send FULL_LOAD_OVER", e);
}
Expand Down Expand Up @@ -959,8 +964,7 @@ public void onProviderRecreated(Provider<? extends EntryItem> provider) {
IntentFilter intentFilter = new IntentFilter(TBLauncherActivity.LOAD_OVER);
ActivityCompat.registerReceiver(context, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);

Intent i = new Intent(TBLauncherActivity.START_LOAD);
context.sendBroadcast(i);
sendBroadcast(context, TBLauncherActivity.START_LOAD, provider.getClass().getSimpleName());

// reload providers for the next steps
for (int step : IProvider.LOAD_STEPS) {
Expand All @@ -986,8 +990,7 @@ public void reloadProviders(int loadStep) {
IntentFilter intentFilter = new IntentFilter(TBLauncherActivity.LOAD_OVER);
ActivityCompat.registerReceiver(context, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);

Intent i = new Intent(TBLauncherActivity.START_LOAD);
context.sendBroadcast(i);
sendBroadcast(context, TBLauncherActivity.START_LOAD, "reload_" + loadStep);

for (int step : IProvider.LOAD_STEPS) {
if (step < loadStep)
Expand All @@ -1007,8 +1010,7 @@ public void reloadProviders() {
IntentFilter intentFilter = new IntentFilter(TBLauncherActivity.LOAD_OVER);
ActivityCompat.registerReceiver(context, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED);

Intent i = new Intent(TBLauncherActivity.START_LOAD);
context.sendBroadcast(i);
sendBroadcast(context, TBLauncherActivity.START_LOAD, "reload");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
toggleableProviders(prefs);
Expand Down

0 comments on commit 54e7b2a

Please sign in to comment.