aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/receiver/AudioCapabilitiesReceiver.java
blob: 3fb66245a02db0e414a39b187a98e5448190529c (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * 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 android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.android.tv.TvSingletons;
import com.android.tv.analytics.Analytics;
import com.android.tv.analytics.Tracker;
import com.android.tv.common.util.SharedPreferencesUtils;

/**
 * Creates HDMI plug broadcast receiver, and reports AC3 passthrough capabilities to Google
 * Analytics and listeners. Call {@link #register} to start receiving notifications, and {@link
 * #unregister} to stop.
 */
public final class AudioCapabilitiesReceiver {
    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 Analytics mAnalytics;
    private final Tracker mTracker;
    @Nullable private final OnAc3PassthroughCapabilityChangeListener mListener;
    private final BroadcastReceiver mReceiver = new HdmiAudioPlugBroadcastReceiver();

    /**
     * Constructs a new audio capabilities receiver.
     *
     * @param context context for registering to receive broadcasts
     * @param listener listener which receives AC3 passthrough capability change notification
     */
    public AudioCapabilitiesReceiver(
            @NonNull Context context, @Nullable OnAc3PassthroughCapabilityChangeListener listener) {
        mContext = context;
        TvSingletons tvSingletons = TvSingletons.getSingletons(context);
        mAnalytics = tvSingletons.getAnalytics();
        mTracker = tvSingletons.getTracker();
        mListener = listener;
    }

    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;
            }
            boolean supported = false;
            int[] supportedEncodings = intent.getIntArrayExtra(AudioManager.EXTRA_ENCODINGS);
            if (supportedEncodings != null) {
                for (int supportedEncoding : supportedEncodings) {
                    if (supportedEncoding == AudioFormat.ENCODING_AC3) {
                        supported = true;
                        break;
                    }
                }
            }
            if (mListener != null) {
                mListener.onAc3PassthroughCapabilityChange(supported);
            }
            if (!mAnalytics.isAppOptOut()) {
                reportAudioCapabilities(supported);
            }
        }
    }

    private void reportAudioCapabilities(boolean ac3Supported) {
        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 != ac3Supported || REPORT_REVISION > revision) {
            mTracker.sendAc3PassthroughCapabilities(ac3Supported);
            setBoolean(SETTINGS_KEY_AC3_PASSTHRU_REPORTED, true);
            setBoolean(SETTINGS_KEY_AC3_PASSTHRU_CAPABILITIES, ac3Supported);
            if (REPORT_REVISION > revision) {
                setInt(SETTINGS_KEY_AC3_REPORT_REVISION, REPORT_REVISION);
            }
        }
    }

    private SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences(
                SharedPreferencesUtils.SHARED_PREF_AUDIO_CAPABILITIES, 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();
    }

    /** Listener notified when AC3 passthrough capability changes. */
    public interface OnAc3PassthroughCapabilityChangeListener {
        /** Called when the AC3 passthrough capability changes. */
        void onAc3PassthroughCapabilityChange(boolean capability);
    }
}