mobile/android/base/WebappAllocator.java

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

mercurial