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.quicksearchbox.util; 18 19 import android.database.DataSetObservable; 20 import android.os.Handler; 21 22 /** 23 * A version of {@link DataSetObservable} that performs callbacks on given {@link Handler}. 24 */ 25 public class AsyncDataSetObservable extends DataSetObservable { 26 27 private final Handler mHandler; 28 29 private final Runnable mChangedRunnable = new Runnable() { 30 public void run() { 31 AsyncDataSetObservable.super.notifyChanged(); 32 } 33 }; 34 35 private final Runnable mInvalidatedRunnable = new Runnable() { 36 public void run() { 37 AsyncDataSetObservable.super.notifyInvalidated(); 38 } 39 }; 40 41 /** 42 * @param handler Handler to run callbacks on. 43 */ AsyncDataSetObservable(Handler handler)44 public AsyncDataSetObservable(Handler handler) { 45 mHandler = handler; 46 } 47 48 @Override notifyChanged()49 public void notifyChanged() { 50 if (mHandler == null) { 51 super.notifyChanged(); 52 } else { 53 mHandler.post(mChangedRunnable); 54 } 55 } 56 57 @Override notifyInvalidated()58 public void notifyInvalidated() { 59 if (mHandler == null) { 60 super.notifyInvalidated(); 61 } else { 62 mHandler.post(mInvalidatedRunnable); 63 } 64 } 65 66 } 67