| Index: components/memory_coordinator/android/java/src/org/chromium/components/memory_coordinator/MemoryStateListener.java
|
| diff --git a/components/memory_coordinator/android/java/src/org/chromium/components/memory_coordinator/MemoryStateListener.java b/components/memory_coordinator/android/java/src/org/chromium/components/memory_coordinator/MemoryStateListener.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b4554068807b22b81fb04154aed5be87db99382f
|
| --- /dev/null
|
| +++ b/components/memory_coordinator/android/java/src/org/chromium/components/memory_coordinator/MemoryStateListener.java
|
| @@ -0,0 +1,66 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.components.memory_coordinator;
|
| +
|
| +import android.app.Application;
|
| +import android.content.ComponentCallbacks2;
|
| +import android.content.Context;
|
| +import android.content.res.Configuration;
|
| +
|
| +import org.chromium.base.annotations.CalledByNative;
|
| +import org.chromium.base.annotations.JNINamespace;
|
| +import org.chromium.base.annotations.MainDex;
|
| +
|
| +/**
|
| + * This class registers a ComponentCallbacks2 which delegates actual memory
|
| + * management to internal memory coordinator implementation.
|
| + */
|
| +@JNINamespace("memory_coordinator")
|
| +@MainDex
|
| +public class MemoryStateListener {
|
| + private static ComponentCallbacks2 sCallbacks = null;
|
| +
|
| + @CalledByNative
|
| + private static void registerCallback(Context context) {
|
| + sCallbacks = new ComponentCallbacks2() {
|
| + @Override
|
| + public void onTrimMemory(int level) {
|
| + // Though the API document says that we shouldn't compare
|
| + // to exact values we filter UI_HIDDEN because it's not
|
| + // exactly a memory pressure signal.
|
| + if (level != ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
|
| + nativeOnTrimMemory(level);
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void onLowMemory() {
|
| + // Don't support old onLowMemory() API.
|
| + }
|
| +
|
| + @Override
|
| + public void onConfigurationChanged(Configuration config) {
|
| + }
|
| + };
|
| + context.registerComponentCallbacks(sCallbacks);
|
| + }
|
| +
|
| + @CalledByNative
|
| + private static void unregisterCallback(Context context) {
|
| + if (sCallbacks == null) {
|
| + return;
|
| + }
|
| + context.unregisterComponentCallbacks(sCallbacks);
|
| + sCallbacks = null;
|
| + }
|
| +
|
| + @CalledByNative
|
| + private static void simulateOnTrimMemory(Context context, int level) {
|
| + Application application = (Application) context.getApplicationContext();
|
| + application.onTrimMemory(level);
|
| + }
|
| +
|
| + private static native void nativeOnTrimMemory(int level);
|
| +}
|
|
|