• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.adt.internal;
18 
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 import com.android.ide.eclipse.adt.AdtPlugin.CheckSdkErrorHandler;
21 import com.android.ide.eclipse.adt.AdtPlugin.CheckSdkErrorHandler.Solution;
22 import com.android.ide.eclipse.adt.Messages;
23 import com.android.sdklib.SdkConstants;
24 import com.android.sdklib.repository.PkgProps;
25 
26 import org.osgi.framework.Constants;
27 import org.osgi.framework.Version;
28 
29 import java.io.BufferedReader;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 
36 /**
37  * Class handling the version check for the plugin vs. the SDK.<br>
38  * The plugin must be able to support all version of the SDK.
39  *
40  * <p/>An SDK can require a new version of the plugin.
41  * <p/>The SDK contains a file with the minimum version for the plugin. This file is inside the
42  * <code>tools/lib</code> directory, and is called <code>plugin.prop</code>.<br>
43  * Inside that text file, there is a line in the format "plugin.version=#.#.#". This is checked
44  * against the current plugin version.<br>
45  *
46  */
47 public final class VersionCheck {
48     /**
49      * The minimum version of the SDK Tools that this version of ADT requires.
50      */
51     private final static int MIN_TOOLS_REV = 17;
52 
53     /**
54      * Pattern to get the minimum plugin version supported by the SDK. This is read from
55      * the file <code>$SDK/tools/lib/plugin.prop</code>.
56      */
57     private final static Pattern sPluginVersionPattern = Pattern.compile(
58             "^plugin.version=(\\d+)\\.(\\d+)\\.(\\d+).*$"); //$NON-NLS-1$
59     private final static Pattern sSourcePropPattern = Pattern.compile(
60             "^" + PkgProps.PKG_REVISION + "=(\\d+).*$"); //$NON-NLS-1$
61 
62     /**
63      * Checks the plugin and the SDK have compatible versions.
64      * @param osSdkPath The path to the SDK
65      * @return true if compatible.
66      */
checkVersion(String osSdkPath, CheckSdkErrorHandler errorHandler)67     public static boolean checkVersion(String osSdkPath, CheckSdkErrorHandler errorHandler) {
68         AdtPlugin plugin = AdtPlugin.getDefault();
69         String osLibs = osSdkPath + SdkConstants.OS_SDK_TOOLS_LIB_FOLDER;
70 
71         // get the plugin property file, and grab the minimum plugin version required
72         // to work with the sdk
73         int minMajorVersion = -1;
74         int minMinorVersion = -1;
75         int minMicroVersion = -1;
76         FileReader reader = null;
77         try {
78             reader = new FileReader(osLibs + SdkConstants.FN_PLUGIN_PROP);
79             BufferedReader bReader = new BufferedReader(reader);
80             String line;
81             while ((line = bReader.readLine()) != null) {
82                 Matcher m = sPluginVersionPattern.matcher(line);
83                 if (m.matches()) {
84                     minMajorVersion = Integer.parseInt(m.group(1));
85                     minMinorVersion = Integer.parseInt(m.group(2));
86                     minMicroVersion = Integer.parseInt(m.group(3));
87                     break;
88                 }
89             }
90         } catch (FileNotFoundException e) {
91             // the build id will be null, and this is handled by the builders.
92         } catch (IOException e) {
93             // the build id will be null, and this is handled by the builders.
94         } finally {
95             if (reader != null) {
96                 try {
97                     reader.close();
98                 } catch (IOException e) {
99                 } finally {
100                     reader = null;
101                 }
102             }
103         }
104 
105         // Failed to get the min plugin version number?
106         if (minMajorVersion == -1 || minMinorVersion == -1 || minMicroVersion ==-1) {
107             return errorHandler.handleWarning(
108                     Solution.OPEN_SDK_MANAGER,
109                     Messages.VersionCheck_Plugin_Version_Failed);
110         }
111 
112         // test the plugin number
113         String versionString = (String) plugin.getBundle().getHeaders().get(
114                 Constants.BUNDLE_VERSION);
115         Version version = new Version(versionString);
116 
117         boolean valid = true;
118         if (version.getMajor() < minMajorVersion) {
119             valid = false;
120         } else if (version.getMajor() == minMajorVersion) {
121             if (version.getMinor() < minMinorVersion) {
122                 valid = false;
123             } else if (version.getMinor() == minMinorVersion) {
124                 if (version.getMicro() < minMicroVersion) {
125                     valid = false;
126                 }
127             }
128         }
129 
130         if (valid == false) {
131             return errorHandler.handleError(
132                     Solution.OPEN_P2_UPDATE,
133                     String.format(Messages.VersionCheck_Plugin_Too_Old,
134                             minMajorVersion, minMinorVersion, minMicroVersion, versionString));
135         }
136 
137         // now check whether the tools are new enough.
138         String osTools = osSdkPath + SdkConstants.OS_SDK_TOOLS_FOLDER;
139         int toolsRevision = Integer.MAX_VALUE;
140         try {
141             reader = new FileReader(osTools + SdkConstants.FN_SOURCE_PROP);
142             BufferedReader bReader = new BufferedReader(reader);
143             String line;
144             while ((line = bReader.readLine()) != null) {
145                 Matcher m = sSourcePropPattern.matcher(line);
146                 if (m.matches()) {
147                     toolsRevision = Integer.parseInt(m.group(1));
148                     break;
149                 }
150             }
151         } catch (FileNotFoundException e) {
152             // the build id will be null, and this is handled by the builders.
153         } catch (IOException e) {
154             // the build id will be null, and this is handled by the builders.
155         } finally {
156             if (reader != null) {
157                 try {
158                     reader.close();
159                 } catch (IOException e) {
160                 } finally {
161                     reader = null;
162                 }
163             }
164         }
165 
166         if (toolsRevision < MIN_TOOLS_REV) {
167             // this is a warning only as we need to parse the SDK to allow updating
168             // of the tools!
169             return errorHandler.handleWarning(
170                     Solution.OPEN_SDK_MANAGER,
171                     String.format(Messages.VersionCheck_Tools_Too_Old,
172                             MIN_TOOLS_REV, toolsRevision));
173         }
174 
175         return true; // no error!
176     }
177 }
178