Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
| 22 | import android.os.RemoteException; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 23 | |
| 24 | import com.android.server.location.CallerIdentity; |
| 25 | |
| 26 | import java.util.NoSuchElementException; |
| 27 | import java.util.function.Consumer; |
| 28 | |
| 29 | /** |
| 30 | * Shared utilities for LocationManagerService and GnssManager. |
| 31 | */ |
| 32 | public class LocationManagerServiceUtils { |
| 33 | |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Listener that can be linked to a binder. |
| 36 | * @param <TListener> listener type |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 37 | * @param <TRequest> request type |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 38 | */ |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 39 | public static class LinkedListener<TRequest, TListener> extends |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 40 | LinkedListenerBase { |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 41 | @Nullable protected final TRequest mRequest; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 42 | private final TListener mListener; |
| 43 | private final Consumer<TListener> mBinderDeathCallback; |
| 44 | |
| 45 | public LinkedListener( |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 46 | @Nullable TRequest request, |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 47 | @NonNull TListener listener, |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 48 | @NonNull CallerIdentity callerIdentity, |
| 49 | @NonNull Consumer<TListener> binderDeathCallback) { |
Soonil Nagarkar | 39766392 | 2020-03-28 13:59:26 -0700 | [diff] [blame] | 50 | super(callerIdentity); |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 51 | mListener = listener; |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 52 | mRequest = request; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 53 | mBinderDeathCallback = binderDeathCallback; |
| 54 | } |
| 55 | |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame] | 56 | @Nullable |
| 57 | public TRequest getRequest() { |
| 58 | return mRequest; |
| 59 | } |
| 60 | |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void binderDied() { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 63 | mBinderDeathCallback.accept(mListener); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Skeleton class of listener that can be linked to a binder. |
| 69 | */ |
| 70 | public abstract static class LinkedListenerBase implements IBinder.DeathRecipient { |
| 71 | protected final CallerIdentity mCallerIdentity; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 72 | |
Soonil Nagarkar | 39766392 | 2020-03-28 13:59:26 -0700 | [diff] [blame] | 73 | LinkedListenerBase(@NonNull CallerIdentity callerIdentity) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 74 | mCallerIdentity = callerIdentity; |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String toString() { |
Soonil Nagarkar | 39766392 | 2020-03-28 13:59:26 -0700 | [diff] [blame] | 79 | return mCallerIdentity.toString(); |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | public CallerIdentity getCallerIdentity() { |
| 83 | return mCallerIdentity; |
| 84 | } |
| 85 | |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Link listener (i.e. callback) to a binder, so that it will be called upon binder's death. |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 88 | */ |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 89 | public boolean linkToListenerDeathNotificationLocked(IBinder binder) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 90 | try { |
| 91 | binder.linkToDeath(this, 0 /* flags */); |
| 92 | return true; |
| 93 | } catch (RemoteException e) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Unlink death listener (i.e. callback) from binder. |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 100 | */ |
Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 101 | public void unlinkFromListenerDeathNotificationLocked(IBinder binder) { |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 102 | try { |
| 103 | binder.unlinkToDeath(this, 0 /* flags */); |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 104 | } catch (NoSuchElementException e) { |
Soonil Nagarkar | 39766392 | 2020-03-28 13:59:26 -0700 | [diff] [blame] | 105 | // ignore |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Sasha Kuznetsov | a8ace3c | 2019-09-13 14:14:40 -0700 | [diff] [blame] | 108 | } |
| 109 | } |