summaryrefslogtreecommitdiff
path: root/android/sdl_android/src/main/java/com/smartdevicelink/managers/SdlManager.java
blob: 6dc3ce4aa95780a5bbbb4dc162d50ba227eccf92 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * Copyright (c) 2019 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Livio Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.smartdevicelink.managers;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.livio.taskmaster.Taskmaster;
import com.smartdevicelink.managers.audio.AudioStreamManager;
import com.smartdevicelink.managers.file.FileManager;
import com.smartdevicelink.managers.lifecycle.SystemCapabilityManager;
import com.smartdevicelink.managers.lockscreen.LockScreenConfig;
import com.smartdevicelink.managers.lockscreen.LockScreenManager;
import com.smartdevicelink.managers.permission.PermissionManager;
import com.smartdevicelink.managers.screen.ScreenManager;
import com.smartdevicelink.managers.video.VideoStreamManager;
import com.smartdevicelink.protocol.ISdlServiceListener;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.proxy.RPCMessage;
import com.smartdevicelink.proxy.rpc.RegisterAppInterfaceResponse;
import com.smartdevicelink.proxy.rpc.SdlMsgVersion;
import com.smartdevicelink.proxy.rpc.enums.AppHMIType;
import com.smartdevicelink.proxy.rpc.listeners.OnMultipleRequestListener;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCListener;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCRequestListener;
import com.smartdevicelink.streaming.video.VideoStreamingParameters;
import com.smartdevicelink.transport.BaseTransportConfig;
import com.smartdevicelink.transport.MultiplexTransportConfig;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.TransportRecord;
import com.smartdevicelink.util.DebugTool;
import com.smartdevicelink.util.Version;

import java.util.List;

/**
 * <strong>SDLManager</strong> <br>
 * <p>
 * This is the main point of contact between an application and SDL <br>
 * <p>
 * It is broken down to these areas: <br>
 * <p>
 * 1. SDLManagerBuilder <br>
 * 2. ISdl Interface along with its overridden methods - This can be passed into attached managers <br>
 * 3. Sending Requests <br>
 * 4. Helper methods
 */
public class SdlManager extends BaseSdlManager {
    private Context context;
    private LockScreenConfig lockScreenConfig;

    // Managers
    private LockScreenManager lockScreenManager;
    private VideoStreamManager videoStreamManager;
    private AudioStreamManager audioStreamManager;

    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable changeRegistrationRunnable = new Runnable() {
        @Override
        public void run() {
            checkLifecycleConfiguration();
            DebugTool.logInfo(TAG, "Retry Change Registration Count: " + changeRegistrationRetry);
        }
    };

    public SdlManager() {
        DebugTool.logWarning(TAG, "SdlManager must be created with SdlManager.Builder");
    }

    /**
     * Starts up a SdlManager, and calls provided callback called once all BaseSubManagers are done setting up
     */
    @Override
    public void start() {
        if (lifecycleManager == null) {
            if (transport != null && transport.getTransportType() == TransportType.MULTIPLEX) {
                //Do the thing
                MultiplexTransportConfig multiplexTransportConfig = (MultiplexTransportConfig) (transport);
                final MultiplexTransportConfig.TransportListener devListener = multiplexTransportConfig.getTransportListener();
                multiplexTransportConfig.setTransportListener(new MultiplexTransportConfig.TransportListener() {
                    @Override
                    public void onTransportEvent(List<TransportRecord> connectedTransports, boolean audioStreamTransportAvail, boolean videoStreamTransportAvail) {

                        //Pass to sub managers that need it
                        if (videoStreamManager != null) {
                            videoStreamManager.handleTransportUpdated(connectedTransports, audioStreamTransportAvail, videoStreamTransportAvail);
                        }

                        if (audioStreamManager != null) {
                            audioStreamManager.handleTransportUpdated(connectedTransports, audioStreamTransportAvail, videoStreamTransportAvail);
                        }
                        //If the developer supplied a listener to start, it is time to call that
                        if (devListener != null) {
                            devListener.onTransportEvent(connectedTransports, audioStreamTransportAvail, videoStreamTransportAvail);
                        }
                    }
                });

                //If the requires audio support has not been set, it should be set to true if the
                //app is a media app, and false otherwise
                if (multiplexTransportConfig.requiresAudioSupport() == null) {
                    multiplexTransportConfig.setRequiresAudioSupport(isMediaApp);
                }
            }

            super.start();

            lifecycleManager.setContext(context);
            lifecycleManager.start();
        }
    }

    @Override
    protected void initialize() {
        // Instantiate sub managers
        this.permissionManager = new PermissionManager(_internalInterface);
        this.fileManager = new FileManager(_internalInterface, context, fileManagerConfig);
        if (lockScreenConfig.getDisplayMode() != LockScreenConfig.DISPLAY_MODE_NEVER) {
            this.lockScreenManager = new LockScreenManager(lockScreenConfig, context, _internalInterface);
        }
        this.screenManager = new ScreenManager(_internalInterface, this.fileManager);
        if (getAppTypes().contains(AppHMIType.NAVIGATION) || getAppTypes().contains(AppHMIType.PROJECTION)) {
            this.videoStreamManager = new VideoStreamManager(_internalInterface);
        } else {
            this.videoStreamManager = null;
        }
        if (getAppTypes().contains(AppHMIType.NAVIGATION) || getAppTypes().contains(AppHMIType.PROJECTION)) {
            this.audioStreamManager = new AudioStreamManager(_internalInterface, context);
        } else {
            this.audioStreamManager = null;
        }

        // Start sub managers
        this.permissionManager.start(subManagerListener);
        this.fileManager.start(subManagerListener);
        if (lockScreenConfig.getDisplayMode() != LockScreenConfig.DISPLAY_MODE_NEVER) {
            this.lockScreenManager.start(subManagerListener);
        }
        this.screenManager.start(subManagerListener);
    }

    @Override
    void checkState() {
        if (permissionManager != null && fileManager != null && screenManager != null && (lockScreenConfig.getDisplayMode() == LockScreenConfig.DISPLAY_MODE_NEVER || lockScreenManager != null)) {
            if (permissionManager.getState() == BaseSubManager.READY && fileManager.getState() == BaseSubManager.READY && screenManager.getState() == BaseSubManager.READY && (lockScreenConfig.getDisplayMode() == LockScreenConfig.DISPLAY_MODE_NEVER || lockScreenManager.getState() == BaseSubManager.READY)) {
                DebugTool.logInfo(TAG, "Starting sdl manager, all sub managers are in ready state");
                transitionToState(BaseSubManager.READY);
                handleQueuedNotifications();
                notifyDevListener(null);
                onReady();
            } else if (permissionManager.getState() == BaseSubManager.ERROR && fileManager.getState() == BaseSubManager.ERROR && screenManager.getState() == BaseSubManager.ERROR && (lockScreenConfig.getDisplayMode() == LockScreenConfig.DISPLAY_MODE_NEVER || lockScreenManager.getState() == BaseSubManager.ERROR)) {
                String info = "ERROR starting sdl manager, all sub managers are in error state";
                DebugTool.logError(TAG, info);
                transitionToState(BaseSubManager.ERROR);
                notifyDevListener(info);
            } else if (permissionManager.getState() == BaseSubManager.SETTING_UP || fileManager.getState() == BaseSubManager.SETTING_UP || screenManager.getState() == BaseSubManager.SETTING_UP || (lockScreenConfig.getDisplayMode() != LockScreenConfig.DISPLAY_MODE_NEVER && lockScreenManager != null && lockScreenManager.getState() == BaseSubManager.SETTING_UP)) {
                DebugTool.logInfo(TAG, "SETTING UP sdl manager, some sub managers are still setting up");
                transitionToState(BaseSubManager.SETTING_UP);
                // No need to notify developer here!
            } else {
                DebugTool.logWarning(TAG, "LIMITED starting sdl manager, some sub managers are in error or limited state and the others finished setting up");
                transitionToState(BaseSubManager.LIMITED);
                handleQueuedNotifications();
                notifyDevListener(null);
                onReady();
            }
        } else {
            // We should never be here, but somehow one of the sub-sub managers is null
            String info = "ERROR one of the sdl sub managers is null";
            DebugTool.logError(TAG, info);
            transitionToState(BaseSubManager.ERROR);
            notifyDevListener(info);
        }
    }

    private void notifyDevListener(String info) {
        if (managerListener != null) {
            if (getState() == BaseSubManager.ERROR) {
                managerListener.onError(info, null);
            } else {
                managerListener.onStart();
            }
        }
    }

    @Override
    void retryChangeRegistration() {
        changeRegistrationRetry++;
        if (changeRegistrationRetry < MAX_RETRY) {
            if (handler != null && changeRegistrationRunnable != null) {
                handler.postDelayed(changeRegistrationRunnable, 3000);
            }
        }
    }

    /**
     * Dispose SdlManager and clean its resources
     * <strong>Note: new instance of SdlManager should be created on every connection. SdlManager cannot be reused after getting disposed.</strong>
     */
    @SuppressLint("NewApi")
    @Override
    public synchronized void dispose() {
        int state = getState();
        if (state == BaseSubManager.SHUTDOWN || state == BaseSubManager.ERROR) {
            DebugTool.logInfo(TAG, "SdlManager already disposed");
            return;
        }
        if (this.permissionManager != null) {
            this.permissionManager.dispose();
        }

        if (this.fileManager != null) {
            this.fileManager.dispose();
        }

        if (this.lockScreenManager != null) {
            this.lockScreenManager.dispose();
        }

        if (this.screenManager != null) {
            this.screenManager.dispose();
        }

        if (this.videoStreamManager != null) {
            this.videoStreamManager.dispose();
        }

        // SuppressLint("NewApi") is used because audioStreamManager is only available on android >= jelly bean
        if (this.audioStreamManager != null) {
            this.audioStreamManager.dispose();
        }

        if (this.lifecycleManager != null) {
            this.lifecycleManager.stop();
        }

        if (handler != null) {
            if (changeRegistrationRunnable != null) {
                handler.removeCallbacks(changeRegistrationRunnable);
                changeRegistrationRunnable = null;
            }

            handler = null;
        }

        if (managerListener != null) {
            managerListener.onDestroy();
            managerListener = null;
        }

        transitionToState(BaseSubManager.SHUTDOWN);
    }

    // MANAGER GETTERS

    /**
     * Gets the VideoStreamManager. <br>
     * The VideoStreamManager returned will only be not null if the registered app type is
     * either NAVIGATION or PROJECTION. Once the VideoStreamManager is retrieved, its start()
     * method will need to be called before use.
     * <br><br><strong>Note: VideoStreamManager should be used only after SdlManager.start() CompletionListener callback is completed successfully.</strong>
     *
     * @return a VideoStreamManager object attached to this SdlManager instance
     */
    public @Nullable
    VideoStreamManager getVideoStreamManager() {
        checkSdlManagerState();
        return videoStreamManager;
    }

    /**
     * Gets the AudioStreamManager. <br>
     * The AudioStreamManager returned will only be not null if the registered app type is
     * either NAVIGATION or PROJECTION. Once the AudioStreamManager is retrieved, its start()
     * method will need to be called before use.
     * <br><strong>Note: AudioStreamManager should be used only after SdlManager.start() CompletionListener callback is completed successfully.</strong>
     *
     * @return a AudioStreamManager object
     */
    public @Nullable
    AudioStreamManager getAudioStreamManager() {
        checkSdlManagerState();
        return audioStreamManager;
    }

    /**
     * Gets the LockScreenManager. <br>
     * <strong>Note: LockScreenManager should be used only after SdlManager.start() CompletionListener callback is completed successfully.</strong>
     *
     * @return a LockScreenManager object
     */
    public LockScreenManager getLockScreenManager() {
        if (lockScreenManager.getState() != BaseSubManager.READY && lockScreenManager.getState() != BaseSubManager.LIMITED) {
            DebugTool.logError(TAG, "LockScreenManager should not be accessed because it is not in READY/LIMITED state");
        }
        checkSdlManagerState();
        return lockScreenManager;
    }

    // PROTECTED GETTERS
    protected LockScreenConfig getLockScreenConfig() {
        return lockScreenConfig;
    }

    // BUILDER
    public static class Builder extends BaseSdlManager.Builder {
        /**
         * Builder for the SdlManager. Parameters in the constructor are required.
         *
         * @param context  the current context
         * @param appId    the app's ID
         * @param appName  the app's name
         * @param listener a SdlManagerListener object
         */
        public Builder(@NonNull Context context, @NonNull final String appId, @NonNull final String appName, @NonNull final SdlManagerListener listener) {
            super(appId, appName, listener);
            setContext(context);
        }

        /**
         * Builder for the SdlManager. Parameters in the constructor are required.
         *
         * @param context  the current context
         * @param appId    the app's ID
         * @param appName  the app's name
         * @param listener a SdlManagerListener object
         */
        public Builder(@NonNull Context context, @NonNull final String appId, @NonNull final String appName, @NonNull BaseTransportConfig transport, @NonNull final SdlManagerListener listener) {
            super(appId, appName, listener);
            setContext(context);
            setTransportType(transport);
        }

        /**
         * Sets the LockScreenConfig for the session. <br>
         * <strong>Note: If not set, the default configuration will be used.</strong>
         *
         * @param lockScreenConfig - configuration options
         */
        public Builder setLockScreenConfig(final LockScreenConfig lockScreenConfig) {
            sdlManager.lockScreenConfig = lockScreenConfig;
            return this;
        }

        /**
         * Sets the Context
         *
         * @param context the current context
         */
        public Builder setContext(Context context) {
            sdlManager.context = context;
            return this;
        }

        /**
         * Build SdlManager ang get it ready to be started
         * <strong>Note: new instance of SdlManager should be created on every connection. SdlManager cannot be reused after getting disposed.</strong>
         *
         * @return SdlManager instance that is ready to be started
         */
        public SdlManager build() {
            if (sdlManager.transport == null) {
                throw new IllegalArgumentException("You must set a transport type object");
            }

            if (sdlManager.lockScreenConfig == null) {
                // if lock screen params are not set, use default
                sdlManager.lockScreenConfig = new LockScreenConfig();
            }

            super.build();

            return sdlManager;
        }
    }

     ISdl _internalInterface = new ISdl() {
        @Override
        public void start() {
            lifecycleManager.getInternalInterface(SdlManager.this).start();
        }

        @Override
        public void stop() {
            lifecycleManager.getInternalInterface(SdlManager.this).stop();
        }

        @Override
        public boolean isConnected() {
            return lifecycleManager.getInternalInterface(SdlManager.this).isConnected();
        }

        @Override
        public void addServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener) {
            lifecycleManager.getInternalInterface(SdlManager.this).addServiceListener(serviceType, sdlServiceListener);
        }

        @Override
        public void removeServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener) {
            lifecycleManager.getInternalInterface(SdlManager.this).removeServiceListener(serviceType, sdlServiceListener);
        }

        @Override
        public void startVideoService(VideoStreamingParameters parameters, boolean encrypted, boolean withPendingRestart) {
            lifecycleManager.getInternalInterface(SdlManager.this).startVideoService(parameters, encrypted, withPendingRestart);
        }

        @Override
        public void startAudioService(boolean encrypted) {
            lifecycleManager.getInternalInterface(SdlManager.this).startAudioService(encrypted);
        }

        @Override
        public void sendRPC(RPCMessage message) {
            lifecycleManager.getInternalInterface(SdlManager.this).sendRPC(message);
        }

        @Override
        public void sendRPCs(List<? extends RPCMessage> rpcs, OnMultipleRequestListener listener) {
            lifecycleManager.getInternalInterface(SdlManager.this).sendRPCs(rpcs, listener);
        }

        @Override
        public void sendSequentialRPCs(List<? extends RPCMessage> rpcs, OnMultipleRequestListener listener) {
            lifecycleManager.getInternalInterface(SdlManager.this).sendSequentialRPCs(rpcs, listener);
        }

        @Override
        public void addOnRPCNotificationListener(FunctionID notificationId, OnRPCNotificationListener listener) {
            lifecycleManager.getInternalInterface(SdlManager.this).addOnRPCNotificationListener(notificationId, listener);
        }

        @Override
        public boolean removeOnRPCNotificationListener(FunctionID notificationId, OnRPCNotificationListener listener) {
            return lifecycleManager.getInternalInterface(SdlManager.this).removeOnRPCNotificationListener(notificationId, listener);
        }

        @Override
        public void addOnRPCRequestListener(FunctionID functionID, OnRPCRequestListener listener) {
            lifecycleManager.getInternalInterface(SdlManager.this).addOnRPCRequestListener(functionID, listener);
        }

        @Override
        public boolean removeOnRPCRequestListener(FunctionID functionID, OnRPCRequestListener listener) {
            return lifecycleManager.getInternalInterface(SdlManager.this).removeOnRPCRequestListener(functionID, listener);
        }

        @Override
        public void addOnRPCListener(FunctionID responseId, OnRPCListener listener) {
            lifecycleManager.getInternalInterface(SdlManager.this).addOnRPCListener(responseId, listener);
        }

        @Override
        public boolean removeOnRPCListener(FunctionID responseId, OnRPCListener listener) {
            return lifecycleManager.getInternalInterface(SdlManager.this).removeOnRPCListener(responseId, listener);
        }

        @Override
        public RegisterAppInterfaceResponse getRegisterAppInterfaceResponse() {
            return lifecycleManager.getInternalInterface(SdlManager.this).getRegisterAppInterfaceResponse();
        }

        @Override
        public boolean isTransportForServiceAvailable(SessionType serviceType) {
            return lifecycleManager.getInternalInterface(SdlManager.this).isTransportForServiceAvailable(serviceType);
        }

        @NonNull
        @Override
        public SdlMsgVersion getSdlMsgVersion() {
            return lifecycleManager.getInternalInterface(SdlManager.this).getSdlMsgVersion();
        }

        @NonNull
        @Override
        public Version getProtocolVersion() {
            return lifecycleManager.getInternalInterface(SdlManager.this).getProtocolVersion();
        }

         @Override
         public long getMtu(SessionType serviceType) {
             return lifecycleManager.getInternalInterface(SdlManager.this).getMtu(serviceType);
         }

        @Override
        public void startRPCEncryption() {
            lifecycleManager.getInternalInterface(SdlManager.this).startRPCEncryption();
        }

        @Override
        public Taskmaster getTaskmaster() {
            return lifecycleManager.getInternalInterface(SdlManager.this).getTaskmaster();
        }

        @Override
        public SystemCapabilityManager getSystemCapabilityManager() {
            return lifecycleManager.getInternalInterface(SdlManager.this).getSystemCapabilityManager();
        }

        @Override
        public PermissionManager getPermissionManager() {
            return permissionManager;
        }
    };
}