1 /* 2 * Copyright 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 package com.android.tv.common.support.tis; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.media.tv.TvInputManager; 23 import android.media.tv.TvInputService; 24 import android.support.annotation.Nullable; 25 import android.support.annotation.VisibleForTesting; 26 import com.android.tv.common.support.tis.TifSession.TifSessionFactory; 27 28 /** Abstract TVInputService. */ 29 public abstract class BaseTvInputService extends TvInputService { 30 31 private static final IntentFilter INTENT_FILTER = new IntentFilter(); 32 33 static { 34 INTENT_FILTER.addAction(TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED); 35 INTENT_FILTER.addAction(TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED); 36 } 37 38 @VisibleForTesting 39 protected final BroadcastReceiver broadcastReceiver = 40 new BroadcastReceiver() { 41 @Override 42 public void onReceive(Context context, Intent intent) { 43 switch (intent.getAction()) { 44 case TvInputManager.ACTION_PARENTAL_CONTROLS_ENABLED_CHANGED: 45 case TvInputManager.ACTION_BLOCKED_RATINGS_CHANGED: 46 for (Session session : getSessionManager().getSessions()) { 47 if (session instanceof WrappedSession) { 48 ((WrappedSession) session).onParentalControlsChanged(); 49 } 50 } 51 break; 52 default: 53 // do nothing 54 } 55 } 56 }; 57 58 @Nullable 59 @Override onCreateSession(String inputId)60 public final WrappedSession onCreateSession(String inputId) { 61 SessionManager sessionManager = getSessionManager(); 62 if (sessionManager.canCreateNewSession()) { 63 WrappedSession session = 64 new WrappedSession( 65 getApplicationContext(), 66 sessionManager, 67 getTifSessionFactory(), 68 inputId); 69 sessionManager.addSession(session); 70 return session; 71 } 72 return null; 73 } 74 75 @Override onCreate()76 public void onCreate() { 77 super.onCreate(); 78 registerReceiver(broadcastReceiver, INTENT_FILTER); 79 } 80 81 @Override onDestroy()82 public void onDestroy() { 83 super.onDestroy(); 84 unregisterReceiver(broadcastReceiver); 85 } 86 getTifSessionFactory()87 protected abstract TifSessionFactory getTifSessionFactory(); 88 getSessionManager()89 protected abstract SessionManager getSessionManager(); 90 } 91