1/*
2 * Copyright (C) 2006 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
17package com.android.server.am;
18
19import android.content.IIntentReceiver;
20import android.content.Intent;
21import android.os.Binder;
22import android.os.Bundle;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.PrintWriterPrinter;
26import android.util.Printer;
27
28import java.io.PrintWriter;
29import java.util.ArrayList;
30
31/**
32 * A receiver object that has registered for one or more broadcasts.
33 * The ArrayList holds BroadcastFilter objects.
34 */
35class ReceiverList extends ArrayList<BroadcastFilter>
36        implements IBinder.DeathRecipient {
37    final ActivityManagerService owner;
38    public final IIntentReceiver receiver;
39    public final ProcessRecord app;
40    public final int pid;
41    public final int uid;
42    BroadcastRecord curBroadcast = null;
43    boolean linkedToDeath = false;
44
45    String stringName;
46
47    ReceiverList(ActivityManagerService _owner, ProcessRecord _app,
48            int _pid, int _uid, IIntentReceiver _receiver) {
49        owner = _owner;
50        receiver = _receiver;
51        app = _app;
52        pid = _pid;
53        uid = _uid;
54    }
55
56    // Want object identity, not the array identity we are inheriting.
57    public boolean equals(Object o) {
58        return this == o;
59    }
60    public int hashCode() {
61        return System.identityHashCode(this);
62    }
63
64    public void binderDied() {
65        linkedToDeath = false;
66        owner.unregisterReceiver(receiver);
67    }
68
69    void dumpLocal(PrintWriter pw, String prefix) {
70        pw.print(prefix); pw.print("app="); pw.print(app);
71            pw.print(" pid="); pw.print(pid); pw.print(" uid="); pw.println(uid);
72        if (curBroadcast != null || linkedToDeath) {
73            pw.print(prefix); pw.print("curBroadcast="); pw.print(curBroadcast);
74                pw.print(" linkedToDeath="); pw.println(linkedToDeath);
75        }
76    }
77
78    void dump(PrintWriter pw, String prefix) {
79        Printer pr = new PrintWriterPrinter(pw);
80        dumpLocal(pw, prefix);
81        String p2 = prefix + "  ";
82        final int N = size();
83        for (int i=0; i<N; i++) {
84            BroadcastFilter bf = get(i);
85            pw.print(prefix); pw.print("Filter #"); pw.print(i);
86                    pw.print(": BroadcastFilter{");
87                    pw.print(Integer.toHexString(System.identityHashCode(bf)));
88                    pw.println('}');
89            bf.dumpInReceiverList(pw, pr, p2);
90        }
91    }
92
93    public String toString() {
94        if (stringName != null) {
95            return stringName;
96        }
97        StringBuilder sb = new StringBuilder(128);
98        sb.append("ReceiverList{");
99        sb.append(Integer.toHexString(System.identityHashCode(this)));
100        sb.append(' ');
101        sb.append(pid);
102        sb.append(' ');
103        sb.append((app != null ? app.processName : "(unknown name)"));
104        sb.append('/');
105        sb.append(uid);
106        sb.append((receiver.asBinder() instanceof Binder) ? " local:" : " remote:");
107        sb.append(Integer.toHexString(System.identityHashCode(receiver.asBinder())));
108        sb.append('}');
109        return stringName = sb.toString();
110    }
111}
112