2012-06-18 10:03:03 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-04-08 00:51:56 -07:00
|
|
|
import org.mozilla.gecko.gfx.BitmapUtils;
|
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
|
|
|
|
2012-06-18 10:03:03 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2012-09-19 10:24:26 -07:00
|
|
|
import android.graphics.Bitmap;
|
2013-03-25 13:03:58 -07:00
|
|
|
import android.util.Log;
|
2012-09-19 10:24:26 -07:00
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public class WebAppAllocator {
|
2012-09-19 10:24:26 -07:00
|
|
|
private final String LOGTAG = "GeckoWebAppAllocator";
|
2012-06-18 10:03:03 -07:00
|
|
|
// The number of WebApp# and WEBAPP# activites/apps/intents
|
2012-07-13 16:19:26 -07:00
|
|
|
private final static int MAX_WEB_APPS = 100;
|
2012-06-18 10:03:03 -07:00
|
|
|
|
|
|
|
protected static WebAppAllocator sInstance = null;
|
|
|
|
public static WebAppAllocator getInstance() {
|
2013-05-24 09:21:01 -07:00
|
|
|
return getInstance(GeckoAppShell.getContext());
|
2012-06-18 10:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized WebAppAllocator getInstance(Context cx) {
|
|
|
|
if (sInstance == null) {
|
2012-08-22 08:37:14 -07:00
|
|
|
sInstance = new WebAppAllocator(cx);
|
|
|
|
}
|
2012-06-18 10:03:03 -07:00
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedPreferences mPrefs;
|
|
|
|
|
|
|
|
protected WebAppAllocator(Context context) {
|
|
|
|
mPrefs = context.getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:24:26 -07:00
|
|
|
public static String appKey(int index) {
|
2012-06-18 10:03:03 -07:00
|
|
|
return "app" + index;
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:24:26 -07:00
|
|
|
static public String iconKey(int index) {
|
|
|
|
return "icon" + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int findAndAllocateIndex(String app, String name, String aIconData) {
|
2013-03-25 13:03:58 -07:00
|
|
|
Bitmap icon = (aIconData != null) ? BitmapUtils.getBitmapFromDataURI(aIconData) : null;
|
|
|
|
return findAndAllocateIndex(app, name, icon);
|
2012-09-19 10:24:26 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 08:55:52 -07:00
|
|
|
public synchronized int findAndAllocateIndex(final String app, final String name, final Bitmap aIcon) {
|
2012-06-18 10:03:03 -07:00
|
|
|
int index = getIndexForApp(app);
|
|
|
|
if (index != -1)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_WEB_APPS; ++i) {
|
|
|
|
if (!mPrefs.contains(appKey(i))) {
|
|
|
|
// found unused index i
|
2013-03-25 13:03:58 -07:00
|
|
|
updateAppAllocation(app, i, aIcon);
|
2012-06-18 10:03:03 -07:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no more apps!
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-25 13:03:58 -07:00
|
|
|
public synchronized void updateAppAllocation(final String app,
|
|
|
|
final int index,
|
|
|
|
final Bitmap aIcon) {
|
|
|
|
if (aIcon != null) {
|
|
|
|
ThreadUtils.getBackgroundHandler().post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int color = 0;
|
|
|
|
try {
|
|
|
|
color = BitmapUtils.getDominantColor(aIcon);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception during getDominantColor", e);
|
|
|
|
}
|
|
|
|
mPrefs.edit()
|
|
|
|
.putString(appKey(index), app)
|
|
|
|
.putInt(iconKey(index), color).commit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
mPrefs.edit()
|
|
|
|
.putString(appKey(index), app)
|
|
|
|
.putInt(iconKey(index), 0).commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 10:03:03 -07:00
|
|
|
public synchronized int getIndexForApp(String app) {
|
|
|
|
for (int i = 0; i < MAX_WEB_APPS; ++i) {
|
|
|
|
if (mPrefs.getString(appKey(i), "").equals(app)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized String getAppForIndex(int index) {
|
2012-09-19 10:24:26 -07:00
|
|
|
return mPrefs.getString(appKey(index), null);
|
2012-06-18 10:03:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int releaseIndexForApp(String app) {
|
|
|
|
int index = getIndexForApp(app);
|
|
|
|
if (index == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
releaseIndex(index);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2012-10-02 18:58:03 -07:00
|
|
|
public synchronized void releaseIndex(final int index) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-10-02 18:58:03 -07:00
|
|
|
public void run() {
|
|
|
|
mPrefs.edit()
|
|
|
|
.remove(appKey(index))
|
|
|
|
.remove(iconKey(index))
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
});
|
2012-06-18 10:03:03 -07:00
|
|
|
}
|
|
|
|
}
|