Chromium Code Reviews| Index: net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java |
| diff --git a/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java b/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..40458b2fa3a62daa722404f0b1a815d241b424d7 |
| --- /dev/null |
| +++ b/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java |
| @@ -0,0 +1,83 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
Philippe
2013/04/23 11:34:25
Nit: 2013.
felipeg
2013/04/23 13:10:13
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import android.util.Log; |
| + |
| +import org.chromium.base.ActivityStatus; |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.base.JNINamespace; |
| +import org.chromium.base.NativeClassQualifiedName; |
| + |
| +import android.os.Handler; |
| +import android.os.Looper; |
| + |
| +/** |
| + * Used by the SimpleIndex in net/disk_cache/simple/ to listens to changes in the android app state |
|
Philippe
2013/04/23 11:34:25
Nit: s/listens/listen
felipeg
2013/04/23 13:10:13
Done.
|
| + * such as the app going to the background or foreground. |
| + */ |
| +public class SimpleCacheActivityStatusNotifier |
| + implements ActivityStatus.StateListener { |
|
Philippe
2013/04/23 11:34:25
Nit: this should fit on the line above I believe.
felipeg
2013/04/23 13:10:13
Done.
|
| + // Pointer to native object. |
|
Philippe
2013/04/23 11:34:25
Nit: you can remove this comment.
felipeg
2013/04/23 13:10:13
Done.
|
| + private int mNativePtr = 0; |
| + final private Looper mIoLooper; |
|
Philippe
2013/04/23 11:34:25
Nit: the access modifier keyword should be first.
felipeg
2013/04/23 13:10:13
Done.
felipeg
2013/04/23 13:10:13
Done.
|
| + |
| + @CalledByNative |
| + static public SimpleCacheActivityStatusNotifier newInstance(int nativePtr) { |
| + // Run IO Thread |
|
Philippe
2013/04/23 11:34:25
Nit: I would say here and below 'Run on the IO thr
felipeg
2013/04/23 13:10:13
Done.
|
| + return new SimpleCacheActivityStatusNotifier(nativePtr); |
| + } |
| + |
| + |
| + private SimpleCacheActivityStatusNotifier(int nativePtr) { |
| + // Run IO Thread |
| + this.mIoLooper = Looper.myLooper(); |
| + this.mNativePtr = nativePtr; |
| + |
| + final SimpleCacheActivityStatusNotifier this_ptr = this; |
|
Philippe
2013/04/23 11:34:25
I don't think you need this. 'this' should already
felipeg
2013/04/23 13:10:13
Done.
|
| + // Call the singleton's ActivityStatus in the UI thread. |
|
Philippe
2013/04/23 11:34:25
Nit: you can remove this comment.
felipeg
2013/04/23 13:10:13
Done.
|
| + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| + @Override |
| + public void run() { |
| + ActivityStatus.registerStateListener(this_ptr); |
| + } |
| + }); |
| + } |
| + |
| + @CalledByNative |
| + public void prepareToBeDestroyed() { |
| + // Run IO Thread |
| + this.mNativePtr = 0; |
| + |
| + final SimpleCacheActivityStatusNotifier this_ptr = this; |
| + // Call the singleton's ActivityStatus in the UI thread. |
| + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| + @Override |
| + public void run() { |
| + ActivityStatus.unregisterStateListener(this_ptr); |
| + } |
| + }); |
| + } |
| + |
| + // ActivityStatus.StateListener |
| + @Override |
| + public void onActivityStateChange(final int state) { |
|
Philippe
2013/04/23 11:34:25
Is 'final' needed here?
felipeg
2013/04/23 13:10:13
yes
it is used inside the Runnable bellow
|
| + if (state == ActivityStatus.RESUMED || |
| + state == ActivityStatus.STOPPED) { |
| + final SimpleCacheActivityStatusNotifier this_ptr = this; |
|
Philippe
2013/04/23 11:34:25
Nit: 4 space indent (although this line should be
felipeg
2013/04/23 13:10:13
Done.
|
| + // Post task to IO Thread. |
|
Philippe
2013/04/23 11:34:25
Nit: you can remove this comment too.
felipeg
2013/04/23 13:10:13
Done.
|
| + new Handler(mIoLooper).post(new Runnable() { |
| + @Override |
| + public void run() { |
| + if (this_ptr.mNativePtr != 0) |
| + nativeNotifyActivityStatusChanged(this_ptr.mNativePtr, state); |
| + } |
| + }); |
| + } |
| + } |
| + |
| + @NativeClassQualifiedName("net::SimpleCacheActivityStatusNotifier") |
| + private native void nativeNotifyActivityStatusChanged(int nativePtr, int newActivityStatus); |
| +} |