OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.app.ActivityManager; | 7 import android.app.ActivityManager; |
8 import android.content.ComponentCallbacks2; | 8 import android.content.ComponentCallbacks2; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.res.Configuration; | 10 import android.content.res.Configuration; |
11 | 11 |
| 12 import org.chromium.base.Log; |
12 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
13 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
14 | 15 |
15 /** | 16 /** |
16 * Android implementation of MemoryMonitor. | 17 * Android implementation of MemoryMonitor. |
17 */ | 18 */ |
18 @JNINamespace("content") | 19 @JNINamespace("content") |
19 class MemoryMonitorAndroid { | 20 class MemoryMonitorAndroid { |
| 21 private static final String TAG = "MemoryMonitorAndroid"; |
20 private static final ActivityManager.MemoryInfo sMemoryInfo = | 22 private static final ActivityManager.MemoryInfo sMemoryInfo = |
21 new ActivityManager.MemoryInfo(); | 23 new ActivityManager.MemoryInfo(); |
22 private static ComponentCallbacks2 sCallbacks = null; | 24 private static ComponentCallbacks2 sCallbacks = null; |
23 | 25 |
24 private MemoryMonitorAndroid() { | 26 private MemoryMonitorAndroid() { |
25 } | 27 } |
26 | 28 |
27 /** | 29 /** |
28 * Get the current MemoryInfo from ActivityManager and invoke the native | 30 * Get the current MemoryInfo from ActivityManager and invoke the native |
29 * callback to populate the MemoryInfo. | 31 * callback to populate the MemoryInfo. |
30 * | 32 * |
31 * @param context The context of the application. | 33 * @param context The context of the application. |
32 * @param outPtr A native output pointer to populate MemoryInfo. This is | 34 * @param outPtr A native output pointer to populate MemoryInfo. This is |
33 * passed back to the native callback. | 35 * passed back to the native callback. |
34 */ | 36 */ |
35 @CalledByNative | 37 @CalledByNative |
36 private static void getMemoryInfo(Context context, long outPtr) { | 38 private static void getMemoryInfo(Context context, long outPtr) { |
37 ActivityManager am = (ActivityManager) context.getSystemService( | 39 ActivityManager am = (ActivityManager) context.getSystemService( |
38 Context.ACTIVITY_SERVICE); | 40 Context.ACTIVITY_SERVICE); |
39 am.getMemoryInfo(sMemoryInfo); | 41 try { |
| 42 am.getMemoryInfo(sMemoryInfo); |
| 43 } catch (RuntimeException e) { |
| 44 // RuntimeException can be thrown when the system is going to |
| 45 // restart. Pass arbitrary values to the callback. |
| 46 Log.e(TAG, |
| 47 "Failed to get memory info due to a runtime exception: %s", |
| 48 e); |
| 49 sMemoryInfo.availMem = 1; |
| 50 sMemoryInfo.lowMemory = true; |
| 51 sMemoryInfo.threshold = 1; |
| 52 sMemoryInfo.totalMem = 1; |
| 53 } |
40 nativeGetMemoryInfoCallback( | 54 nativeGetMemoryInfoCallback( |
41 sMemoryInfo.availMem, sMemoryInfo.lowMemory, | 55 sMemoryInfo.availMem, sMemoryInfo.lowMemory, |
42 sMemoryInfo.threshold, sMemoryInfo.totalMem, outPtr); | 56 sMemoryInfo.threshold, sMemoryInfo.totalMem, outPtr); |
43 } | 57 } |
44 | 58 |
45 /** | 59 /** |
46 * Register ComponentCallbacks2 to receive memory pressure signals. | 60 * Register ComponentCallbacks2 to receive memory pressure signals. |
47 * | 61 * |
48 * @param context The context of the application. | 62 * @param context The context of the application. |
49 */ | 63 */ |
(...skipping 16 matching lines...) Expand all Loading... |
66 }; | 80 }; |
67 context.registerComponentCallbacks(sCallbacks); | 81 context.registerComponentCallbacks(sCallbacks); |
68 } | 82 } |
69 | 83 |
70 private static native void nativeGetMemoryInfoCallback( | 84 private static native void nativeGetMemoryInfoCallback( |
71 long availMem, boolean lowMemory, | 85 long availMem, boolean lowMemory, |
72 long threshold, long totalMem, long outPtr); | 86 long threshold, long totalMem, long outPtr); |
73 | 87 |
74 private static native void nativeOnTrimMemory(int level); | 88 private static native void nativeOnTrimMemory(int level); |
75 } | 89 } |
OLD | NEW |