如何在Android上使用StorageStatsManager.queryStatsForPackage O?

How to use StorageStatsManager.queryStatsForPackage on Android O?

背景

之前 Android O,为了得到一个应用程序的大小,你必须有一个特殊的权限,并使用一个隐藏的 API。

问题

这样的解决方案将不再有效,但是 Google 提供了一个官方的 API 更细粒度的:queryStatsForPackage , which returns StorageStats 对象。

但是,我找不到任何关于如何使用它的信息。

与只需要应用程序包名称的隐藏 API 相反,这个还需要 "String volumeUuid" 和 "UserHandle user"。

问题

  1. 如何提供这些参数?
  2. 它们是什么意思?每个 volumeUuid 是指不同的存储(例如内置存储和真正的 sd 卡),而 UserHandle 是针对每个用户的吗?
  3. 如果 #2 是正确的,我真的应该为每个可能的值调用它们中的每一个,以获得应用程序实际占用的总存储空间吗?如果是,怎么做?

将 null 作为 VolumeUuid 传递,以获取默认内部存储的值,或通过 StorageManager 系统服务获取特定卷。

您可以通过Process.myUserHandle() 获取您自己的UserHandle。当您在您的设备上为某人提供访客登录时,或者当设备上安装了 Android 工作配置文件时,系统的其他用户就会出现。我认为没有一种简单的方法可以枚举有权访问该设备的所有用户。

好的,我知道如何使用新的 API。我已经为此准备了一个小样本,获取有关卷存储和特定应用程序的一些信息(这次是 Play 商店,但您可以根据需要更改它):

public class MainActivity extends AppCompatActivity {

    public static final String PACKAGE_NAME = "com.android.vending";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!hasUsageStatsPermission(this))
            startActivityForResult(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY), 1);
        else {
            final Context context = this;
            AsyncTask.execute(new Runnable() {
                @TargetApi(VERSION_CODES.O)
                @Override
                public void run() {
                    @SuppressLint("WrongConstant") final StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
                    final StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
                    final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
                    final UserHandle user = android.os.Process.myUserHandle();
                    for (StorageVolume storageVolume : storageVolumes) {
                        final String uuidStr = storageVolume.getUuid();
                        final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
                        try {
                            Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(context) + " : " + storageVolume.getState());
                            Log.d("AppLog", "getFreeBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getFreeBytes(uuid)));
                            Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(context, storageStatsManager.getTotalBytes(uuid)));
                            Log.d("AppLog", "storage stats for app of package name:" + PACKAGE_NAME + " : ");

                            final StorageStats storageStats = storageStatsManager.queryStatsForPackage(uuid, PACKAGE_NAME, user);
                            Log.d("AppLog", "getAppBytes:" + Formatter.formatShortFileSize(context, storageStats.getAppBytes()) +
                                    " getCacheBytes:" + Formatter.formatShortFileSize(context, storageStats.getCacheBytes()) +
                                    " getDataBytes:" + Formatter.formatShortFileSize(context, storageStats.getDataBytes()));
                        } catch (NameNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });

        }
    }

    @TargetApi(VERSION_CODES.M)
    public static boolean hasUsageStatsPermission(Context context) {
        //
        if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)
            return false;
        AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        final int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), context.getPackageName());
        boolean granted = false;
        if (mode == AppOpsManager.MODE_DEFAULT)
            granted = (context.checkCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) == PackageManager.PERMISSION_GRANTED);
        else
            granted = (mode == AppOpsManager.MODE_ALLOWED);
        return granted;
    }
}

清单文件应包含:

<uses-permission
    android:name="android.permission.PACKAGE_USAGE_STATS"
    tools:ignore="ProtectedPermissions"/>

编辑:请注意 UUID.fromString(uuidStr) 的部分对于 SD 卡可能会失败,Google 的原因是 this:

Android can't maintain quota-style statistics for public volumes (SD card etc.). Apps will need to build size calculation logic themselves.

遗憾的是,在 Android 11 日,由于到达“Android”文件夹的新限制,即使这是不可能的。在此请求更改它:

@t0m 但即使按照他们写的,按照他们所说的去做也不是正式不可能的,因为存储权限在 Android 11 上受到了非常严格的限制,不允许你达到 Android文件夹正确。要求在此处解决此问题:

请考虑主演。

另一种获取 UUID 的方法。

要获取UUID,您还可以使用StorageManager.getUuidForPath(文件路径)函数。 至于路径,使用 Context.getFilesDir() 和 Context.getExternalFilesDirs() 获取应用数据将保存到的所有可能目录。

请注意,不同的路径可能 return 相同的 UUID,因此您必须使用哈希集来收集唯一的 UUID