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 package com.android.tv.audiotvservice; 17 18 import android.annotation.TargetApi; 19 import android.app.Notification; 20 import android.app.Service; 21 import android.content.Intent; 22 import android.media.session.MediaSession; 23 import android.media.tv.AitInfo; 24 import android.net.Uri; 25 import android.os.Build; 26 import android.os.IBinder; 27 import android.support.annotation.Nullable; 28 import android.util.Log; 29 import com.android.tv.data.ChannelImpl; 30 import com.android.tv.data.StreamInfo; 31 import com.android.tv.data.api.Channel; 32 import com.android.tv.ui.TunableTvView; 33 import com.android.tv.ui.TunableTvView.OnTuneListener; 34 35 /** Foreground service for audio-only TV inputs. */ 36 public class AudioOnlyTvService extends Service implements OnTuneListener { 37 // TODO(b/110969180): implement this service. 38 private static final String TAG = "AudioOnlyTvService"; 39 private static final int NOTIFICATION_ID = 1; 40 41 @Nullable private String mTvInputId; 42 private TunableTvView mTvView; 43 // TODO(b/110969180): perhaps use MediaSessionWrapper 44 private MediaSession mMediaSession; 45 46 @Nullable 47 @Override onBind(Intent intent)48 public IBinder onBind(Intent intent) { 49 Log.i(TAG, "onBind"); 50 return null; 51 } 52 53 @Override onCreate()54 public void onCreate() { 55 Log.i(TAG, "onCreate"); 56 // TODO(b/110969180): create TvView 57 58 } 59 60 @Override onStartCommand(Intent intent, int flags, int startId)61 public int onStartCommand(Intent intent, int flags, int startId) { 62 Log.i(TAG, "onStartCommand. flags = " + flags + ", startId = " + startId); 63 // TODO(b/110969180): real notification and or media session 64 startForeground(NOTIFICATION_ID, new Notification()); 65 mTvInputId = AudioOnlyTvServiceUtil.getInputIdFromIntent(intent); 66 tune(mTvInputId); 67 return START_STICKY; 68 } 69 tune(String tvInputId)70 private void tune(String tvInputId) { 71 Channel channel = ChannelImpl.createPassthroughChannel(tvInputId); 72 mTvView.tuneTo(channel, null, this); 73 } 74 75 @Override onDestroy()76 public void onDestroy() { 77 Log.i(TAG, "onDestroy"); 78 mTvInputId = null; 79 // TODO(b/110969180): clear TvView 80 } 81 82 // TODO(b/110969180): figure out when to stop ourselves, mediaSession event? 83 84 // TODO(b/110969180): handle OnTuner Listener 85 @Override onTuneFailed(Channel channel)86 public void onTuneFailed(Channel channel) {} 87 88 @Override onUnexpectedStop(Channel channel)89 public void onUnexpectedStop(Channel channel) {} 90 91 @Override onStreamInfoChanged(StreamInfo info, boolean allowAutoSelectionOfTrack)92 public void onStreamInfoChanged(StreamInfo info, boolean allowAutoSelectionOfTrack) {} 93 94 @Override onChannelRetuned(Uri channel)95 public void onChannelRetuned(Uri channel) {} 96 97 @Override onContentBlocked()98 public void onContentBlocked() {} 99 100 @Override onContentAllowed()101 public void onContentAllowed() {} 102 103 @Override onChannelSignalStrength()104 public void onChannelSignalStrength() {} 105 106 @TargetApi(Build.VERSION_CODES.TIRAMISU) 107 @Override onAitInfoUpdated(String inputId, AitInfo aitInfo)108 public void onAitInfoUpdated(String inputId, AitInfo aitInfo) {} 109 } 110