aboutsummaryrefslogtreecommitdiff
path: root/car-lib/src/android/car/oem/OemCarService.java
blob: de62ab67868a30087fcdcb6be83f3545f7c5df5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 2022 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.car.oem;

import android.annotation.CallSuper;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.Service;
import android.car.CarVersion;
import android.car.annotation.ApiRequirements;
import android.car.builtin.util.Slogf;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Map;

/**
 * This code will be running as part of the OEM Service. This provides basic implementation for OEM
 * Service. OEMs should extend this class and override relevant methods.
 *
 * <p>
 * OEM service implementation should have {@code android.car.permission.BIND_OEM_CAR_SERVICE} as
 * required permission in manifest to connect to the OEM service.
 *
 * @hide
 */
@SystemApi
public abstract class OemCarService extends Service {

    private static final String TAG = OemCarService.class.getSimpleName();
    private static final boolean DBG = Slogf.isLoggable(TAG, Log.DEBUG);
    private static final String PERMISSION_BIND_OEM_CAR_SERVICE =
            "android.car.permission.BIND_OEM_CAR_SERVICE";

    // OEM Service components
    // Note: Change the size as more components are added.
    @GuardedBy("mLock")
    private final ArrayMap<Class<?>, OemCarServiceComponent> mOemCarServiceComponents =
            new ArrayMap<Class<?>, OemCarServiceComponent>(1);

    private final Object mLock = new Object();

    private final IOemCarService mInterface = new IOemCarService.Stub() {
        // Component services
        @Override
        public IOemCarAudioFocusService getOemAudioFocusService() {
            assertPermission();
            synchronized (mLock) {
                return (IOemCarAudioFocusService) mOemCarServiceComponents
                        .getOrDefault(IOemCarAudioFocusService.class, null);
            }
        }

        @Override
        public void onCarServiceReady(IOemCarServiceCallback callback) throws RemoteException {
            assertPermission();
            OemCarService.this.onCarServiceReady();
            synchronized (mLock) {
                for (int i = 0; i < mOemCarServiceComponents.size(); i++) {
                    if (DBG) {
                        Slogf.d(TAG, "Calling onCarServiceReady for %s\n",
                                mOemCarServiceComponents.keyAt(i).getSimpleName());
                    }
                    mOemCarServiceComponents.valueAt(i).onCarServiceReady();
                }
            }
            callback.sendOemCarServiceReady();
        }

        @Override
        public CarVersion getSupportedCarVersion() {
            assertPermission();
            return OemCarService.this.getSupportedCarVersion();
        }

        @Override
        public String getAllStackTraces() {
            assertPermission();
            Map<Thread, StackTraceElement[]> tracesMap = Thread.getAllStackTraces();
            StringBuilder sb = new StringBuilder();
            sb.append("OemService stack trace:\n");
            int i = 0;
            for (Map.Entry<Thread, StackTraceElement[]> entry : tracesMap.entrySet()) {
                sb.append("Thread: ").append(i++).append('\n');
                StackTraceElement[] stack = entry.getValue();
                for (int j = 0; j < stack.length; j++) {
                    sb.append(stack[j].toString()).append('\n');
                }
            }

            return sb.toString();
        }

        private void assertPermission() {
            if (checkCallingPermission(
                    PERMISSION_BIND_OEM_CAR_SERVICE) != PackageManager.PERMISSION_GRANTED) {
                String errorMsg = "Caller with uid:" + Binder.getCallingUid()
                        + " doesn't have permission " + PERMISSION_BIND_OEM_CAR_SERVICE;
                Slogf.e(TAG, errorMsg);
                throw new SecurityException(errorMsg);
            }
        }
    };


    /**
     * {@inheritDoc}
     *
     * <p>
     * OEM should override this method and do the initialization. OEM should also call super after
     * initialization as this method would call {@link OemCarServiceComponent#init()} for each
     * component implemented by OEM.
     *
     * <p>
     * Car Service will not be available at the time of this initialization. If the OEM needs
     * anything from CarService, they should wait for the CarServiceReady() call. It is expected
     * that most of the initialization will finish in this call.
     */
    @Override
    @CallSuper
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public void onCreate() {
        if (DBG) {
            Slogf.d(TAG, "OnCreate");
        }

        // Create all components
        OemCarAudioFocusService oemCarAudioFocusService = getOemAudioFocusService();
        synchronized (mLock) {
            if (oemCarAudioFocusService != null) {
                mOemCarServiceComponents.put(IOemCarAudioFocusService.class,
                        new OemCarAudioFocusServiceImpl(oemCarAudioFocusService));
            }

            // Initialize them
            for (int i = 0; i < mOemCarServiceComponents.size(); i++) {
                if (DBG) {
                    Slogf.d(TAG, "Initializing %s\n",
                            mOemCarServiceComponents.keyAt(i).getSimpleName());
                }
                mOemCarServiceComponents.valueAt(i).init();
            }
        }
        super.onCreate();
    }

    /**
     * {@inheritDoc}
     *
     * <p>
     * OEM should override this method and do all the resources deallocation. OEM should also call
     * super after resource deallocation as this method would call
     * {@link OemCarServiceComponent#release()} for each component implemented by OEM.
     */
    @Override
    @CallSuper
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public void onDestroy() {
        if (DBG) {
            Slogf.d(TAG, "OnDestroy");
        }

        // Destroy all components and release the resources
        synchronized (mLock) {
            for (int i = 0; i < mOemCarServiceComponents.size(); i++) {
                mOemCarServiceComponents.valueAt(i).release();
            }
        }

        super.onDestroy();
    }

    @Override
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public final int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        if (DBG) {
            Slogf.d(TAG, "onStartCommand");
        }
        return START_STICKY;
    }

    @NonNull
    @Override
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public final IBinder onBind(@Nullable Intent intent) {
        if (DBG) {
            Slogf.d(TAG, "onBind");
        }
        return mInterface.asBinder();
    }

    /**
     * Gets Audio Focus Service implemented by OEM Service.
     *
     * @return audio focus service if implemented by OEM service, else return {@code null}.
     */
    @Nullable
    @SuppressWarnings("[OnNameExpected]")
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public OemCarAudioFocusService getOemAudioFocusService() {
        if (DBG) {
            Slogf.d(TAG, "getOemUserService");
        }
        return null;
    }

    @CallSuper
    @Override
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public void dump(@Nullable FileDescriptor fd, @Nullable PrintWriter writer,
            @Nullable String[] args) {
        writer.println("**** Dump OemCarService ****");
        synchronized (mLock) {
            for (int i = 0; i < mOemCarServiceComponents.size(); i++) {
                mOemCarServiceComponents.valueAt(i).dump(writer, args);
            }
        }
    }

    /**
     * Checks the supported CarVersion by the OEM service.
     */
    @SuppressWarnings("[OnNameExpected]")
    @NonNull
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public abstract CarVersion getSupportedCarVersion();

    /**
     * Informs OEM service that CarService is now ready for communication.
     *
     * <p>
     * OEM should override this method if there is any initialization depending on CarService.
     */
    @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.TIRAMISU_2,
            minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0)
    public abstract void onCarServiceReady();
}