blob: 435c505adf1f1c0c524c8f6f6fbbe82f9ba7ace6 [file] [log] [blame]
Brad Ebinger116b8592015-11-18 11:40:11 -08001/*
2 * Copyright (C) 2015 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.telecom;
18
19import android.annotation.NonNull;
20
21import java.util.ArrayList;
22
23/**
24 * The session that stores information about a thread's point of entry into the Telecom code that
25 * persists until the thread exits Telecom.
26 */
27public class Session {
28
29 public static final String START_SESSION = "START_SESSION";
30 public static final String CREATE_SUBSESSION = "CREATE_SUBSESSION";
31 public static final String CONTINUE_SUBSESSION = "CONTINUE_SUBSESSION";
32 public static final String END_SUBSESSION = "END_SUBSESSION";
33 public static final String END_SESSION = "END_SESSION";
34
35 public static final int UNDEFINED = -1;
36
37 private String mSessionId;
38 private String mShortMethodName;
39 private long mExecutionStartTimeMs;
40 private long mExecutionEndTimeMs = UNDEFINED;
41 private Session mParentSession;
42 private ArrayList<Session> mChildSessions;
43 private boolean mIsCompleted = false;
Brad Ebinger11623a32015-11-25 13:52:02 -080044 private int mChildCounter = 0;
45 private long mThreadId = 0;
Brad Ebinger116b8592015-11-18 11:40:11 -080046
Brad Ebinger11623a32015-11-25 13:52:02 -080047 public Session(String sessionId, String shortMethodName, long startTimeMs, long threadID) {
Brad Ebinger116b8592015-11-18 11:40:11 -080048 setSessionId(sessionId);
49 setShortMethodName(shortMethodName);
50 mExecutionStartTimeMs = startTimeMs;
51 mParentSession = null;
52 mChildSessions = new ArrayList<>(5);
Brad Ebinger11623a32015-11-25 13:52:02 -080053 mThreadId = threadID;
Brad Ebinger116b8592015-11-18 11:40:11 -080054 }
55
56 public void setSessionId(@NonNull String sessionId) {
57 if(sessionId == null) {
58 mSessionId = "?";
59 }
60 mSessionId = sessionId;
61 }
62
63 public String getShortMethodName() {
64 return mShortMethodName;
65 }
66
67 public void setShortMethodName(String shortMethodName) {
68 if(shortMethodName == null) {
69 shortMethodName = "";
70 }
71 mShortMethodName = shortMethodName;
72 }
73
74 public void setParentSession(Session parentSession) {
75 mParentSession = parentSession;
76 }
77
78 public void addChild(Session childSession) {
79 if(childSession != null) {
80 mChildSessions.add(childSession);
81 }
82 }
83
84 public void removeChild(Session child) {
85 if(child != null) {
86 mChildSessions.remove(child);
87 }
88 }
89
90 public long getExecutionStartTimeMilliseconds() {
91 return mExecutionStartTimeMs;
92 }
93
Brad Ebinger11623a32015-11-25 13:52:02 -080094 public void setExecutionStartTimeMs(long startTimeMs) {
95 mExecutionStartTimeMs = startTimeMs;
96 }
97
Brad Ebinger116b8592015-11-18 11:40:11 -080098 public Session getParentSession() {
99 return mParentSession;
100 }
101
102 public ArrayList<Session> getChildSessions() {
103 return mChildSessions;
104 }
105
106 public boolean isSessionCompleted() {
107 return mIsCompleted;
108 }
109
110 // Mark this session complete. This will be deleted by Log when all subsessions are complete
111 // as well.
112 public void markSessionCompleted(long executionEndTimeMs) {
113 mExecutionEndTimeMs = executionEndTimeMs;
114 mIsCompleted = true;
115 }
116
117 public long getLocalExecutionTime() {
118 if(mExecutionEndTimeMs == UNDEFINED) {
119 return UNDEFINED;
120 }
121 return mExecutionEndTimeMs - mExecutionStartTimeMs;
122 }
123
Brad Ebinger11623a32015-11-25 13:52:02 -0800124 public synchronized String getNextChildId() {
125 return String.valueOf(mChildCounter++);
126 }
127
128 public long getThreadId () {
129 return mThreadId;
Brad Ebinger116b8592015-11-18 11:40:11 -0800130 }
131
132 @Override
133 public boolean equals(Object obj) {
134 if (!(obj instanceof Session)) {
135 return false;
136 }
137 if (obj == this) {
138 return true;
139 }
140 Session otherSession = (Session) obj;
141 return (mSessionId.equals(otherSession.mSessionId)) &&
142 (mShortMethodName.equals(otherSession.mShortMethodName)) &&
143 mExecutionStartTimeMs == otherSession.mExecutionStartTimeMs &&
144 mParentSession == otherSession.mParentSession &&
145 mChildSessions.equals(otherSession.mChildSessions) &&
146 mIsCompleted == otherSession.mIsCompleted &&
Brad Ebinger11623a32015-11-25 13:52:02 -0800147 mExecutionEndTimeMs == otherSession.mExecutionEndTimeMs &&
148 mChildCounter == otherSession.mChildCounter &&
149 mThreadId == otherSession.mThreadId;
Brad Ebinger116b8592015-11-18 11:40:11 -0800150 }
151
152 // Builds full session id recursively
153 private String getFullSessionId() {
154 if(mParentSession == null) {
155 return mSessionId;
156 } else {
157 return mParentSession.getFullSessionId() + "_" + mSessionId;
158 }
159 }
160
Brad Ebinger11623a32015-11-25 13:52:02 -0800161 // Print out the full Session tree from any subsession node
162 public String printFullSessionTree() {
163 // Get to the top of the tree
164 Session topNode = this;
165 while(topNode.getParentSession() != null) {
166 topNode = topNode.getParentSession();
167 }
168 return topNode.printSessionTree();
169 }
170
171 // Recursively move down session tree using DFS, but print out each node when it is reached.
172 public String printSessionTree() {
173 StringBuilder sb = new StringBuilder();
174 printSessionTree(0, sb);
175 return sb.toString();
176 }
177
178 private void printSessionTree(int tabI, StringBuilder sb) {
179 sb.append(toString());
180 for (Session child : mChildSessions) {
181 sb.append("\n");
182 for(int i = 0; i <= tabI; i++) {
183 sb.append("\t");
184 }
185 child.printSessionTree(tabI + 1, sb);
186 }
187 }
188
Brad Ebinger116b8592015-11-18 11:40:11 -0800189 @Override
190 public String toString() {
191 return mShortMethodName + "@" + getFullSessionId();
192 }
193}