michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.gfx.BitmapUtils; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import android.content.Context; michael@0: import android.content.SharedPreferences; michael@0: import android.content.SharedPreferences.Editor; michael@0: import android.graphics.Bitmap; michael@0: import android.util.Log; michael@0: michael@0: import java.util.ArrayList; michael@0: michael@0: public class WebappAllocator { michael@0: private final String LOGTAG = "GeckoWebappAllocator"; michael@0: // The number of Webapp# and WEBAPP# activites/apps/intents michael@0: private final static int MAX_WEB_APPS = 100; michael@0: michael@0: protected static WebappAllocator sInstance = null; michael@0: public static WebappAllocator getInstance() { michael@0: return getInstance(GeckoAppShell.getContext()); michael@0: } michael@0: michael@0: public static synchronized WebappAllocator getInstance(Context cx) { michael@0: if (sInstance == null) { michael@0: sInstance = new WebappAllocator(cx); michael@0: } michael@0: michael@0: return sInstance; michael@0: } michael@0: michael@0: SharedPreferences mPrefs; michael@0: michael@0: protected WebappAllocator(Context context) { michael@0: mPrefs = context.getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS); michael@0: } michael@0: michael@0: public static String appKey(int index) { michael@0: return "app" + index; michael@0: } michael@0: michael@0: static public String iconKey(int index) { michael@0: return "icon" + index; michael@0: } michael@0: michael@0: public synchronized int findAndAllocateIndex(String app, String name, String aIconData) { michael@0: Bitmap icon = (aIconData != null) ? BitmapUtils.getBitmapFromDataURI(aIconData) : null; michael@0: return findAndAllocateIndex(app, name, icon); michael@0: } michael@0: michael@0: public synchronized int findAndAllocateIndex(final String app, final String name, final Bitmap aIcon) { michael@0: int index = getIndexForApp(app); michael@0: if (index != -1) michael@0: return index; michael@0: michael@0: for (int i = 0; i < MAX_WEB_APPS; ++i) { michael@0: if (!mPrefs.contains(appKey(i))) { michael@0: // found unused index i michael@0: updateAppAllocation(app, i, aIcon); michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: // no more apps! michael@0: return -1; michael@0: } michael@0: michael@0: public synchronized void updateAppAllocation(final String app, michael@0: final int index, michael@0: final Bitmap aIcon) { michael@0: if (aIcon != null) { michael@0: ThreadUtils.getBackgroundHandler().post(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: int color = 0; michael@0: try { michael@0: color = BitmapUtils.getDominantColor(aIcon); michael@0: } catch (Exception e) { michael@0: Log.e(LOGTAG, "Exception during getDominantColor", e); michael@0: } michael@0: mPrefs.edit() michael@0: .putString(appKey(index), app) michael@0: .putInt(iconKey(index), color).commit(); michael@0: } michael@0: }); michael@0: } else { michael@0: mPrefs.edit() michael@0: .putString(appKey(index), app) michael@0: .putInt(iconKey(index), 0).commit(); michael@0: } michael@0: } michael@0: michael@0: public synchronized int getIndexForApp(String app) { michael@0: for (int i = 0; i < MAX_WEB_APPS; ++i) { michael@0: if (mPrefs.getString(appKey(i), "").equals(app)) { michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: public synchronized String getAppForIndex(int index) { michael@0: return mPrefs.getString(appKey(index), null); michael@0: } michael@0: michael@0: public synchronized int releaseIndexForApp(String app) { michael@0: int index = getIndexForApp(app); michael@0: if (index == -1) michael@0: return -1; michael@0: michael@0: releaseIndex(index); michael@0: return index; michael@0: } michael@0: michael@0: public synchronized void releaseIndex(final int index) { michael@0: ThreadUtils.postToBackgroundThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: mPrefs.edit() michael@0: .remove(appKey(index)) michael@0: .remove(iconKey(index)) michael@0: .commit(); michael@0: } michael@0: }); michael@0: } michael@0: }