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.ide.eclipse.ddms.IToolsLocator;
21 
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.List;
25 
26 /**
27  * {@link SdkToolsLocator} has two functions: <ul>
28  *  <li> It implements all the methods of {@link IToolsLocator} interface, and
29  *       so can be used as such. </li>
30  *  <li> It provides the {@link #isValidInstallation()} method to check the validity
31  *       of an installation. </li>
32  *  The only reason this class does not explicitly implement the {@link IToolsLocator} interface
33  *  is that it is used very early during the startup to check the validity of the installation.
34  *  Actually implementing that interface causes other bundles to be activated before the
35  *  Eclipse Platform is fully initialized, resulting in startup error.
36  */
37 public class SdkToolsLocator {
38     public static final String PLATFORM_EXECUTABLE_EXTENSION =
39             (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS) ?
40                     ".exe" : ""; //$NON-NLS-1$
41 
42     public static final String PLATFORM_SCRIPT_EXTENSION =
43             (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS) ?
44                     ".bat" : ""; //$NON-NLS-1$
45 
46     public static final String FN_HPROF_CONV = "hprof-conv" + PLATFORM_EXECUTABLE_EXTENSION; //$NON-NLS-1$
47     public static final String FN_TRACEVIEW = "traceview" + PLATFORM_SCRIPT_EXTENSION; //$NON-NLS-1$
48 
49     private final File mSdkFolder;
50 
SdkToolsLocator(File sdkFolder)51     public SdkToolsLocator(File sdkFolder) {
52         mSdkFolder = sdkFolder;
53     }
54 
getAdbLocation()55     public String getAdbLocation() {
56         return new File(getSdkPlatformToolsFolder(), SdkConstants.FN_ADB).getAbsolutePath();
57     }
58 
getTraceViewLocation()59     public String getTraceViewLocation() {
60         return new File(getSdkToolsFolder(), FN_TRACEVIEW).getAbsolutePath();
61     }
62 
getHprofConvLocation()63     public String getHprofConvLocation() {
64         return new File(getSdkPlatformToolsFolder(), FN_HPROF_CONV).getAbsolutePath();
65     }
66 
getSdkToolsFolder()67     private String getSdkToolsFolder() {
68         return new File(mSdkFolder, SdkConstants.FD_TOOLS).getAbsolutePath();
69     }
70 
getSdkPlatformToolsFolder()71     private String getSdkPlatformToolsFolder() {
72         return new File(mSdkFolder, SdkConstants.FD_PLATFORM_TOOLS).getAbsolutePath();
73     }
74 
isValidInstallation()75     public SdkInstallStatus isValidInstallation() {
76         List<String> executables = Arrays.asList(
77                 getTraceViewLocation(),
78                 getHprofConvLocation());
79 
80         for (String exe : executables) {
81             File f = new File(exe);
82             if (!f.exists()) {
83                 return SdkInstallStatus.invalidInstallation(exe + " not present.");
84             }
85             if (!f.canExecute()) {
86                 return SdkInstallStatus.invalidInstallation(exe + " is not executable.");
87             }
88         }
89 
90         return SdkInstallStatus.VALID;
91     }
92 
93     public static class SdkInstallStatus {
94         private boolean mValid;
95         private String mCause;
96 
SdkInstallStatus(boolean valid, String errorMessage)97         private SdkInstallStatus(boolean valid, String errorMessage) {
98             mValid = valid;
99             mCause = errorMessage;
100         }
101 
isValid()102         public boolean isValid() {
103             return mValid;
104         }
105 
getErrorMessage()106         public String getErrorMessage() {
107             return mCause;
108         }
109 
110         public static final SdkInstallStatus VALID = new SdkInstallStatus(true, "");
111 
invalidInstallation(String errorMessage)112         public static SdkInstallStatus invalidInstallation(String errorMessage) {
113             return new SdkInstallStatus(false, errorMessage);
114         }
115     }
116 }
117