aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.monitor/src/com/android/ide/eclipse/monitor/MonitorStartup.java
blob: 2de260ee6a123720bc8505dc0470773ee3e1d85f (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
/*
 * Copyright (C) 2012 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.SdkConstants;
import com.android.sdkstats.SdkStatsService;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.ui.IStartup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class MonitorStartup implements IStartup {
    @Override
    public void earlyStartup() {
        Job pingJob = new Job("Android SDK Ping") {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                SdkStatsService stats = new SdkStatsService();
                File sdkFolder = MonitorPlugin.getDefault().getSdkFolder();
                if (sdkFolder == null) {
                    return Status.OK_STATUS;
                }

                String toolsPath = new File(sdkFolder, SdkConstants.FD_TOOLS).getAbsolutePath();
                ping(stats, toolsPath);
                return Status.OK_STATUS;
            }
        };
        pingJob.setPriority(Job.DECORATE); // lowest priority
        pingJob.schedule();
    }

    private static void ping(SdkStatsService stats, String toolsLocation) {
        Properties p = new Properties();
        try{
            File sourceProp;
            if (toolsLocation != null && toolsLocation.length() > 0) {
                sourceProp = new File(toolsLocation, "source.properties"); //$NON-NLS-1$
            } else {
                sourceProp = new File("source.properties"); //$NON-NLS-1$
            }
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(sourceProp);
                p.load(fis);
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException ignore) {
                    }
                }
            }

            String revision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
            if (revision != null && revision.length() > 0) {
                stats.ping("ddms", revision);  //$NON-NLS-1$
            }
        } catch (FileNotFoundException e) {
            // couldn't find the file? don't ping.
        } catch (IOException e) {
            // couldn't find the file? don't ping.
        }
    }
}