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.net; |
| 6 |
| 7 import org.chromium.base.ApplicationState; |
| 8 import org.chromium.base.ApplicationStatus; |
| 9 import org.chromium.base.VisibleForTesting; |
| 10 |
| 11 /** |
| 12 * Regsitration policy which depends on the ApplicationState. |
| 13 */ |
| 14 public class RegistrationPolicyApplicationStatus |
| 15 extends NetworkChangeNotifierAutoDetect.RegistrationPolicy |
| 16 implements ApplicationStatus.ApplicationStateListener { |
| 17 private boolean mDestroyed = false; |
| 18 |
| 19 @Override |
| 20 protected void init(NetworkChangeNotifierAutoDetect notifier) { |
| 21 super.init(notifier); |
| 22 ApplicationStatus.registerApplicationStateListener(this); |
| 23 onApplicationStateChange(getApplicationState()); |
| 24 } |
| 25 |
| 26 protected void destroy() { |
| 27 if (mDestroyed) return; |
| 28 ApplicationStatus.unregisterApplicationStateListener(this); |
| 29 mDestroyed = true; |
| 30 } |
| 31 |
| 32 // ApplicationStatus.ApplicationStateListener |
| 33 @Override |
| 34 public void onApplicationStateChange(int newState) { |
| 35 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { |
| 36 register(); |
| 37 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { |
| 38 unregister(); |
| 39 } |
| 40 } |
| 41 |
| 42 /** |
| 43 * Returns the activity's status. |
| 44 * @return an {@code int} that is one of {@code ApplicationState.HAS_*_ACTIV
ITIES}. |
| 45 */ |
| 46 @VisibleForTesting |
| 47 int getApplicationState() { |
| 48 return ApplicationStatus.getStateForApplication(); |
| 49 } |
| 50 } |
OLD | NEW |