1 /*
2  * Copyright (C) 2023 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.server.vibrator;
18 
19 import android.annotation.NonNull;
20 import android.frameworks.vibrator.IVibratorController;
21 import android.os.IBinder;
22 import android.os.RemoteException;
23 import android.util.Slog;
24 
25 /**
26  * Holder class for {@link IVibratorController}.
27  */
28 public final class VibratorControllerHolder implements IBinder.DeathRecipient {
29     private static final String TAG = "VibratorControllerHolder";
30 
31     private IVibratorController mVibratorController;
32 
getVibratorController()33     public IVibratorController getVibratorController() {
34         return mVibratorController;
35     }
36 
37     /**
38      * Sets the {@link IVibratorController} in {@link VibratorControllerHolder} to the new
39      * controller. This will also take care of registering and unregistering death notifications
40      * for the cached {@link IVibratorController}.
41      */
setVibratorController(IVibratorController controller)42     public void setVibratorController(IVibratorController controller) {
43         try {
44             if (mVibratorController != null) {
45                 mVibratorController.asBinder().unlinkToDeath(this, 0);
46             }
47             mVibratorController = controller;
48             if (mVibratorController != null) {
49                 mVibratorController.asBinder().linkToDeath(this, 0);
50             }
51         } catch (RemoteException e) {
52             Slog.wtf(TAG, "Failed to set IVibratorController: " + this, e);
53         }
54     }
55 
56     @Override
binderDied(@onNull IBinder deadBinder)57     public void binderDied(@NonNull IBinder deadBinder) {
58         if (mVibratorController != null && deadBinder == mVibratorController.asBinder()) {
59             setVibratorController(null);
60         }
61     }
62 
63     @Override
binderDied()64     public void binderDied() {
65         // Should not be used as binderDied(IBinder who) is overridden.
66         Slog.wtf(TAG, "binderDied() called unexpectedly.");
67     }
68 }
69