• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ddms;
18 
19 import com.android.ddmlib.AndroidDebugBridge;
20 import com.android.ddmlib.DebugPortManager;
21 import com.android.ddmlib.Log;
22 import com.android.sdkstats.SdkStatsService;
23 
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.io.StringWriter;
30 import java.lang.management.ManagementFactory;
31 import java.lang.management.RuntimeMXBean;
32 import java.util.Properties;
33 
34 
35 /**
36  * Start the UI and network.
37  */
38 public class Main {
39 
40     public static String sRevision;
41 
Main()42     public Main() {
43     }
44 
45     /*
46      * If a thread bails with an uncaught exception, bring the whole
47      * thing down.
48      */
49     private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
uncaughtException(Thread t, Throwable e)50         public void uncaughtException(Thread t, Throwable e) {
51             Log.e("ddms", "shutting down due to uncaught exception");
52 
53             StringWriter sw = new StringWriter();
54             PrintWriter pw = new PrintWriter(sw);
55             e.printStackTrace(pw);
56             Log.e("ddms", sw.toString());
57 
58             System.exit(1);
59         }
60     }
61 
62     /**
63      * Parse args, start threads.
64      */
main(String[] args)65     public static void main(String[] args) {
66         // In order to have the AWT/SWT bridge work on Leopard, we do this little hack.
67         String os = System.getProperty("os.name"); //$NON-NLS-1$
68         if (os.startsWith("Mac OS")) { //$NON-NLS-1$
69             RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
70             System.setProperty(
71                     "JAVA_STARTED_ON_FIRST_THREAD_" + (rt.getName().split("@"))[0], //$NON-NLS-1$
72                     "1"); //$NON-NLS-1$
73         }
74 
75         Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler());
76 
77         // load prefs and init the default values
78         PrefsDialog.init();
79 
80         Log.d("ddms", "Initializing");
81 
82         // the "ping" argument means to check in with the server and exit
83         // the application name and version number must also be supplied
84         if (args.length >= 3 && args[0].equals("ping")) {
85             SdkStatsService.ping(args[1], args[2], null);
86             return;
87         } else if (args.length > 0) {
88             Log.e("ddms", "Unknown argument: " + args[0]);
89             System.exit(1);
90         }
91 
92         // get the ddms parent folder location
93         String ddmsParentLocation = System.getProperty("com.android.ddms.bindir"); //$NON-NLS-1$
94 
95         // we're past the point where ddms can be called just to send a ping, so we can
96         // ping for ddms itself.
97         ping(ddmsParentLocation);
98 
99         DebugPortManager.setProvider(DebugPortProvider.getInstance());
100 
101         // create the three main threads
102         UIThread ui = UIThread.getInstance();
103 
104         try {
105             ui.runUI(ddmsParentLocation);
106         } finally {
107             PrefsDialog.save();
108 
109             AndroidDebugBridge.terminate();
110         }
111 
112         Log.d("ddms", "Bye");
113 
114         // this is kinda bad, but on MacOS the shutdown doesn't seem to finish because of
115         // a thread called AWT-Shutdown. This will help while I track this down.
116         System.exit(0);
117     }
118 
ping(String ddmsParentLocation)119     public static void ping(String ddmsParentLocation) {
120         Properties p = new Properties();
121         try{
122             File sourceProp;
123             if (ddmsParentLocation != null && ddmsParentLocation.length() > 0) {
124                 sourceProp = new File(ddmsParentLocation, "source.properties"); //$NON-NLS-1$
125             } else {
126                 sourceProp = new File("source.properties"); //$NON-NLS-1$
127             }
128             p.load(new FileInputStream(sourceProp));
129             sRevision = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
130             if (sRevision != null && sRevision.length() > 0) {
131                 SdkStatsService.ping("ddms", sRevision, null);  //$NON-NLS-1$
132             }
133         } catch (FileNotFoundException e) {
134             // couldn't find the file? don't ping.
135         } catch (IOException e) {
136             // couldn't find the file? don't ping.
137         }
138     }
139 }
140