aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/MonitorApplication.java
blob: c2602401a6dcc040ecf8ffe5b21641516c46f647 (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
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.monitor;

import com.android.ide.eclipse.monitor.SdkToolsLocator.SdkInstallStatus;
import com.android.prefs.AndroidLocation;
import com.android.sdklib.SdkManager;
import com.android.sdkstats.SdkStatsService;
import com.android.sdkuilib.internal.repository.ui.AdtUpdateDialog;
import com.android.utils.ILogger;
import com.android.utils.NullLogger;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import java.io.File;

public class MonitorApplication implements IApplication {
    private static final String SDK_PATH_ENVVAR = "com.android.sdk.path";
    private static final String MONITOR_WORKSPACE_PATH = "monitor-workspace";

    @Override
    public Object start(IApplicationContext context) throws Exception {
        Display display = PlatformUI.createDisplay();

        // set workspace location
        Location instanceLoc = Platform.getInstanceLocation();
        IPath workspacePath = new Path(AndroidLocation.getFolder()).append(MONITOR_WORKSPACE_PATH);
        instanceLoc.set(workspacePath.toFile().toURI().toURL(), true);

        // figure out path to SDK
        String sdkPath = findSdkPath(display);
        if (!isValidSdkLocation(sdkPath)) {
            // exit with return code -1
            return Integer.valueOf(-1);
        }
        MonitorPlugin.getDefault().setSdkPath(sdkPath);

        // install platform tools if necessary
        ILogger sdkLog = NullLogger.getLogger();
        SdkManager manager = SdkManager.createManager(sdkPath, sdkLog);
        if (manager.getPlatformToolsVersion() == null) {
            AdtUpdateDialog window = new AdtUpdateDialog(new Shell(display), sdkLog, sdkPath);
            window.installPlatformTools();
        }

        // If this is the first time using ddms or adt, open up the stats service
        // opt out dialog, and request user for permissions.
        // Note that the actual ping is performed in MonitorStartup
        SdkStatsService stats = new SdkStatsService();
        stats.checkUserPermissionForPing(new Shell(display));

        // open up RCP
        try {
            int returnCode = PlatformUI.createAndRunWorkbench(display,
                    new MonitorWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART) {
                return IApplication.EXIT_RESTART;
            }
            return IApplication.EXIT_OK;
        } finally {
            display.dispose();
        }
    }

    @Override
    public void stop() {
        if (!PlatformUI.isWorkbenchRunning())
            return;
        final IWorkbench workbench = PlatformUI.getWorkbench();
        final Display display = workbench.getDisplay();
        display.syncExec(new Runnable() {
            @Override
            public void run() {
                if (!display.isDisposed())
                    workbench.close();
            }
        });
    }

    private String findSdkPath(Display display) {
        // see if there is a system property set (passed in via a command line arg)
        String sdkLocation = System.getProperty(SDK_PATH_ENVVAR);
        if (isValidSdkLocation(sdkLocation)) {
            return sdkLocation;
        }

        // see if there is an environment variable set
        sdkLocation = System.getenv(SDK_PATH_ENVVAR);
        if (isValidSdkLocation(sdkLocation)) {
            return sdkLocation;
        }

        // check for the last used SDK
        sdkLocation = MonitorPlugin.getDdmsPreferenceStore().getLastSdkPath();
        if (isValidSdkLocation(sdkLocation)) {
            return sdkLocation;
        }

        // The monitor app should be located in "<sdk>/tools/monitor/"
        // So see if the folder one level up from the install location is a valid SDK.
        Location install = Platform.getInstallLocation();
        if (install != null && install.getURL() != null) {
            String toolsFolder = new File(install.getURL().getFile()).getParent();
            if (toolsFolder != null) {
                sdkLocation = new File(toolsFolder).getParent();
                if (isValidSdkLocation(sdkLocation)) {
                    MonitorPlugin.getDdmsPreferenceStore().setLastSdkPath(sdkLocation);
                    return sdkLocation;
                }
            }
        }

        // if nothing else works, prompt the user
        sdkLocation = getSdkLocationFromUser(new Shell(display));
        if (isValidSdkLocation(sdkLocation)) {
            MonitorPlugin.getDdmsPreferenceStore().setLastSdkPath(sdkLocation);
        }

        return sdkLocation;
    }

    private boolean isValidSdkLocation(String sdkLocation) {
        if (sdkLocation == null) {
            return false;
        }

        if (sdkLocation.trim().length() == 0) {
            return false;
        }

        SdkToolsLocator locator = new SdkToolsLocator(sdkLocation);
        return locator.isValidInstallation() == SdkInstallStatus.VALID;
    }

    private String getSdkLocationFromUser(Shell shell) {
        SdkLocationChooserDialog dlg = new SdkLocationChooserDialog(shell);
        if (dlg.open() == Window.OK) {
            return dlg.getPath();
        } else {
            return null;
        }
    }
}