• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.database.ContentObserver;
21 import android.net.Uri;
22 import android.os.PersistableBundle;
23 import android.provider.Telephony;
24 import android.telephony.CarrierConfigManager;
25 import android.telephony.SubscriptionInfo;
26 import android.telephony.ims.ImsManager;
27 import android.util.Log;
28 
29 import androidx.annotation.VisibleForTesting;
30 import androidx.fragment.app.FragmentManager;
31 import androidx.lifecycle.Lifecycle;
32 import androidx.lifecycle.LifecycleObserver;
33 import androidx.lifecycle.OnLifecycleEvent;
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceScreen;
36 import androidx.preference.SwitchPreference;
37 
38 import com.android.settings.network.SubscriptionUtil;
39 
40 /**
41  * Controller for the "Contact Discovery" option present in MobileNetworkSettings.
42  */
43 public class ContactDiscoveryPreferenceController extends TelephonyTogglePreferenceController
44         implements LifecycleObserver {
45     private static final String TAG = "ContactDiscoveryPref";
46     private static final Uri UCE_URI = Uri.withAppendedPath(Telephony.SimInfo.CONTENT_URI,
47             Telephony.SimInfo.COLUMN_IMS_RCS_UCE_ENABLED);
48 
49     private ImsManager mImsManager;
50     private CarrierConfigManager mCarrierConfigManager;
51     private ContentObserver mUceSettingObserver;
52     private FragmentManager mFragmentManager;
53 
54     @VisibleForTesting
55     public Preference preference;
56 
ContactDiscoveryPreferenceController(Context context, String key)57     public ContactDiscoveryPreferenceController(Context context, String key) {
58         super(context, key);
59         mImsManager = mContext.getSystemService(ImsManager.class);
60         mCarrierConfigManager = mContext.getSystemService(CarrierConfigManager.class);
61     }
62 
init(FragmentManager fragmentManager, int subId, Lifecycle lifecycle)63     public ContactDiscoveryPreferenceController init(FragmentManager fragmentManager, int subId,
64             Lifecycle lifecycle) {
65         mFragmentManager = fragmentManager;
66         mSubId = subId;
67         lifecycle.addObserver(this);
68         return this;
69     }
70 
71     @Override
isChecked()72     public boolean isChecked() {
73         return MobileNetworkUtils.isContactDiscoveryEnabled(mImsManager, mSubId);
74     }
75 
76     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
onResume()77     public void onResume() {
78         registerUceObserver();
79     }
80 
81     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
onPause()82     public void onPause() {
83         unregisterUceObserver();
84     }
85 
86     @Override
setChecked(boolean isChecked)87     public boolean setChecked(boolean isChecked) {
88         if (isChecked) {
89             showContentDiscoveryDialog();
90             // launch dialog and wait for activity to return and ContentObserver to fire to update.
91             return false;
92         }
93         MobileNetworkUtils.setContactDiscoveryEnabled(mImsManager, mSubId, false /*isEnabled*/);
94         return true;
95     }
96 
97     @Override
getAvailabilityStatus(int subId)98     public int getAvailabilityStatus(int subId) {
99         PersistableBundle bundle = mCarrierConfigManager.getConfigForSubId(subId);
100         boolean shouldShowPresence = bundle != null
101                 && (bundle.getBoolean(
102                 CarrierConfigManager.KEY_USE_RCS_PRESENCE_BOOL, false /*default*/)
103                 || bundle.getBoolean(
104                 CarrierConfigManager.Ims.KEY_RCS_BULK_CAPABILITY_EXCHANGE_BOOL, false /*default*/));
105         return shouldShowPresence ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
106     }
107 
108     @Override
displayPreference(PreferenceScreen screen)109     public void displayPreference(PreferenceScreen screen) {
110         super.displayPreference(screen);
111         preference = screen.findPreference(getPreferenceKey());
112     }
113 
registerUceObserver()114     private void registerUceObserver() {
115         mUceSettingObserver = new ContentObserver(mContext.getMainThreadHandler()) {
116             @Override
117             public void onChange(boolean selfChange) {
118                 onChange(selfChange, null /*uri*/);
119             }
120 
121             @Override
122             public void onChange(boolean selfChange, Uri uri) {
123                 Log.d(TAG, "UCE setting changed, re-evaluating.");
124                 SwitchPreference switchPref = (SwitchPreference) preference;
125                 switchPref.setChecked(isChecked());
126             }
127         };
128         mContext.getContentResolver().registerContentObserver(UCE_URI, true /*notifyForDecendants*/,
129                 mUceSettingObserver);
130     }
131 
unregisterUceObserver()132     private void unregisterUceObserver() {
133         mContext.getContentResolver().unregisterContentObserver(mUceSettingObserver);
134     }
135 
showContentDiscoveryDialog()136     private void showContentDiscoveryDialog() {
137         ContactDiscoveryDialogFragment dialog = ContactDiscoveryDialogFragment.newInstance(
138                 mSubId, getCarrierDisplayName(preference.getContext()));
139         dialog.show(mFragmentManager, ContactDiscoveryDialogFragment.getFragmentTag(mSubId));
140     }
141 
getCarrierDisplayName(Context context)142     private CharSequence getCarrierDisplayName(Context context) {
143         CharSequence result = "";
144 
145         for (SubscriptionInfo info : SubscriptionUtil.getAvailableSubscriptions(context)) {
146             if (mSubId == info.getSubscriptionId()) {
147                 result = SubscriptionUtil.getUniqueSubscriptionDisplayName(info, context);
148                 break;
149             }
150         }
151         return result;
152     }
153 }
154