DvrSessionManager.java revision 3a72b93e554bd22a5c64e71a6956d9604ce05108
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.tv.dvr;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.media.tv.TvContract;
22import android.support.annotation.Nullable;
23import android.support.v4.util.ArrayMap;
24
25import com.android.tv.common.feature.CommonFeatures;
26import com.android.tv.common.recording.RecordingCapability;
27import com.android.tv.common.recording.TvRecording;
28import com.android.tv.data.Channel;
29import com.android.tv.util.SoftPreconditions;
30import com.android.usbtuner.tvinput.UsbTunerTvInputService;
31
32/**
33 * Manages Dvr Sessions.
34 * Responsible for:
35 * <ul>
36 *     <li>Manage DvrSession</li>
37 *     <li>Manage capabilities (conflict)</li>
38 * </ul>
39 */
40public class DvrSessionManager {
41    private final static String TAG = "DvrSessionManager";
42    private final Context mContext;
43    private TvRecording.TvRecordingClient mRecordingClient;
44    private ArrayMap<String, RecordingCapability> mCapabilityMap = new ArrayMap<>();
45
46    public DvrSessionManager(Context context) {
47        SoftPreconditions.checkFeatureEnabled(context, CommonFeatures.DVR, TAG);
48        mContext = context.getApplicationContext();
49        // TODO(DVR): get a session to all clients, for now just get USB a TestInput
50        final String inputId = TvContract
51                .buildInputId(new ComponentName(context, UsbTunerTvInputService.class));
52        mRecordingClient = acquireDvrSession(inputId, null);
53        mRecordingClient.connect(inputId, new TvRecording.ClientCallback() {
54            @Override
55            public void onCapabilityReceived(RecordingCapability capability) {
56                mCapabilityMap.put(inputId, capability);
57                mRecordingClient.release();
58                mRecordingClient = null;
59            }
60        });
61        if (CommonFeatures.DVR.isEnabled(context)) { // STOPSHIP(DVR)
62            String testInputId = "com.android.tv.testinput/.TestTvInputService";
63            mCapabilityMap.put(testInputId,
64                    RecordingCapability.builder()
65                            .setInputId(testInputId)
66                            .setMaxConcurrentPlayingSessions(2)
67                            .setMaxConcurrentTunedSessions(2)
68                            .setMaxConcurrentSessionsOfAllTypes(3)
69                            .build());
70
71        }
72    }
73
74    public TvRecording.TvRecordingClient acquireDvrSession(String inputId, Channel channel) {
75        // TODO(DVR): use input and channel or change API
76        TvRecording.TvRecordingClient sessionClient = new TvRecording.TvRecordingClient(mContext);
77        return sessionClient;
78    }
79
80    public boolean canAcquireDvrSession(String inputId, Channel channel) {
81        // TODO(DVR): implement
82        return true;
83    }
84
85    public void releaseDvrSession(TvRecording.TvRecordingClient session) {
86        session.release();
87    }
88
89    @Nullable
90    public RecordingCapability getRecordingCapability(String inputId) {
91        return mCapabilityMap.get(inputId);
92    }
93}
94