aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/receiver/AudioCapabilitiesReceiver.java
blob: 949222a928a1fbca33b819a5804995e4752ccd94 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2015 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.tv.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.AudioFormat;
import android.media.AudioManager;

import com.android.tv.analytics.Tracker;

import java.util.Arrays;

/**
 * Creates HDMI plug broadcast receiver, and reports AC3 passthrough capabilities
 * to Google Analytics. Call {@link #register} to start receiving notifications,
 * and {@link #unregister} to stop.
 */
public final class AudioCapabilitiesReceiver {
    private static final String PREFS_NAME = "com.android.tv.audio_capabilities";
    private static final String SETTINGS_KEY_AC3_PASSTHRU_REPORTED = "ac3_passthrough_reported";
    private static final String SETTINGS_KEY_AC3_PASSTHRU_CAPABILITIES = "ac3_passthrough";
    private static final String SETTINGS_KEY_AC3_REPORT_REVISION = "ac3_report_revision";

    // AC3 capabilities stat is sent to Google Analytics just once in order to avoid
    // duplicated stat reports since it doesn't change over time in most cases.
    // Increase this revision when we should force the stat to be sent again.
    // TODO: Consier using custom metrics.
    private static final int REPORT_REVISION = 1;

    private final Context mContext;
    private final Tracker mTracker;
    private final BroadcastReceiver mReceiver = new HdmiAudioPlugBroadcastReceiver();

    /**
     * Constructs a new audio capabilities receiver.
     *
     * @param context context for registering to receive broadcasts
     * @param tracker tracker object used to upload capabilities info to Google Analytics
     */
    public AudioCapabilitiesReceiver(Context context, Tracker tracker) {
        mContext = context;
        mTracker = tracker;
    }

    public void register() {
        mContext.registerReceiver(mReceiver, new IntentFilter(AudioManager.ACTION_HDMI_AUDIO_PLUG));
    }

    public void unregister() {
        mContext.unregisterReceiver(mReceiver);
    }

    private final class HdmiAudioPlugBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!action.equals(AudioManager.ACTION_HDMI_AUDIO_PLUG)) {
                return;
            }
            reportAudioCapabilities(intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS));
        }
    }

    private void reportAudioCapabilities(int[] supportedEncodings) {
        boolean newVal = supportedEncodings != null
                && Arrays.binarySearch(supportedEncodings, AudioFormat.ENCODING_AC3) >= 0;
        boolean oldVal = getBoolean(SETTINGS_KEY_AC3_PASSTHRU_CAPABILITIES, false);
        boolean reported = getBoolean(SETTINGS_KEY_AC3_PASSTHRU_REPORTED, false);
        int revision = getInt(SETTINGS_KEY_AC3_REPORT_REVISION, 0);

        // Send the value just once. But we send it again if the value changed, to include
        // the case where users have switched TV device with different AC3 passthrough capabilities.
        if (!reported || oldVal != newVal || REPORT_REVISION > revision) {
            mTracker.sendAc3PassthroughCapabilities(newVal);
            setBoolean(SETTINGS_KEY_AC3_PASSTHRU_REPORTED, true);
            setBoolean(SETTINGS_KEY_AC3_PASSTHRU_CAPABILITIES, newVal);
            if (REPORT_REVISION > revision) {
                setInt(SETTINGS_KEY_AC3_REPORT_REVISION, REPORT_REVISION);
            }
        }
    }

    private SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    private boolean getBoolean(String key, boolean def) {
        return getSharedPreferences().getBoolean(key, def);
    }

    private void setBoolean(String key, boolean val) {
        getSharedPreferences().edit().putBoolean(key, val).apply();
    }

    private int getInt(String key, int def) {
        return getSharedPreferences().getInt(key, def);
    }

    private void setInt(String key, int val) {
        getSharedPreferences().edit().putInt(key, val).apply();
    }
}