replicant-packages_apps_Email/src/com/android/email/data/ThrottlingCursorLoader.java

89 lines
2.8 KiB
Java

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.email.data;
import com.android.email.Email;
import com.android.email.Throttle;
import android.content.Context;
import android.content.CursorLoader;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
/**
* A {@link CursorLoader} variant that throttle auto-requery on content changes using
* {@link Throttle}.
*/
public class ThrottlingCursorLoader extends CursorLoader {
private final Throttle mThrottle;
/** Constructor with default timeout */
public ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
this(context, uri, projection, selection, selectionArgs, sortOrder,
Throttle.DEFAULT_MIN_TIMEOUT, Throttle.DEFAULT_MAX_TIMEOUT);
}
/** Constructor that takes custom timeout */
public ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder, int minTimeout, int maxTimeout) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
Runnable forceLoadRunnable = new Runnable() {
@Override
public void run() {
forceLoad();
}
};
mThrottle = new Throttle(uri.toString(), forceLoadRunnable, new Handler(),
minTimeout, maxTimeout);
}
private void debugLog(String message) {
Log.d(Email.LOG_TAG, "ThrottlingCursorLoader: [" + getUri() + "] " + message);
}
@Override
public void startLoading() {
if (Throttle.DEBUG) debugLog("startLoading");
mThrottle.cancelScheduledCallback();
super.startLoading();
}
@Override
public void forceLoad() {
if (Throttle.DEBUG) debugLog("forceLoad");
mThrottle.cancelScheduledCallback();
super.forceLoad();
}
@Override
public void stopLoading() {
if (Throttle.DEBUG) debugLog("stopLoading");
mThrottle.cancelScheduledCallback();
super.stopLoading();
}
@Override
public void onContentChanged() {
if (Throttle.DEBUG) debugLog("onContentChanged");
mThrottle.onEvent();
}
}