1 /*
2  * Copyright (C) 2022 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 /** A version of [DataSetObservable] that performs callbacks on given [Handler]. */
23 class AsyncDataSetObservable(handler: Handler?) : DataSetObservable() {
24   private val mHandler: Handler?
25   private val mChangedRunnable: Runnable =
26     object : Runnable {
runnull27       override fun run() {
28         super@AsyncDataSetObservable.notifyChanged()
29       }
30     }
31   private val mInvalidatedRunnable: Runnable =
32     object : Runnable {
runnull33       override fun run() {
34         super@AsyncDataSetObservable.notifyInvalidated()
35       }
36     }
37 
38   @Override
notifyChangednull39   override fun notifyChanged() {
40     if (mHandler == null) {
41       super.notifyChanged()
42     } else {
43       mHandler.post(mChangedRunnable)
44     }
45   }
46 
47   @Override
notifyInvalidatednull48   override fun notifyInvalidated() {
49     if (mHandler == null) {
50       super.notifyInvalidated()
51     } else {
52       mHandler.post(mInvalidatedRunnable)
53     }
54   }
55 
56   /** @param handler Handler to run callbacks on. */
57   init {
58     mHandler = handler
59   }
60 }
61