| 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.base; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | |
| 9 | import java.util.concurrent.ConcurrentHashMap; |
| 10 | import java.util.Map; |
| 11 | |
| 12 | /** |
| 13 | * Maintains the {@link Context}-to-"context type" mapping. The context type |
| 14 | * {@code MODE_APP} is chosen for the application context associated with |
| 15 | * the activity running in application mode, while {@code MODE_NORMAL} for main |
| 16 | * Chromium activity. |
| 17 | * |
| 18 | * <p>Used as singleton instance. |
| 19 | */ |
| 20 | public class ContextTypes { |
| 21 | |
| 22 | // Available context types. |
| 23 | public static final int CONTEXT_TYPE_NORMAL = 1; |
| 24 | public static final int CONTEXT_TYPE_WEBAPP = 2; |
| 25 | |
| 26 | private final Map<Context, Integer> mContextMap; |
| 27 | |
| 28 | private ContextTypes() { |
| 29 | mContextMap = new ConcurrentHashMap<Context, Integer>(); |
| 30 | } |
| 31 | |
| 32 | private static class ContextTypesHolder { |
| 33 | private static final ContextTypes INSTANCE = new ContextTypes(); |
| 34 | } |
| 35 | |
| 36 | public static ContextTypes getInstance() { |
| 37 | return ContextTypesHolder.INSTANCE; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Adds the mapping for the given {@link Context}. |
| 42 | * |
| 43 | * @param context {@link Context} in interest |
| 44 | * @param type the type associated with the context |
| 45 | * @throws IllegalArgumentException if type is not a valid one. |
| 46 | */ |
| 47 | public void put(Context context, int type) throws IllegalArgumentException { |
| 48 | if (type != CONTEXT_TYPE_NORMAL && type != CONTEXT_TYPE_WEBAPP) { |
| 49 | throw new IllegalArgumentException("Wrong context type"); |
| 50 | } |
| 51 | mContextMap.put(context, type); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Removes the mapping for the given context. |
| 56 | * |
| 57 | * @param context {@link Context} in interest |
| 58 | */ |
| 59 | public void remove(Context context) { |
| 60 | mContextMap.remove(context); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns type of the given context. |
| 65 | * |
| 66 | * @param context {@link Context} in interest |
| 67 | * @return type associated with the context. Returns {@code MODE_NORMAL} by |
| 68 | * default if the mapping for the queried context is not present. |
| 69 | */ |
| 70 | public int getType(Context context) { |
| 71 | Integer contextType = mContextMap.get(context); |
| 72 | return contextType == null ? CONTEXT_TYPE_NORMAL : contextType; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns whether activity is running in web app mode. |
| 77 | * |
| 78 | * @param appContext {@link Context} in interest |
| 79 | * @return {@code true} when activity is running in web app mode. |
| 80 | */ |
| 81 | @CalledByNative |
| 82 | public static boolean isRunningInWebapp(Context appContext) { |
| 83 | return ContextTypes.getInstance().getType(appContext) |
| 84 | == CONTEXT_TYPE_WEBAPP; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Checks if the mapping exists for the given context. |
| 89 | * |
| 90 | * @param context {@link Context} in interest |
| 91 | * @return {@code true} if the mapping exists; otherwise {@code false} |
| 92 | */ |
| 93 | public boolean contains(Context context) { |
| 94 | return mContextMap.containsKey(context); |
| 95 | } |
| 96 | } |