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