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 package android.car; 17 18 import android.annotation.SystemApi; 19 20 /** 21 * Collection of utilities for handling zones 22 * @hide 23 */ 24 @SystemApi 25 public class VehicleZoneUtil { 26 27 /** 28 * Change zone flag into index with available zones. 29 * For example with zoned of 0x80000001, 0x1 will be index 0 and 0x80000000 will be index 2. 30 * @param available zones, should not be zero 31 * @param zone flag for the zone to get index, should not be zero and should be one of the flags 32 * defined in zones. 33 * @return index of desired zone. 34 * @throws IllegalArgumentException if zones or zone is invalid. 35 */ 36 public static int zoneToIndex(int zones, int zone) throws IllegalArgumentException { 37 if ((zone == 0) || // check that zone is non-zero 38 (zones == 0) || // check that zones is non-zero 39 ((zone & zones) != zone) || // check that zone is inside of zones 40 ((zone & (zone - 1)) != 0)) { // check that zone only has one bit set 41 throw new IllegalArgumentException("Invalid zones 0x" + Integer.toHexString(zones) + 42 " or zone 0x" + Integer.toHexString(zone)); 43 } 44 int index = -1; 45 while((zone & zones) != 0) { 46 index++; 47 zones &= zones - 1; 48 } 49 return index; 50 } 51 52 /** 53 * Return number of zones (non-zero flag) from given zones. 54 */ 55 public static int getNumberOfZones(int zones) { 56 int numZones = 0; 57 while (zones != 0) { 58 zones &= zones - 1; 59 numZones++; 60 } 61 return numZones; 62 } 63 64 /** 65 * Return bit flag of first zone. If zones is 0, it will just return 0. 66 * @param zones can be 0 if there is no zone 67 * @return 68 */ 69 public static int getFirstZone(int zones) { 70 if (zones == 0) { 71 return 0; 72 } 73 int flag = 0x1; 74 for (int i = 0; i < 32; i++) { 75 if ((flag & zones) != 0) { 76 return flag; 77 } 78 flag <<= 1; 79 } 80 return 0; 81 } 82 83 /** 84 * Return bit flag of zone available after startingZone. For zones of 0x7 with startingZone of 85 * 0x2, it will return 0x4. If no zone exist after startingZone, it will return 0. 86 * @param zones 87 * @param startingZone A big flag representing a zone. This does not necessarily be one of flags 88 * available in zones. 89 * @return 90 * @throws IllegalArgumentException If startingZone is invalid. 91 */ 92 public static int getNextZone(int zones, int startingZone) throws IllegalArgumentException { 93 if ((startingZone & (startingZone - 1)) != 0 || startingZone == 0) { 94 throw new IllegalArgumentException( 95 "Starting zone should represent only one bit flag: 0x" + 96 Integer.toHexString(startingZone)); 97 } 98 int flag = startingZone<<1; 99 while (flag != 0x80000000) { 100 if ((flag & zones) != 0) { 101 return flag; 102 } 103 flag <<= 1; 104 } 105 if ((flag & zones) != 0) { 106 return flag; 107 } 108 return 0; 109 } 110 111 /** 112 * Return array of zone with each active zone in one index. This can be useful for iterating 113 * all available zones. 114 */ 115 public static int[] listAllZones(int zones) { 116 int numberOfZones = getNumberOfZones(zones); 117 int[] list = new int[numberOfZones]; 118 if (numberOfZones == 0) { 119 return list; 120 } 121 int flag = 0x1; 122 int arrayIndex = 0; 123 for (int i = 0; i < 32; i++) { 124 if ((flag & zones) != 0) { 125 list[arrayIndex] = flag; 126 arrayIndex++; 127 } 128 flag <<= 1; 129 } 130 return list; 131 } 132 } 133