aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/util/PipInputManager.java
blob: 2c51d5a01ad920b498ca1fa6d0f2691cc7a1ec0f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * 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.util;

import android.content.Context;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.media.tv.TvInputManager.TvInputCallback;
import android.util.ArraySet;
import android.util.Log;

import com.android.tv.ChannelTuner;
import com.android.tv.R;
import com.android.tv.data.Channel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A class that manages inputs for PIP. All tuner inputs are represented to one tuner input for PIP.
 * Hidden inputs should not be visible to the users.
 */
public class PipInputManager {
    private static final String TAG = "PipInputManager";

    // Tuner inputs aren't distinguished each other in PipInput. They are handled as one input.
    // Therefore, we define a fake input id for the unified input.
    private static final String TUNER_INPUT_ID = "tuner_input_id";

    private final Context mContext;
    private final TvInputManagerHelper mInputManager;
    private final ChannelTuner mChannelTuner;
    private boolean mStarted;
    private final Map<String, PipInput> mPipInputMap = new HashMap<>();  // inputId -> PipInput
    private final Set<Listener> mListeners = new ArraySet<>();

    private final TvInputCallback mTvInputCallback = new TvInputCallback() {
        @Override
        public void onInputAdded(String inputId) {
            TvInputInfo input = mInputManager.getTvInputInfo(inputId);
            if (input.isPassthroughInput()) {
                boolean available = mInputManager.getInputState(input)
                        == TvInputManager.INPUT_STATE_CONNECTED;
                mPipInputMap.put(inputId, new PipInput(inputId, available));
            } else if (!mPipInputMap.containsKey(TUNER_INPUT_ID)) {
                boolean available = mChannelTuner.getBrowsableChannelCount() != 0;
                mPipInputMap.put(TUNER_INPUT_ID, new PipInput(TUNER_INPUT_ID, available));
            } else {
                return;
            }
            for (Listener l : mListeners) {
                l.onPipInputListUpdated();
            }
        }

        @Override
        public void onInputRemoved(String inputId) {
            PipInput pipInput = mPipInputMap.remove(inputId);
            if (pipInput == null) {
                if (!mPipInputMap.containsKey(TUNER_INPUT_ID)) {
                    Log.w(TAG, "A TV input (" + inputId + ") isn't tracked in PipInputManager");
                    return;
                }
                if (mInputManager.getTunerTvInputSize() > 0) {
                    return;
                }
                mPipInputMap.remove(TUNER_INPUT_ID);
            }
            for (Listener l : mListeners) {
                l.onPipInputListUpdated();
            }
        }

        @Override
        public void onInputStateChanged(String inputId, int state) {
            PipInput pipInput = mPipInputMap.get(inputId);
            if (pipInput == null) {
                // For tuner input, state change is handled in mChannelTunerListener.
                return;
            }
            pipInput.updateAvailability();
        }
    };

    private final ChannelTuner.Listener mChannelTunerListener = new ChannelTuner.Listener() {
        @Override
        public void onLoadFinished() { }

        @Override
        public void onCurrentChannelUnavailable(Channel channel) { }

        @Override
        public void onBrowsableChannelListChanged() {
            PipInput tunerInput = mPipInputMap.get(TUNER_INPUT_ID);
            if (tunerInput == null) {
                return;
            }
            tunerInput.updateAvailability();
        }

        @Override
        public void onChannelChanged(Channel previousChannel, Channel currentChannel) {
            if (previousChannel != null && currentChannel != null
                    && !previousChannel.isPassthrough() && !currentChannel.isPassthrough()) {
                // Channel change between channels for tuner inputs.
                return;
            }
            PipInput previousMainInput = getPipInput(previousChannel);
            if (previousMainInput != null) {
                previousMainInput.updateAvailability();
            }
            PipInput currentMainInput = getPipInput(currentChannel);
            if (currentMainInput != null) {
                currentMainInput.updateAvailability();
            }
        }
    };

    public PipInputManager(Context context, TvInputManagerHelper inputManager,
            ChannelTuner channelTuner) {
        mContext = context;
        mInputManager = inputManager;
        mChannelTuner = channelTuner;
    }

    /**
     * Starts {@link PipInputManager}.
     */
    public void start() {
        if (mStarted) {
            return;
        }
        mStarted = true;
        mInputManager.addCallback(mTvInputCallback);
        mChannelTuner.addListener(mChannelTunerListener);
        initializePipInputList();
    }

    /**
     * Stops {@link PipInputManager}.
     */
    public void stop() {
        if (!mStarted) {
            return;
        }
        mStarted = false;
        mInputManager.removeCallback(mTvInputCallback);
        mChannelTuner.removeListener(mChannelTunerListener);
        mPipInputMap.clear();
    }

    /**
     * Adds a {@link PipInputManager.Listener}.
     */
    public void addListener(Listener listener) {
        mListeners.add(listener);
    }

    /**
     * Removes a {@link PipInputManager.Listener}.
     */
    public void removeListener(Listener listener) {
        mListeners.remove(listener);
    }

    /**
     * Gets the size of inputs for PIP.
     *
     * <p>The hidden inputs are not counted.
     *
     * @param availableOnly If {@code true}, it counts only available PIP inputs. Please see {@link
     *        PipInput#isAvailable()} for the details of availability.
     */
    public int getPipInputSize(boolean availableOnly) {
        int count = 0;
        for (PipInput pipInput : mPipInputMap.values()) {
            if (!pipInput.isHidden() && (!availableOnly || pipInput.mAvailable)) {
                ++count;
            }
            if (pipInput.isPassthrough()) {
                TvInputInfo info = pipInput.getInputInfo();
                // Do not count HDMI ports if a CEC device is directly connected to the port.
                if (info.getParentId() != null && !info.isConnectedToHdmiSwitch()) {
                    --count;
                }
            }
        }
        return count;
    }

    /**
     * Gets the list of inputs for PIP..
     *
     * <p>The hidden inputs are excluded.
     *
     * @param availableOnly If true, it returns only available PIP inputs. Please see {@link
     *        PipInput#isAvailable()} for the details of availability.
     */
    public List<PipInput> getPipInputList(boolean availableOnly) {
        List<PipInput> pipInputs = new ArrayList<>();
        List<PipInput> removeInputs = new ArrayList<>();
        for (PipInput pipInput : mPipInputMap.values()) {
            if (!pipInput.isHidden() && (!availableOnly || pipInput.mAvailable)) {
                pipInputs.add(pipInput);
            }
            if (pipInput.isPassthrough()) {
                TvInputInfo info = pipInput.getInputInfo();
                // Do not show HDMI ports if a CEC device is directly connected to the port.
                if (info.getParentId() != null && !info.isConnectedToHdmiSwitch()) {
                    removeInputs.add(mPipInputMap.get(info.getParentId()));
                }
            }
        }
        if (!removeInputs.isEmpty()) {
            pipInputs.removeAll(removeInputs);
        }
        Collections.sort(pipInputs, new Comparator<PipInput>() {
            @Override
            public int compare(PipInput lhs, PipInput rhs) {
                if (!lhs.mIsPassthrough) {
                    return -1;
                }
                if (!rhs.mIsPassthrough) {
                    return 1;
                }
                String a = lhs.getLabel();
                String b = rhs.getLabel();
                return a.compareTo(b);
            }
        });
        return pipInputs;
    }

    /**
     * Returns an PIP input corresponding to {@code channel}.
     */
    public PipInput getPipInput(Channel channel) {
        if (channel == null) {
            return null;
        }
        if (channel.isPassthrough()) {
            return mPipInputMap.get(channel.getInputId());
        } else {
            return mPipInputMap.get(TUNER_INPUT_ID);
        }
    }

    /**
     * Returns true, if {@code channel1} and {@code channel2} belong to the same input. For example,
     * two channels from different tuner inputs are also in the same input "Tuner" from PIP
     * point of view.
     */
    public boolean areInSamePipInput(Channel channel1, Channel channel2) {
        PipInput input1 = getPipInput(channel1);
        PipInput input2 = getPipInput(channel2);
        return input1 != null && input2 != null
                && getPipInput(channel1).equals(getPipInput(channel2));
    }

    private void initializePipInputList() {
        boolean hasTunerInput = false;
        for (TvInputInfo input : mInputManager.getTvInputInfos(false, false)) {
            if (input.isPassthroughInput()) {
                boolean available = mInputManager.getInputState(input)
                        == TvInputManager.INPUT_STATE_CONNECTED;
                mPipInputMap.put(input.getId(), new PipInput(input.getId(), available));
            } else if (!hasTunerInput) {
                hasTunerInput = true;
                boolean available = mChannelTuner.getBrowsableChannelCount() != 0;
                mPipInputMap.put(TUNER_INPUT_ID, new PipInput(TUNER_INPUT_ID, available));
            }
        }
        PipInput input = getPipInput(mChannelTuner.getCurrentChannel());
        if (input != null) {
            input.updateAvailability();
        }
        for (Listener l : mListeners) {
            l.onPipInputListUpdated();
        }
    }

    /**
     * Listeners to notify PIP input state changes.
     */
    public interface Listener {
        /**
         * Called when the state (availability) of PIP inputs is changed.
         */
        void onPipInputStateUpdated();

        /**
         * Called when the list of PIP inputs is changed.
         */
        void onPipInputListUpdated();
    }

    /**
     * Input class for PIP. It has useful methods for PIP handling.
     */
    public class PipInput {
        private final String mInputId;
        private final boolean mIsPassthrough;
        private final TvInputInfo mInputInfo;
        private boolean mAvailable;

        private PipInput(String inputId, boolean available) {
            mInputId = inputId;
            mIsPassthrough = !mInputId.equals(TUNER_INPUT_ID);
            if (mIsPassthrough) {
                mInputInfo = mInputManager.getTvInputInfo(mInputId);
            } else {
                mInputInfo = null;
            }
            mAvailable = available;
        }

        /**
         * Returns the {@link TvInputInfo} object that matches to this PIP input.
         */
        public TvInputInfo getInputInfo() {
            return mInputInfo;
        }

        /**
         * Returns {@code true}, if the input is available for PIP. If a channel of an input is
         * already played or an input is not connected state or there is no browsable channel, the
         * input is unavailable.
         */
        public boolean isAvailable() {
            return mAvailable;
        }

        /**
         * Returns true, if the input is a passthrough TV input.
         */
        public boolean isPassthrough() {
            return mIsPassthrough;
        }

        /**
         * Gets a channel to play in a PIP view.
         */
        public Channel getChannel() {
            if (mIsPassthrough) {
                return Channel.createPassthroughChannel(mInputId);
            } else {
                return mChannelTuner.findNearestBrowsableChannel(
                        Utils.getLastWatchedChannelId(mContext));
            }
        }

        /**
         * Gets a label of the input.
         */
        public String getLabel() {
            if (mIsPassthrough) {
                return mInputInfo.loadLabel(mContext).toString();
            } else {
                return mContext.getString(R.string.input_selector_tuner_label);
            }
        }

        /**
         * Gets a long label including a customized label.
         */
        public String getLongLabel() {
            if (mIsPassthrough) {
                String customizedLabel = Utils.loadLabel(mContext, mInputInfo);
                String label = getLabel();
                if (label.equals(customizedLabel)) {
                    return customizedLabel;
                }
                return customizedLabel + " (" + label + ")";
            } else {
                return mContext.getString(R.string.input_long_label_for_tuner);
            }
        }

        /**
         * Updates availability. It returns true, if availability is changed.
         */
        private void updateAvailability() {
            boolean available;
            // current playing input cannot be available for PIP.
            Channel currentChannel = mChannelTuner.getCurrentChannel();
            if (mIsPassthrough) {
                if (currentChannel != null && currentChannel.getInputId().equals(mInputId)) {
                    available = false;
                } else {
                    available = mInputManager.getInputState(mInputId)
                            == TvInputManager.INPUT_STATE_CONNECTED;
                }
            } else {
                if (currentChannel != null && !currentChannel.isPassthrough()) {
                    available = false;
                } else {
                    available = mChannelTuner.getBrowsableChannelCount() > 0;
                }
            }
            if (mAvailable != available) {
                mAvailable = available;
                for (Listener l : mListeners) {
                    l.onPipInputStateUpdated();
                }
            }
        }

        private boolean isHidden() {
            // mInputInfo is null for the tuner input and it's always visible.
            return mInputInfo != null && mInputInfo.isHidden(mContext);
        }
    }
}