OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 org.chromium.base.annotations.CalledByNative; |
| 10 import org.chromium.base.annotations.JNINamespace; |
| 11 |
| 12 /** |
| 13 * This class provides Android Context utility methods. |
| 14 */ |
| 15 @JNINamespace("base::android") |
| 16 public class ContextUtils { |
| 17 private static Context sApplicationContext; |
| 18 |
| 19 /** |
| 20 * Get the Android application context. |
| 21 * |
| 22 * Under normal circumstances there is only one application context in a pro
cess, so it's safe |
| 23 * to treat this as a global. In WebView it's possible for more than one app
using WebView to be |
| 24 * running in a single process, but this mechanism is rarely used and this i
s not the only |
| 25 * problem in that scenario, so we don't currently forbid using it as a glob
al. |
| 26 * |
| 27 * Do not downcast the context returned by this method to Application (or an
y subclass). It may |
| 28 * not be an Application object; it may be wrapped in a ContextWrapper. The
only assumption you |
| 29 * may make is that it is a Context whose lifetime is the same as the lifeti
me of the process. |
| 30 */ |
| 31 public static Context getApplicationContext() { |
| 32 assert sApplicationContext != null; |
| 33 return sApplicationContext; |
| 34 } |
| 35 |
| 36 /** |
| 37 * Initialize the Android application context. |
| 38 * |
| 39 * Either this or the native equivalent base::android::InitApplicationContex
t must be called |
| 40 * once during startup. JNI bindings must have been initialized, as the cont
ext is stored on |
| 41 * both sides. |
| 42 */ |
| 43 public static void initApplicationContext(Context appContext) { |
| 44 initJavaSideApplicationContext(appContext); |
| 45 nativeInitNativeSideApplicationContext(appContext); |
| 46 } |
| 47 |
| 48 @CalledByNative |
| 49 private static void initJavaSideApplicationContext(Context appContext) { |
| 50 assert appContext != null; |
| 51 assert sApplicationContext == null || sApplicationContext == appContext; |
| 52 sApplicationContext = appContext; |
| 53 } |
| 54 |
| 55 private static native void nativeInitNativeSideApplicationContext(Context ap
pContext); |
| 56 } |
OLD | NEW |