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.accessibility;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.provider.Settings;
24 import android.telecom.TelecomManager;
25 import android.text.TextUtils;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.BasePreferenceController;
33 
34 import java.util.List;
35 
36 /** A controller to control the status for RTT setting in Accessibility screen.*/
37 public class RTTSettingPreferenceController extends BasePreferenceController {
38 
39     private static final String DIALER_RTT_CONFIGURATION = "dialer_rtt_configuration";
40 
41     private final Context mContext;
42     private final PackageManager mPackageManager;
43     private final TelecomManager mTelecomManager;
44     private final CharSequence[] mModes;
45     private final String mDialerPackage;
46 
47     @VisibleForTesting
48     Intent mRTTIntent;
49 
RTTSettingPreferenceController(Context context, String preferenceKey)50     public RTTSettingPreferenceController(Context context, String preferenceKey) {
51         super(context, preferenceKey);
52         mContext = context;
53         mModes = mContext.getResources().getTextArray(R.array.rtt_setting_mode);
54         mDialerPackage = mContext.getString(R.string.config_rtt_setting_package_name);
55         mPackageManager = context.getPackageManager();
56         mTelecomManager = context.getSystemService(TelecomManager.class);
57         mRTTIntent = new Intent(context.getString(R.string.config_rtt_setting_intent_action));
58     }
59 
60     @Override
getAvailabilityStatus()61     public int getAvailabilityStatus() {
62         final List<ResolveInfo> resolved =
63                 mPackageManager.queryIntentActivities(mRTTIntent, 0 /* flags */);
64         return resolved != null && !resolved.isEmpty() && isDialerSupportRTTSetting()
65                 ? AVAILABLE
66                 : UNSUPPORTED_ON_DEVICE;
67     }
68 
69     @Override
displayPreference(PreferenceScreen screen)70     public void displayPreference(PreferenceScreen screen) {
71         super.displayPreference(screen);
72         final Preference pref = screen.findPreference(getPreferenceKey());
73         pref.setIntent(mRTTIntent);
74     }
75 
76     @Override
getSummary()77     public CharSequence getSummary() {
78         final int option = Settings.Secure.getInt(mContext.getContentResolver(),
79                 DIALER_RTT_CONFIGURATION, 1 /* not visible */);
80         return mModes[option];
81     }
82 
83     @VisibleForTesting
isDialerSupportRTTSetting()84     boolean isDialerSupportRTTSetting() {
85         return TextUtils.equals(mTelecomManager.getDefaultDialerPackage(), mDialerPackage);
86     }
87 }
88