Skip to content

Commit

Permalink
Add support for starting external Sessions
Browse files Browse the repository at this point in the history
This change adds support for parceling Session Information to be
transmitted over AIDLs and starting an external session in the external
process.

Test: Telecom Unit tests and new SessionManager Tests.
Bug: 26571395
Change-Id: Ie27ed88d4a8c344b34568025ddcf150fdcb38d62
  • Loading branch information
Brad Ebinger committed Oct 25, 2016
1 parent 836ffa6 commit a0dc976
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 38 deletions.
9 changes: 9 additions & 0 deletions telecomm/java/android/telecom/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ public static void startSession(String shortMethodName, String callerIdentificat
getSessionManager().startSession(shortMethodName, callerIdentification);
}

public static void startSession(Session.Info info, String shortMethodName,
String callerIdentification) {
getSessionManager().startSession(info, shortMethodName, callerIdentification);
}

public static void startExternalSession(Session.Info sessionInfo, String shortMethodName) {
getSessionManager().startExternalSession(sessionInfo, shortMethodName);
}

public static Session createSubsession() {
return getSessionManager().createSubsession();
}
Expand Down
108 changes: 106 additions & 2 deletions telecomm/java/android/telecom/Logging/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package android.telecom.Logging;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayList;

Expand All @@ -33,19 +38,66 @@ public class Session {
public static final String END_SUBSESSION = "END_SUBSESSION";
public static final String END_SESSION = "END_SESSION";

public static final String SUBSESSION_SEPARATION_CHAR = "->";
public static final String EXTERNAL_INDICATOR = "E-";

/**
* Initial value of mExecutionEndTimeMs and the final value of {@link #getLocalExecutionTime()}
* if the Session is canceled.
*/
public static final int UNDEFINED = -1;

public static class Info implements Parcelable {
public final String sessionId;
public final String shortMethodName;

private Info(String id, String methodName) {
sessionId = id;
shortMethodName = methodName;
}

public static Info getInfo (Session s) {
return new Info(s.getFullSessionId(), s.getShortMethodName());
}

/** Responsible for creating Info objects for deserialized Parcels. */
public static final Parcelable.Creator<Info> CREATOR =
new Parcelable.Creator<Info> () {
@Override
public Info createFromParcel(Parcel source) {
String id = source.readString();
String methodName = source.readString();
return new Info(id, methodName);
}

@Override
public Info[] newArray(int size) {
return new Info[size];
}
};

/** {@inheritDoc} */
@Override
public int describeContents() {
return 0;
}

/** Writes Info object into a Parcel. */
@Override
public void writeToParcel(Parcel destination, int flags) {
destination.writeString(sessionId);
destination.writeString(shortMethodName);
}
}

private String mSessionId;
private String mShortMethodName;
private long mExecutionStartTimeMs;
private long mExecutionEndTimeMs = UNDEFINED;
private Session mParentSession;
private ArrayList<Session> mChildSessions;
private boolean mIsCompleted = false;
private boolean mIsExternal = false;
private int mChildCounter = 0;
// True if this is a subsession that has been started from the same thread as the parent
// session. This can happen if Log.startSession(...) is called multiple times on the same
Expand All @@ -56,8 +108,11 @@ public class Session {
// Optionally provided info about the method/class/component that started the session in order
// to make Logging easier. This info will be provided in parentheses along with the session.
private String mOwnerInfo;
// Cache Full Method path so that recursive population of the full method path only needs to
// be calculated once.
private String mFullMethodPathCache;

public Session(String sessionId, String shortMethodName, long startTimeMs, long threadID,
public Session(String sessionId, String shortMethodName, long startTimeMs,
boolean isStartedFromActiveSession, String ownerInfo) {
setSessionId(sessionId);
setShortMethodName(shortMethodName);
Expand Down Expand Up @@ -86,6 +141,14 @@ public void setShortMethodName(String shortMethodName) {
mShortMethodName = shortMethodName;
}

public void setIsExternal(boolean isExternal) {
mIsExternal = isExternal;
}

public boolean isExternal() {
return mIsExternal;
}

public void setParentSession(Session parentSession) {
mParentSession = parentSession;
}
Expand Down Expand Up @@ -126,6 +189,15 @@ public boolean isStartedFromActiveSession() {
return mIsStartedFromActiveSession;
}

public Info getInfo() {
return Info.getInfo(this);
}

@VisibleForTesting
public String getSessionId() {
return mSessionId;
}

// Mark this session complete. This will be deleted by Log when all subsessions are complete
// as well.
public void markSessionCompleted(long executionEndTimeMs) {
Expand Down Expand Up @@ -186,6 +258,38 @@ private void printSessionTree(int tabI, StringBuilder sb) {
}
}

// Recursively concatenate mShortMethodName with the parent Sessions to create full method
// path. Caches this string so that multiple calls for the path will be quick.
public String getFullMethodPath() {
StringBuilder sb = new StringBuilder();
getFullMethodPath(sb);
return sb.toString();
}

private synchronized void getFullMethodPath(StringBuilder sb) {
// Don't calculate if we have already figured it out!
if (!TextUtils.isEmpty(mFullMethodPathCache)) {
sb.append(mFullMethodPathCache);
return;
}
Session parentSession = getParentSession();
boolean isSessionStarted = false;
if (parentSession != null) {
// Check to see if the session has been renamed yet. If it has not, then the session
// has not been continued.
isSessionStarted = !mShortMethodName.equals(parentSession.mShortMethodName);
parentSession.getFullMethodPath(sb);
sb.append(SUBSESSION_SEPARATION_CHAR);
}
sb.append(mShortMethodName);

if(isSessionStarted) {
// Cache this value so that we do not have to do this work next time!
// We do not cache the value if the session being evaluated hasn't been continued yet.
mFullMethodPathCache = sb.toString();
}
}

@Override
public int hashCode() {
int result = mSessionId != null ? mSessionId.hashCode() : 0;
Expand Down Expand Up @@ -238,7 +342,7 @@ public String toString() {
return mParentSession.toString();
} else {
StringBuilder methodName = new StringBuilder();
methodName.append(mShortMethodName);
methodName.append(getFullMethodPath());
if (mOwnerInfo != null && !mOwnerInfo.isEmpty()) {
methodName.append("(InCall package: ");
methodName.append(mOwnerInfo);
Expand Down
Loading

0 comments on commit a0dc976

Please sign in to comment.