From aea07315fda4874e688c7e383f24fcd7111e1922 Mon Sep 17 00:00:00 2001 From: Nick Chalko Date: Wed, 8 Jan 2020 15:22:23 -0800 Subject: [PATCH] Create a OnTunerEventListener Test: m framework-minus-apex Change-Id: I422b40c12edf60c3d19dc9b37260174be1ae55e0 Bug: 139308734 --- api/system-current.txt | 14 ++++ media/java/android/media/tv/tuner/Tuner.java | 71 +++++++++++++++---- .../media/tv/tuner/TunerConstants.java | 24 ------- .../tv/tuner/frontend/FrontendCallback.java | 30 -------- .../tuner/frontend/OnTuneEventListener.java | 48 +++++++++++++ 5 files changed, 118 insertions(+), 69 deletions(-) delete mode 100644 media/java/android/media/tv/tuner/frontend/FrontendCallback.java create mode 100644 media/java/android/media/tv/tuner/frontend/OnTuneEventListener.java diff --git a/api/system-current.txt b/api/system-current.txt index e532a3afab8a9..3ff611675b1d2 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -4660,7 +4660,10 @@ package android.media.tv.tuner { public final class Tuner implements java.lang.AutoCloseable { ctor public Tuner(@NonNull android.content.Context); + method @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) public void clearOnTuneEventListener(); method @Nullable @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) public android.media.tv.tuner.Tuner.Descrambler openDescrambler(); + method @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) public void setOnTuneEventListener(@NonNull java.util.concurrent.Executor, @NonNull android.media.tv.tuner.frontend.OnTuneEventListener); + method @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) public int stopTune(); method @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) public int tune(@NonNull android.media.tv.tuner.FrontendSettings); } @@ -4742,6 +4745,17 @@ package android.media.tv.tuner.filter { } +package android.media.tv.tuner.frontend { + + public interface OnTuneEventListener { + method public void onTuneEvent(int); + field public static final int SIGNAL_LOCKED = 0; // 0x0 + field public static final int SIGNAL_LOST_LOCK = 2; // 0x2 + field public static final int SIGNAL_NO_SIGNAL = 1; // 0x1 + } + +} + package android.metrics { public class LogMaker { diff --git a/media/java/android/media/tv/tuner/Tuner.java b/media/java/android/media/tv/tuner/Tuner.java index 490f9389d9e78..b76001facb452 100644 --- a/media/java/android/media/tv/tuner/Tuner.java +++ b/media/java/android/media/tv/tuner/Tuner.java @@ -32,9 +32,9 @@ import android.media.tv.tuner.filter.Filter.Subtype; import android.media.tv.tuner.filter.Filter.Type; import android.media.tv.tuner.filter.FilterEvent; import android.media.tv.tuner.filter.TimeFilter; -import android.media.tv.tuner.frontend.FrontendCallback; import android.media.tv.tuner.frontend.FrontendInfo; import android.media.tv.tuner.frontend.FrontendStatus; +import android.media.tv.tuner.frontend.OnTuneEventListener; import android.media.tv.tuner.frontend.ScanCallback; import android.os.Handler; import android.os.Looper; @@ -57,7 +57,6 @@ public final class Tuner implements AutoCloseable { private static final String TAG = "MediaTvTuner"; private static final boolean DEBUG = false; - private static final int MSG_ON_FRONTEND_EVENT = 1; private static final int MSG_ON_FILTER_EVENT = 2; private static final int MSG_ON_FILTER_STATUS = 3; private static final int MSG_ON_LNB_EVENT = 4; @@ -76,6 +75,10 @@ public final class Tuner implements AutoCloseable { private List mLnbIds; private Lnb mLnb; @Nullable + private OnTuneEventListener mOnTuneEventListener; + @Nullable + private Executor mOnTunerEventExecutor; + @Nullable private ScanCallback mScanCallback; @Nullable private Executor mScanCallbackExecutor; @@ -222,11 +225,6 @@ public final class Tuner implements AutoCloseable { @Override public void handleMessage(Message msg) { switch (msg.what) { - case MSG_ON_FRONTEND_EVENT: - if (mFrontend != null && mFrontend.mCallback != null) { - mFrontend.mCallback.onEvent(msg.arg1); - } - break; case MSG_ON_FILTER_STATUS: { Filter filter = (Filter) msg.obj; if (filter.mCallback != null) { @@ -242,7 +240,6 @@ public final class Tuner implements AutoCloseable { private class Frontend { private int mId; - private FrontendCallback mCallback; private Frontend(int id) { mId = id; @@ -250,13 +247,59 @@ public final class Tuner implements AutoCloseable { } /** - * Tunes the frontend to the settings given. + * Listens for tune events. * - * @return result status of tune operation. + *

+ * Tuner events are started when {@link #tune(FrontendSettings)} is called and end when {@link + * #stopTune()} is called. + * + * @param eventListener receives tune events. * @throws SecurityException if the caller does not have appropriate permissions. - * TODO: add result constants or throw exceptions. + * @see #tune(FrontendSettings) */ @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) + public void setOnTuneEventListener(@NonNull @CallbackExecutor Executor executor, + @NonNull OnTuneEventListener eventListener) { + TunerUtils.checkTunerPermission(mContext); + mOnTuneEventListener = eventListener; + mOnTunerEventExecutor = executor; + } + + /** + * Clears the {@link OnTuneEventListener} and its associated {@link Executor}. + * + * @throws SecurityException if the caller does not have appropriate permissions. + * @see #setOnTuneEventListener(Executor, OnTuneEventListener) + */ + @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) + public void clearOnTuneEventListener() { + TunerUtils.checkTunerPermission(mContext); + mOnTuneEventListener = null; + mOnTunerEventExecutor = null; + + } + + /** + * Tunes the frontend to using the settings given. + * + *

+ * This locks the frontend to a frequency by providing signal + * delivery information. If previous tuning isn't completed, this stop the previous tuning, and + * start a new tuning. + * + *

+ * Tune is an async call, with {@link OnTuneEventListener#LOCKED LOCKED} and {@link + * OnTuneEventListener#NO_SIGNAL NO_SIGNAL} events sent to the {@link OnTuneEventListener} + * specified in {@link #setOnTuneEventListener(Executor, OnTuneEventListener)}. + * + * @param settings Signal delivery information the frontend uses to + * search and lock the signal. + * @return result status of tune operation. + * @throws SecurityException if the caller does not have appropriate permissions. + * @see #setOnTuneEventListener(Executor, OnTuneEventListener) + */ + @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) + @Result public int tune(@NonNull FrontendSettings settings) { TunerUtils.checkTunerPermission(mContext); return nativeTune(settings.getType(), settings); @@ -269,8 +312,6 @@ public final class Tuner implements AutoCloseable { * will be sent to attached filters. * * @return result status of the operation. - * - * @hide */ @RequiresPermission(android.Manifest.permission.ACCESS_TV_TUNER) @Result @@ -501,8 +542,8 @@ public final class Tuner implements AutoCloseable { } private void onFrontendEvent(int eventType) { - if (mHandler != null) { - mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_FRONTEND_EVENT, eventType, 0)); + if (mOnTunerEventExecutor != null && mOnTuneEventListener != null) { + mOnTunerEventExecutor.execute(() -> mOnTuneEventListener.onTuneEvent(eventType)); } } diff --git a/media/java/android/media/tv/tuner/TunerConstants.java b/media/java/android/media/tv/tuner/TunerConstants.java index 5a0c91f8d7499..20b77e6635866 100644 --- a/media/java/android/media/tv/tuner/TunerConstants.java +++ b/media/java/android/media/tv/tuner/TunerConstants.java @@ -47,30 +47,6 @@ public final class TunerConstants { */ public static final int INVALID_STREAM_ID = Constants.Constant.INVALID_STREAM_ID; - - /** @hide */ - @IntDef(prefix = "FRONTEND_EVENT_TYPE_", - value = {FRONTEND_EVENT_TYPE_LOCKED, FRONTEND_EVENT_TYPE_NO_SIGNAL, - FRONTEND_EVENT_TYPE_LOST_LOCK}) - @Retention(RetentionPolicy.SOURCE) - public @interface FrontendEventType {} - /** - * Frontend locked. - * @hide - */ - public static final int FRONTEND_EVENT_TYPE_LOCKED = Constants.FrontendEventType.LOCKED; - /** - * No signal detected. - * @hide - */ - public static final int FRONTEND_EVENT_TYPE_NO_SIGNAL = Constants.FrontendEventType.NO_SIGNAL; - /** - * Frontend lock lost. - * @hide - */ - public static final int FRONTEND_EVENT_TYPE_LOST_LOCK = Constants.FrontendEventType.LOST_LOCK; - - /** @hide */ @IntDef(flag = true, prefix = "FILTER_STATUS_", value = {FILTER_STATUS_DATA_READY, FILTER_STATUS_LOW_WATER, FILTER_STATUS_HIGH_WATER, FILTER_STATUS_OVERFLOW}) diff --git a/media/java/android/media/tv/tuner/frontend/FrontendCallback.java b/media/java/android/media/tv/tuner/frontend/FrontendCallback.java deleted file mode 100644 index 9c4f4606a9bcb..0000000000000 --- a/media/java/android/media/tv/tuner/frontend/FrontendCallback.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.media.tv.tuner.frontend; - -/** - * Frontend Callback. - * - * @hide - */ -public interface FrontendCallback { - - /** - * Invoked when there is a frontend event. - */ - void onEvent(int frontendEventType); -} diff --git a/media/java/android/media/tv/tuner/frontend/OnTuneEventListener.java b/media/java/android/media/tv/tuner/frontend/OnTuneEventListener.java new file mode 100644 index 0000000000000..5cf0d319c7c96 --- /dev/null +++ b/media/java/android/media/tv/tuner/frontend/OnTuneEventListener.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.media.tv.tuner.frontend; + +import android.annotation.IntDef; +import android.annotation.SystemApi; +import android.hardware.tv.tuner.V1_0.Constants; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Listens for tune events. + * + * @hide + */ +@SystemApi +public interface OnTuneEventListener { + + /** @hide */ + @IntDef(prefix = "SIGNAL_", value = {SIGNAL_LOCKED, SIGNAL_NO_SIGNAL, SIGNAL_LOST_LOCK}) + @Retention(RetentionPolicy.SOURCE) + @interface TuneEvent {} + + /** The frontend has locked to the signal specified by the tune method. */ + int SIGNAL_LOCKED = Constants.FrontendEventType.LOCKED; + /** The frontend is unable to lock to the signal specified by the tune method. */ + int SIGNAL_NO_SIGNAL = Constants.FrontendEventType.NO_SIGNAL; + /** The frontend has lost the lock to the signal specified by the tune method. */ + int SIGNAL_LOST_LOCK = Constants.FrontendEventType.LOST_LOCK; + + /** Tune Event from the frontend */ + void onTuneEvent(@TuneEvent int tuneEvent); +}