1 /*
2  * Copyright (C) 2007 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.ddms.preferences;
18 
19 import com.android.ddmlib.DdmPreferences;
20 import com.android.ddmlib.Log.LogLevel;
21 import com.android.ddmuilib.DdmUiPreferences;
22 import com.android.ide.eclipse.ddms.DdmsPlugin;
23 import com.android.ide.eclipse.ddms.LogCatMonitor;
24 import com.android.ide.eclipse.ddms.views.DeviceView.HProfHandler;
25 import com.android.ide.eclipse.ddms.views.LogCatView;
26 
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.content.IContentType;
29 import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.graphics.FontData;
33 
34 /**
35  * Class used to initialize default preference values.
36  */
37 public class PreferenceInitializer extends AbstractPreferenceInitializer {
38 
39     public final static String ATTR_LOG_LEVEL =
40         DdmsPlugin.PLUGIN_ID + ".logLevel"; //$NON-NLS-1$
41 
42     public final static String ATTR_DEBUG_PORT_BASE =
43         DdmsPlugin.PLUGIN_ID + ".adbDebugBasePort"; //$NON-NLS-1$
44 
45     public final static String ATTR_SELECTED_DEBUG_PORT =
46         DdmsPlugin.PLUGIN_ID + ".debugSelectedPort"; //$NON-NLS-1$
47 
48     public final static String ATTR_DEFAULT_THREAD_UPDATE =
49         DdmsPlugin.PLUGIN_ID + ".defaultThreadUpdateEnabled"; //$NON-NLS-1$
50 
51     public final static String ATTR_DEFAULT_HEAP_UPDATE =
52         DdmsPlugin.PLUGIN_ID + ".defaultHeapUpdateEnabled"; //$NON-NLS-1$
53 
54     public final static String ATTR_THREAD_INTERVAL =
55         DdmsPlugin.PLUGIN_ID + ".threadStatusInterval"; //$NON-NLS-1$
56 
57     public final static String ATTR_IMAGE_SAVE_DIR =
58         DdmsPlugin.PLUGIN_ID + ".imageSaveDir"; //$NON-NLS-1$
59 
60     public final static String ATTR_LAST_IMAGE_SAVE_DIR =
61         DdmsPlugin.PLUGIN_ID + ".lastImageSaveDir"; //$NON-NLS-1$
62 
63     public final static String ATTR_LOGCAT_FONT =
64         DdmsPlugin.PLUGIN_ID + ".logcatFont"; //$NON-NLS-1$
65 
66     public final static String ATTR_HPROF_ACTION =
67         DdmsPlugin.PLUGIN_ID + ".hprofAction"; //$NON-NLS-1$
68 
69     public final static String ATTR_TIME_OUT =
70         DdmsPlugin.PLUGIN_ID + ".timeOut"; //$NON-NLS-1$
71 
72     public final static String ATTR_USE_ADBHOST =
73         DdmsPlugin.PLUGIN_ID + ".useAdbHost"; //$NON-NLS-1$
74 
75     public final static String ATTR_ADBHOST_VALUE =
76         DdmsPlugin.PLUGIN_ID + ".adbHostValue"; //$NON-NLS-1$
77 
78     public final static String ATTR_SWITCH_PERSPECTIVE =
79         DdmsPlugin.PLUGIN_ID + ".switchPerspective"; //$NON-NLS-1$
80 
81     public final static String ATTR_PERSPECTIVE_ID =
82         DdmsPlugin.PLUGIN_ID + ".perspectiveId"; //$NON-NLS-1$
83 
84     public static final String ATTR_PROFILER_BUFSIZE_MB =
85         DdmsPlugin.PLUGIN_ID + ".profilerBufferSizeMb"; //$NON-NLS-1$
86 
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer
91      * #initializeDefaultPreferences()
92      */
93     @Override
initializeDefaultPreferences()94     public void initializeDefaultPreferences() {
95         IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore();
96 
97         store.setDefault(ATTR_DEBUG_PORT_BASE, DdmPreferences.DEFAULT_DEBUG_PORT_BASE);
98 
99         store.setDefault(ATTR_SELECTED_DEBUG_PORT, DdmPreferences.DEFAULT_SELECTED_DEBUG_PORT);
100 
101         store.setDefault(ATTR_DEFAULT_THREAD_UPDATE, DdmPreferences.DEFAULT_INITIAL_THREAD_UPDATE);
102         store.setDefault(ATTR_DEFAULT_HEAP_UPDATE,
103                 DdmPreferences.DEFAULT_INITIAL_HEAP_UPDATE);
104 
105         store.setDefault(ATTR_PROFILER_BUFSIZE_MB, DdmPreferences.DEFAULT_PROFILER_BUFFER_SIZE_MB);
106 
107         store.setDefault(ATTR_THREAD_INTERVAL, DdmUiPreferences.DEFAULT_THREAD_REFRESH_INTERVAL);
108 
109         String homeDir = System.getProperty("user.home"); //$NON-NLS-1$
110         store.setDefault(ATTR_IMAGE_SAVE_DIR, homeDir);
111 
112         store.setDefault(ATTR_LOG_LEVEL, DdmPreferences.DEFAULT_LOG_LEVEL.getStringValue());
113 
114         store.setDefault(ATTR_LOGCAT_FONT,
115                 new FontData("Courier", 10, SWT.NORMAL).toString()); //$NON-NLS-1$
116 
117         // When obtaining hprof files from the device, default to opening the file
118         // only if there is a registered content type for the hprof extension.
119         store.setDefault(ATTR_HPROF_ACTION, HProfHandler.ACTION_SAVE);
120         for (IContentType contentType: Platform.getContentTypeManager().getAllContentTypes()) {
121             if (contentType.isAssociatedWith(HProfHandler.DOT_HPROF)) {
122                 store.setDefault(ATTR_HPROF_ACTION, HProfHandler.ACTION_OPEN);
123                 break;
124             }
125         }
126 
127         store.setDefault(ATTR_TIME_OUT, DdmPreferences.DEFAULT_TIMEOUT);
128 
129         store.setDefault(ATTR_USE_ADBHOST, DdmPreferences.DEFAULT_USE_ADBHOST);
130         store.setDefault(ATTR_ADBHOST_VALUE, DdmPreferences.DEFAULT_ADBHOST_VALUE);
131         store.setDefault(ATTR_SWITCH_PERSPECTIVE, LogCatView.DEFAULT_SWITCH_PERSPECTIVE);
132         store.setDefault(ATTR_PERSPECTIVE_ID, LogCatView.DEFAULT_PERSPECTIVE_ID);
133 
134         store.setDefault(LogCatMonitor.AUTO_MONITOR_PREFKEY, true);
135         store.setDefault(LogCatMonitor.AUTO_MONITOR_LOGLEVEL, LogLevel.VERBOSE.getStringValue());
136     }
137 
138     /**
139      * Initializes the preferences of ddmlib and ddmuilib with values from the eclipse store.
140      */
setupPreferences()141     public synchronized static void setupPreferences() {
142         IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore();
143 
144         DdmPreferences.setDebugPortBase(store.getInt(ATTR_DEBUG_PORT_BASE));
145         DdmPreferences.setSelectedDebugPort(store.getInt(ATTR_SELECTED_DEBUG_PORT));
146         DdmPreferences.setLogLevel(store.getString(ATTR_LOG_LEVEL));
147         DdmPreferences.setInitialThreadUpdate(store.getBoolean(ATTR_DEFAULT_THREAD_UPDATE));
148         DdmPreferences.setInitialHeapUpdate(store.getBoolean(ATTR_DEFAULT_HEAP_UPDATE));
149         DdmPreferences.setProfilerBufferSizeMb(store.getInt(ATTR_PROFILER_BUFSIZE_MB));
150         DdmUiPreferences.setThreadRefreshInterval(store.getInt(ATTR_THREAD_INTERVAL));
151         DdmPreferences.setTimeOut(store.getInt(ATTR_TIME_OUT));
152         DdmPreferences.setUseAdbHost(store.getBoolean(ATTR_USE_ADBHOST));
153         DdmPreferences.setAdbHostValue(store.getString(ATTR_ADBHOST_VALUE));
154     }
155 }
156