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