aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrSessionManager.java
blob: fba05cb61e1b7a9b7f0195239cae559ac71ec4e5 (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
/*
 * 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.dvr;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.tv.TvInputInfo;
import android.media.tv.TvInputManager;
import android.media.tv.TvRecordingClient;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.android.tv.common.SoftPreconditions;
import com.android.tv.common.feature.CommonFeatures;
import com.android.tv.data.Channel;

/**
 * Manages Dvr Sessions.
 * Responsible for:
 * <ul>
 *     <li>Manage DvrSession</li>
 *     <li>Manage capabilities (conflict)</li>
 * </ul>
 */
@TargetApi(Build.VERSION_CODES.N)
public class DvrSessionManager extends TvInputManager.TvInputCallback {
    //consider moving all of this to TvInputManagerHelper
    private final static String TAG = "DvrSessionManager";
    private static final boolean DEBUG = false;

    private final Context mContext;
    private final TvInputManager mTvInputManager;
    private final ArrayMap<String, TvInputInfo> mRecordingTvInputs = new ArrayMap<>();

    public DvrSessionManager(Context context) {
        this(context, (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE),
                new Handler());
    }

    @VisibleForTesting
    DvrSessionManager(Context context, TvInputManager tvInputManager, Handler handler) {
        SoftPreconditions.checkFeatureEnabled(context, CommonFeatures.DVR, TAG);
        mTvInputManager = tvInputManager;
        mContext = context.getApplicationContext();
        for (TvInputInfo info : tvInputManager.getTvInputList()) {
            if (DEBUG) {
                Log.d(TAG, info + " canRecord=" + info.canRecord() + " tunerCount=" + info
                        .getTunerCount());
            }
            if (info.canRecord()) {
                mRecordingTvInputs.put(info.getId(), info);
            }
        }
        tvInputManager.registerCallback(this, handler);

    }

    public TvRecordingClient createTvRecordingClient(String tag,
            TvRecordingClient.RecordingCallback callback, Handler handler) {
        return new TvRecordingClient(mContext, tag, callback, handler);
    }

    public boolean canAcquireDvrSession(String inputId, Channel channel) {
        // TODO(DVR): implement checking tuner count etc.
        TvInputInfo info = mRecordingTvInputs.get(inputId);
        return info != null;
    }

    public void releaseTvRecordingClient(TvRecordingClient recordingClient) {
        recordingClient.release();
    }

    @Override
    public void onInputAdded(String inputId) {
        super.onInputAdded(inputId);
        TvInputInfo info = mTvInputManager.getTvInputInfo(inputId);
        if (DEBUG) {
            Log.d(TAG, "onInputAdded " + info.toString() + " canRecord=" + info.canRecord()
                    + " tunerCount=" + info.getTunerCount());
        }
        if (info.canRecord()) {
            mRecordingTvInputs.put(inputId, info);
        }
    }

    @Override
    public void onInputRemoved(String inputId) {
        super.onInputRemoved(inputId);
        if (DEBUG) Log.d(TAG, "onInputRemoved " + inputId);
        mRecordingTvInputs.remove(inputId);
    }

    @Override
    public void onInputUpdated(String inputId) {
        super.onInputUpdated(inputId);
        TvInputInfo info = mTvInputManager.getTvInputInfo(inputId);
        if (DEBUG) {
            Log.d(TAG, "onInputUpdated " + info.toString() + " canRecord=" + info.canRecord()
                    + " tunerCount=" + info.getTunerCount());
        }
        if (info.canRecord()) {
            mRecordingTvInputs.put(inputId, info);
        } else {
            mRecordingTvInputs.remove(inputId);
        }
    }

    @Nullable
    public TvInputInfo getTvInputInfo(String inputId) {
        return mRecordingTvInputs.get(inputId);
    }
}