• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.car.settings.qc;
18 
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.provider.Settings;
25 import android.telephony.SignalStrength;
26 import android.telephony.SubscriptionManager;
27 import android.telephony.TelephonyCallback;
28 import android.telephony.TelephonyManager;
29 
30 import java.io.IOException;
31 
32 /**
33  * Base background worker for mobile data QCItems.
34  * @param <E> The {@link SettingsQCItem} the background worker is associated with.
35  */
36 public abstract class MobileDataBaseWorker<E extends SettingsQCItem>
37         extends SettingsQCBackgroundWorker<E> {
38 
39     private final TelephonyManager mTelephonyManager;
40     private final int mSubId;
41     private final SignalStrengthsListener mSignalStrengthsListener;
42     private boolean mCallbacksRegistered;
43 
44     private final ContentObserver mMobileDataChangeObserver = new ContentObserver(
45             new Handler(Looper.getMainLooper())) {
46         @Override
47         public void onChange(boolean selfChange) {
48             super.onChange(selfChange);
49             notifyQCItemChange();
50         }
51     };
52 
MobileDataBaseWorker(Context context, Uri uri)53     protected MobileDataBaseWorker(Context context, Uri uri) {
54         super(context, uri);
55         mTelephonyManager = context.getSystemService(TelephonyManager.class);
56         mSubId = SubscriptionManager.getDefaultDataSubscriptionId();
57         mSignalStrengthsListener = new SignalStrengthsListener();
58     }
59 
60     @Override
onQCItemSubscribe()61     protected void onQCItemSubscribe() {
62         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && !mCallbacksRegistered) {
63             mTelephonyManager.registerTelephonyCallback(getContext().getMainExecutor(),
64                     mSignalStrengthsListener);
65             getContext().getContentResolver().registerContentObserver(getObservableUri(mSubId),
66                     /* notifyForDescendants= */ false, mMobileDataChangeObserver);
67             mCallbacksRegistered = true;
68         }
69     }
70 
71     @Override
onQCItemUnsubscribe()72     protected void onQCItemUnsubscribe() {
73         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
74             unregisterCallbacks();
75         }
76     }
77 
78     @Override
close()79     public void close() throws IOException {
80         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
81             unregisterCallbacks();
82         }
83     }
84 
unregisterCallbacks()85     private void unregisterCallbacks() {
86         if (mCallbacksRegistered) {
87             mTelephonyManager.unregisterTelephonyCallback(mSignalStrengthsListener);
88             getContext().getContentResolver().unregisterContentObserver(mMobileDataChangeObserver);
89             mCallbacksRegistered = false;
90         }
91     }
92 
getObservableUri(int subId)93     private Uri getObservableUri(int subId) {
94         Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA);
95         if (TelephonyManager.from(getContext()).getSimCount() != 1) {
96             uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + subId);
97         }
98         return uri;
99     }
100 
101     private class SignalStrengthsListener extends TelephonyCallback
102             implements TelephonyCallback.SignalStrengthsListener {
103 
104         @Override
onSignalStrengthsChanged(SignalStrength signalStrength)105         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
106             notifyQCItemChange();
107         }
108     }
109 }
110