1 /* 2 * Copyright (C) 2020 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.location.injector; 18 19 import android.location.util.identity.CallerIdentity; 20 21 import java.util.concurrent.CopyOnWriteArrayList; 22 23 /** 24 * Provides helpers and listeners for appops. 25 */ 26 public abstract class AppOpsHelper { 27 28 /** 29 * Listener for current user changes. 30 */ 31 public interface LocationAppOpListener { 32 33 /** 34 * Called when something has changed about a location appop for the given package. 35 */ onAppOpsChanged(String packageName)36 void onAppOpsChanged(String packageName); 37 } 38 39 private final CopyOnWriteArrayList<LocationAppOpListener> mListeners; 40 AppOpsHelper()41 public AppOpsHelper() { 42 mListeners = new CopyOnWriteArrayList<>(); 43 } 44 notifyAppOpChanged(String packageName)45 protected final void notifyAppOpChanged(String packageName) { 46 for (LocationAppOpListener listener : mListeners) { 47 listener.onAppOpsChanged(packageName); 48 } 49 } 50 51 /** 52 * Adds a listener for app ops events. Callbacks occur on an unspecified thread. 53 */ addListener(LocationAppOpListener listener)54 public final void addListener(LocationAppOpListener listener) { 55 mListeners.add(listener); 56 } 57 58 /** 59 * Removes a listener for app ops events. 60 */ removeListener(LocationAppOpListener listener)61 public final void removeListener(LocationAppOpListener listener) { 62 mListeners.remove(listener); 63 } 64 65 /** 66 * Starts the given appop. 67 */ startOpNoThrow(int appOp, CallerIdentity callerIdentity)68 public abstract boolean startOpNoThrow(int appOp, CallerIdentity callerIdentity); 69 70 /** 71 * Finishes the given appop. 72 */ finishOp(int appOp, CallerIdentity callerIdentity)73 public abstract void finishOp(int appOp, CallerIdentity callerIdentity); 74 75 /** 76 * Checks the given appop. 77 */ checkOpNoThrow(int appOp, CallerIdentity callerIdentity)78 public abstract boolean checkOpNoThrow(int appOp, CallerIdentity callerIdentity); 79 80 /** 81 * Notes the given appop (and may throw a security exception). 82 */ noteOp(int appOp, CallerIdentity callerIdentity)83 public abstract boolean noteOp(int appOp, CallerIdentity callerIdentity); 84 85 /** 86 * Notes the given appop. 87 */ noteOpNoThrow(int appOp, CallerIdentity callerIdentity)88 public abstract boolean noteOpNoThrow(int appOp, CallerIdentity callerIdentity); 89 } 90