1 /* 2 * Copyright (C) 2017 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.server.connectivity.tethering; 18 19 import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED; 20 import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE; 21 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.net.util.VersionedBroadcastListener; 27 import android.net.util.VersionedBroadcastListener.IntentCallback; 28 import android.os.Handler; 29 import android.util.Log; 30 31 import com.android.internal.telephony.TelephonyIntents; 32 33 import java.util.concurrent.atomic.AtomicInteger; 34 import java.util.function.Consumer; 35 36 37 /** 38 * A utility class that runs the provided callback on the provided handler when 39 * observing a new SIM card having been loaded. 40 * 41 * @hide 42 */ 43 public class SimChangeListener extends VersionedBroadcastListener { 44 private static final String TAG = SimChangeListener.class.getSimpleName(); 45 private static final boolean DBG = false; 46 47 public SimChangeListener(Context ctx, Handler handler, Runnable onSimCardLoadedCallback) { 48 super(TAG, ctx, handler, makeIntentFilter(), makeCallback(onSimCardLoadedCallback)); 49 } 50 51 private static IntentFilter makeIntentFilter() { 52 final IntentFilter filter = new IntentFilter(); 53 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); 54 return filter; 55 } 56 57 private static Consumer<Intent> makeCallback(Runnable onSimCardLoadedCallback) { 58 return new Consumer<Intent>() { 59 private boolean mSimNotLoadedSeen = false; 60 61 @Override 62 public void accept(Intent intent) { 63 final String state = intent.getStringExtra(INTENT_KEY_ICC_STATE); 64 Log.d(TAG, "got Sim changed to state " + state + ", mSimNotLoadedSeen=" + 65 mSimNotLoadedSeen); 66 67 if (!INTENT_VALUE_ICC_LOADED.equals(state)) { 68 mSimNotLoadedSeen = true; 69 return; 70 } 71 72 if (mSimNotLoadedSeen) { 73 mSimNotLoadedSeen = false; 74 onSimCardLoadedCallback.run(); 75 } 76 } 77 }; 78 } 79 } 80