• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.network;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.database.ContentObserver;
24 import android.net.Uri;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.provider.Settings;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
30 import android.util.Log;
31 
32 import com.android.internal.telephony.TelephonyIntents;
33 
34 /** Helper class for listening to changes in availability of telephony subscriptions */
35 public class SubscriptionsChangeListener extends ContentObserver {
36 
37     private static final String TAG = "SubscriptionsChangeListener";
38 
39     public interface SubscriptionsChangeListenerClient {
onAirplaneModeChanged(boolean airplaneModeEnabled)40         void onAirplaneModeChanged(boolean airplaneModeEnabled);
onSubscriptionsChanged()41         void onSubscriptionsChanged();
42     }
43 
44     private Context mContext;
45     private SubscriptionsChangeListenerClient mClient;
46     private SubscriptionManager mSubscriptionManager;
47     private OnSubscriptionsChangedListener mSubscriptionsChangedListener;
48     private Uri mAirplaneModeSettingUri;
49     private BroadcastReceiver mBroadcastReceiver;
50     private boolean mRunning = false;
51 
SubscriptionsChangeListener(Context context, SubscriptionsChangeListenerClient client)52     public SubscriptionsChangeListener(Context context, SubscriptionsChangeListenerClient client) {
53         super(new Handler(Looper.getMainLooper()));
54         mContext = context;
55         mClient = client;
56         mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
57         mSubscriptionsChangedListener = new OnSubscriptionsChangedListener(Looper.getMainLooper()) {
58             @Override
59             public void onSubscriptionsChanged() {
60                 subscriptionsChangedCallback();
61             }
62         };
63         mAirplaneModeSettingUri = Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON);
64         mBroadcastReceiver = new BroadcastReceiver() {
65             @Override
66             public void onReceive(Context context, Intent intent) {
67                 if (!isInitialStickyBroadcast()) {
68                     subscriptionsChangedCallback();
69                 }
70             }
71         };
72     }
73 
start()74     public void start() {
75         mSubscriptionManager.addOnSubscriptionsChangedListener(
76                 mContext.getMainExecutor(), mSubscriptionsChangedListener);
77         mContext.getContentResolver()
78                 .registerContentObserver(mAirplaneModeSettingUri, false, this);
79         final IntentFilter radioTechnologyChangedFilter = new IntentFilter(
80                 TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
81         mContext.registerReceiver(mBroadcastReceiver, radioTechnologyChangedFilter);
82         mRunning = true;
83     }
84 
stop()85     public void stop() {
86         if (mRunning) {
87             mSubscriptionManager.removeOnSubscriptionsChangedListener(
88                     mSubscriptionsChangedListener);
89             mContext.getContentResolver().unregisterContentObserver(this);
90             mContext.unregisterReceiver(mBroadcastReceiver);
91             mRunning = false;
92         } else {
93             Log.d(TAG, "Stop has been called without associated Start.");
94         }
95     }
96 
isAirplaneModeOn()97     public boolean isAirplaneModeOn() {
98         return Settings.Global.getInt(mContext.getContentResolver(),
99                 Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
100     }
101 
subscriptionsChangedCallback()102     private void subscriptionsChangedCallback() {
103         mClient.onSubscriptionsChanged();
104     }
105 
106     @Override
onChange(boolean selfChange, Uri uri)107     public void onChange(boolean selfChange, Uri uri) {
108         if (uri.equals(mAirplaneModeSettingUri)) {
109             mClient.onAirplaneModeChanged(isAirplaneModeOn());
110         }
111     }
112 }
113