1 /**
2  * Copyright (C) 2021 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 package com.android.car.voicecontrol;
17 
18 import android.annotation.Nullable;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 
22 import androidx.preference.PreferenceManager;
23 
24 /**
25  * Minimal implementation of a preferences controller.
26  */
27 public class PreferencesController {
28     private SharedPreferences mPreferences;
29     private static PreferencesController sInstance;
30 
31     public static final String PREF_KEY_USER_NAME = "user_name";
32     public static final String PREF_KEY_VOICE = "voice";
33     public static final String PREF_KEY_DEBUG_DIRECT_SEND = "toggle_debug_direct_send";
34 
PreferencesController(Context context)35     private PreferencesController(Context context) {
36         mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
37     }
38 
39     /**
40      * @return a singleton instance of {@link PreferencesController}
41      */
getInstance(Context context)42     public static PreferencesController getInstance(Context context) {
43         if (sInstance == null) {
44             sInstance = new PreferencesController(context.getApplicationContext());
45         }
46         return sInstance;
47     }
48 
49     /**
50      * @return whether the user has signed or not
51      */
isUserSignedIn()52     public boolean isUserSignedIn() {
53         return getUsername() != null;
54     }
55 
56     /**
57      * Stores the username of the currently logged user. Set this to null if no user is currently
58      * logged in.
59      */
setUsername(String username)60     public void setUsername(String username) {
61         mPreferences.edit()
62                 .putString(PREF_KEY_USER_NAME, username)
63                 .apply();
64     }
65 
66     /**
67      * @return the user name of the authenticated user
68      */
getUsername()69     public String getUsername() {
70         return mPreferences.getString(PREF_KEY_USER_NAME, null);
71     }
72 
73     /**
74      * Sets the preferred voice to use for text-to-speech.
75      *
76      * @param name Should be the name of one of the voices returned by
77      * {@link android.speech.tts.TextToSpeech#getVoices()}
78      */
setVoice(@ullable String name)79     public void setVoice(@Nullable String name) {
80         mPreferences.edit()
81                 .putString(PREF_KEY_VOICE, name)
82                 .apply();
83     }
84 
85     /**
86      * @return preferred voice name or null if there is no selection
87      */
getVoice()88     public String getVoice() {
89         return mPreferences.getString(PREF_KEY_VOICE, null);
90     }
91 
92     /**
93      * @return true if text input enabled, false otherwise
94      */
isDirectSendTextInput()95     public boolean isDirectSendTextInput() {
96         return mPreferences.getBoolean(PREF_KEY_DEBUG_DIRECT_SEND, false);
97     }
98 }
99