summaryrefslogtreecommitdiffstats
path: root/packages/Keyguard/src/com/android/keyguard/ObscureSpeechDelegate.java
blob: 410a43a6789a2dac747334ae95e00c282f0b31d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.keyguard;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.android.internal.R;

/**
 * Accessibility delegate that obscures speech for a view when the user has
 * not turned on the "speak passwords" preference and is not listening
 * through headphones.
 */
class ObscureSpeechDelegate extends AccessibilityDelegate {
    /** Whether any client has announced the "headset" notification. */
    static boolean sAnnouncedHeadset = false;

    private final ContentResolver mContentResolver;
    private final AudioManager mAudioManager;

    public ObscureSpeechDelegate(Context context) {
        mContentResolver = context.getContentResolver();
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public void sendAccessibilityEvent(View host, int eventType) {
        super.sendAccessibilityEvent(host, eventType);

        // Play the "headset required" announcement the first time the user
        // places accessibility focus on a key.
        if ((eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED)
                && !sAnnouncedHeadset && shouldObscureSpeech()) {
            sAnnouncedHeadset = true;
            host.announceForAccessibility(host.getContext().getString(
                    R.string.keyboard_headset_required_to_hear_password));
        }
    }

    @Override
    public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(host, event);

        if ((event.getEventType() != AccessibilityEvent.TYPE_ANNOUNCEMENT)
                && shouldObscureSpeech()) {
            event.getText().clear();
            event.setContentDescription(host.getContext().getString(
                    R.string.keyboard_password_character_no_headset));
        }
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(host, info);

        if (shouldObscureSpeech()) {
            final Context ctx = host.getContext();
            info.setText(null);
            info.setContentDescription(
                    ctx.getString(R.string.keyboard_password_character_no_headset));
        }
    }

    @SuppressWarnings("deprecation")
    private boolean shouldObscureSpeech() {
        // The user can optionally force speaking passwords.
        if (Settings.Secure.getIntForUser(mContentResolver,
                Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD, 0, UserHandle.USER_CURRENT) != 0) {
            return false;
        }

        // Always speak if the user is listening through headphones.
        if (mAudioManager.isWiredHeadsetOn() || mAudioManager.isBluetoothA2dpOn()) {
            return false;
        }

        // Don't speak since this key is used to type a password.
        return true;
    }
}