mobile/android/base/WebappAllocator.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/WebappAllocator.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko;
    1.10 +
    1.11 +import org.mozilla.gecko.gfx.BitmapUtils;
    1.12 +import org.mozilla.gecko.util.ThreadUtils;
    1.13 +
    1.14 +import android.content.Context;
    1.15 +import android.content.SharedPreferences;
    1.16 +import android.content.SharedPreferences.Editor;
    1.17 +import android.graphics.Bitmap;
    1.18 +import android.util.Log;
    1.19 +
    1.20 +import java.util.ArrayList;
    1.21 +
    1.22 +public class WebappAllocator {
    1.23 +    private final String LOGTAG = "GeckoWebappAllocator";
    1.24 +    // The number of Webapp# and WEBAPP# activites/apps/intents
    1.25 +    private final static int MAX_WEB_APPS = 100;
    1.26 +
    1.27 +    protected static WebappAllocator sInstance = null;
    1.28 +    public static WebappAllocator getInstance() {
    1.29 +        return getInstance(GeckoAppShell.getContext());
    1.30 +    }
    1.31 +
    1.32 +    public static synchronized WebappAllocator getInstance(Context cx) {
    1.33 +        if (sInstance == null) {
    1.34 +            sInstance = new WebappAllocator(cx);
    1.35 +        }
    1.36 +
    1.37 +        return sInstance;
    1.38 +    }
    1.39 +
    1.40 +    SharedPreferences mPrefs;
    1.41 +
    1.42 +    protected WebappAllocator(Context context) {
    1.43 +        mPrefs = context.getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
    1.44 +    }
    1.45 +
    1.46 +    public static String appKey(int index) {
    1.47 +        return "app" + index;
    1.48 +    }
    1.49 +
    1.50 +    static public String iconKey(int index) {
    1.51 +        return "icon" + index;
    1.52 +    }
    1.53 +
    1.54 +    public synchronized int findAndAllocateIndex(String app, String name, String aIconData) {
    1.55 +        Bitmap icon = (aIconData != null) ? BitmapUtils.getBitmapFromDataURI(aIconData) : null;
    1.56 +        return findAndAllocateIndex(app, name, icon);
    1.57 +    }
    1.58 +
    1.59 +    public synchronized int findAndAllocateIndex(final String app, final String name, final Bitmap aIcon) {
    1.60 +        int index = getIndexForApp(app);
    1.61 +        if (index != -1)
    1.62 +            return index;
    1.63 +
    1.64 +        for (int i = 0; i < MAX_WEB_APPS; ++i) {
    1.65 +            if (!mPrefs.contains(appKey(i))) {
    1.66 +                // found unused index i
    1.67 +                updateAppAllocation(app, i, aIcon);
    1.68 +                return i;
    1.69 +            }
    1.70 +        }
    1.71 +
    1.72 +        // no more apps!
    1.73 +        return -1;
    1.74 +    }
    1.75 +
    1.76 +    public synchronized void updateAppAllocation(final String app,
    1.77 +                                                 final int index,
    1.78 +                                                 final Bitmap aIcon) {
    1.79 +        if (aIcon != null) {
    1.80 +            ThreadUtils.getBackgroundHandler().post(new Runnable() {
    1.81 +                @Override
    1.82 +                public void run() {
    1.83 +                    int color = 0;
    1.84 +                    try {
    1.85 +                        color = BitmapUtils.getDominantColor(aIcon);
    1.86 +                    } catch (Exception e) {
    1.87 +                        Log.e(LOGTAG, "Exception during getDominantColor", e);
    1.88 +                    }
    1.89 +                    mPrefs.edit()
    1.90 +                          .putString(appKey(index), app)
    1.91 +                          .putInt(iconKey(index), color).commit();
    1.92 +                }
    1.93 +            });
    1.94 +        } else {
    1.95 +            mPrefs.edit()
    1.96 +                  .putString(appKey(index), app)
    1.97 +                  .putInt(iconKey(index), 0).commit();
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    public synchronized int getIndexForApp(String app) {
   1.102 +        for (int i = 0; i < MAX_WEB_APPS; ++i) {
   1.103 +            if (mPrefs.getString(appKey(i), "").equals(app)) {
   1.104 +                return i;
   1.105 +            }
   1.106 +        }
   1.107 +
   1.108 +        return -1;
   1.109 +    }
   1.110 +
   1.111 +    public synchronized String getAppForIndex(int index) {
   1.112 +        return mPrefs.getString(appKey(index), null);
   1.113 +    }
   1.114 +
   1.115 +    public synchronized int releaseIndexForApp(String app) {
   1.116 +        int index = getIndexForApp(app);
   1.117 +        if (index == -1)
   1.118 +            return -1;
   1.119 +
   1.120 +        releaseIndex(index);
   1.121 +        return index;
   1.122 +    }
   1.123 +
   1.124 +    public synchronized void releaseIndex(final int index) {
   1.125 +        ThreadUtils.postToBackgroundThread(new Runnable() {
   1.126 +            @Override
   1.127 +            public void run() {
   1.128 +                mPrefs.edit()
   1.129 +                    .remove(appKey(index))
   1.130 +                    .remove(iconKey(index))
   1.131 +                    .commit();
   1.132 +            }
   1.133 +        });
   1.134 +    }
   1.135 +}

mercurial