Commit 91fa852d authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Fix: on API >= 34 broadcast intents must have explicit package

parent ce69ca6c
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2024 Bartek Fabiszewski
 * http://www.fabiszewski.net
 *
 * This file is part of μlogger-android.
 * Licensed under GPL, either version 3, or any later.
 * See <http://www.gnu.org/licenses/>
 */

package net.fabiszewski.ulogger;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class BroadcastHelper {

    /**
     * Send broadcast message
     * @param context Context
     * @param broadcast Broadcast message
     */
    static void sendBroadcast(@NonNull Context context, @NonNull String broadcast) {
        sendBroadcast(context, broadcast, null);
    }

    /**
     * Send broadcast message with optional extras
     * @param context Context
     * @param broadcast Broadcast message
     * @param extras Extras bundle
     */
    static void sendBroadcast(@NonNull Context context, @NonNull String broadcast, @Nullable Bundle extras) {
        Intent intent = new Intent(broadcast);
        if (extras != null) {
            intent.putExtras(extras);
        }
        intent.setPackage(context.getPackageName());
        context.sendBroadcast(intent);
    }
}
+10 −19
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@ public class LoggerService extends Service {
            locationHelper.updatePreferences();
            locationHelper.requestLocationUpdates(locationListener, looper);
            setRunning(true);
            sendBroadcast(BROADCAST_LOCATION_STARTED);
            BroadcastHelper.sendBroadcast(this, BROADCAST_LOCATION_STARTED);

            syncIntent = new Intent(getApplicationContext(), WebSyncService.class);

@@ -103,9 +103,9 @@ public class LoggerService extends Service {
        } catch (LocationHelper.LoggerException e) {
            int errorCode = e.getCode();
            if (errorCode == E_DISABLED) {
                sendBroadcast(BROADCAST_LOCATION_DISABLED);
                BroadcastHelper.sendBroadcast(this, BROADCAST_LOCATION_DISABLED);
            } else if (errorCode == E_PERMISSION) {
                sendBroadcast(BROADCAST_LOCATION_PERMISSION_DENIED);
                BroadcastHelper.sendBroadcast(this, BROADCAST_LOCATION_PERMISSION_DENIED);
            }
        }
        return false;
@@ -179,7 +179,7 @@ public class LoggerService extends Service {
        setRunning(false);

        notificationHelper.cancelNotification();
        sendBroadcast(BROADCAST_LOCATION_STOPPED);
        BroadcastHelper.sendBroadcast(this, BROADCAST_LOCATION_STOPPED);

        if (thread != null) {
            thread.interrupt();
@@ -232,15 +232,6 @@ public class LoggerService extends Service {
        lastLocation = null;
    }

    /**
     * Send broadcast message
     * @param broadcast Broadcast message
     */
    private void sendBroadcast(String broadcast) {
        Intent intent = new Intent(broadcast);
        sendBroadcast(intent);
    }

    /**
     * Location listener class
     */
@@ -259,7 +250,7 @@ public class LoggerService extends Service {
            if (meetsCriteria(location)) {
                lastLocation = location;
                DbAccess.writeLocation(LoggerService.this, location);
                sendBroadcast(BROADCAST_LOCATION_UPDATED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_UPDATED);
                if (locationHelper.isLiveSync()) {
                    getApplicationContext().startService(syncIntent);
                }
@@ -309,12 +300,12 @@ public class LoggerService extends Service {
        public void onProviderDisabled(@NonNull String provider) {
            if (Logger.DEBUG) { Log.d(TAG, "[location provider " + provider + " disabled]"); }
            if (provider.equals(LocationManager.GPS_PROVIDER)) {
                sendBroadcast(BROADCAST_LOCATION_GPS_DISABLED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_GPS_DISABLED);
            } else if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
                sendBroadcast(BROADCAST_LOCATION_NETWORK_DISABLED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_NETWORK_DISABLED);
            }
            if (!locationHelper.hasEnabledProviders()) {
                sendBroadcast(BROADCAST_LOCATION_DISABLED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_DISABLED);
            }
        }

@@ -326,9 +317,9 @@ public class LoggerService extends Service {
        public void onProviderEnabled(@NonNull String provider) {
            if (Logger.DEBUG) { Log.d(TAG, "[location provider " + provider + " enabled]"); }
            if (provider.equals(LocationManager.GPS_PROVIDER)) {
                sendBroadcast(BROADCAST_LOCATION_GPS_ENABLED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_GPS_ENABLED);
            } else if (provider.equals(LocationManager.NETWORK_PROVIDER)) {
                sendBroadcast(BROADCAST_LOCATION_NETWORK_ENABLED);
                BroadcastHelper.sendBroadcast(LoggerService.this, BROADCAST_LOCATION_NETWORK_ENABLED);
            }
        }

+7 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
@@ -197,8 +198,7 @@ public class WebSyncService extends Service {
                params.put(WebHelper.PARAM_TRACKID, String.valueOf(trackId));
                web.postPosition(params);
                db.setSynced(getApplicationContext(), rowId);
                Intent intent = new Intent(BROADCAST_SYNC_DONE);
                sendBroadcast(intent);
                BroadcastHelper.sendBroadcast(this, BROADCAST_SYNC_DONE);
            }
        } catch (IOException e) {
            // handle web errors
@@ -246,9 +246,11 @@ public class WebSyncService extends Service {
        if (Logger.DEBUG) { Log.d(TAG, "[handleError: retry: " + message + "]"); }

        db.setError(message);
        Intent intent = new Intent(BROADCAST_SYNC_FAILED);
        intent.putExtra("message", message);
        sendBroadcast(intent);

        Bundle extras = new Bundle();
        extras.putString("message", message);
        BroadcastHelper.sendBroadcast(this, BROADCAST_SYNC_FAILED, extras);

        // retry only if tracking is on
        if (LoggerService.isRunning()) {
            setPending();