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

Commit 730bf968 authored by Jeffrey Huang's avatar Jeffrey Huang
Browse files

Migrate pullNotificationRemoteViews

Test: atest NotificationStatsTest
Test: adb shell cmd stats pull-source 10066
Change-Id: I8fd848b233920a2fe6bba1d87ec22eeed0bcaf91
parent bf4eef83
Loading
Loading
Loading
Loading
+0 −39
Original line number Original line Diff line number Diff line
@@ -773,39 +773,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
        }
    }
    }


    private INotificationManager mNotificationManager =
            INotificationManager.Stub.asInterface(
                    ServiceManager.getService(Context.NOTIFICATION_SERVICE));

    private void pullNotificationStats(int reportId, int tagId, long elapsedNanos,
            long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        final long callingToken = Binder.clearCallingIdentity();
        try {
            // determine last pull tine. Copy file trick from pullProcessStats?
            long lastNotificationStatsNs = wallClockNanos -
                    TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS);

            List<ParcelFileDescriptor> statsFiles = new ArrayList<>();
            long notificationStatsNs = mNotificationManager.pullStats(
                    lastNotificationStatsNs, reportId, true, statsFiles);
            if (statsFiles.size() != 1) {
                return;
            }
            unpackStreamedData(tagId, elapsedNanos, wallClockNanos, pulledData, statsFiles);
        } catch (IOException e) {
            Log.e(TAG, "Getting notistats failed: ", e);

        } catch (RemoteException e) {
            Log.e(TAG, "Getting notistats failed: ", e);
        } catch (SecurityException e) {
            Log.e(TAG, "Getting notistats failed: ", e);
        } finally {
            Binder.restoreCallingIdentity(callingToken);
        }

    }

    static void unpackStreamedData(int tagId, long elapsedNanos, long wallClockNanos,
    static void unpackStreamedData(int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData, List<ParcelFileDescriptor> statsFiles)
            List<StatsLogEventWrapper> pulledData, List<ParcelFileDescriptor> statsFiles)
            throws IOException {
            throws IOException {
@@ -1346,12 +1313,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                break;
                break;
            }
            }


            case StatsLog.NOTIFICATION_REMOTE_VIEWS: {
                pullNotificationStats(NotificationManagerService.REPORT_REMOTE_VIEWS,
                        tagId, elapsedNanos, wallClockNanos, ret);
                break;
            }

            default:
            default:
                Slog.w(TAG, "No such tagId data as " + tagId);
                Slog.w(TAG, "No such tagId data as " + tagId);
                return null;
                return null;
+0 −4
Original line number Original line Diff line number Diff line
@@ -168,10 +168,6 @@ std::map<PullerKey, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
         {.additiveFields = {5, 6, 7, 8, 9, 10},
         {.additiveFields = {5, 6, 7, 8, 9, 10},
          .puller = new CarStatsPuller(android::util::VMS_CLIENT_STATS)}},
          .puller = new CarStatsPuller(android::util::VMS_CLIENT_STATS)}},


        // NotiifcationRemoteViews.
        {{.atomTag = android::util::NOTIFICATION_REMOTE_VIEWS},
         {.puller = new StatsCompanionServicePuller(android::util::NOTIFICATION_REMOTE_VIEWS)}},

        // PermissionStateSampled.
        // PermissionStateSampled.
        {{.atomTag = android::util::DANGEROUS_PERMISSION_STATE_SAMPLED},
        {{.atomTag = android::util::DANGEROUS_PERMISSION_STATE_SAMPLED},
         {.puller =
         {.puller =
+104 −3
Original line number Original line Diff line number Diff line
@@ -201,6 +201,9 @@ public class StatsPullAtomService extends SystemService {
    private final Object mStoragedLock = new Object();
    private final Object mStoragedLock = new Object();
    @GuardedBy("mStoragedLock")
    @GuardedBy("mStoragedLock")
    private IStoraged mStorageService;
    private IStoraged mStorageService;
    private final Object mNotificationStatsLock = new Object();
    @GuardedBy("mNotificationStatsLock")
    private INotificationManager mNotificationManagerService;


    private final Context mContext;
    private final Context mContext;
    private StatsManager mStatsManager;
    private StatsManager mStatsManager;
@@ -376,6 +379,28 @@ public class StatsPullAtomService extends SystemService {
        return mStorageService;
        return mStorageService;
    }
    }


    private INotificationManager getINotificationManagerService() {
        synchronized (mNotificationStatsLock) {
            if (mNotificationManagerService == null) {
                mNotificationManagerService = INotificationManager.Stub.asInterface(
                                ServiceManager.getService(Context.NOTIFICATION_SERVICE));
            }
            if (mNotificationManagerService != null) {
                try {
                    mNotificationManagerService.asBinder().linkToDeath(() -> {
                        synchronized (mNotificationStatsLock) {
                            mNotificationManagerService = null;
                        }
                    }, /* flags */ 0);
                } catch (RemoteException e) {
                    Slog.e(TAG, "linkToDeath with notificationManager failed", e);
                    mNotificationManagerService = null;
                }
            }
        }
        return mNotificationManagerService;
    }

    private void registerWifiBytesTransfer() {
    private void registerWifiBytesTransfer() {
        int tagId = StatsLog.WIFI_BYTES_TRANSFER;
        int tagId = StatsLog.WIFI_BYTES_TRANSFER;
        PullAtomMetadata metadata = new PullAtomMetadata.Builder()
        PullAtomMetadata metadata = new PullAtomMetadata.Builder()
@@ -2167,12 +2192,88 @@ public class StatsPullAtomService extends SystemService {
        // No op.
        // No op.
    }
    }


    static void unpackStreamedData(int atomTag, List<StatsEvent> pulledData,
            List<ParcelFileDescriptor> statsFiles) throws IOException {
        InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(statsFiles.get(0));
        int[] len = new int[1];
        byte[] stats = readFully(stream, len);
        StatsEvent e = StatsEvent.newBuilder()
                .setAtomId(atomTag)
                .writeByteArray(Arrays.copyOf(stats, len[0]))
                .build();
        pulledData.add(e);
    }

    static byte[] readFully(InputStream stream, int[] outLen) throws IOException {
        int pos = 0;
        final int initialAvail = stream.available();
        byte[] data = new byte[initialAvail > 0 ? (initialAvail + 1) : 16384];
        while (true) {
            int amt = stream.read(data, pos, data.length - pos);
            if (DEBUG) {
                Slog.i(TAG, "Read " + amt + " bytes at " + pos + " of avail " + data.length);
            }
            if (amt < 0) {
                if (DEBUG) {
                    Slog.i(TAG, "**** FINISHED READING: pos=" + pos + " len=" + data.length);
                }
                outLen[0] = pos;
                return data;
            }
            pos += amt;
            if (pos >= data.length) {
                byte[] newData = new byte[pos + 16384];
                if (DEBUG) {
                    Slog.i(TAG, "Copying " + pos + " bytes to new array len " + newData.length);
                }
                System.arraycopy(data, 0, newData, 0, pos);
                data = newData;
            }
        }
    }

    private void registerNotificationRemoteViews() {
    private void registerNotificationRemoteViews() {
        // No op.
        int tagId = StatsLog.NOTIFICATION_REMOTE_VIEWS;
        mStatsManager.registerPullAtomCallback(
                tagId,
                null, // use default PullAtomMetadata values
                (atomTag, data) -> pullNotificationRemoteViews(atomTag, data),
                BackgroundThread.getExecutor()
        );
    }
    }


    private void pullNotificationRemoteViews() {
    private int pullNotificationRemoteViews(int atomTag, List<StatsEvent> pulledData) {
        // No op.
        INotificationManager notificationManagerService = getINotificationManagerService();
        if (notificationManagerService == null) {
            return StatsManager.PULL_SKIP;
        }
        final long callingToken = Binder.clearCallingIdentity();
        try {
            // determine last pull tine. Copy file trick from pullProcessStats?
            long wallClockNanos = SystemClock.currentTimeMicro() * 1000L;
            long lastNotificationStatsNs = wallClockNanos -
                    TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS);

            List<ParcelFileDescriptor> statsFiles = new ArrayList<>();
            notificationManagerService.pullStats(lastNotificationStatsNs,
                    NotificationManagerService.REPORT_REMOTE_VIEWS, true, statsFiles);
            if (statsFiles.size() != 1) {
                return StatsManager.PULL_SKIP;
            }
            unpackStreamedData(atomTag, pulledData, statsFiles);
        } catch (IOException e) {
            Slog.e(TAG, "Getting notistats failed: ", e);
            return StatsManager.PULL_SKIP;
        } catch (RemoteException e) {
            Slog.e(TAG, "Getting notistats failed: ", e);
            return StatsManager.PULL_SKIP;
        } catch (SecurityException e) {
            Slog.e(TAG, "Getting notistats failed: ", e);
            return StatsManager.PULL_SKIP;
        } finally {
            Binder.restoreCallingIdentity(callingToken);
        }
        return StatsManager.PULL_SUCCESS;
    }
    }


    private void registerDangerousPermissionStateSampled() {
    private void registerDangerousPermissionStateSampled() {