The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.os.IBinder; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 22 | import android.util.ArrayMap; |
Dianne Hackborn | 24c98e8 | 2014-09-14 17:23:54 -0700 | [diff] [blame] | 23 | import android.util.ArraySet; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 24 | import android.util.proto.ProtoOutputStream; |
| 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import java.io.PrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A particular Intent that has been bound to a Service. |
| 30 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 31 | final class IntentBindRecord { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** The running service. */ |
| 33 | final ServiceRecord service; |
| 34 | /** The intent that is bound.*/ |
| 35 | final Intent.FilterComparison intent; // |
| 36 | /** All apps that have bound to this Intent. */ |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 37 | final ArrayMap<ProcessRecord, AppBindRecord> apps |
| 38 | = new ArrayMap<ProcessRecord, AppBindRecord>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** Binder published from service. */ |
| 40 | IBinder binder; |
| 41 | /** Set when we have initiated a request for this binder. */ |
| 42 | boolean requested; |
| 43 | /** Set when we have received the requested binder. */ |
| 44 | boolean received; |
| 45 | /** Set when we still need to tell the service all clients are unbound. */ |
| 46 | boolean hasBound; |
| 47 | /** Set when the service's onUnbind() has asked to be told about new clients. */ |
| 48 | boolean doRebind; |
| 49 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 50 | String stringName; // caching of toString |
| 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 53 | pw.print(prefix); pw.print("service="); pw.println(service); |
| 54 | dumpInService(pw, prefix); |
| 55 | } |
| 56 | |
| 57 | void dumpInService(PrintWriter pw, String prefix) { |
| 58 | pw.print(prefix); pw.print("intent={"); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 59 | pw.print(intent.getIntent().toShortString(false, true, false, false)); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 60 | pw.println('}'); |
| 61 | pw.print(prefix); pw.print("binder="); pw.println(binder); |
| 62 | pw.print(prefix); pw.print("requested="); pw.print(requested); |
| 63 | pw.print(" received="); pw.print(received); |
| 64 | pw.print(" hasBound="); pw.print(hasBound); |
| 65 | pw.print(" doRebind="); pw.println(doRebind); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 66 | for (int i=0; i<apps.size(); i++) { |
| 67 | AppBindRecord a = apps.valueAt(i); |
| 68 | pw.print(prefix); pw.print("* Client AppBindRecord{"); |
| 69 | pw.print(Integer.toHexString(System.identityHashCode(a))); |
| 70 | pw.print(' '); pw.print(a.client); pw.println('}'); |
| 71 | a.dumpInIntentBind(pw, prefix + " "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | IntentBindRecord(ServiceRecord _service, Intent.FilterComparison _intent) { |
| 76 | service = _service; |
| 77 | intent = _intent; |
| 78 | } |
| 79 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 80 | int collectFlags() { |
| 81 | int flags = 0; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 82 | for (int i=apps.size()-1; i>=0; i--) { |
Dianne Hackborn | 24c98e8 | 2014-09-14 17:23:54 -0700 | [diff] [blame] | 83 | final ArraySet<ConnectionRecord> connections = apps.valueAt(i).connections; |
| 84 | for (int j=connections.size()-1; j>=0; j--) { |
| 85 | flags |= connections.valueAt(j).flags; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return flags; |
| 89 | } |
| 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 92 | if (stringName != null) { |
| 93 | return stringName; |
| 94 | } |
| 95 | StringBuilder sb = new StringBuilder(128); |
| 96 | sb.append("IntentBindRecord{"); |
| 97 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 98 | sb.append(' '); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 99 | if ((collectFlags()&Context.BIND_AUTO_CREATE) != 0) { |
| 100 | sb.append("CR "); |
| 101 | } |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 102 | sb.append(service.shortInstanceName); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 103 | sb.append(':'); |
| 104 | if (intent != null) { |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 105 | intent.getIntent().toShortString(sb, false, false, false, false); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 106 | } |
| 107 | sb.append('}'); |
| 108 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 110 | |
| 111 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 112 | long token = proto.start(fieldId); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 113 | if (intent != null) { |
| 114 | intent.getIntent().writeToProto(proto, |
| 115 | IntentBindRecordProto.INTENT, false, true, false, false); |
| 116 | } |
| 117 | if (binder != null) { |
| 118 | proto.write(IntentBindRecordProto.BINDER, binder.toString()); |
| 119 | } |
Yi Jin | 163967f | 2018-03-15 13:49:44 -0700 | [diff] [blame] | 120 | proto.write(IntentBindRecordProto.AUTO_CREATE, |
| 121 | (collectFlags()&Context.BIND_AUTO_CREATE) != 0); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 122 | proto.write(IntentBindRecordProto.REQUESTED, requested); |
| 123 | proto.write(IntentBindRecordProto.RECEIVED, received); |
| 124 | proto.write(IntentBindRecordProto.HAS_BOUND, hasBound); |
| 125 | proto.write(IntentBindRecordProto.DO_REBIND, doRebind); |
| 126 | |
| 127 | final int N = apps.size(); |
| 128 | for (int i=0; i<N; i++) { |
| 129 | AppBindRecord a = apps.valueAt(i); |
| 130 | if (a != null) { |
| 131 | a.writeToProto(proto, IntentBindRecordProto.APPS); |
| 132 | } |
| 133 | } |
| 134 | proto.end(token); |
| 135 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |