• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.keyguard;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.media.AudioManager;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 import android.view.View;
25 import android.view.View.AccessibilityDelegate;
26 import android.view.accessibility.AccessibilityEvent;
27 import android.view.accessibility.AccessibilityNodeInfo;
28 
29 import com.android.internal.R;
30 
31 /**
32  * Accessibility delegate that obscures speech for a view when the user has
33  * not turned on the "speak passwords" preference and is not listening
34  * through headphones.
35  */
36 class ObscureSpeechDelegate extends AccessibilityDelegate {
37     /** Whether any client has announced the "headset" notification. */
38     static boolean sAnnouncedHeadset = false;
39 
40     private final ContentResolver mContentResolver;
41     private final AudioManager mAudioManager;
42 
ObscureSpeechDelegate(Context context)43     public ObscureSpeechDelegate(Context context) {
44         mContentResolver = context.getContentResolver();
45         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
46     }
47 
48     @Override
sendAccessibilityEvent(View host, int eventType)49     public void sendAccessibilityEvent(View host, int eventType) {
50         super.sendAccessibilityEvent(host, eventType);
51 
52         // Play the "headset required" announcement the first time the user
53         // places accessibility focus on a key.
54         if ((eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED)
55                 && !sAnnouncedHeadset && shouldObscureSpeech()) {
56             sAnnouncedHeadset = true;
57             host.announceForAccessibility(host.getContext().getString(
58                     R.string.keyboard_headset_required_to_hear_password));
59         }
60     }
61 
62     @Override
onPopulateAccessibilityEvent(View host, AccessibilityEvent event)63     public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
64         super.onPopulateAccessibilityEvent(host, event);
65 
66         if ((event.getEventType() != AccessibilityEvent.TYPE_ANNOUNCEMENT)
67                 && shouldObscureSpeech()) {
68             event.getText().clear();
69             event.setContentDescription(host.getContext().getString(
70                     R.string.keyboard_password_character_no_headset));
71         }
72     }
73 
74     @Override
onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info)75     public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
76         super.onInitializeAccessibilityNodeInfo(host, info);
77 
78         if (shouldObscureSpeech()) {
79             final Context ctx = host.getContext();
80             info.setText(null);
81             info.setContentDescription(
82                     ctx.getString(R.string.keyboard_password_character_no_headset));
83         }
84     }
85 
86     @SuppressWarnings("deprecation")
shouldObscureSpeech()87     private boolean shouldObscureSpeech() {
88         // The user can optionally force speaking passwords.
89         if (Settings.Secure.getIntForUser(mContentResolver,
90                 Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0, UserHandle.USER_CURRENT) != 0) {
91             return false;
92         }
93 
94         // Always speak if the user is listening through headphones.
95         if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) {
96             return false;
97         }
98 
99         // Don't speak since this key is used to type a password.
100         return true;
101     }
102 }