• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.Shared.VERBOSE;
20 
21 import android.annotation.MainThread;
22 import android.annotation.Nullable;
23 import android.util.Log;
24 
25 import com.android.documentsui.base.Shared;
26 import com.android.documentsui.selection.BandController;
27 
28 /**
29  * A lock used by {@link DirectoryLoader} and {@link BandController} to ensure refresh is blocked
30  * while Band Selection is active.
31  */
32 public final class DirectoryReloadLock {
33     private static final String TAG = "DirectoryReloadLock";
34 
35     private int mPauseCount = 0;
36     private @Nullable Runnable mCallback;
37 
38     /**
39      * Increment the block count by 1
40      */
41     @MainThread
block()42     public void block() {
43         Shared.checkMainLoop();
44         mPauseCount++;
45         if (VERBOSE) Log.v(TAG, "Block count increments to " + mPauseCount + ".");
46     }
47 
48     /**
49      * Decrement the block count by 1; If no other object is trying to block and there exists some
50      * callback, that callback will be run
51      */
52     @MainThread
unblock()53     public void unblock() {
54         Shared.checkMainLoop();
55         assert(mPauseCount > 0);
56         mPauseCount--;
57         if (VERBOSE) Log.v(TAG, "Block count decrements to " + mPauseCount + ".");
58         if (mPauseCount == 0 && mCallback != null) {
59             mCallback.run();
60             mCallback = null;
61         }
62     }
63 
64     /**
65      * Attempts to run the given Runnable if not-blocked, or else the Runnable is set to be ran next
66      * (replacing any previous set Runnables).
67      */
tryUpdate(Runnable update)68     public void tryUpdate(Runnable update) {
69         if (mPauseCount == 0) {
70             update.run();
71         } else {
72             mCallback = update;
73         }
74     }
75 }
76