Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 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 | /** |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 24 | * A holder that contains a collection of regions identified by int id. Each individual region can |
| 25 | * be updated separately. |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 26 | */ |
| 27 | class TapExcludeRegionHolder { |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 28 | private SparseArray<Region> mTapExcludeRegions = new SparseArray<>(); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 29 | |
| 30 | /** Update the specified region with provided position and size. */ |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 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. |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 40 | mTapExcludeRegions.put(regionId, region); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Union the provided region with current region formed by this container. |
| 45 | */ |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 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); |
Tiger Huang | 04dc4cc | 2019-01-17 18:41:41 +0800 | [diff] [blame] | 51 | } |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 52 | region.op(r, Region.Op.UNION); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |