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 rectangular areas identified by int id. Each individual
25  * region can be updated separately.
26  */
27 class TapExcludeRegionHolder {
28     private SparseArray<Rect> mTapExcludeRects = new SparseArray<>();
29 
30     /** Update the specified region with provided position and size. */
updateRegion(int regionId, int left, int top, int width, int height)31     void updateRegion(int regionId, int left, int top, int width, int height) {
32         if (width <= 0 || height <= 0) {
33             // A region became empty - remove it.
34             mTapExcludeRects.remove(regionId);
35             return;
36         }
37 
38         Rect region = mTapExcludeRects.get(regionId);
39         if (region == null) {
40             region = new Rect();
41         }
42         region.set(left, top, left + width, top + height);
43         mTapExcludeRects.put(regionId, region);
44     }
45 
46     /**
47      * Union the provided region with current region formed by this container.
48      */
amendRegion(Region region, Rect boundingRegion)49     void amendRegion(Region region, Rect boundingRegion) {
50         for (int i = mTapExcludeRects.size() - 1; i>= 0 ; --i) {
51             final Rect rect = mTapExcludeRects.valueAt(i);
52             rect.intersect(boundingRegion);
53             region.union(rect);
54         }
55     }
56 }
57