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; |
| 20 | import android.app.ActivityManager; |
| 21 | import android.content.Context; |
| 22 | import android.os.IBinder; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.server.location.CallerIdentity; |
| 27 | |
| 28 | import java.util.NoSuchElementException; |
| 29 | import java.util.function.Consumer; |
| 30 | |
| 31 | /** |
| 32 | * Shared utilities for LocationManagerService and GnssManager. |
| 33 | */ |
| 34 | public class LocationManagerServiceUtils { |
| 35 | |
| 36 | private static final String TAG = "LocManagerServiceUtils"; |
| 37 | private static final boolean D = Log.isLoggable(TAG, Log.DEBUG); |
| 38 | |
| 39 | /** |
| 40 | * Listener that can be linked to a binder. |
| 41 | * @param <TListener> listener type |
| 42 | */ |
| 43 | public static class LinkedListener<TListener> extends |
| 44 | LinkedListenerBase { |
| 45 | private final TListener mListener; |
| 46 | private final Consumer<TListener> mBinderDeathCallback; |
| 47 | |
| 48 | public LinkedListener( |
| 49 | @NonNull TListener listener, |
| 50 | String listenerName, |
| 51 | @NonNull CallerIdentity callerIdentity, |
| 52 | @NonNull Consumer<TListener> binderDeathCallback) { |
| 53 | super(callerIdentity, listenerName); |
| 54 | mListener = listener; |
| 55 | mBinderDeathCallback = binderDeathCallback; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void binderDied() { |
| 60 | if (D) Log.d(TAG, "Remote " + mListenerName + " died."); |
| 61 | mBinderDeathCallback.accept(mListener); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Skeleton class of listener that can be linked to a binder. |
| 67 | */ |
| 68 | public abstract static class LinkedListenerBase implements IBinder.DeathRecipient { |
| 69 | protected final CallerIdentity mCallerIdentity; |
| 70 | protected final String mListenerName; |
| 71 | |
| 72 | LinkedListenerBase( |
| 73 | @NonNull CallerIdentity callerIdentity, @NonNull String listenerName) { |
| 74 | mCallerIdentity = callerIdentity; |
| 75 | mListenerName = listenerName; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String toString() { |
| 80 | return mListenerName + "[" + mCallerIdentity.mPackageName + "(" + mCallerIdentity.mPid |
| 81 | + ")]"; |
| 82 | } |
| 83 | |
| 84 | public CallerIdentity getCallerIdentity() { |
| 85 | return mCallerIdentity; |
| 86 | } |
| 87 | |
| 88 | public String getListenerName() { |
| 89 | return mListenerName; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Link listener (i.e. callback) to a binder, so that it will be called upon binder's death. |
| 94 | * |
| 95 | * @param binder that calls listener upon death |
| 96 | * @return true if listener is successfully linked to binder, false otherwise |
| 97 | */ |
| 98 | public boolean linkToListenerDeathNotificationLocked( |
| 99 | IBinder binder) { |
| 100 | try { |
| 101 | binder.linkToDeath(this, 0 /* flags */); |
| 102 | return true; |
| 103 | } catch (RemoteException e) { |
| 104 | // if the remote process registering the listener is already dead, just swallow the |
| 105 | // exception and return |
| 106 | Log.w(TAG, "Could not link " + mListenerName + " death callback.", e); |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Unlink death listener (i.e. callback) from binder. |
| 113 | * |
| 114 | * @param binder that calls listener upon death |
| 115 | * @return true if binder is successfully unlinked from binder, false otherwise |
| 116 | */ |
| 117 | public boolean unlinkFromListenerDeathNotificationLocked( |
| 118 | IBinder binder) { |
| 119 | try { |
| 120 | binder.unlinkToDeath(this, 0 /* flags */); |
| 121 | return true; |
| 122 | } catch (NoSuchElementException e) { |
| 123 | // if the death callback isn't connected (it should be...), log error, |
| 124 | // swallow the exception and return |
| 125 | Log.w(TAG, "Could not unlink " + mListenerName + " death callback.", e); |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Convert boolean foreground into "foreground" or "background" string. |
| 134 | * |
| 135 | * @param foreground boolean indicating foreground |
| 136 | * @return "foreground" string if true, false otherwise |
| 137 | */ |
| 138 | public static String foregroundAsString(boolean foreground) { |
| 139 | return foreground ? "foreground" : "background"; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Classifies importance level as foreground or not. |
| 145 | * |
| 146 | * @param importance level as int |
| 147 | * @return boolean indicating if importance level is foreground or greater |
| 148 | */ |
| 149 | public static boolean isImportanceForeground(int importance) { |
| 150 | return importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get package importance level. |
| 155 | * |
| 156 | * @param packageName package name |
| 157 | * @return package importance level as int |
| 158 | */ |
| 159 | public static int getPackageImportance(String packageName, Context context) { |
| 160 | return ((ActivityManager) context.getSystemService( |
| 161 | Context.ACTIVITY_SERVICE)).getPackageImportance(packageName); |
| 162 | } |
| 163 | } |