{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/stetho/pulls/comments/29014859","id":29014859,"diff_hunk":"@@ -0,0 +1,209 @@\n+/*\n+ * Copyright (c) 2014-present, Facebook, Inc.\n+ * All rights reserved.\n+ *\n+ * This source code is licensed under the BSD-style license found in the\n+ * LICENSE file in the root directory of this source tree. An additional grant\n+ * of patent rights can be found in the PATENTS file in the same directory.\n+ */\n+\n+package com.facebook.stetho.inspector.elements.android;\n+\n+import android.annotation.TargetApi;\n+import android.app.Activity;\n+import android.app.Application;\n+import android.database.Observable;\n+import android.os.Build;\n+import android.os.Bundle;\n+import android.os.Looper;\n+import com.facebook.stetho.common.Util;\n+\n+import javax.annotation.Nullable;\n+import javax.annotation.concurrent.GuardedBy;\n+import javax.annotation.concurrent.NotThreadSafe;\n+import java.lang.ref.WeakReference;\n+import java.util.ArrayList;\n+import java.util.Collections;\n+import java.util.Iterator;\n+import java.util.List;\n+\n+/**\n+ * Tracks which {@link Activity} instances have been created and not yet destroyed in creation\n+ * order for use by Stetho features.  Note that automatic tracking is not available for\n+ * all versions of Android but it is possible to manually track activities using the {@link #add}\n+ * and {@link #remove} methods exposed below.  Be aware that this is an easy opportunity to\n+ * cause serious memory leaks in your application however.  Use with caution.\n+ * <p/>\n+ * Most callers can and should ignore this class, though it is necessary if you are implementing\n+ * Activity tracking pre-ICS.\n+ */\n+@NotThreadSafe\n+public class ActivityTracker {\n+  private static final ActivityTracker sInstance = new ActivityTracker();\n+\n+  @GuardedBy(\"Looper.getMainLooper()\")\n+  private final ArrayList<Activity> mActivities = new ArrayList<>();\n+  private final List<Activity> mActivitiesUnmodifiable = Collections.unmodifiableList(mActivities);\n+\n+  @GuardedBy(\"ActivityTrackerObservable.mObservers\")\n+  private final ActivityTrackerObservable mObservable = new ActivityTrackerObservable();\n+\n+  @Nullable\n+  private AutomaticTracker mAutomaticTracker;\n+\n+  public static ActivityTracker get() {\n+    return sInstance;\n+  }\n+\n+  public void registerListener(Listener listener) {\n+    mObservable.registerObserver(listener);\n+  }\n+\n+  public void unregisterListener(Listener listener) {\n+    mObservable.unregisterObserver(listener);\n+  }\n+\n+  /**\n+   * Start automatic tracking if we are running on ICS+.\n+   *\n+   * @return Automatic tracking has been started.  No need to manually invoke {@link #add} or\n+   *     {@link #remove} methods.\n+   */\n+  public boolean beginTrackingIfPossible(Application application) {\n+    if (mAutomaticTracker == null) {\n+      AutomaticTracker automaticTracker =\n+          AutomaticTracker.newInstance(application, this /* tracker */);\n+      if (automaticTracker != null) {\n+        automaticTracker.register();\n+        mAutomaticTracker = automaticTracker;\n+        return true;\n+      }\n+    }\n+    return false;","path":"stetho/src/main/java/com/facebook/stetho/inspector/elements/android/ActivityTracker.java","position":82,"original_position":82,"commit_id":"71f007507f35e621a29a7450a2ce8caf77d3d1ac","original_commit_id":"71f007507f35e621a29a7450a2ce8caf77d3d1ac","user":{"login":"jasta","id":83791,"avatar_url":"https://avatars.githubusercontent.com/u/83791?v=3","gravatar_id":"","url":"https://api.github.com/users/jasta","html_url":"https://github.com/jasta","followers_url":"https://api.github.com/users/jasta/followers","following_url":"https://api.github.com/users/jasta/following{/other_user}","gists_url":"https://api.github.com/users/jasta/gists{/gist_id}","starred_url":"https://api.github.com/users/jasta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasta/subscriptions","organizations_url":"https://api.github.com/users/jasta/orgs","repos_url":"https://api.github.com/users/jasta/repos","events_url":"https://api.github.com/users/jasta/events{/privacy}","received_events_url":"https://api.github.com/users/jasta/received_events","type":"User","site_admin":false},"body":"Prefer the caller doing this per your previous suggestion.","created_at":"2015-04-24T00:04:10Z","updated_at":"2015-04-24T00:04:10Z","html_url":"https://github.com/facebook/stetho/pull/159#discussion_r29014859","pull_request_url":"https://api.github.com/repos/facebook/stetho/pulls/159","_links":{"self":{"href":"https://api.github.com/repos/facebook/stetho/pulls/comments/29014859"},"html":{"href":"https://github.com/facebook/stetho/pull/159#discussion_r29014859"},"pull_request":{"href":"https://api.github.com/repos/facebook/stetho/pulls/159"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/stetho/pulls/159","id":33125128,"html_url":"https://github.com/facebook/stetho/pull/159","diff_url":"https://github.com/facebook/stetho/pull/159.diff","patch_url":"https://github.com/facebook/stetho/pull/159.patch","issue_url":"https://api.github.com/repos/facebook/stetho/issues/159","number":159,"state":"open","locked":false,"title":"Improve support pre-JB for Activity detection","user":{"login":"jasta","id":83791,"avatar_url":"https://avatars.githubusercontent.com/u/83791?v=3","gravatar_id":"","url":"https://api.github.com/users/jasta","html_url":"https://github.com/jasta","followers_url":"https://api.github.com/users/jasta/followers","following_url":"https://api.github.com/users/jasta/following{/other_user}","gists_url":"https://api.github.com/users/jasta/gists{/gist_id}","starred_url":"https://api.github.com/users/jasta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasta/subscriptions","organizations_url":"https://api.github.com/users/jasta/orgs","repos_url":"https://api.github.com/users/jasta/repos","events_url":"https://api.github.com/users/jasta/events{/privacy}","received_events_url":"https://api.github.com/users/jasta/received_events","type":"User","site_admin":false},"body":"This uses an older API (available to ICS+) to track activities instead\r\nof relying on an ugly reflection trick known to work only on JB+ (and of\r\ncourse not future proof).  There's a slight overhead increase in\r\nStetho.initialize as a result, but the timings added indicate it is\r\nstill very small (1-2ms on my Nexus 5).\r\n\r\nThe mechanism for tracking activities is partially exposed via\r\nActivityTracker which has a manual add/remove method which allows\r\ndevelopers to add support for pre-ICS even by hooking their own\r\napplication onCreate/onDestroy events just like HierarchyViewer's\r\nViewServer used in the past.\r\n\r\nCloses #123","created_at":"2015-04-12T19:39:37Z","updated_at":"2015-04-24T00:04:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"072d7701d223286a2421d093d5811a5c748695e7","assignee":{"login":"rickbrew","id":10873410,"avatar_url":"https://avatars.githubusercontent.com/u/10873410?v=3","gravatar_id":"","url":"https://api.github.com/users/rickbrew","html_url":"https://github.com/rickbrew","followers_url":"https://api.github.com/users/rickbrew/followers","following_url":"https://api.github.com/users/rickbrew/following{/other_user}","gists_url":"https://api.github.com/users/rickbrew/gists{/gist_id}","starred_url":"https://api.github.com/users/rickbrew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rickbrew/subscriptions","organizations_url":"https://api.github.com/users/rickbrew/orgs","repos_url":"https://api.github.com/users/rickbrew/repos","events_url":"https://api.github.com/users/rickbrew/events{/privacy}","received_events_url":"https://api.github.com/users/rickbrew/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/facebook/stetho/pulls/159/commits","review_comments_url":"https://api.github.com/repos/facebook/stetho/pulls/159/comments","review_comment_url":"https://api.github.com/repos/facebook/stetho/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/stetho/issues/159/comments","statuses_url":"https://api.github.com/repos/facebook/stetho/statuses/71f007507f35e621a29a7450a2ce8caf77d3d1ac","head":{"label":"jasta:use-lifecycle-cbs","ref":"use-lifecycle-cbs","sha":"71f007507f35e621a29a7450a2ce8caf77d3d1ac","user":{"login":"jasta","id":83791,"avatar_url":"https://avatars.githubusercontent.com/u/83791?v=3","gravatar_id":"","url":"https://api.github.com/users/jasta","html_url":"https://github.com/jasta","followers_url":"https://api.github.com/users/jasta/followers","following_url":"https://api.github.com/users/jasta/following{/other_user}","gists_url":"https://api.github.com/users/jasta/gists{/gist_id}","starred_url":"https://api.github.com/users/jasta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasta/subscriptions","organizations_url":"https://api.github.com/users/jasta/orgs","repos_url":"https://api.github.com/users/jasta/repos","events_url":"https://api.github.com/users/jasta/events{/privacy}","received_events_url":"https://api.github.com/users/jasta/received_events","type":"User","site_admin":false},"repo":{"id":30987450,"name":"stetho","full_name":"jasta/stetho","owner":{"login":"jasta","id":83791,"avatar_url":"https://avatars.githubusercontent.com/u/83791?v=3","gravatar_id":"","url":"https://api.github.com/users/jasta","html_url":"https://github.com/jasta","followers_url":"https://api.github.com/users/jasta/followers","following_url":"https://api.github.com/users/jasta/following{/other_user}","gists_url":"https://api.github.com/users/jasta/gists{/gist_id}","starred_url":"https://api.github.com/users/jasta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasta/subscriptions","organizations_url":"https://api.github.com/users/jasta/orgs","repos_url":"https://api.github.com/users/jasta/repos","events_url":"https://api.github.com/users/jasta/events{/privacy}","received_events_url":"https://api.github.com/users/jasta/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jasta/stetho","description":"Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.","fork":true,"url":"https://api.github.com/repos/jasta/stetho","forks_url":"https://api.github.com/repos/jasta/stetho/forks","keys_url":"https://api.github.com/repos/jasta/stetho/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jasta/stetho/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jasta/stetho/teams","hooks_url":"https://api.github.com/repos/jasta/stetho/hooks","issue_events_url":"https://api.github.com/repos/jasta/stetho/issues/events{/number}","events_url":"https://api.github.com/repos/jasta/stetho/events","assignees_url":"https://api.github.com/repos/jasta/stetho/assignees{/user}","branches_url":"https://api.github.com/repos/jasta/stetho/branches{/branch}","tags_url":"https://api.github.com/repos/jasta/stetho/tags","blobs_url":"https://api.github.com/repos/jasta/stetho/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jasta/stetho/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jasta/stetho/git/refs{/sha}","trees_url":"https://api.github.com/repos/jasta/stetho/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jasta/stetho/statuses/{sha}","languages_url":"https://api.github.com/repos/jasta/stetho/languages","stargazers_url":"https://api.github.com/repos/jasta/stetho/stargazers","contributors_url":"https://api.github.com/repos/jasta/stetho/contributors","subscribers_url":"https://api.github.com/repos/jasta/stetho/subscribers","subscription_url":"https://api.github.com/repos/jasta/stetho/subscription","commits_url":"https://api.github.com/repos/jasta/stetho/commits{/sha}","git_commits_url":"https://api.github.com/repos/jasta/stetho/git/commits{/sha}","comments_url":"https://api.github.com/repos/jasta/stetho/comments{/number}","issue_comment_url":"https://api.github.com/repos/jasta/stetho/issues/comments{/number}","contents_url":"https://api.github.com/repos/jasta/stetho/contents/{+path}","compare_url":"https://api.github.com/repos/jasta/stetho/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jasta/stetho/merges","archive_url":"https://api.github.com/repos/jasta/stetho/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jasta/stetho/downloads","issues_url":"https://api.github.com/repos/jasta/stetho/issues{/number}","pulls_url":"https://api.github.com/repos/jasta/stetho/pulls{/number}","milestones_url":"https://api.github.com/repos/jasta/stetho/milestones{/number}","notifications_url":"https://api.github.com/repos/jasta/stetho/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jasta/stetho/labels{/name}","releases_url":"https://api.github.com/repos/jasta/stetho/releases{/id}","created_at":"2015-02-18T21:17:34Z","updated_at":"2015-03-28T21:14:57Z","pushed_at":"2015-04-16T16:42:05Z","git_url":"git://github.com/jasta/stetho.git","ssh_url":"git@github.com:jasta/stetho.git","clone_url":"https://github.com/jasta/stetho.git","svn_url":"https://github.com/jasta/stetho","homepage":"http://facebook.github.io/stetho/","size":1263,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"facebook:master","ref":"master","sha":"f408e5bef4f0df5f8079f89fd032ed8551265a99","user":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"repo":{"id":29703871,"name":"stetho","full_name":"facebook/stetho","owner":{"login":"facebook","id":69631,"avatar_url":"https://avatars.githubusercontent.com/u/69631?v=3","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/facebook/stetho","description":"Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.","fork":false,"url":"https://api.github.com/repos/facebook/stetho","forks_url":"https://api.github.com/repos/facebook/stetho/forks","keys_url":"https://api.github.com/repos/facebook/stetho/keys{/key_id}","collaborators_url":"https://api.github.com/repos/facebook/stetho/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/facebook/stetho/teams","hooks_url":"https://api.github.com/repos/facebook/stetho/hooks","issue_events_url":"https://api.github.com/repos/facebook/stetho/issues/events{/number}","events_url":"https://api.github.com/repos/facebook/stetho/events","assignees_url":"https://api.github.com/repos/facebook/stetho/assignees{/user}","branches_url":"https://api.github.com/repos/facebook/stetho/branches{/branch}","tags_url":"https://api.github.com/repos/facebook/stetho/tags","blobs_url":"https://api.github.com/repos/facebook/stetho/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/facebook/stetho/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/facebook/stetho/git/refs{/sha}","trees_url":"https://api.github.com/repos/facebook/stetho/git/trees{/sha}","statuses_url":"https://api.github.com/repos/facebook/stetho/statuses/{sha}","languages_url":"https://api.github.com/repos/facebook/stetho/languages","stargazers_url":"https://api.github.com/repos/facebook/stetho/stargazers","contributors_url":"https://api.github.com/repos/facebook/stetho/contributors","subscribers_url":"https://api.github.com/repos/facebook/stetho/subscribers","subscription_url":"https://api.github.com/repos/facebook/stetho/subscription","commits_url":"https://api.github.com/repos/facebook/stetho/commits{/sha}","git_commits_url":"https://api.github.com/repos/facebook/stetho/git/commits{/sha}","comments_url":"https://api.github.com/repos/facebook/stetho/comments{/number}","issue_comment_url":"https://api.github.com/repos/facebook/stetho/issues/comments{/number}","contents_url":"https://api.github.com/repos/facebook/stetho/contents/{+path}","compare_url":"https://api.github.com/repos/facebook/stetho/compare/{base}...{head}","merges_url":"https://api.github.com/repos/facebook/stetho/merges","archive_url":"https://api.github.com/repos/facebook/stetho/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/facebook/stetho/downloads","issues_url":"https://api.github.com/repos/facebook/stetho/issues{/number}","pulls_url":"https://api.github.com/repos/facebook/stetho/pulls{/number}","milestones_url":"https://api.github.com/repos/facebook/stetho/milestones{/number}","notifications_url":"https://api.github.com/repos/facebook/stetho/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/facebook/stetho/labels{/name}","releases_url":"https://api.github.com/repos/facebook/stetho/releases{/id}","created_at":"2015-01-22T22:34:10Z","updated_at":"2015-04-23T16:27:10Z","pushed_at":"2015-04-22T23:55:36Z","git_url":"git://github.com/facebook/stetho.git","ssh_url":"git@github.com:facebook/stetho.git","clone_url":"https://github.com/facebook/stetho.git","svn_url":"https://github.com/facebook/stetho","homepage":"http://facebook.github.io/stetho/","size":3457,"stargazers_count":2477,"watchers_count":2477,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":172,"mirror_url":null,"open_issues_count":26,"forks":172,"open_issues":26,"watchers":2477,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/stetho/pulls/159"},"html":{"href":"https://github.com/facebook/stetho/pull/159"},"issue":{"href":"https://api.github.com/repos/facebook/stetho/issues/159"},"comments":{"href":"https://api.github.com/repos/facebook/stetho/issues/159/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/stetho/pulls/159/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/stetho/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/stetho/pulls/159/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/stetho/statuses/71f007507f35e621a29a7450a2ce8caf77d3d1ac"}}}}