Commit a0cfa6d4 authored by Kartikaya Gupta's avatar Kartikaya Gupta
Browse files

Bug 789923 - Drop saved thumbnails when device storage is low. r=mfinkle, lucasr

parent e6dbae8c
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@

package org.mozilla.gecko;

import org.mozilla.gecko.db.BrowserDB;

import android.content.BroadcastReceiver;
import android.content.ComponentCallbacks2;
import android.content.Context;
@@ -106,8 +108,7 @@ class MemoryMonitor extends BroadcastReceiver {
        if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(intent.getAction())) {
            Log.d(LOGTAG, "Device storage is low");
            mStoragePressure = true;
            // TODO: drop or shrink disk caches
            // TODO: drop stuff from browser.db
            GeckoAppShell.getHandler().post(new StorageReducer());
        } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(intent.getAction())) {
            Log.d(LOGTAG, "Device storage is ok");
            mStoragePressure = false;
@@ -201,4 +202,23 @@ class MemoryMonitor extends BroadcastReceiver {
            GeckoAppShell.getHandler().postDelayed(this, DECREMENT_DELAY);
        }
    }

    class StorageReducer implements Runnable {
        @Override
        public void run() {
            // this might get run right on startup, if so wait 10 seconds and try again
            if (!GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning)) {
                GeckoAppShell.getHandler().postDelayed(this, 10000);
                return;
            }

            if (!mStoragePressure) {
                // pressure is off, so we can abort
                return;
            }

            BrowserDB.removeThumbnails(Tabs.getInstance().getContentResolver());
            // TODO: drop or shrink disk caches
        }
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -75,6 +75,8 @@ public class BrowserDB {

        public byte[] getThumbnailForUrl(ContentResolver cr, String uri);

        public void removeThumbnails(ContentResolver cr);

        public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);

        public void registerHistoryObserver(ContentResolver cr, ContentObserver observer);
@@ -186,6 +188,10 @@ public class BrowserDB {
        return sDb.getThumbnailForUrl(cr, uri);
    }

    public static void removeThumbnails(ContentResolver cr) {
        sDb.removeThumbnails(cr);
    }

    public static void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
        sDb.registerBookmarkObserver(cr, observer);
    }
+3 −3
Original line number Diff line number Diff line
@@ -1662,10 +1662,10 @@ public class BrowserProvider extends ContentProvider {

                String url = values.getAsString(Images.URL);

                // if no URL is provided, update all of the entries
                if (TextUtils.isEmpty(url))
                    throw new IllegalArgumentException("Images.URL is required");

                if (shouldUpdateOrInsert(uri))
                    updated = updateExistingImage(uri, values, null, null);
                else if (shouldUpdateOrInsert(uri))
                    updated = updateOrInsertImage(uri, values, Images.URL + " = ?",
                                                  new String[] { url });
                else
+6 −0
Original line number Diff line number Diff line
@@ -680,6 +680,12 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
        return b;
    }

    public void removeThumbnails(ContentResolver cr) {
        ContentValues values = new ContentValues();
        values.putNull(Images.THUMBNAIL);
        cr.update(mImagesUriWithProfile, values, null, null);
    }

    // Utility function for updating existing history using batch operations
    public void updateHistoryInBatch(ContentResolver cr,
                                     Collection<ContentProviderOperation> operations,