This source file includes following definitions.
- setRegisteredTypes
- refreshRegisteredTypes
- setRegisteredObjectIds
- start
- stop
- get
- onApplicationStateChange
- getInvalidatorClientId
package org.chromium.chrome.browser.invalidation;
import android.accounts.Account;
import android.content.Context;
import android.content.Intent;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.chromium.base.ApplicationState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.CalledByNative;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.notifier.InvalidationClientNameProvider;
import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences;
import org.chromium.sync.notifier.InvalidationService;
import org.chromium.sync.notifier.SyncStatusHelper;
import java.util.Set;
public class InvalidationController implements ApplicationStatus.ApplicationStateListener {
private static final Object LOCK = new Object();
private static InvalidationController sInstance;
private final Context mContext;
public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) {
Intent registerIntent =
InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
registerIntent.setClass(mContext, InvalidationService.class);
mContext.startService(registerIntent);
}
public void refreshRegisteredTypes(Set<ModelType> types) {
InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
Set<String> savedSyncedTypes = invalidationPreferences.getSavedSyncedTypes();
Account account = invalidationPreferences.getSavedSyncedAccount();
boolean allTypes = savedSyncedTypes != null &&
savedSyncedTypes.contains(ModelType.ALL_TYPES_TYPE);
setRegisteredTypes(account, allTypes, types);
}
@CalledByNative
public void setRegisteredObjectIds(int[] objectSources, String[] objectNames) {
InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
Account account = invalidationPreferences.getSavedSyncedAccount();
Intent registerIntent =
InvalidationIntentProtocol.createRegisterIntent(
account, objectSources, objectNames);
registerIntent.setClass(mContext, InvalidationService.class);
mContext.startService(registerIntent);
}
public void start() {
Intent intent = new Intent(mContext, InvalidationService.class);
mContext.startService(intent);
}
public void stop() {
Intent intent = new Intent(mContext, InvalidationService.class);
intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
mContext.startService(intent);
}
@CalledByNative
public static InvalidationController get(Context context) {
synchronized (LOCK) {
if (sInstance == null) {
sInstance = new InvalidationController(context);
}
return sInstance;
}
}
@VisibleForTesting
InvalidationController(Context context) {
mContext = Preconditions.checkNotNull(context.getApplicationContext());
ApplicationStatus.registerApplicationStateListener(this);
}
@Override
public void onApplicationStateChange(int newState) {
if (SyncStatusHelper.get(mContext).isSyncEnabled()) {
if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
start();
} else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
stop();
}
}
}
@CalledByNative
public byte[] getInvalidatorClientId() {
return InvalidationClientNameProvider.get().getInvalidatorClientName();
}
}