Search code examples
androidwebviewweb-storage

How to get the WebStorage usage/quota for an Android webview?


I want to get the usage (in bytes) used by an Android Webview.

I did get through http://developer.android.com/reference/android/webkit/WebStorage.html but the documentation is not very clear for me.

Any clue on how which origins should I use ?

for now I'm stuck here :

    WebStorage
            .getInstance()
            .getOrigins(
                    new ValueCallback<Map>() {
                                            @Override
                                            public void onReceiveValue(Map webStorageOrigins) {
                                                //WebStorage.getInstance().getUsageForOrigin();
                                                // What now with : webStorageOrigins;

                                            }
                                        }
            );

Solution

  • Found It ! thanks to somebody at https://code.google.com/p/android/issues/detail?id=24180

                WebStorage.getInstance().getOrigins(new ValueCallback<Map> () {
                    @Override
                    public void onReceiveValue(Map map) {
                        for(Object key : map.keySet()) {
                            if(Build.VERSION.SDK_INT >= 11) {
                                WebStorage.Origin origin = (WebStorage.Origin) map.get(key);
                                Log.e("AppCache", String.format("Origin: %s Quota: %s Usage: %s",
                                        origin.getOrigin(), origin.getQuota(), origin.getUsage()));
                            } else {
                                Log.e("AppCache", "Key: " + key + " Value: " + map.get(key));
                            }
                        }
                    }
                });
    

    I was having hard time to iterate on the Map.

    Other way to do it when you have the origin string

        WebStorage.getInstance().getUsageForOrigin(TheUrl, new ValueCallback<Long>() {
            @Override
                public void onReceiveValue(Long usage) {
                Log.e("AppCache", "Usage:onReceiveValue: "+ usage);
            }
        });