blob: c8e15c1a2f4fee5ed179bbb1cf9bfad7cf716428 [file] [log] [blame]
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -08001/**
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
17package com.android.server.broadcastradio.hal2;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080021import android.hardware.radio.ITuner;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080022import android.hardware.radio.RadioManager;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080023import android.hardware.broadcastradio.V2_0.AmFmRegionConfig;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080024import android.hardware.broadcastradio.V2_0.IBroadcastRadio;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080025import android.hardware.broadcastradio.V2_0.ITunerSession;
26import android.hardware.broadcastradio.V2_0.Result;
27import android.os.ParcelableException;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080028import android.os.RemoteException;
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080029import android.util.MutableInt;
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080030import android.util.Slog;
31
32import java.util.Objects;
33
34class RadioModule {
35 private static final String TAG = "BcRadio2Srv.module";
36
37 @NonNull private final IBroadcastRadio mService;
38 @NonNull public final RadioManager.ModuleProperties mProperties;
39
40 private RadioModule(@NonNull IBroadcastRadio service,
41 @NonNull RadioManager.ModuleProperties properties) {
42 mProperties = Objects.requireNonNull(properties);
43 mService = Objects.requireNonNull(service);
44 }
45
46 public static @Nullable RadioModule tryLoadingModule(int idx, @NonNull String fqName) {
47 try {
48 IBroadcastRadio service = IBroadcastRadio.getService();
49 if (service == null) return null;
50
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080051 Mutable<AmFmRegionConfig> amfmConfig = new Mutable<>();
52 service.getAmFmRegionConfig(false, (int result, AmFmRegionConfig config) -> {
53 if (result == Result.OK) amfmConfig.value = config;
54 });
55
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080056 RadioManager.ModuleProperties prop =
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080057 Convert.propertiesFromHal(idx, fqName, service.getProperties(), amfmConfig.value);
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080058
59 return new RadioModule(service, prop);
60 } catch (RemoteException ex) {
61 Slog.e(TAG, "failed to load module " + fqName, ex);
62 return null;
63 }
64 }
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080065
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -080066 public @NonNull TunerSession openSession(@NonNull android.hardware.radio.ITunerCallback userCb)
67 throws RemoteException {
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080068 TunerCallback cb = new TunerCallback(Objects.requireNonNull(userCb));
69 Mutable<ITunerSession> hwSession = new Mutable<>();
70 MutableInt halResult = new MutableInt(Result.UNKNOWN_ERROR);
71
Tomasz Wasilczyk436128f2018-01-08 16:46:09 -080072 mService.openSession(cb, (int result, ITunerSession session) -> {
73 hwSession.value = session;
74 halResult.value = result;
75 });
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080076
77 Convert.throwOnError("openSession", halResult.value);
78 Objects.requireNonNull(hwSession.value);
79
Tomasz Wasilczykca98cde2018-01-04 12:26:40 -080080 return new TunerSession(hwSession.value, cb);
Tomasz Wasilczykf58305d2017-12-28 14:03:15 -080081 }
Tomasz Wasilczykd65b3ca2017-12-13 08:26:25 -080082}