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.telephony;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 import android.telephony.data.ApnSetting;
25 
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.network.MobileDataContentObserver;
29 import com.android.settingslib.core.lifecycle.LifecycleObserver;
30 import com.android.settingslib.core.lifecycle.events.OnStart;
31 import com.android.settingslib.core.lifecycle.events.OnStop;
32 
33 /**
34  * Preference controller for "Mobile data"
35  */
36 public class MmsMessagePreferenceController extends TelephonyTogglePreferenceController implements
37         LifecycleObserver, OnStart, OnStop {
38     private TelephonyManager mTelephonyManager;
39     private SubscriptionManager mSubscriptionManager;
40     private MobileDataContentObserver mMobileDataContentObserver;
41     private PreferenceScreen mScreen;
42 
MmsMessagePreferenceController(Context context, String key)43     public MmsMessagePreferenceController(Context context, String key) {
44         super(context, key);
45         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
46         mMobileDataContentObserver = new MobileDataContentObserver(
47                 new Handler(Looper.getMainLooper()));
48         mMobileDataContentObserver.setOnMobileDataChangedListener(()->refreshPreference());
49     }
50 
51     @Override
getAvailabilityStatus(int subId)52     public int getAvailabilityStatus(int subId) {
53         final TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class)
54                 .createForSubscriptionId(subId);
55         return (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
56                 && !telephonyManager.isDataEnabled()
57                 && telephonyManager.isApnMetered(ApnSetting.TYPE_MMS))
58                 ? AVAILABLE
59                 : CONDITIONALLY_UNAVAILABLE;
60     }
61 
62     @Override
onStart()63     public void onStart() {
64         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
65             mMobileDataContentObserver.register(mContext, mSubId);
66         }
67     }
68 
69     @Override
onStop()70     public void onStop() {
71         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
72             mMobileDataContentObserver.unRegister(mContext);
73         }
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79         mScreen = screen;
80     }
81 
82 
init(int subId)83     public void init(int subId) {
84         mSubId = subId;
85         mTelephonyManager = mContext.getSystemService(TelephonyManager.class)
86                 .createForSubscriptionId(mSubId);
87     }
88 
89     @Override
setChecked(boolean isChecked)90     public boolean setChecked(boolean isChecked) {
91         return mTelephonyManager.setAlwaysAllowMmsData(isChecked);
92     }
93 
94     @Override
isChecked()95     public boolean isChecked() {
96         return mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS);
97     }
98 
refreshPreference()99     private void refreshPreference() {
100         if (mScreen != null) {
101             super.displayPreference(mScreen);
102         }
103     }
104 }
105