mobile/android/base/WebappAllocator.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import org.mozilla.gecko.gfx.BitmapUtils;
michael@0 9 import org.mozilla.gecko.util.ThreadUtils;
michael@0 10
michael@0 11 import android.content.Context;
michael@0 12 import android.content.SharedPreferences;
michael@0 13 import android.content.SharedPreferences.Editor;
michael@0 14 import android.graphics.Bitmap;
michael@0 15 import android.util.Log;
michael@0 16
michael@0 17 import java.util.ArrayList;
michael@0 18
michael@0 19 public class WebappAllocator {
michael@0 20 private final String LOGTAG = "GeckoWebappAllocator";
michael@0 21 // The number of Webapp# and WEBAPP# activites/apps/intents
michael@0 22 private final static int MAX_WEB_APPS = 100;
michael@0 23
michael@0 24 protected static WebappAllocator sInstance = null;
michael@0 25 public static WebappAllocator getInstance() {
michael@0 26 return getInstance(GeckoAppShell.getContext());
michael@0 27 }
michael@0 28
michael@0 29 public static synchronized WebappAllocator getInstance(Context cx) {
michael@0 30 if (sInstance == null) {
michael@0 31 sInstance = new WebappAllocator(cx);
michael@0 32 }
michael@0 33
michael@0 34 return sInstance;
michael@0 35 }
michael@0 36
michael@0 37 SharedPreferences mPrefs;
michael@0 38
michael@0 39 protected WebappAllocator(Context context) {
michael@0 40 mPrefs = context.getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
michael@0 41 }
michael@0 42
michael@0 43 public static String appKey(int index) {
michael@0 44 return "app" + index;
michael@0 45 }
michael@0 46
michael@0 47 static public String iconKey(int index) {
michael@0 48 return "icon" + index;
michael@0 49 }
michael@0 50
michael@0 51 public synchronized int findAndAllocateIndex(String app, String name, String aIconData) {
michael@0 52 Bitmap icon = (aIconData != null) ? BitmapUtils.getBitmapFromDataURI(aIconData) : null;
michael@0 53 return findAndAllocateIndex(app, name, icon);
michael@0 54 }
michael@0 55
michael@0 56 public synchronized int findAndAllocateIndex(final String app, final String name, final Bitmap aIcon) {
michael@0 57 int index = getIndexForApp(app);
michael@0 58 if (index != -1)
michael@0 59 return index;
michael@0 60
michael@0 61 for (int i = 0; i < MAX_WEB_APPS; ++i) {
michael@0 62 if (!mPrefs.contains(appKey(i))) {
michael@0 63 // found unused index i
michael@0 64 updateAppAllocation(app, i, aIcon);
michael@0 65 return i;
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 // no more apps!
michael@0 70 return -1;
michael@0 71 }
michael@0 72
michael@0 73 public synchronized void updateAppAllocation(final String app,
michael@0 74 final int index,
michael@0 75 final Bitmap aIcon) {
michael@0 76 if (aIcon != null) {
michael@0 77 ThreadUtils.getBackgroundHandler().post(new Runnable() {
michael@0 78 @Override
michael@0 79 public void run() {
michael@0 80 int color = 0;
michael@0 81 try {
michael@0 82 color = BitmapUtils.getDominantColor(aIcon);
michael@0 83 } catch (Exception e) {
michael@0 84 Log.e(LOGTAG, "Exception during getDominantColor", e);
michael@0 85 }
michael@0 86 mPrefs.edit()
michael@0 87 .putString(appKey(index), app)
michael@0 88 .putInt(iconKey(index), color).commit();
michael@0 89 }
michael@0 90 });
michael@0 91 } else {
michael@0 92 mPrefs.edit()
michael@0 93 .putString(appKey(index), app)
michael@0 94 .putInt(iconKey(index), 0).commit();
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 public synchronized int getIndexForApp(String app) {
michael@0 99 for (int i = 0; i < MAX_WEB_APPS; ++i) {
michael@0 100 if (mPrefs.getString(appKey(i), "").equals(app)) {
michael@0 101 return i;
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 return -1;
michael@0 106 }
michael@0 107
michael@0 108 public synchronized String getAppForIndex(int index) {
michael@0 109 return mPrefs.getString(appKey(index), null);
michael@0 110 }
michael@0 111
michael@0 112 public synchronized int releaseIndexForApp(String app) {
michael@0 113 int index = getIndexForApp(app);
michael@0 114 if (index == -1)
michael@0 115 return -1;
michael@0 116
michael@0 117 releaseIndex(index);
michael@0 118 return index;
michael@0 119 }
michael@0 120
michael@0 121 public synchronized void releaseIndex(final int index) {
michael@0 122 ThreadUtils.postToBackgroundThread(new Runnable() {
michael@0 123 @Override
michael@0 124 public void run() {
michael@0 125 mPrefs.edit()
michael@0 126 .remove(appKey(index))
michael@0 127 .remove(iconKey(index))
michael@0 128 .commit();
michael@0 129 }
michael@0 130 });
michael@0 131 }
michael@0 132 }

mercurial