1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email.data; 18 19 import com.android.email.Throttle; 20 import com.android.emailcommon.Logging; 21 22 import android.content.Context; 23 import android.content.CursorLoader; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.os.Handler; 27 import android.util.Log; 28 29 /** 30 * A {@link CursorLoader} variant that throttle auto-requery on content changes using 31 * {@link Throttle}. 32 */ 33 public class ThrottlingCursorLoader extends CursorLoader { 34 private final Throttle mThrottle; 35 36 /** Constructor with default timeout */ ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)37 public ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection, 38 String[] selectionArgs, String sortOrder) { 39 this(context, uri, projection, selection, selectionArgs, sortOrder, 40 Throttle.DEFAULT_MIN_TIMEOUT, Throttle.DEFAULT_MAX_TIMEOUT); 41 } 42 43 /** Constructor that takes custom timeout */ ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int minTimeout, int maxTimeout)44 public ThrottlingCursorLoader(Context context, Uri uri, String[] projection, String selection, 45 String[] selectionArgs, String sortOrder, int minTimeout, int maxTimeout) { 46 super(context, uri, projection, selection, selectionArgs, sortOrder); 47 48 Runnable forceLoadRunnable = new Runnable() { 49 @Override 50 public void run() { 51 callSuperOnContentChanged(); 52 } 53 }; 54 mThrottle = new Throttle(uri.toString(), forceLoadRunnable, new Handler(), 55 minTimeout, maxTimeout); 56 } 57 debugLog(String message)58 private void debugLog(String message) { 59 Log.d(Logging.LOG_TAG, "ThrottlingCursorLoader: [" + getUri() + "] " + message); 60 } 61 62 @Override onStartLoading()63 protected void onStartLoading() { 64 if (Throttle.DEBUG) debugLog("startLoading"); 65 mThrottle.cancelScheduledCallback(); 66 super.onStartLoading(); 67 } 68 69 @Override onForceLoad()70 protected void onForceLoad() { 71 if (Throttle.DEBUG) debugLog("forceLoad"); 72 mThrottle.cancelScheduledCallback(); 73 super.onForceLoad(); 74 } 75 76 @Override onStopLoading()77 protected void onStopLoading() { 78 if (Throttle.DEBUG) debugLog("stopLoading"); 79 mThrottle.cancelScheduledCallback(); 80 super.onStopLoading(); 81 } 82 83 @Override onCanceled(Cursor cursor)84 public void onCanceled(Cursor cursor) { 85 if (Throttle.DEBUG) debugLog("onCancelled"); 86 mThrottle.cancelScheduledCallback(); 87 super.onCanceled(cursor); 88 } 89 90 @Override onReset()91 protected void onReset() { 92 if (Throttle.DEBUG) debugLog("onReset"); 93 mThrottle.cancelScheduledCallback(); 94 super.onReset(); 95 } 96 97 @Override onContentChanged()98 public void onContentChanged() { 99 if (Throttle.DEBUG) debugLog("onContentChanged"); 100 101 mThrottle.onEvent(); 102 } 103 callSuperOnContentChanged()104 private void callSuperOnContentChanged() { 105 if (Throttle.DEBUG) debugLog("callSuperOnContentChanged"); 106 super.onContentChanged(); 107 } 108 } 109