blob: af1031e5b887a4ddad93a62a75d9c3e1c7c87ed7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070019import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.app.IServiceConnection;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070022import android.app.PendingIntent;
Dianne Hackborn0c380492012-08-20 17:23:30 -070023import android.content.Context;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070024import android.util.Slog;
Yi Jin6b514142017-10-30 14:54:12 -070025import android.util.proto.ProtoOutputStream;
Yi Jin148d7f42017-11-28 14:23:56 -080026import android.util.proto.ProtoUtils;
Yi Jin6b514142017-10-30 14:54:12 -070027
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070028import com.android.internal.app.procstats.AssociationState;
29import com.android.internal.app.procstats.ProcessStats;
Wale Ogunwale59507092018-10-29 09:00:30 -070030import com.android.server.wm.ActivityServiceConnectionsHolder;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import java.io.PrintWriter;
33
34/**
35 * Description of a single binding to a service.
36 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070037final class ConnectionRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 final AppBindRecord binding; // The application/service binding.
Wale Ogunwalec4e63a42018-10-02 13:19:54 -070039 final ActivityServiceConnectionsHolder<ConnectionRecord> activity; // If non-null, the owning activity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 final IServiceConnection conn; // The client connection.
41 final int flags; // Binding options.
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070042 final int clientLabel; // String resource labeling this client.
43 final PendingIntent clientIntent; // How to launch the client.
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070044 final int clientUid; // The identity of this connection's client
45 final String clientProcessName; // The source process of this connection's client
Dianne Hackborn24bbe582018-12-17 11:58:31 -080046 final String clientPackageName; // The source package of this connection's client
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070047 public AssociationState.SourceState association; // Association tracking
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070048 String stringName; // Caching of toString.
Dianne Hackborn130b0d22011-07-26 22:07:48 -070049 boolean serviceDead; // Well is it?
Yi Jin148d7f42017-11-28 14:23:56 -080050
51 // Please keep the following two enum list synced.
Dianne Hackborna631d562018-11-20 15:58:15 -080052 private static final int[] BIND_ORIG_ENUMS = new int[] {
Yi Jin148d7f42017-11-28 14:23:56 -080053 Context.BIND_AUTO_CREATE,
54 Context.BIND_DEBUG_UNBIND,
55 Context.BIND_NOT_FOREGROUND,
56 Context.BIND_IMPORTANT_BACKGROUND,
57 Context.BIND_ABOVE_CLIENT,
58 Context.BIND_ALLOW_OOM_MANAGEMENT,
59 Context.BIND_WAIVE_PRIORITY,
60 Context.BIND_IMPORTANT,
61 Context.BIND_ADJUST_WITH_ACTIVITY,
62 Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
63 Context.BIND_FOREGROUND_SERVICE,
64 Context.BIND_TREAT_LIKE_ACTIVITY,
65 Context.BIND_VISIBLE,
66 Context.BIND_SHOWING_UI,
67 Context.BIND_NOT_VISIBLE,
68 };
Dianne Hackborna631d562018-11-20 15:58:15 -080069 private static final int[] BIND_PROTO_ENUMS = new int[] {
Yi Jin148d7f42017-11-28 14:23:56 -080070 ConnectionRecordProto.AUTO_CREATE,
71 ConnectionRecordProto.DEBUG_UNBIND,
72 ConnectionRecordProto.NOT_FG,
73 ConnectionRecordProto.IMPORTANT_BG,
74 ConnectionRecordProto.ABOVE_CLIENT,
75 ConnectionRecordProto.ALLOW_OOM_MANAGEMENT,
76 ConnectionRecordProto.WAIVE_PRIORITY,
77 ConnectionRecordProto.IMPORTANT,
78 ConnectionRecordProto.ADJUST_WITH_ACTIVITY,
79 ConnectionRecordProto.FG_SERVICE_WHILE_AWAKE,
80 ConnectionRecordProto.FG_SERVICE,
81 ConnectionRecordProto.TREAT_LIKE_ACTIVITY,
82 ConnectionRecordProto.VISIBLE,
83 ConnectionRecordProto.SHOWING_UI,
84 ConnectionRecordProto.NOT_VISIBLE,
85 };
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 void dump(PrintWriter pw, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 pw.println(prefix + "binding=" + binding);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070089 if (activity != null) {
Wale Ogunwalec4e63a42018-10-02 13:19:54 -070090 activity.dump(pw, prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 pw.println(prefix + "conn=" + conn.asBinder()
93 + " flags=0x" + Integer.toHexString(flags));
94 }
Yi Jin148d7f42017-11-28 14:23:56 -080095
Wale Ogunwalec4e63a42018-10-02 13:19:54 -070096 ConnectionRecord(AppBindRecord _binding,
97 ActivityServiceConnectionsHolder<ConnectionRecord> _activity,
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070098 IServiceConnection _conn, int _flags,
99 int _clientLabel, PendingIntent _clientIntent,
Dianne Hackborn24bbe582018-12-17 11:58:31 -0800100 int _clientUid, String _clientProcessName, String _clientPackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 binding = _binding;
102 activity = _activity;
103 conn = _conn;
104 flags = _flags;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700105 clientLabel = _clientLabel;
106 clientIntent = _clientIntent;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700107 clientUid = _clientUid;
108 clientProcessName = _clientProcessName;
Dianne Hackborn24bbe582018-12-17 11:58:31 -0800109 clientPackageName = _clientPackageName;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700110 }
111
112 public void startAssociationIfNeeded() {
Dianne Hackborn95031ef2018-07-09 09:09:05 -0700113 // If we don't already have an active association, create one... but only if this
114 // is an association between two different processes.
Dianne Hackborn2fd8ce42018-07-12 14:51:54 -0700115 if (ActivityManagerService.TRACK_PROCSTATS_ASSOCIATIONS
116 && association == null && binding.service.app != null
117 && (binding.service.appInfo.uid != clientUid
118 || !binding.service.processName.equals(clientProcessName))) {
119 ProcessStats.ProcessStateHolder holder = binding.service.app.pkgList.get(
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800120 binding.service.instanceName.getPackageName());
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700121 if (holder == null) {
122 Slog.wtf(TAG_AM, "No package in referenced service "
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800123 + binding.service.shortInstanceName + ": proc=" + binding.service.app);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700124 } else if (holder.pkg == null) {
125 Slog.wtf(TAG_AM, "Inactive holder in referenced service "
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800126 + binding.service.shortInstanceName + ": proc=" + binding.service.app);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700127 } else {
Dianne Hackborn95031ef2018-07-09 09:09:05 -0700128 association = holder.pkg.getAssociationStateLocked(holder.state,
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800129 binding.service.instanceName.getClassName()).startSource(clientUid,
Dianne Hackborn24bbe582018-12-17 11:58:31 -0800130 clientProcessName, clientPackageName);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700131
132 }
133 }
134 }
135
Dianne Hackborn95031ef2018-07-09 09:09:05 -0700136 public void trackProcState(int procState, int seq, long now) {
137 if (association != null) {
138 association.trackProcState(procState, seq, now);
139 }
140 }
141
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700142 public void stopAssociation() {
143 if (association != null) {
144 association.stop();
145 association = null;
146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
148
149 public String toString() {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700150 if (stringName != null) {
151 return stringName;
152 }
153 StringBuilder sb = new StringBuilder(128);
154 sb.append("ConnectionRecord{");
155 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700156 sb.append(" u");
157 sb.append(binding.client.userId);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700158 sb.append(' ');
Dianne Hackborn0c380492012-08-20 17:23:30 -0700159 if ((flags&Context.BIND_AUTO_CREATE) != 0) {
160 sb.append("CR ");
161 }
162 if ((flags&Context.BIND_DEBUG_UNBIND) != 0) {
163 sb.append("DBG ");
164 }
165 if ((flags&Context.BIND_NOT_FOREGROUND) != 0) {
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700166 sb.append("!FG ");
Dianne Hackborn0c380492012-08-20 17:23:30 -0700167 }
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700168 if ((flags&Context.BIND_IMPORTANT_BACKGROUND) != 0) {
169 sb.append("IMPB ");
170 }
Dianne Hackborn0c380492012-08-20 17:23:30 -0700171 if ((flags&Context.BIND_ABOVE_CLIENT) != 0) {
172 sb.append("ABCLT ");
173 }
174 if ((flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
175 sb.append("OOM ");
176 }
177 if ((flags&Context.BIND_WAIVE_PRIORITY) != 0) {
178 sb.append("WPRI ");
179 }
180 if ((flags&Context.BIND_IMPORTANT) != 0) {
181 sb.append("IMP ");
182 }
183 if ((flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
Dianne Hackbornd69e4c12015-04-24 09:54:54 -0700184 sb.append("WACT ");
185 }
186 if ((flags&Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE) != 0) {
187 sb.append("FGSA ");
188 }
189 if ((flags&Context.BIND_FOREGROUND_SERVICE) != 0) {
190 sb.append("FGS ");
191 }
192 if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
193 sb.append("LACT ");
Dianne Hackborn0c380492012-08-20 17:23:30 -0700194 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700195 if ((flags&Context.BIND_VISIBLE) != 0) {
196 sb.append("VIS ");
Dianne Hackborn0c380492012-08-20 17:23:30 -0700197 }
Dianne Hackborn8585d682013-07-18 15:39:51 -0700198 if ((flags&Context.BIND_SHOWING_UI) != 0) {
199 sb.append("UI ");
200 }
201 if ((flags&Context.BIND_NOT_VISIBLE) != 0) {
202 sb.append("!VIS ");
203 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -0700204 if (serviceDead) {
205 sb.append("DEAD ");
206 }
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800207 sb.append(binding.service.shortInstanceName);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700208 sb.append(":@");
209 sb.append(Integer.toHexString(System.identityHashCode(conn.asBinder())));
210 sb.append('}');
211 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
Yi Jin6b514142017-10-30 14:54:12 -0700213
214 public void writeToProto(ProtoOutputStream proto, long fieldId) {
215 if (binding == null) return; // if binding is null, don't write data, something is wrong.
216 long token = proto.start(fieldId);
217 proto.write(ConnectionRecordProto.HEX_HASH,
218 Integer.toHexString(System.identityHashCode(this)));
219 if (binding.client != null) {
220 proto.write(ConnectionRecordProto.USER_ID, binding.client.userId);
221 }
Yi Jin148d7f42017-11-28 14:23:56 -0800222 ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, ConnectionRecordProto.FLAGS,
223 flags, BIND_ORIG_ENUMS, BIND_PROTO_ENUMS);
Yi Jin6b514142017-10-30 14:54:12 -0700224 if (serviceDead) {
225 proto.write(ConnectionRecordProto.FLAGS, ConnectionRecordProto.DEAD);
226 }
227 if (binding.service != null) {
Dianne Hackborn27b4d942018-11-12 15:01:40 -0800228 proto.write(ConnectionRecordProto.SERVICE_NAME, binding.service.shortInstanceName);
Yi Jin6b514142017-10-30 14:54:12 -0700229 }
Yi Jin6b514142017-10-30 14:54:12 -0700230 proto.end(token);
231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232}