1 /*
2  * Copyright (C) 2020 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.systemui.accessibility;
18 
19 import android.annotation.Nullable;
20 import android.hardware.display.DisplayManager;
21 import android.util.SparseArray;
22 import android.view.Display;
23 
24 import androidx.annotation.NonNull;
25 
26 import java.util.function.Consumer;
27 
28 /**
29  * Supplies the instance with given display Id. It generates a new instance if the corresponding
30  * one is not existed. It should run in single thread to avoid race conditions.
31  *
32  * @param <T> the type of results supplied by {@link #createInstance(Display)}.
33  */
34 abstract class DisplayIdIndexSupplier<T> {
35 
36     private final SparseArray<T> mSparseArray = new SparseArray<>();
37     private final DisplayManager mDisplayManager;
38 
39     /**
40      * @param displayManager DisplayManager
41      */
DisplayIdIndexSupplier(DisplayManager displayManager)42     DisplayIdIndexSupplier(DisplayManager displayManager) {
43         mDisplayManager = displayManager;
44     }
45 
46     /**
47      * @param displayId the logical display Id
48      * @return {@code null} if the given display id is invalid
49      */
50     @Nullable
get(int displayId)51     public T get(int displayId) {
52         T instance = mSparseArray.get(displayId);
53         if (instance != null) {
54             return instance;
55         }
56         final Display display = mDisplayManager.getDisplay(displayId);
57         if (display == null) {
58             return null;
59         }
60         instance = createInstance(display);
61         mSparseArray.put(displayId, instance);
62         return instance;
63     }
64 
65     /**
66      * Returns the object with the given display id.
67      *
68      *
69      * @param displayId the logical display Id
70      * @return T
71      */
72     @Nullable
valueAt(int displayId)73     public T valueAt(int displayId) {
74         return mSparseArray.get(displayId);
75     }
76 
77     @NonNull
createInstance(Display display)78     protected abstract T createInstance(Display display);
79 
80     /**
81      * Removes the instance with given display Id.
82      *
83      * @param displayId the logical display id
84      */
remove(int displayId)85     public void remove(int displayId) {
86         mSparseArray.remove(displayId);
87     }
88 
89     /**
90      * Clears all elements.
91      */
clear()92     public void clear() {
93         mSparseArray.clear();
94     }
95 
96     /**
97      * Gets the element size.
98      *
99      * @return size of all elements
100      */
getSize()101     public int getSize() {
102         return mSparseArray.size();
103     }
104 
105     /**
106      * Runs task for each object.
107      *
108      * @param task of each object
109      */
forEach(Consumer<T> task)110     public void forEach(Consumer<T> task) {
111         for (int i = 0; i < mSparseArray.size(); i++) {
112             task.accept(mSparseArray.valueAt(i));
113         }
114     }
115 }
116