1 /*
2  * Copyright 2018 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.server.wm;
18 
19 import android.graphics.Rect;
20 import android.graphics.Region;
21 import android.util.SparseArray;
22 
23 /**
24  * A holder that contains a collection of regions identified by int id. Each individual region can
25  * be updated separately.
26  */
27 class TapExcludeRegionHolder {
28     private SparseArray<Region> mTapExcludeRegions = new SparseArray<>();
29 
30     /** Update the specified region with provided position and size. */
updateRegion(int regionId, Region region)31     void updateRegion(int regionId, Region region) {
32         // Remove the previous one because there is a new one incoming.
33         mTapExcludeRegions.remove(regionId);
34 
35         if (region == null || region.isEmpty()) {
36             // The incoming region is invalid. Don't use it.
37             return;
38         }
39 
40         mTapExcludeRegions.put(regionId, region);
41     }
42 
43     /**
44      * Union the provided region with current region formed by this container.
45      */
amendRegion(Region region, Rect bounds)46     void amendRegion(Region region, Rect bounds) {
47         for (int i = mTapExcludeRegions.size() - 1; i >= 0; --i) {
48             final Region r = mTapExcludeRegions.valueAt(i);
49             if (bounds != null) {
50                 r.op(bounds, Region.Op.INTERSECT);
51             }
52             region.op(r, Region.Op.UNION);
53         }
54     }
55 
56     /**
57      * Return true if tap exclude region is empty.
58      */
isEmpty()59     boolean isEmpty() {
60         return mTapExcludeRegions.size() == 0;
61     }
62 }
63