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.TvView; 21 import android.os.Build.VERSION_CODES; 22 import android.os.Bundle; 23 import android.support.annotation.RequiresApi; 24 import android.util.ArrayMap; 25 import android.util.AttributeSet; 26 import com.android.tv.common.compat.api.PrivateCommandSender; 27 import com.android.tv.common.compat.api.TvInputCallbackCompatEvents; 28 import com.android.tv.common.compat.api.TvViewCompatCommands; 29 import com.android.tv.common.compat.internal.TvViewCompatProcessor; 30 31 /** 32 * TIF Compatibility for {@link TvView}. 33 * 34 * <p>Extends {@code TvView} in a backwards compatible way. 35 */ 36 @RequiresApi(api = VERSION_CODES.LOLLIPOP) 37 public class TvViewCompat extends TvView implements TvViewCompatCommands, PrivateCommandSender { 38 39 private final TvViewCompatProcessor mTvViewCompatProcessor; 40 TvViewCompat(Context context)41 public TvViewCompat(Context context) { 42 this(context, null); 43 } 44 TvViewCompat(Context context, AttributeSet attrs)45 public TvViewCompat(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 TvViewCompat(Context context, AttributeSet attrs, int defStyleAttr)49 public TvViewCompat(Context context, AttributeSet attrs, int defStyleAttr) { 50 super(context, attrs, defStyleAttr); 51 mTvViewCompatProcessor = new TvViewCompatProcessor(this); 52 } 53 54 @Override setCallback(TvInputCallback callback)55 public void setCallback(TvInputCallback callback) { 56 super.setCallback(callback); 57 if (callback instanceof TvInputCallbackCompat) { 58 TvInputCallbackCompat compatEvents = (TvInputCallbackCompat) callback; 59 mTvViewCompatProcessor.setCallback(compatEvents); 60 compatEvents.mTvViewCompatProcessor = mTvViewCompatProcessor; 61 } 62 } 63 64 @Override devMessage(String message)65 public void devMessage(String message) { 66 mTvViewCompatProcessor.devMessage(message); 67 } 68 69 /** 70 * TIF Compatibility for {@link TvInputCallback}. 71 * 72 * <p>Extends {@code TvInputCallback} in a backwards compatible way. 73 */ 74 public static class TvInputCallbackCompat extends TvInputCallback 75 implements TvInputCallbackCompatEvents { 76 private final ArrayMap<String, Integer> inputCompatVersionMap = new ArrayMap<>(); 77 private TvViewCompatProcessor mTvViewCompatProcessor; 78 79 @Override onEvent(String inputId, String eventType, Bundle eventArgs)80 public void onEvent(String inputId, String eventType, Bundle eventArgs) { 81 if (mTvViewCompatProcessor != null 82 && !mTvViewCompatProcessor.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 @Override onDevToast(String inputId, String message)94 public void onDevToast(String inputId, String message) {} 95 96 /** 97 * This is called when the signal strength is notified. 98 * 99 * @param inputId The ID of the TV input bound to this view. 100 * @param value The current signal strength. Should be one of the followings. 101 * <ul> 102 * <li>{@link TvInputConstantCompat#SIGNAL_STRENGTH_NOT_USED} 103 * <li>{@link TvInputConstantCompat#SIGNAL_STRENGTH_ERROR} 104 * <li>{@link TvInputConstantCompat#SIGNAL_STRENGTH_UNKNOWN} 105 * <li>{int [0, 100]} 106 * </ul> 107 */ 108 @Override onSignalStrength(String inputId, int value)109 public void onSignalStrength(String inputId, int value) {} 110 } 111 } 112