• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.Context;
20 import android.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.Handler;
23 import android.provider.Settings;
24 import android.telephony.TelephonyManager;
25 
26 /**
27  * {@link ContentObserver} to listen to update of mobile data change
28  */
29 public class MobileDataContentObserver extends ContentObserver {
30     private OnMobileDataChangedListener mListener;
31 
MobileDataContentObserver(Handler handler)32     public MobileDataContentObserver(Handler handler) {
33         super(handler);
34     }
35 
getObservableUri(int subId)36     public static Uri getObservableUri(int subId) {
37         Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA);
38         if (TelephonyManager.getDefault().getSimCount() != 1) {
39             uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + subId);
40         }
41         return uri;
42     }
43 
setOnMobileDataChangedListener(OnMobileDataChangedListener lsn)44     public void setOnMobileDataChangedListener(OnMobileDataChangedListener lsn) {
45         mListener = lsn;
46     }
47 
48     @Override
onChange(boolean selfChange)49     public void onChange(boolean selfChange) {
50         super.onChange(selfChange);
51         if (mListener != null) {
52             mListener.onMobileDataChanged();
53         }
54     }
55 
register(Context context, int subId)56     public void register(Context context, int subId) {
57         final Uri uri = getObservableUri(subId);
58         context.getContentResolver().registerContentObserver(uri, false, this);
59 
60     }
61 
unRegister(Context context)62     public void unRegister(Context context) {
63         context.getContentResolver().unregisterContentObserver(this);
64     }
65 
66     /**
67      * Listener for update of mobile data(ON vs OFF)
68      */
69     public interface OnMobileDataChangedListener {
onMobileDataChanged()70         void onMobileDataChanged();
71     }
72 }
73