Home | History | Annotate | Download | only in platform
      1 /**
      2  * Copyright (C) 2017 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 com.android.car.radio.platform;
     18 
     19 import android.annotation.NonNull;
     20 import android.annotation.Nullable;
     21 import android.hardware.radio.ProgramSelector;
     22 import android.hardware.radio.RadioManager;
     23 import android.hardware.radio.RadioMetadata;
     24 import android.hardware.radio.RadioTuner;
     25 import android.os.Handler;
     26 import android.os.Looper;
     27 
     28 import java.util.Map;
     29 import java.util.Objects;
     30 
     31 /**
     32  * Proposed extensions to android.hardware.radio.TunerCallbackAdapter.
     33  *
     34  * This class is not compatible with the original at all (because they operate
     35  * on different callback types), it just represents a proposed feature
     36  * extensions.
     37  *
     38  * They might eventually get pushed to the framework.
     39  */
     40 class TunerCallbackAdapterExt extends RadioTuner.Callback {
     41     private static final int INIT_TIMEOUT_MS = 10000;  // 10s
     42 
     43     private final Object mInitLock = new Object();
     44     private boolean mIsInitialized = false;
     45 
     46     private final RadioTuner.Callback mCallback;
     47     private final Handler mHandler;
     48 
     49     TunerCallbackAdapterExt(@NonNull RadioTuner.Callback callback, @Nullable Handler handler) {
     50         mCallback = Objects.requireNonNull(callback);
     51         if (handler == null) {
     52             mHandler = new Handler(Looper.getMainLooper());
     53         } else {
     54             mHandler = handler;
     55         }
     56     }
     57 
     58     public boolean waitForInitialization() {
     59         synchronized (mInitLock) {
     60             if (mIsInitialized) return true;
     61             try {
     62                 mInitLock.wait(INIT_TIMEOUT_MS);
     63             } catch (InterruptedException ex) {
     64                 // ignore the exception, as we check mIsInitialized anyway
     65             }
     66             return mIsInitialized;
     67         }
     68     }
     69 
     70     @Override
     71     public void onError(int status) {
     72         mHandler.post(() -> mCallback.onError(status));
     73     }
     74 
     75     @Override
     76     public void onTuneFailed(int result, @Nullable ProgramSelector selector) {
     77         mHandler.post(() -> mCallback.onTuneFailed(result, selector));
     78     }
     79 
     80     @Override
     81     public void onConfigurationChanged(RadioManager.BandConfig config) {
     82         mHandler.post(() -> mCallback.onConfigurationChanged(config));
     83         if (mIsInitialized) return;
     84         synchronized (mInitLock) {
     85             mIsInitialized = true;
     86             mInitLock.notifyAll();
     87         }
     88     }
     89 
     90     public void onProgramInfoChanged(RadioManager.ProgramInfo info) {
     91         mHandler.post(() -> mCallback.onProgramInfoChanged(info));
     92     }
     93 
     94     public void onMetadataChanged(RadioMetadata metadata) {
     95         mHandler.post(() -> mCallback.onMetadataChanged(metadata));
     96     }
     97 
     98     public void onTrafficAnnouncement(boolean active) {
     99         mHandler.post(() -> mCallback.onTrafficAnnouncement(active));
    100     }
    101 
    102     public void onEmergencyAnnouncement(boolean active) {
    103         mHandler.post(() -> mCallback.onEmergencyAnnouncement(active));
    104     }
    105 
    106     public void onAntennaState(boolean connected) {
    107         mHandler.post(() -> mCallback.onAntennaState(connected));
    108     }
    109 
    110     public void onControlChanged(boolean control) {
    111         mHandler.post(() -> mCallback.onControlChanged(control));
    112     }
    113 
    114     public void onBackgroundScanAvailabilityChange(boolean isAvailable) {
    115         mHandler.post(() -> mCallback.onBackgroundScanAvailabilityChange(isAvailable));
    116     }
    117 
    118     public void onBackgroundScanComplete() {
    119         mHandler.post(() -> mCallback.onBackgroundScanComplete());
    120     }
    121 
    122     public void onProgramListChanged() {
    123         mHandler.post(() -> mCallback.onProgramListChanged());
    124     }
    125 
    126     public void onParametersUpdated(@NonNull Map<String, String> parameters) {
    127         mHandler.post(() -> mCallback.onParametersUpdated(parameters));
    128     }
    129 }
    130