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

Commit 95543247 authored by Yangster-mac's avatar Yangster-mac
Browse files

Code refactoring in ag/5261950

Test: statsd test
Change-Id: I25eaa3b8572074305e61506a693b636adbd3218d
parent e08bef25
Loading
Loading
Loading
Loading
+41 −36
Original line number Original line Diff line number Diff line
@@ -174,11 +174,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
    private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
    private IWifiManager mWifiManager = null;
    private IWifiManager mWifiManager = null;
    private TelephonyManager mTelephony = null;
    private TelephonyManager mTelephony = null;
    private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
    private final StatFs mStatFsSystem =
            new StatFs(Environment.getRootDirectory().getAbsolutePath());
    private final StatFs mStatFsTemp =
            new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
    @GuardedBy("sStatsdLock")
    @GuardedBy("sStatsdLock")
    private final HashSet<Long> mDeathTimeMillis = new HashSet<>();
    private final HashSet<Long> mDeathTimeMillis = new HashSet<>();
    @GuardedBy("sStatsdLock")
    @GuardedBy("sStatsdLock")
@@ -772,7 +767,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private void pullBluetoothBytesTransfer(
    private void pullBluetoothBytesTransfer(
            int tagId, long elapsedNanos, long wallClockNanos,
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
            List<StatsLogEventWrapper> pulledData) {
        BluetoothActivityEnergyInfo info = pullBluetoothData();
        BluetoothActivityEnergyInfo info = fetchBluetoothData();
        if (info.getUidTraffic() != null) {
        if (info.getUidTraffic() != null) {
            for (UidTraffic traffic : info.getUidTraffic()) {
            for (UidTraffic traffic : info.getUidTraffic()) {
                StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos,
                StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos,
@@ -884,9 +879,12 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            int tagId, long elapsedNanos, long wallClockNanos,
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
            List<StatsLogEventWrapper> pulledData) {
        long token = Binder.clearCallingIdentity();
        long token = Binder.clearCallingIdentity();
        synchronized (this) {
            if (mWifiManager == null) {
            if (mWifiManager == null) {
                mWifiManager =
                mWifiManager =
                    IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
                        IWifiManager.Stub.asInterface(
                                ServiceManager.getService(Context.WIFI_SERVICE));
            }
        }
        }
        if (mWifiManager != null) {
        if (mWifiManager != null) {
            try {
            try {
@@ -916,9 +914,11 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            int tagId, long elapsedNanos, long wallClockNanos,
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
            List<StatsLogEventWrapper> pulledData) {
        long token = Binder.clearCallingIdentity();
        long token = Binder.clearCallingIdentity();
        synchronized (this) {
            if (mTelephony == null) {
            if (mTelephony == null) {
                mTelephony = TelephonyManager.from(mContext);
                mTelephony = TelephonyManager.from(mContext);
            }
            }
        }
        if (mTelephony != null) {
        if (mTelephony != null) {
            SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
            SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
            mTelephony.requestModemActivityInfo(modemReceiver);
            mTelephony.requestModemActivityInfo(modemReceiver);
@@ -941,7 +941,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private void pullBluetoothActivityInfo(
    private void pullBluetoothActivityInfo(
            int tagId, long elapsedNanos, long wallClockNanos,
            int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
            List<StatsLogEventWrapper> pulledData) {
        BluetoothActivityEnergyInfo info = pullBluetoothData();
        BluetoothActivityEnergyInfo info = fetchBluetoothData();
        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
        e.writeLong(info.getTimeStamp());
        e.writeLong(info.getTimeStamp());
        e.writeInt(info.getBluetoothStackState());
        e.writeInt(info.getBluetoothStackState());
@@ -952,7 +952,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        pulledData.add(e);
        pulledData.add(e);
    }
    }


    private synchronized BluetoothActivityEnergyInfo pullBluetoothData() {
    private synchronized BluetoothActivityEnergyInfo fetchBluetoothData() {
        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
        if (adapter != null) {
            SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver(
            SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver(
@@ -1323,6 +1323,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {


    private void pullProcessStats(int section, int tagId, long elapsedNanos, long wallClockNanos,
    private void pullProcessStats(int section, int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
            List<StatsLogEventWrapper> pulledData) {
        synchronized (this) {
            try {
            try {
                long lastHighWaterMark = readProcStatsHighWaterMark(section);
                long lastHighWaterMark = readProcStatsHighWaterMark(section);
                List<ParcelFileDescriptor> statsFiles = new ArrayList<>();
                List<ParcelFileDescriptor> statsFiles = new ArrayList<>();
@@ -1331,13 +1332,16 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                if (statsFiles.size() != 1) {
                if (statsFiles.size() != 1) {
                    return;
                    return;
                }
                }
            InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(statsFiles.get(0));
                InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(
                        statsFiles.get(0));
                int[] len = new int[1];
                int[] len = new int[1];
                byte[] stats = readFully(stream, len);
                byte[] stats = readFully(stream, len);
            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
                StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos,
                        wallClockNanos);
                e.writeStorage(Arrays.copyOf(stats, len[0]));
                e.writeStorage(Arrays.copyOf(stats, len[0]));
                pulledData.add(e);
                pulledData.add(e);
            new File(mBaseDir.getAbsolutePath() + "/" + section + "_" + lastHighWaterMark).delete();
                new File(mBaseDir.getAbsolutePath() + "/" + section + "_"
                        + lastHighWaterMark).delete();
                new File(
                new File(
                        mBaseDir.getAbsolutePath() + "/" + section + "_"
                        mBaseDir.getAbsolutePath() + "/" + section + "_"
                                + highWaterMark).createNewFile();
                                + highWaterMark).createNewFile();
@@ -1349,6 +1353,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                Log.e(TAG, "Getting procstats failed: ", e);
                Log.e(TAG, "Getting procstats failed: ", e);
            }
            }
        }
        }
    }


    static byte[] readFully(InputStream stream, int[] outLen) throws IOException {
    static byte[] readFully(InputStream stream, int[] outLen) throws IOException {
        int pos = 0;
        int pos = 0;