aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/DebuggerConnector.java
blob: 233ab2ed9cc01e3f47a855c057d737936aebb29e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt;

import com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController;
import com.android.ide.eclipse.adt.internal.project.ProjectHelper;
import com.android.ide.eclipse.adt.internal.resources.manager.GlobalProjectMonitor;
import com.android.ide.eclipse.adt.internal.resources.manager.GlobalProjectMonitor.IProjectListener;
import com.android.ide.eclipse.ddms.IDebuggerConnector;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Implementation of the com.android.ide.ddms.debuggerConnector extension point.
 */
public class DebuggerConnector implements IDebuggerConnector {
    private WorkspaceAppCache mWorkspaceAppCache;

    public DebuggerConnector() {
        mWorkspaceAppCache = new WorkspaceAppCache();
        GlobalProjectMonitor.getMonitor().addProjectListener(mWorkspaceAppCache);
    }

    @Override
    public boolean connectDebugger(String appName, int appPort, int selectedPort) {
        // search for an android project matching the process name
        IProject project = ProjectHelper.findAndroidProjectByAppName(appName);
        if (project != null) {
            AndroidLaunchController.debugRunningApp(project, appPort);
            return true;
        }

        return false;
    }

    /** {@inheritDoc} */
    @Override
    public boolean isWorkspaceApp(String appName) {
        return mWorkspaceAppCache.isWorkspaceApp(appName);
    }

    /**
     * A cache of Android application name to workspace project name mappings.
     * Users can query whether an application is present in the workspace using
     * {@link #isWorkspaceApp(String)}. The cache listens to workspace changes
     * (project open/close/delete etc), and keeps its internal mappings up-to-date.<br>
     * This class is designed with the following assumptions:
     * <ul>
     *   <li> There are a significant number of calls to {@link #isWorkspaceApp(String)}, with
     *        a large number of possible application names as arguments. </li>
     *   <li> The number of projects actually present in the workspace, and the
     *        number of user initiated project changes (project open/close/delete/rename) are both
     *        relatively small. </li>
     * </ul>
     */
    static class WorkspaceAppCache implements IProjectListener {
        /** Apps known to be not in the workspace. */
        private Set<String> mAppsNotInWorkspace;

        /** Mapping of application name to project name for apps present in the workspace. */
        private Map<String, String> mAppsInWorkspace;

        public WorkspaceAppCache() {
            mAppsNotInWorkspace = new HashSet<String>();
            mAppsInWorkspace = new HashMap<String, String>();
        }

        public boolean isWorkspaceApp(String appName) {
            if (mAppsNotInWorkspace.contains(appName)) {
                return false;
            }

            String projectName = mAppsInWorkspace.get(appName);
            if (projectName == null) {
                IProject p = ProjectHelper.findAndroidProjectByAppName(appName);
                if (p == null) {
                    mAppsNotInWorkspace.add(appName);
                } else {
                    projectName = p.getName();
                    mAppsInWorkspace.put(appName, projectName);
                }
            }
            return projectName != null;
        }

        /** {@inheritDoc} */
        @Override
        public void projectRenamed(IProject project, IPath from) {
            // when a project is renamed, ideally we should just update the current
            // known mapping of app name -> project name. However, the projectRenamed
            // callback is called only after projectDeleted() and projectOpened() are called,
            // so there is really nothing we can do here..
        }

        /** {@inheritDoc} */
        @Override
        public void projectOpenedWithWorkspace(IProject project) {
            // don't do anything as the cache is lazily initialized
        }

        @Override
        public void allProjectsOpenedWithWorkspace() {
            // don't do anything as the cache is lazily initialized
        }

        /** {@inheritDoc} */
        @Override
        public void projectOpened(IProject project) {
            // A newly opened project could contribute some Android application.
            // So we invalidate the set of apps that are known to be not in the workspace, as
            // that set could be out of date now.
            mAppsNotInWorkspace.clear();
        }

        /** {@inheritDoc} */
        @Override
        public void projectDeleted(IProject project) {
            // Deletion is effectively the same as closing
            projectClosed(project);
        }

        /** {@inheritDoc} */
        @Override
        public void projectClosed(IProject project) {
            // When a project is closed, remove all mappings contributed by the project.
            Map<String, String> updatedCache = new HashMap<String, String>();

            String name = project.getName();
            for (Entry<String, String> e : mAppsInWorkspace.entrySet()) {
                if (!e.getValue().equals(name)) {
                    updatedCache.put(e.getKey(), e.getValue());
                }
            }

            mAppsInWorkspace = updatedCache;
        }
    }
}