Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 97670d44 authored by Muhammad Qureshi's avatar Muhammad Qureshi
Browse files

Alternative to @hide FLAG_RECEIVER_INCLUDE_BACKGROUND

Implementing an alternative to sending broadcast with @hide flag
FLAG_RECEIVER_INCLUDE_BACKGROUND. Instead of using the flag, find the
list of registered broadcast receivers and send them directed broadcasts
to wake them up.

This follows the logic in ag/10055770

Bug: 149090705
Test: m
Test: fastboot flashall
Test: ensure broadcast is received by inspecting logcat
Change-Id: I978cd6481401eb68b26f482e19e412657a0da13c
parent 27f83cdb
Loading
Loading
Loading
Loading
+21 −9
Original line number Diff line number Diff line
@@ -21,11 +21,13 @@ import android.app.AlarmManager;
import android.app.AlarmManager.OnAlarmListener;
import android.app.StatsManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
@@ -85,12 +87,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {

    public static final int DEATH_THRESHOLD = 10;

    // TODO(b/149090705): Implement an alternative to sending broadcast with @hide flag
    // FLAG_RECEIVER_INCLUDE_BACKGROUND. Instead of using the flag, find the
    // list of registered broadcast receivers and send them directed broadcasts
    // to wake them up. See b/147374337.
    private static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;

    static final class CompanionHandler extends Handler {
        CompanionHandler(Looper looper) {
            super(looper);
@@ -498,9 +494,25 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            Log.d(TAG, "learned that statsdReady");
        }
        sayHiToStatsd(); // tell statsd that we're ready too and link to it
        mContext.sendBroadcastAsUser(new Intent(StatsManager.ACTION_STATSD_STARTED)
                        .addFlags(FLAG_RECEIVER_INCLUDE_BACKGROUND),
                UserHandle.SYSTEM, android.Manifest.permission.DUMP);

        final Intent intent = new Intent(StatsManager.ACTION_STATSD_STARTED);
        // Retrieve list of broadcast receivers for this broadcast & send them directed broadcasts
        // to wake them up (if they're in background).
        List<ResolveInfo> resolveInfos =
                mContext.getPackageManager().queryBroadcastReceiversAsUser(
                        intent, 0, UserHandle.SYSTEM);
        if (resolveInfos == null || resolveInfos.isEmpty()) {
            return; // No need to send broadcast.
        }

        for (ResolveInfo resolveInfo : resolveInfos) {
            Intent intentToSend = new Intent(intent);
            intentToSend.setComponent(new ComponentName(
                    resolveInfo.activityInfo.applicationInfo.packageName,
                    resolveInfo.activityInfo.name));
            mContext.sendBroadcastAsUser(intentToSend, UserHandle.SYSTEM,
                    android.Manifest.permission.DUMP);
        }
    }

    @Override