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.car.settings.tts;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.speech.tts.TextToSpeech;
23 import android.speech.tts.TtsEngines;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.common.Logger;
29 import com.android.car.settings.common.PreferenceController;
30 import com.android.car.ui.preference.CarUiTwoActionIconPreference;
31 
32 /** Business logic to set the summary for the preferred engine entry setting. */
33 public class PreferredEngineEntryPreferenceController extends
34         PreferenceController<CarUiTwoActionIconPreference> {
35 
36     private static final Logger LOG = new Logger(PreferredEngineEntryPreferenceController.class);
37     private TtsEngines mEnginesHelper;
38 
PreferredEngineEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)39     public PreferredEngineEntryPreferenceController(Context context, String preferenceKey,
40             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
41         this(context, preferenceKey, fragmentController, uxRestrictions, new TtsEngines(context));
42     }
43 
44     @VisibleForTesting
PreferredEngineEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, TtsEngines enginesHelper)45     public PreferredEngineEntryPreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
47             TtsEngines enginesHelper) {
48         super(context, preferenceKey, fragmentController, uxRestrictions);
49         mEnginesHelper = enginesHelper;
50     }
51 
52     @Override
getPreferenceType()53     protected Class<CarUiTwoActionIconPreference> getPreferenceType() {
54         return CarUiTwoActionIconPreference.class;
55     }
56 
57     @Override
onCreateInternal()58     protected void onCreateInternal() {
59         getPreference().setOnSecondaryActionClickListener(() -> {
60             TextToSpeech.EngineInfo info = mEnginesHelper.getEngineInfo(
61                     mEnginesHelper.getDefaultEngine());
62             if (info == null) {
63                 LOG.e("EngineInfo is null");
64                 return;
65             }
66             Intent subSettingsIntent = mEnginesHelper.getSettingsIntent(info.name);
67             if (subSettingsIntent != null) {
68                 getContext().startActivity(subSettingsIntent);
69             } else {
70                 LOG.e("subSettingsIntent is null");
71             }
72         });
73     }
74 
75     @Override
updateState(CarUiTwoActionIconPreference preference)76     protected void updateState(CarUiTwoActionIconPreference preference) {
77         TextToSpeech.EngineInfo info = mEnginesHelper.getEngineInfo(
78                 mEnginesHelper.getDefaultEngine());
79         if (info == null) {
80             LOG.e("EngineInfo is null");
81             return;
82         }
83         preference.setSummary(info.label);
84     }
85 }
86