1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.monitor;
18 
19 import com.android.SdkConstants;
20 import com.android.sdkstats.SdkStatsService;
21 
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.ui.IStartup;
27 
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.util.Properties;
33 
34 public class MonitorStartup implements IStartup {
35     @Override
earlyStartup()36     public void earlyStartup() {
37         Job pingJob = new Job("Android SDK Ping") {
38             @Override
39             protected IStatus run(IProgressMonitor monitor) {
40                 SdkStatsService stats = new SdkStatsService();
41                 File sdkFolder = MonitorPlugin.getDefault().getSdkFolder();
42                 if (sdkFolder == null) {
43                     return Status.OK_STATUS;
44                 }
45 
46                 String toolsPath = new File(sdkFolder, SdkConstants.FD_TOOLS).getAbsolutePath();
47                 ping(stats, toolsPath);
48                 return Status.OK_STATUS;
49             }
50         };
51         pingJob.setPriority(Job.DECORATE); // lowest priority
52         pingJob.schedule();
53     }
54 
ping(SdkStatsService stats, String toolsLocation)55     private static void ping(SdkStatsService stats, String toolsLocation) {
56         Properties p = new Properties();
57         try{
58             File sourceProp;
59             if (toolsLocation != null && toolsLocation.length() > 0) {
60                 sourceProp = new File(toolsLocation, "source.properties"); //$NON-NLS-1$
61             } else {
62                 sourceProp = new File("source.properties"); //$NON-NLS-1$
63             }
64             FileInputStream fis = null;
65             try {
66                 fis = new FileInputStream(sourceProp);
67                 p.load(fis);
68             } finally {
69                 if (fis != null) {
70                     try {
71                         fis.close();
72                     } catch (IOException ignore) {
73                     }
74                 }
75             }
76 
77             String revision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
78             if (revision != null && revision.length() > 0) {
79                 stats.ping("ddms", revision);  //$NON-NLS-1$
80             }
81         } catch (FileNotFoundException e) {
82             // couldn't find the file? don't ping.
83         } catch (IOException e) {
84             // couldn't find the file? don't ping.
85         }
86     }
87 }
88