1 /* 2 * Copyright 2021 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 android.media.tv.tuner.cts; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.hardware.tv.tuner.Result; 23 import android.media.tv.tuner.Tuner; 24 import android.media.tv.tuner.frontend.FrontendInfo; 25 import android.media.tv.tuner.frontend.FrontendSettings; 26 import android.os.IBinder; 27 import android.util.Log; 28 29 import java.util.List; 30 31 public class TunerResourceTestService extends Service { 32 private static final String TAG = "TunerResourceTestService"; 33 private Context mContext = null; 34 private Tuner mTuner = null; 35 private FrontendSettings mFeSettings; 36 private FrontendInfo mFeInfo; 37 private final Object mLock = new Object(); 38 39 @Override onBind(Intent intent)40 public IBinder onBind(Intent intent) { 41 mContext = this; 42 return mBinder; 43 } 44 45 @Override onUnbind(Intent intent)46 public boolean onUnbind(Intent intent) { 47 synchronized (mLock) { 48 closeTunerInternal(); 49 return false; 50 } 51 } 52 closeTunerInternal()53 private void closeTunerInternal() { 54 if (mTuner != null) { 55 mTuner.close(); 56 mTuner = null; 57 } 58 } 59 tuneInternal(int frontendIndex)60 private int tuneInternal(int frontendIndex) { 61 // make sure mTuner is not null 62 if (mTuner == null) { 63 Log.e(TAG, "tune called on null tuner"); 64 return Result.INVALID_STATE; 65 } 66 67 // construct FrontendSettings to tune 68 List<FrontendInfo> infos = mTuner.getAvailableFrontendInfos(); 69 mFeInfo = infos.get(frontendIndex); 70 mFeSettings = TunerTest.createFrontendSettings(mFeInfo); 71 72 // tune 73 return mTuner.tune(mFeSettings); 74 } 75 76 private final ITunerResourceTestServer.Stub mBinder = new ITunerResourceTestServer.Stub() { 77 public void createTuner(int useCase) { 78 synchronized (mLock) { 79 closeTunerInternal(); 80 mTuner = new Tuner(mContext, null, useCase); 81 } 82 } 83 84 public void tuneAsync(int frontendIndex) { 85 synchronized (mLock) { 86 tuneInternal(frontendIndex); 87 } 88 } 89 90 public int tune(int frontendIndex) { 91 synchronized (mLock) { 92 return tuneInternal(frontendIndex); 93 } 94 } 95 96 public void closeTuner() { 97 synchronized (mLock) { 98 closeTunerInternal(); 99 } 100 } 101 102 public boolean verifyTunerIsNull() { 103 synchronized (mLock) { 104 return mTuner == null; 105 } 106 } 107 }; 108 } 109