1 /*
2  * Copyright (C) 2019 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.documentsui;
18 
19 import static com.android.documentsui.base.SharedMinimal.DEBUG;
20 import static com.android.documentsui.base.SharedMinimal.TAG;
21 
22 import android.database.ContentObserver;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.util.Log;
26 
27 /**
28  * A custom {@link ContentObserver} which constructed by a {@link ContentLock}
29  * and a {@link Runnable} callback. It will callback when it's onChange and ContentLock is unlock.
30  */
31 public final class LockingContentObserver extends ContentObserver {
32     private final ContentLock mLock;
33     private final Runnable mContentChangedCallback;
34 
LockingContentObserver(ContentLock lock, Runnable contentChangedCallback)35     public LockingContentObserver(ContentLock lock, Runnable contentChangedCallback) {
36         super(new Handler(Looper.getMainLooper()));
37         mLock = lock;
38         mContentChangedCallback = contentChangedCallback;
39     }
40 
41     @Override
deliverSelfNotifications()42     public boolean deliverSelfNotifications() {
43         return true;
44     }
45 
46     @Override
onChange(boolean selfChange)47     public void onChange(boolean selfChange) {
48         if (DEBUG) {
49             Log.d(TAG, "Content updated.");
50         }
51         mLock.runWhenUnlocked(mContentChangedCallback);
52     }
53 }
54