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.content.Context; 19 import android.content.Intent; 20 import android.os.Build; 21 import android.support.annotation.MainThread; 22 import android.support.annotation.Nullable; 23 import android.util.Log; 24 25 /** Utility methods to start and stop audio only TV Player. */ 26 public final class AudioOnlyTvServiceUtil { 27 private static final String TAG = "AudioOnlyTvServiceUtil"; 28 private static final String EXTRA_INPUT_ID = "intputId"; 29 30 @MainThread startAudioOnlyInput(Context context, String tvInputId)31 public static void startAudioOnlyInput(Context context, String tvInputId) { 32 Log.i(TAG, "startAudioOnlyInput"); 33 Intent intent = getIntent(context); 34 if (intent == null) { 35 return; 36 } 37 intent.putExtra(EXTRA_INPUT_ID, tvInputId); 38 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 39 context.startForegroundService(intent); 40 } else { 41 context.startService(intent); 42 } 43 } 44 45 @Nullable getIntent(Context context)46 private static Intent getIntent(Context context) { 47 try { 48 return new Intent( 49 context, Class.forName("com.android.tv.audiotvservice.AudioOnlyTvService")); 50 } catch (ClassNotFoundException e) { 51 Log.wtf(TAG, e); 52 return null; 53 } 54 } 55 56 @MainThread stopAudioOnlyInput(Context context)57 public static void stopAudioOnlyInput(Context context) { 58 Log.i(TAG, "stopForegroundService"); 59 context.stopService(getIntent(context)); 60 } 61 62 @Nullable getInputIdFromIntent(Intent intent)63 public static String getInputIdFromIntent(Intent intent) { 64 return intent.getStringExtra(EXTRA_INPUT_ID); 65 } 66 AudioOnlyTvServiceUtil()67 private AudioOnlyTvServiceUtil() {} 68 } 69