1 package com.android.settings.network;
2 
3 import static com.android.internal.telephony.TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED;
4 
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.telephony.SubscriptionManager;
10 import android.telephony.TelephonyManager;
11 
12 public class DefaultSubscriptionReceiver extends BroadcastReceiver {
13 
14     private Context mContext;
15     private DefaultSubscriptionListener mListener;
16 
DefaultSubscriptionReceiver(Context context, DefaultSubscriptionListener listener)17     public DefaultSubscriptionReceiver(Context context, DefaultSubscriptionListener listener) {
18         mContext = context;
19         mListener = listener;
20     }
21 
registerReceiver()22     public void registerReceiver() {
23         final IntentFilter filter = new IntentFilter();
24         filter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
25         filter.addAction(SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
26         filter.addAction(ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);
27         filter.addAction(SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED);
28         mContext.registerReceiver(this, filter);
29     }
30 
unRegisterReceiver()31     public void unRegisterReceiver() {
32         mContext.unregisterReceiver(this);
33     }
34 
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37         String action = intent.getAction();
38         if (TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED.equals(action)) {
39             mListener.onDefaultDataChanged(SubscriptionManager.getDefaultDataSubscriptionId());
40         } else if (SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED.equals(action)) {
41             mListener.onDefaultSubInfoChanged(SubscriptionManager.getDefaultSubscriptionId());
42         } else if (TelephonyManager.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED.equals(action)) {
43             mListener.onDefaultVoiceChanged(SubscriptionManager.getDefaultVoiceSubscriptionId());
44         } else if (SubscriptionManager.ACTION_DEFAULT_SMS_SUBSCRIPTION_CHANGED.equals(action)) {
45             mListener.onDefaultSmsChanged(SubscriptionManager.getDefaultSmsSubscriptionId());
46         }
47     }
48 
49     public interface DefaultSubscriptionListener {
onDefaultSubInfoChanged(int defaultSubId)50         default void onDefaultSubInfoChanged(int defaultSubId) {
51         }
onDefaultDataChanged(int defaultDataSubId)52         default void onDefaultDataChanged(int defaultDataSubId) {
53         }
onDefaultVoiceChanged(int defaultVoiceSubId)54         default void onDefaultVoiceChanged(int defaultVoiceSubId) {
55         }
onDefaultSmsChanged(int defaultSmsSubId)56         default void onDefaultSmsChanged(int defaultSmsSubId) {
57         }
58     }
59 }
60