1package com.android.server.wifi;
2
3import java.io.IOException;
4
5public class IMSIParameter {
6    private final String mImsi;
7    private final boolean mPrefix;
8
9    public IMSIParameter(String imsi, boolean prefix) {
10        mImsi = imsi;
11        mPrefix = prefix;
12    }
13
14    public IMSIParameter(String imsi) throws IOException {
15        if (imsi == null || imsi.length() == 0) {
16            throw new IOException("Bad IMSI: '" + imsi + "'");
17        }
18
19        int nonDigit;
20        char stopChar = '\0';
21        for (nonDigit = 0; nonDigit < imsi.length(); nonDigit++) {
22            stopChar = imsi.charAt(nonDigit);
23            if (stopChar < '0' || stopChar > '9') {
24                break;
25            }
26        }
27
28        if (nonDigit == imsi.length()) {
29            mImsi = imsi;
30            mPrefix = false;
31        }
32        else if (nonDigit == imsi.length()-1 && stopChar == '*') {
33            mImsi = imsi.substring(0, nonDigit);
34            mPrefix = true;
35        }
36        else {
37            throw new IOException("Bad IMSI: '" + imsi + "'");
38        }
39    }
40
41    public boolean matches(String fullIMSI) {
42        if (mPrefix) {
43            return mImsi.regionMatches(false, 0, fullIMSI, 0, mImsi.length());
44        }
45        else {
46            return mImsi.equals(fullIMSI);
47        }
48    }
49
50    public boolean matchesMccMnc(String mccMnc) {
51        if (mPrefix) {
52            // For a prefix match, the entire prefix must match the mcc+mnc
53            return mImsi.regionMatches(false, 0, mccMnc, 0, mImsi.length());
54        }
55        else {
56            // For regular match, the entire length of mcc+mnc must match this IMSI
57            return mImsi.regionMatches(false, 0, mccMnc, 0, mccMnc.length());
58        }
59    }
60
61    public boolean isPrefix() {
62        return mPrefix;
63    }
64
65    public String getImsi() {
66        return mImsi;
67    }
68
69    public int prefixLength() {
70        return mImsi.length();
71    }
72
73    @Override
74    public boolean equals(Object thatObject) {
75        if (this == thatObject) {
76            return true;
77        }
78        else if (thatObject == null || getClass() != thatObject.getClass()) {
79            return false;
80        }
81
82        IMSIParameter that = (IMSIParameter) thatObject;
83        return mPrefix == that.mPrefix && mImsi.equals(that.mImsi);
84    }
85
86    @Override
87    public int hashCode() {
88        int result = mImsi != null ? mImsi.hashCode() : 0;
89        result = 31 * result + (mPrefix ? 1 : 0);
90        return result;
91    }
92
93    @Override
94    public String toString() {
95        if (mPrefix) {
96            return mImsi + '*';
97        }
98        else {
99            return mImsi;
100        }
101    }
102}
103