mobile/android/base/WebappAllocator.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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/. */
     6 package org.mozilla.gecko;
     8 import org.mozilla.gecko.gfx.BitmapUtils;
     9 import org.mozilla.gecko.util.ThreadUtils;
    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;
    17 import java.util.ArrayList;
    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;
    24     protected static WebappAllocator sInstance = null;
    25     public static WebappAllocator getInstance() {
    26         return getInstance(GeckoAppShell.getContext());
    27     }
    29     public static synchronized WebappAllocator getInstance(Context cx) {
    30         if (sInstance == null) {
    31             sInstance = new WebappAllocator(cx);
    32         }
    34         return sInstance;
    35     }
    37     SharedPreferences mPrefs;
    39     protected WebappAllocator(Context context) {
    40         mPrefs = context.getSharedPreferences("webapps", Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
    41     }
    43     public static String appKey(int index) {
    44         return "app" + index;
    45     }
    47     static public String iconKey(int index) {
    48         return "icon" + index;
    49     }
    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     }
    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;
    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         }
    69         // no more apps!
    70         return -1;
    71     }
    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     }
    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         }
   105         return -1;
   106     }
   108     public synchronized String getAppForIndex(int index) {
   109         return mPrefs.getString(appKey(index), null);
   110     }
   112     public synchronized int releaseIndexForApp(String app) {
   113         int index = getIndexForApp(app);
   114         if (index == -1)
   115             return -1;
   117         releaseIndex(index);
   118         return index;
   119     }
   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