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
17package com.android.internal.telephony;
18
19import android.util.Log;
20
21/**
22 * Wrapper class for an ICC EF containing a bit field of enabled services.
23 */
24public abstract class IccServiceTable {
25    protected final byte[] mServiceTable;
26
27    protected IccServiceTable(byte[] table) {
28        mServiceTable = table;
29    }
30
31    // Get the class name to use for log strings
32    protected abstract String getTag();
33
34    // Get the array of enums to use for toString
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     */
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            Log.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    public String toString() {
55        Object[] values = getValues();
56        int numBytes = mServiceTable.length;
57        StringBuilder builder = new StringBuilder(getTag()).append('[')
58                .append(numBytes * 8).append("]={ ");
59
60        boolean addComma = false;
61        for (int i = 0; i < numBytes; i++) {
62            byte currentByte = mServiceTable[i];
63            for (int bit = 0; bit < 8; bit++) {
64                if ((currentByte & (1 << bit)) != 0) {
65                    if (addComma) {
66                        builder.append(", ");
67                    } else {
68                        addComma = true;
69                    }
70                    int ordinal = (i * 8) + bit;
71                    if (ordinal < values.length) {
72                        builder.append(values[ordinal]);
73                    } else {
74                        builder.append('#').append(ordinal + 1);    // service number (one-based)
75                    }
76                }
77            }
78        }
79        return builder.append(" }").toString();
80    }
81}
82