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.am; 18 19 import android.util.ArraySet; 20 21 import com.android.internal.annotations.GuardedBy; 22 23 import java.io.PrintWriter; 24 25 /** 26 * The state info of all broadcast receivers in the process. 27 */ 28 final class ProcessReceiverRecord { 29 final ProcessRecord mApp; 30 private final ActivityManagerService mService; 31 32 /** 33 * mReceivers currently running in the app. 34 */ 35 private final ArraySet<BroadcastRecord> mCurReceivers = new ArraySet<BroadcastRecord>(); 36 37 /** 38 * All IIntentReceivers that are registered from this process. 39 */ 40 private final ArraySet<ReceiverList> mReceivers = new ArraySet<>(); 41 numberOfCurReceivers()42 int numberOfCurReceivers() { 43 return mCurReceivers.size(); 44 } 45 getCurReceiverAt(int index)46 BroadcastRecord getCurReceiverAt(int index) { 47 return mCurReceivers.valueAt(index); 48 } 49 hasCurReceiver(BroadcastRecord receiver)50 boolean hasCurReceiver(BroadcastRecord receiver) { 51 return mCurReceivers.contains(receiver); 52 } 53 addCurReceiver(BroadcastRecord receiver)54 void addCurReceiver(BroadcastRecord receiver) { 55 mCurReceivers.add(receiver); 56 } 57 removeCurReceiver(BroadcastRecord receiver)58 void removeCurReceiver(BroadcastRecord receiver) { 59 mCurReceivers.remove(receiver); 60 } 61 numberOfReceivers()62 int numberOfReceivers() { 63 return mReceivers.size(); 64 } 65 getReceiverAt(int index)66 ReceiverList getReceiverAt(int index) { 67 return mReceivers.valueAt(index); 68 } 69 addReceiver(ReceiverList receiver)70 void addReceiver(ReceiverList receiver) { 71 mReceivers.add(receiver); 72 } 73 removeReceiver(ReceiverList receiver)74 void removeReceiver(ReceiverList receiver) { 75 mReceivers.remove(receiver); 76 } 77 ProcessReceiverRecord(ProcessRecord app)78 ProcessReceiverRecord(ProcessRecord app) { 79 mApp = app; 80 mService = app.mService; 81 } 82 83 @GuardedBy("mService") onCleanupApplicationRecordLocked()84 void onCleanupApplicationRecordLocked() { 85 // Unregister any mReceivers. 86 for (int i = mReceivers.size() - 1; i >= 0; i--) { 87 mService.removeReceiverLocked(mReceivers.valueAt(i)); 88 } 89 mReceivers.clear(); 90 } 91 dump(PrintWriter pw, String prefix, long nowUptime)92 void dump(PrintWriter pw, String prefix, long nowUptime) { 93 if (!mCurReceivers.isEmpty()) { 94 pw.print(prefix); pw.println("Current mReceivers:"); 95 for (int i = 0, size = mCurReceivers.size(); i < size; i++) { 96 pw.print(prefix); pw.print(" - "); pw.println(mCurReceivers.valueAt(i)); 97 } 98 } 99 if (mReceivers.size() > 0) { 100 pw.print(prefix); pw.println("mReceivers:"); 101 for (int i = 0, size = mReceivers.size(); i < size; i++) { 102 pw.print(prefix); pw.print(" - "); pw.println(mReceivers.valueAt(i)); 103 } 104 } 105 } 106 } 107