• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.preferences;
18 
19 import com.android.ide.eclipse.adt.AdtPlugin;
20 import com.android.prefs.AndroidLocation.AndroidLocationException;
21 import com.android.sdklib.internal.build.DebugKeyProvider;
22 import com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException;
23 
24 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
25 import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 
28 import java.io.File;
29 
30 @SuppressWarnings("deprecation")
31 public final class AdtPrefs extends AbstractPreferenceInitializer {
32     public final static String PREFS_SDK_DIR = AdtPlugin.PLUGIN_ID + ".sdk"; //$NON-NLS-1$
33 
34     public final static String PREFS_BUILD_RES_AUTO_REFRESH = AdtPlugin.PLUGIN_ID + ".resAutoRefresh"; //$NON-NLS-1$
35 
36     public final static String PREFS_BUILD_FORCE_ERROR_ON_NATIVELIB_IN_JAR = AdtPlugin.PLUGIN_ID + ".forceErrorNativeLibInJar"; //$NON-NLS-1$
37 
38     public final static String PREFS_BUILD_VERBOSITY = AdtPlugin.PLUGIN_ID + ".buildVerbosity"; //$NON-NLS-1$
39 
40     public final static String PREFS_DEFAULT_DEBUG_KEYSTORE = AdtPlugin.PLUGIN_ID + ".defaultDebugKeyStore"; //$NON-NLS-1$
41 
42     public final static String PREFS_CUSTOM_DEBUG_KEYSTORE = AdtPlugin.PLUGIN_ID + ".customDebugKeyStore"; //$NON-NLS-1$
43 
44     public final static String PREFS_HOME_PACKAGE = AdtPlugin.PLUGIN_ID + ".homePackage"; //$NON-NLS-1$
45 
46     public final static String PREFS_EMU_OPTIONS = AdtPlugin.PLUGIN_ID + ".emuOptions"; //$NON-NLS-1$
47 
48     /** singleton instance */
49     private final static AdtPrefs sThis = new AdtPrefs();
50 
51     /** default store, provided by eclipse */
52     private IPreferenceStore mStore;
53 
54     /** cached location for the sdk folder */
55     private String mOsSdkLocation;
56 
57     /** Verbosity of the build */
58     private BuildVerbosity mBuildVerbosity = BuildVerbosity.NORMAL;
59 
60     private boolean mBuildForceResResfresh = false;
61     private boolean mBuildForceErrorOnNativeLibInJar = true;
62 
63     public static enum BuildVerbosity {
64         /** Build verbosity "Always". Those messages are always displayed, even in silent mode */
65         ALWAYS(0),
66         /** Build verbosity level "Normal" */
67         NORMAL(1),
68         /** Build verbosity level "Verbose". Those messages are only displayed in verbose mode */
69         VERBOSE(2);
70 
71         private int mLevel;
72 
BuildVerbosity(int level)73         BuildVerbosity(int level) {
74             mLevel = level;
75         }
76 
getLevel()77         public int getLevel() {
78             return mLevel;
79         }
80 
81         /**
82          * Finds and returns a {@link BuildVerbosity} whose {@link #name()} matches a given name.
83          * <p/>This is different from {@link Enum#valueOf(Class, String)} in that it returns null
84          * if no matches are found.
85          *
86          * @param name the name to look up.
87          * @return returns the matching enum or null of no match where found.
88          */
find(String name)89         public static BuildVerbosity find(String name) {
90             for (BuildVerbosity v : values()) {
91                 if (v.name().equals(name)) {
92                     return v;
93                 }
94             }
95 
96             return null;
97         }
98     }
99 
init(IPreferenceStore preferenceStore)100     public static void init(IPreferenceStore preferenceStore) {
101         sThis.mStore = preferenceStore;
102     }
103 
getPrefs()104     public static AdtPrefs getPrefs() {
105         return sThis;
106     }
107 
loadValues(PropertyChangeEvent event)108     public synchronized void loadValues(PropertyChangeEvent event) {
109         // get the name of the property that changed, if any
110         String property = event != null ? event.getProperty() : null;
111 
112         if (property == null || PREFS_SDK_DIR.equals(property)) {
113             mOsSdkLocation = mStore.getString(PREFS_SDK_DIR);
114 
115             // make sure it ends with a separator. Normally this is done when the preference
116             // is set. But to make sure older version still work, we fix it here as well.
117             if (mOsSdkLocation.length() > 0 && mOsSdkLocation.endsWith(File.separator) == false) {
118                 mOsSdkLocation = mOsSdkLocation + File.separator;
119             }
120         }
121 
122         if (property == null || PREFS_BUILD_VERBOSITY.equals(property)) {
123             mBuildVerbosity = BuildVerbosity.find(mStore.getString(PREFS_BUILD_VERBOSITY));
124             if (mBuildVerbosity == null) {
125                 mBuildVerbosity = BuildVerbosity.NORMAL;
126             }
127         }
128 
129         if (property == null || PREFS_BUILD_RES_AUTO_REFRESH.equals(property)) {
130             mBuildForceResResfresh = mStore.getBoolean(PREFS_BUILD_RES_AUTO_REFRESH);
131         }
132 
133         if (property == null || PREFS_BUILD_FORCE_ERROR_ON_NATIVELIB_IN_JAR.equals(property)) {
134             mBuildForceErrorOnNativeLibInJar = mStore.getBoolean(PREFS_BUILD_RES_AUTO_REFRESH);
135         }
136     }
137 
138     /**
139      * Returns the SDK folder.
140      * Guaranteed to be terminated by a platform-specific path separator.
141      */
getOsSdkFolder()142     public synchronized String getOsSdkFolder() {
143         return mOsSdkLocation;
144     }
145 
getBuildVerbosity()146     public synchronized BuildVerbosity getBuildVerbosity() {
147         return mBuildVerbosity;
148     }
149 
getBuildForceResResfresh()150     public  boolean getBuildForceResResfresh() {
151         return mBuildForceResResfresh;
152     }
153 
getBuildForceErrorOnNativeLibInJar()154     public boolean getBuildForceErrorOnNativeLibInJar() {
155         return mBuildForceErrorOnNativeLibInJar;
156     }
157 
158     @Override
initializeDefaultPreferences()159     public void initializeDefaultPreferences() {
160         IPreferenceStore store = AdtPlugin.getDefault().getPreferenceStore();
161 
162         store.setDefault(PREFS_BUILD_RES_AUTO_REFRESH, true);
163         store.setDefault(PREFS_BUILD_FORCE_ERROR_ON_NATIVELIB_IN_JAR, true);
164 
165         store.setDefault(PREFS_BUILD_VERBOSITY, BuildVerbosity.ALWAYS.name());
166 
167         store.setDefault(PREFS_HOME_PACKAGE, "android.process.acore"); //$NON-NLS-1$
168 
169         try {
170             store.setDefault(PREFS_DEFAULT_DEBUG_KEYSTORE,
171                     DebugKeyProvider.getDefaultKeyStoreOsPath());
172         } catch (KeytoolException e) {
173             AdtPlugin.log(e, "Get default debug keystore path failed"); //$NON-NLS-1$
174         } catch (AndroidLocationException e) {
175             AdtPlugin.log(e, "Get default debug keystore path failed"); //$NON-NLS-1$
176         }
177     }
178 }
179