1 /*
2  * Copyright (C) 2011 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.internal.telephony.uicc;
18 
19 import android.telephony.Rlog;
20 
21 /**
22  * Wrapper class for an ICC EF containing a bit field of enabled services.
23  */
24 public abstract class IccServiceTable {
25     protected final byte[] mServiceTable;
26 
IccServiceTable(byte[] table)27     protected IccServiceTable(byte[] table) {
28         mServiceTable = table;
29     }
30 
31     // Get the class name to use for log strings
getTag()32     protected abstract String getTag();
33 
34     // Get the array of enums to use for toString
getValues()35     protected abstract Object[] getValues();
36 
37     /**
38      * Returns if the specified service is available.
39      * @param service the service number as a zero-based offset (the enum ordinal)
40      * @return true if the service is available; false otherwise
41      */
isAvailable(int service)42     protected boolean isAvailable(int service) {
43         int offset = service / 8;
44         if (offset >= mServiceTable.length) {
45             // Note: Enums are zero-based, but the TS service numbering is one-based
46             Rlog.e(getTag(), "isAvailable for service " + (service + 1) + " fails, max service is " +
47                     (mServiceTable.length * 8));
48             return false;
49         }
50         int bit = service % 8;
51         return (mServiceTable[offset] & (1 << bit)) != 0;
52     }
53 
54     @Override
toString()55     public String toString() {
56         Object[] values = getValues();
57         int numBytes = mServiceTable.length;
58         StringBuilder builder = new StringBuilder(getTag()).append('[')
59                 .append(numBytes * 8).append("]={ ");
60 
61         boolean addComma = false;
62         for (int i = 0; i < numBytes; i++) {
63             byte currentByte = mServiceTable[i];
64             for (int bit = 0; bit < 8; bit++) {
65                 if ((currentByte & (1 << bit)) != 0) {
66                     if (addComma) {
67                         builder.append(", ");
68                     } else {
69                         addComma = true;
70                     }
71                     int ordinal = (i * 8) + bit;
72                     if (ordinal < values.length) {
73                         builder.append(values[ordinal]);
74                     } else {
75                         builder.append('#').append(ordinal + 1);    // service number (one-based)
76                     }
77                 }
78             }
79         }
80         return builder.append(" }").toString();
81     }
82 }
83