Chromium Code Reviews| Index: net/android/java/src/org/chromium/net/RegistrationPolicyApplicationStatus.java |
| diff --git a/net/android/java/src/org/chromium/net/RegistrationPolicyApplicationStatus.java b/net/android/java/src/org/chromium/net/RegistrationPolicyApplicationStatus.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fcb556780e39f780f67ad4602dcde86ee85b0ba2 |
| --- /dev/null |
| +++ b/net/android/java/src/org/chromium/net/RegistrationPolicyApplicationStatus.java |
| @@ -0,0 +1,50 @@ |
| +// Copyright 2015 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.net; |
| + |
| +import org.chromium.base.ApplicationState; |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.VisibleForTesting; |
| + |
| +/** |
| + * Regsitration policy which depends on the ApplicationState. |
| + */ |
| +public class RegistrationPolicyApplicationStatus |
| + extends RegistrationPolicy implements ApplicationStatus.ApplicationStateListener { |
| + private boolean mDestroyed = false; |
| + |
| + public RegistrationPolicyApplicationStatus() {} |
|
pauljensen
2015/10/01 12:06:37
why do we need a constructor?
timvolodine
2015/10/05 17:26:45
no need, done.
|
| + |
| + protected void init(Listener listener) { |
| + super.init(listener); |
| + onApplicationStateChange(getApplicationState()); |
| + ApplicationStatus.registerApplicationStateListener(this); |
| + } |
| + |
| + protected void destroy() { |
| + if (mDestroyed) return; |
| + ApplicationStatus.unregisterApplicationStateListener(this); |
| + mDestroyed = true; |
| + } |
| + |
| + // ApplicationStatus.ApplicationStateListener |
| + @Override |
| + public void onApplicationStateChange(int newState) { |
| + if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { |
| + notifyRegister(); |
| + } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { |
| + notifyUnregister(); |
| + } |
| + } |
| + |
| + /** |
| + * Returns the activity's status. |
| + * @return an {@code int} that is one of {@code ApplicationState.HAS_*_ACTIVITIES}. |
| + */ |
| + @VisibleForTesting |
| + int getApplicationState() { |
| + return ApplicationStatus.getStateForApplication(); |
| + } |
| +} |