blob: 3e60f9f6c97ae0f73a730706d175f324092ffb5a [file] [log] [blame]
Erik Kline227648f2017-01-20 20:26:10 +09001/*
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
17package com.android.server.connectivity.tethering;
18
19import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED;
20import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE;
21
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.os.Handler;
27import android.util.Log;
28
29import com.android.internal.telephony.TelephonyIntents;
30
31import java.util.concurrent.atomic.AtomicInteger;
32
33
34/**
35 * A utility class that runs the provided callback on the provided handler when
36 * observing a new SIM card having been loaded.
37 *
38 * @hide
39 */
40public class SimChangeListener {
41 private static final String TAG = SimChangeListener.class.getSimpleName();
42 private static final boolean DBG = false;
43
44 private final Context mContext;
45 private final Handler mTarget;
46 private final AtomicInteger mSimBcastGenerationNumber;
47 private final Runnable mCallback;
48 private BroadcastReceiver mBroadcastReceiver;
49
50 public SimChangeListener(Context ctx, Handler handler, Runnable onSimCardLoadedCallback) {
51 mContext = ctx;
52 mTarget = handler;
53 mCallback = onSimCardLoadedCallback;
54 mSimBcastGenerationNumber = new AtomicInteger(0);
55 }
56
57 public int generationNumber() {
58 return mSimBcastGenerationNumber.get();
59 }
60
61 public void startListening() {
62 if (DBG) Log.d(TAG, "startListening for SIM changes");
63
64 if (mBroadcastReceiver != null) return;
65
66 mBroadcastReceiver = new SimChangeBroadcastReceiver(
67 mSimBcastGenerationNumber.incrementAndGet());
68 final IntentFilter filter = new IntentFilter();
69 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
70
71 mContext.registerReceiver(mBroadcastReceiver, filter, null, mTarget);
72 }
73
74 public void stopListening() {
75 if (DBG) Log.d(TAG, "stopListening for SIM changes");
76
77 if (mBroadcastReceiver == null) return;
78
79 mSimBcastGenerationNumber.incrementAndGet();
80 mContext.unregisterReceiver(mBroadcastReceiver);
81 mBroadcastReceiver = null;
82 }
83
84 private boolean isSimCardLoaded(String state) {
85 return INTENT_VALUE_ICC_LOADED.equals(state);
86 }
87
88 private class SimChangeBroadcastReceiver extends BroadcastReceiver {
89 // used to verify this receiver is still current
90 final private int mGenerationNumber;
91
92 // used to check the sim state transition from non-loaded to loaded
93 private boolean mSimNotLoadedSeen = false;
94
95 public SimChangeBroadcastReceiver(int generationNumber) {
96 mGenerationNumber = generationNumber;
97 }
98
99 @Override
100 public void onReceive(Context context, Intent intent) {
101 final int currentGenerationNumber = mSimBcastGenerationNumber.get();
102
103 if (DBG) {
104 Log.d(TAG, "simchange mGenerationNumber=" + mGenerationNumber +
105 ", current generationNumber=" + currentGenerationNumber);
106 }
107 if (mGenerationNumber != currentGenerationNumber) return;
108
109 final String state = intent.getStringExtra(INTENT_KEY_ICC_STATE);
110 Log.d(TAG, "got Sim changed to state " + state + ", mSimNotLoadedSeen=" +
111 mSimNotLoadedSeen);
112
113 if (!isSimCardLoaded(state)) {
114 mSimNotLoadedSeen = true;
115 return;
116 }
117
118 if (mSimNotLoadedSeen) {
119 mSimNotLoadedSeen = false;
120 mCallback.run();
121 }
122 }
123 }
124}