1 /* 2 * Copyright (C) 2018 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.voicemail.impl; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.os.Build.VERSION_CODES; 22 import android.support.annotation.Nullable; 23 import android.telecom.PhoneAccountHandle; 24 import android.telephony.TelephonyManager; 25 import com.google.auto.value.AutoValue; 26 import java.util.Optional; 27 28 /** Identifies a carrier. */ 29 @AutoValue 30 @TargetApi(VERSION_CODES.O) 31 @SuppressWarnings({"missingpermission"}) 32 public abstract class CarrierIdentifier { 33 mccMnc()34 public abstract String mccMnc(); 35 36 /** 37 * Group ID Level 1. Used to identify MVNO(Mobile Virtual Network Operators) who subleases other 38 * carrier's network and share their mccMnc. MVNO should have a GID1 different from the host. 39 */ gid1()40 public abstract String gid1(); 41 42 /** Builder for the matcher */ 43 @AutoValue.Builder 44 public abstract static class Builder { 45 setMccMnc(String mccMnc)46 public abstract Builder setMccMnc(String mccMnc); 47 setGid1(String gid1)48 public abstract Builder setGid1(String gid1); 49 build()50 public abstract CarrierIdentifier build(); 51 } 52 builder()53 public static Builder builder() { 54 return new AutoValue_CarrierIdentifier.Builder().setGid1(""); 55 } 56 57 /** Create a identifier for a {@link PhoneAccountHandle}. Absent if the handle is not valid. */ forHandle( Context context, @Nullable PhoneAccountHandle phoneAccountHandle)58 public static Optional<CarrierIdentifier> forHandle( 59 Context context, @Nullable PhoneAccountHandle phoneAccountHandle) { 60 if (phoneAccountHandle == null) { 61 return Optional.empty(); 62 } 63 TelephonyManager telephonyManager = 64 context 65 .getSystemService(TelephonyManager.class) 66 .createForPhoneAccountHandle(phoneAccountHandle); 67 if (telephonyManager == null) { 68 return Optional.empty(); 69 } 70 String gid1 = telephonyManager.getGroupIdLevel1(); 71 if (gid1 == null) { 72 gid1 = ""; 73 } 74 75 return Optional.of( 76 builder().setMccMnc(telephonyManager.getSimOperator()).setGid1(gid1).build()); 77 } 78 } 79