1 /* 2 * Copyright (C) 2018 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 17 package com.android.tv.common.compat; 18 19 import android.content.Context; 20 import android.media.tv.TvRecordingClient; 21 import android.os.Build.VERSION_CODES; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.support.annotation.RequiresApi; 25 import android.util.ArrayMap; 26 import com.android.tv.common.compat.api.PrivateCommandSender; 27 import com.android.tv.common.compat.api.RecordingClientCallbackCompatEvents; 28 import com.android.tv.common.compat.api.TvRecordingClientCompatCommands; 29 import com.android.tv.common.compat.internal.RecordingClientCompatProcessor; 30 31 /** 32 * TIF Compatibility for {@link TvRecordingClient}. 33 * 34 * <p>Extends {@code TvRecordingClient} in a backwards compatible way. 35 */ 36 @RequiresApi(api = VERSION_CODES.N) 37 public class TvRecordingClientCompat extends TvRecordingClient 38 implements TvRecordingClientCompatCommands, PrivateCommandSender { 39 40 private final RecordingClientCompatProcessor mProcessor; 41 42 /** 43 * Creates a new TvRecordingClient object. 44 * 45 * @param context The application context to create a TvRecordingClient with. 46 * @param tag A short name for debugging purposes. 47 * @param callback The callback to receive recording status changes. 48 * @param handler The handler to invoke the callback on. 49 */ TvRecordingClientCompat( Context context, String tag, RecordingCallback callback, Handler handler)50 public TvRecordingClientCompat( 51 Context context, String tag, RecordingCallback callback, Handler handler) { 52 super(context, tag, callback, handler); 53 RecordingCallbackCompat compatEvents = 54 callback instanceof RecordingCallbackCompat 55 ? (RecordingCallbackCompat) callback 56 : null; 57 mProcessor = new RecordingClientCompatProcessor(this, compatEvents); 58 if (compatEvents != null) { 59 compatEvents.mClientCompatProcessor = mProcessor; 60 } 61 } 62 63 /** Tell the session to Display a debug message dev builds only. */ 64 @Override devMessage(String message)65 public void devMessage(String message) { 66 mProcessor.devMessage(message); 67 } 68 69 /** 70 * TIF Compatibility for {@link RecordingCallback}. 71 * 72 * <p>Extends {@code RecordingCallback} in a backwards compatible way. 73 */ 74 public static class RecordingCallbackCompat extends RecordingCallback 75 implements RecordingClientCallbackCompatEvents { 76 private final ArrayMap<String, Integer> inputCompatVersionMap = new ArrayMap<>(); 77 private RecordingClientCompatProcessor mClientCompatProcessor; 78 79 @Override onEvent(String inputId, String eventType, Bundle eventArgs)80 public void onEvent(String inputId, String eventType, Bundle eventArgs) { 81 if (mClientCompatProcessor != null 82 && !mClientCompatProcessor.handleEvent(inputId, eventType, eventArgs)) { 83 super.onEvent(inputId, eventType, eventArgs); 84 } 85 } 86 getTifCompatVersionForInput(String inputId)87 public int getTifCompatVersionForInput(String inputId) { 88 return inputCompatVersionMap.containsKey(inputId) 89 ? inputCompatVersionMap.get(inputId) 90 : 0; 91 } 92 93 /** Display a message as a toast on dev builds only. */ 94 @Override onDevToast(String inputId, String message)95 public void onDevToast(String inputId, String message) {} 96 97 /** Recording started. */ 98 @Override onRecordingStarted(String inputId, String recUri)99 public void onRecordingStarted(String inputId, String recUri) {} 100 } 101 } 102