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

Commit 45da5e67 authored by Jeffrey Huang's avatar Jeffrey Huang Committed by Android (Google) Code Review
Browse files

Merge changes Ie1d74670,I249140c5,Ie8f6d1b7,Ifeedbb94

* changes:
  Migrate (AppsOn)ExternalStorageInfo pullers
  Migrate pullRoleHolder to new API
  Migrate pullAppOps to new API
  Migrate pullTimeZoneDataInfo to new API
parents 15415150 6fc44a8d
Loading
Loading
Loading
Loading
+0 −225
Original line number Diff line number Diff line
@@ -856,206 +856,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        pulledData.add(e);
    }

    private void pullAppOps(long elapsedNanos, final long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        long token = Binder.clearCallingIdentity();
        try {
            AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);

            CompletableFuture<HistoricalOps> ops = new CompletableFuture<>();
            HistoricalOpsRequest histOpsRequest =
                    new HistoricalOpsRequest.Builder(0, Long.MAX_VALUE).build();
            appOps.getHistoricalOps(histOpsRequest, mContext.getMainExecutor(), ops::complete);

            HistoricalOps histOps = ops.get(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS,
                    TimeUnit.MILLISECONDS);

            for (int uidIdx = 0; uidIdx < histOps.getUidCount(); uidIdx++) {
                final HistoricalUidOps uidOps = histOps.getUidOpsAt(uidIdx);
                final int uid = uidOps.getUid();
                for (int pkgIdx = 0; pkgIdx < uidOps.getPackageCount(); pkgIdx++) {
                    final HistoricalPackageOps packageOps = uidOps.getPackageOpsAt(pkgIdx);
                    for (int opIdx = 0; opIdx < packageOps.getOpCount(); opIdx++) {
                        final AppOpsManager.HistoricalOp op  = packageOps.getOpAt(opIdx);
                        StatsLogEventWrapper e = new StatsLogEventWrapper(StatsLog.APP_OPS,
                                elapsedNanos, wallClockNanos);

                        e.writeInt(uid);
                        e.writeString(packageOps.getPackageName());
                        e.writeInt(op.getOpCode());
                        e.writeLong(op.getForegroundAccessCount(OP_FLAGS_ALL_TRUSTED));
                        e.writeLong(op.getBackgroundAccessCount(OP_FLAGS_ALL_TRUSTED));
                        e.writeLong(op.getForegroundRejectCount(OP_FLAGS_ALL_TRUSTED));
                        e.writeLong(op.getBackgroundRejectCount(OP_FLAGS_ALL_TRUSTED));
                        e.writeLong(op.getForegroundAccessDuration(OP_FLAGS_ALL_TRUSTED));
                        e.writeLong(op.getBackgroundAccessDuration(OP_FLAGS_ALL_TRUSTED));

                        String perm = AppOpsManager.opToPermission(op.getOpCode());
                        if (perm == null) {
                            e.writeBoolean(false);
                        } else {
                            PermissionInfo permInfo;
                            try {
                                permInfo = mContext.getPackageManager().getPermissionInfo(perm, 0);
                                e.writeBoolean(permInfo.getProtection() == PROTECTION_DANGEROUS);
                            } catch (PackageManager.NameNotFoundException exception) {
                                e.writeBoolean(false);
                            }
                        }

                        pulledData.add(e);
                    }
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, "Could not read appops", t);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }


    /**
     * Add a RoleHolder atom for each package that holds a role.
     *
     * @param elapsedNanos the time since boot
     * @param wallClockNanos the time on the clock
     * @param pulledData the data sink to write to
     */
    private void pullRoleHolders(long elapsedNanos, final long wallClockNanos,
            @NonNull List<StatsLogEventWrapper> pulledData) {
        long callingToken = Binder.clearCallingIdentity();
        try {
            PackageManager pm = mContext.getPackageManager();
            RoleManagerInternal rmi = LocalServices.getService(RoleManagerInternal.class);

            List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers();

            int numUsers = users.size();
            for (int userNum = 0; userNum < numUsers; userNum++) {
                int userId = users.get(userNum).getUserHandle().getIdentifier();

                ArrayMap<String, ArraySet<String>> roles = rmi.getRolesAndHolders(
                        userId);

                int numRoles = roles.size();
                for (int roleNum = 0; roleNum < numRoles; roleNum++) {
                    String roleName = roles.keyAt(roleNum);
                    ArraySet<String> holders = roles.valueAt(roleNum);

                    int numHolders = holders.size();
                    for (int holderNum = 0; holderNum < numHolders; holderNum++) {
                        String holderName = holders.valueAt(holderNum);

                        PackageInfo pkg;
                        try {
                            pkg = pm.getPackageInfoAsUser(holderName, 0, userId);
                        } catch (PackageManager.NameNotFoundException e) {
                            Log.w(TAG, "Role holder " + holderName + " not found");
                            return;
                        }

                        StatsLogEventWrapper e = new StatsLogEventWrapper(StatsLog.ROLE_HOLDER,
                                elapsedNanos, wallClockNanos);
                        e.writeInt(pkg.applicationInfo.uid);
                        e.writeString(holderName);
                        e.writeString(roleName);
                        pulledData.add(e);
                    }
                }
            }
        } finally {
            Binder.restoreCallingIdentity(callingToken);
        }
    }

    private void pullTimeZoneDataInfo(int tagId,
            long elapsedNanos, long wallClockNanos, List<StatsLogEventWrapper> pulledData) {
        String tzDbVersion = "Unknown";
        try {
            tzDbVersion = android.icu.util.TimeZone.getTZDataVersion();
        } catch (Exception e) {
            Log.e(TAG, "Getting tzdb version failed: ", e);
        }

        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
        e.writeString(tzDbVersion);
        pulledData.add(e);
    }

    private void pullExternalStorageInfo(int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        StorageManager storageManager = mContext.getSystemService(StorageManager.class);
        if (storageManager != null) {
            List<VolumeInfo> volumes = storageManager.getVolumes();
            for (VolumeInfo vol : volumes) {
                final String envState = VolumeInfo.getEnvironmentForState(vol.getState());
                final DiskInfo diskInfo = vol.getDisk();
                if (diskInfo != null) {
                    if (envState.equals(Environment.MEDIA_MOUNTED)) {
                        // Get the type of the volume, if it is adoptable or portable.
                        int volumeType = StatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__OTHER;
                        if (vol.getType() == TYPE_PUBLIC) {
                            volumeType = StatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__PUBLIC;
                        } else if (vol.getType() == TYPE_PRIVATE) {
                            volumeType = StatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__PRIVATE;
                        }
                        // Get the type of external storage inserted in the device (sd cards,
                        // usb, etc)
                        int externalStorageType;
                        if (diskInfo.isSd()) {
                            externalStorageType = StorageEnums.SD_CARD;
                        } else if (diskInfo.isUsb()) {
                            externalStorageType = StorageEnums.USB;
                        } else {
                            externalStorageType = StorageEnums.OTHER;
                        }
                        StatsLogEventWrapper e =
                                new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
                        e.writeInt(externalStorageType);
                        e.writeInt(volumeType);
                        e.writeLong(diskInfo.size);
                        pulledData.add(e);
                    }
                }
            }
        }
    }

    private void pullAppsOnExternalStorageInfo(int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        PackageManager pm = mContext.getPackageManager();
        StorageManager storage = mContext.getSystemService(StorageManager.class);
        List<ApplicationInfo> apps = pm.getInstalledApplications(/* flags = */ 0);
        for (ApplicationInfo appInfo : apps) {
            UUID storageUuid = appInfo.storageUuid;
            if (storageUuid != null) {
                VolumeInfo volumeInfo = storage.findVolumeByUuid(appInfo.storageUuid.toString());
                if (volumeInfo != null) {
                    DiskInfo diskInfo = volumeInfo.getDisk();
                    if (diskInfo != null) {
                        int externalStorageType = -1;
                        if (diskInfo.isSd()) {
                            externalStorageType = StorageEnums.SD_CARD;
                        } else if (diskInfo.isUsb()) {
                            externalStorageType = StorageEnums.USB;
                        } else if (appInfo.isExternal()) {
                            externalStorageType = StorageEnums.OTHER;
                        }
                        // App is installed on external storage.
                        if (externalStorageType != -1) {
                            StatsLogEventWrapper e =
                                    new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos);
                            e.writeInt(externalStorageType);
                            e.writeString(appInfo.packageName);
                            pulledData.add(e);
                        }
                    }
                }
            }
        }
    }

    private void pullFaceSettings(int tagId, long elapsedNanos, long wallClockNanos,
            List<StatsLogEventWrapper> pulledData) {
        long callingToken = Binder.clearCallingIdentity();
@@ -1128,36 +928,11 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                break;
            }

            case StatsLog.ROLE_HOLDER: {
                pullRoleHolders(elapsedNanos, wallClockNanos, ret);
                break;
            }

            case StatsLog.TIME_ZONE_DATA_INFO: {
                pullTimeZoneDataInfo(tagId, elapsedNanos, wallClockNanos, ret);
                break;
            }

            case StatsLog.EXTERNAL_STORAGE_INFO: {
                pullExternalStorageInfo(tagId, elapsedNanos, wallClockNanos, ret);
                break;
            }

            case StatsLog.APPS_ON_EXTERNAL_STORAGE_INFO: {
                pullAppsOnExternalStorageInfo(tagId, elapsedNanos, wallClockNanos, ret);
                break;
            }

            case StatsLog.FACE_SETTINGS: {
                pullFaceSettings(tagId, elapsedNanos, wallClockNanos, ret);
                break;
            }

            case StatsLog.APP_OPS: {
                pullAppOps(elapsedNanos, wallClockNanos, ret);
                break;
            }

            default:
                Slog.w(TAG, "No such tagId data as " + tagId);
                return null;
+0 −20
Original line number Diff line number Diff line
@@ -104,21 +104,9 @@ std::map<PullerKey, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
         {.additiveFields = {1, 2, 3, 4},
          .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},

        // RoleHolder.
        {{.atomTag = android::util::ROLE_HOLDER},
         {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},

        // TrainInfo.
        {{.atomTag = android::util::TRAIN_INFO}, {.puller = new TrainInfoPuller()}},

        // TimeZoneDataInfo.
        {{.atomTag = android::util::TIME_ZONE_DATA_INFO},
         {.puller = new StatsCompanionServicePuller(android::util::TIME_ZONE_DATA_INFO)}},

        // ExternalStorageInfo
        {{.atomTag = android::util::EXTERNAL_STORAGE_INFO},
         {.puller = new StatsCompanionServicePuller(android::util::EXTERNAL_STORAGE_INFO)}},

        // GpuStatsGlobalInfo
        {{.atomTag = android::util::GPU_STATS_GLOBAL_INFO},
         {.puller = new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)}},
@@ -127,18 +115,10 @@ std::map<PullerKey, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
        {{.atomTag = android::util::GPU_STATS_APP_INFO},
         {.puller = new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)}},

        // AppsOnExternalStorageInfo
        {{.atomTag = android::util::APPS_ON_EXTERNAL_STORAGE_INFO},
         {.puller = new StatsCompanionServicePuller(android::util::APPS_ON_EXTERNAL_STORAGE_INFO)}},

        // Face Settings
        {{.atomTag = android::util::FACE_SETTINGS},
         {.puller = new StatsCompanionServicePuller(android::util::FACE_SETTINGS)}},

        // App ops
        {{.atomTag = android::util::APP_OPS},
         {.puller = new StatsCompanionServicePuller(android::util::APP_OPS)}},

        // VmsClientStats
        {{.atomTag = android::util::VMS_CLIENT_STATS},
         {.additiveFields = {5, 6, 7, 8, 9, 10},
+240 −15

File changed.

Preview size limit exceeded, changes collapsed.