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 
61     @Override
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         final List<ResolveInfo> resolved =
64                 mPackageManager.queryIntentActivities(mRTTIntent, 0 /* flags */);
65         return resolved != null && !resolved.isEmpty() && isDialerSupportRTTSetting()
66                 ? AVAILABLE
67                 : UNSUPPORTED_ON_DEVICE;
68     }
69 
70     @Override
displayPreference(PreferenceScreen screen)71     public void displayPreference(PreferenceScreen screen) {
72         super.displayPreference(screen);
73         final Preference pref = screen.findPreference(getPreferenceKey());
74         pref.setIntent(mRTTIntent);
75     }
76 
77     @Override
getSummary()78     public CharSequence getSummary() {
79         final int option = Settings.Secure.getInt(mContext.getContentResolver(),
80                 DIALER_RTT_CONFIGURATION, 1 /* not visible */);
81         return mModes[option];
82     }
83 
84     @VisibleForTesting
isDialerSupportRTTSetting()85     boolean isDialerSupportRTTSetting() {
86         return TextUtils.equals(mTelecomManager.getDefaultDialerPackage(), mDialerPackage);
87     }
88 }
89