1 /*
2  * Copyright (C) 2010 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.pdt;
18 
19 import com.android.ide.eclipse.ddms.DdmsPlugin;
20 import com.android.ide.eclipse.pdt.internal.preferences.PrefPage;
21 
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.util.IPropertyChangeListener;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25 import org.eclipse.ui.plugin.AbstractUIPlugin;
26 import org.osgi.framework.BundleContext;
27 
28 public class PdtPlugin extends AbstractUIPlugin {
29 
30     public final static String PLUGIN_ID = "com.android.ide.eclipse.pdt"; //$NON-NLS-1$
31     private static PdtPlugin sPlugin;
32 
PdtPlugin()33     public PdtPlugin() {
34         sPlugin = this;
35     }
36 
37     /**
38      * Returns the shared instance
39      *
40      * @return the shared instance
41      */
getDefault()42     public static synchronized PdtPlugin getDefault() {
43         return sPlugin;
44     }
45 
46     @Override
start(BundleContext context)47     public void start(BundleContext context) throws Exception {
48         super.start(context);
49 
50         // set the listener for the preference change
51         getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
52             @Override
53             public void propertyChange(PropertyChangeEvent event) {
54                 // if the SDK changed, we have to do some extra work
55                 if (PrefPage.PREFS_DEVTREE_DIR.equals(event.getProperty())) {
56                     // restart adb, in case it's a different version
57                     DdmsPlugin.setToolsLocation(getAdbLocation(), true /* startAdb */,
58                             getHprofConvLocation(), getTraceViewLocation());
59                 }
60             }
61         });
62     }
63 
64     /**
65      * Returns the location of the dev tree or <code>null</code> if unknown.
66      */
getDevTree()67     public static String getDevTree() {
68         // this always return a store, even a temp one if an error occurred.
69         IPreferenceStore store = sPlugin.getPreferenceStore();
70 
71         // returns an empty, non-null, string if the preference is not found.
72         String devTree = store.getString(PrefPage.PREFS_DEVTREE_DIR);
73 
74         if (devTree.length() == 0) {
75             devTree = System.getenv("ANDROID_BUILD_TOP"); //$NON-NLS-1$
76         }
77 
78         return devTree;
79     }
80 
81     /**
82      * Returns the location of adb or <code>null</code> if unknown.
83      */
getAdbLocation()84     public static String getAdbLocation() {
85         String devTreeBin = getDevTreeOutBin();
86 
87         if (devTreeBin != null && devTreeBin.length() > 0) {
88             return devTreeBin + "adb"; //$NON-NLS-1$
89         }
90 
91         return null;
92     }
93 
94     /**
95      * Returns the location of hprof-conv or <code>null</code> if unknown.
96      */
getHprofConvLocation()97     public static String getHprofConvLocation() {
98         String devTreeBin = getDevTreeOutBin();
99 
100         if (devTreeBin != null && devTreeBin.length() > 0) {
101             return devTreeBin + "hprof-conv"; //$NON-NLS-1$
102         }
103 
104         return null;
105     }
106 
107     /**
108      * Returns the location of traceview or <code>null</code> if unknown.
109      */
getTraceViewLocation()110     public static String getTraceViewLocation() {
111         String devTreeBin = getDevTreeOutBin();
112 
113         if (devTreeBin != null && devTreeBin.length() > 0) {
114             return devTreeBin + "traceview"; //$NON-NLS-1$
115         }
116 
117         return null;
118     }
119 
getDevTreeOutBin()120     private static String getDevTreeOutBin() {
121         String devTree = getDevTree();
122 
123         if (devTree != null && devTree.length() > 0) {
124             return devTree + "/out/host/" + currentPlatform() + "/bin/"; //$NON-NLS-1$ //$NON-NLS-2$
125         }
126 
127         return null;
128     }
129 
130     /**
131      * Returns the current platform name as used by the Android build system
132      *
133      */
currentPlatform()134     private static String currentPlatform() {
135         String os = System.getProperty("os.name");          //$NON-NLS-1$
136         if (os.startsWith("Mac OS")) {                      //$NON-NLS-1$
137             return "darwin-x86";                            //$NON-NLS-1$
138         } else if (os.startsWith("Windows")) {              //$NON-NLS-1$
139             return "windows";                               //$NON-NLS-1$
140         } else if (os.startsWith("Linux")) {                //$NON-NLS-1$
141             return "linux-x86";                             //$NON-NLS-1$
142         }
143 
144         return ""; //$NON-NLS-1$
145     }
146 }
147