Home | History | Annotate | Download | only in hierarchyviewer
      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.hierarchyviewer;
     18 
     19 import com.android.SdkConstants;
     20 import com.android.hierarchyviewerlib.HierarchyViewerDirector;
     21 
     22 import java.io.File;
     23 import java.util.concurrent.ExecutorService;
     24 import java.util.concurrent.Executors;
     25 
     26 /**
     27  * This is the application version of the director.
     28  */
     29 public class HierarchyViewerApplicationDirector extends HierarchyViewerDirector {
     30 
     31     private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
     32 
     33     public static HierarchyViewerDirector createDirector() {
     34         return sDirector = new HierarchyViewerApplicationDirector();
     35     }
     36 
     37     @Override
     38     public void terminate() {
     39         super.terminate();
     40         mExecutor.shutdown();
     41     }
     42 
     43     /*
     44      * Gets the location of adb. The script that runs the hierarchy viewer
     45      * defines com.android.hierarchyviewer.bindir.
     46      */
     47     @Override
     48     public String getAdbLocation() {
     49         String hvParentLocation = System.getProperty("com.android.hierarchyviewer.bindir"); //$NON-NLS-1$
     50 
     51         // in the new SDK, adb is in the platform-tools, but when run from the command line
     52         // in the Android source tree, then adb is next to hierarchyviewer.
     53         if (hvParentLocation != null && hvParentLocation.length() != 0) {
     54             // check if there's a platform-tools folder
     55             File platformTools = new File(new File(hvParentLocation).getParent(),
     56                     SdkConstants.FD_PLATFORM_TOOLS);
     57             if (platformTools.isDirectory()) {
     58                 return platformTools.getAbsolutePath() + File.separator + SdkConstants.FN_ADB;
     59             }
     60 
     61             return hvParentLocation + File.separator + SdkConstants.FN_ADB;
     62         }
     63 
     64         return SdkConstants.FN_ADB;
     65     }
     66 
     67     /*
     68      * In the application, we handle background tasks using a single thread,
     69      * just to get rid of possible race conditions that can occur. We update the
     70      * progress bar to show that we are doing something in the background.
     71      */
     72     @Override
     73     public void executeInBackground(final String taskName, final Runnable task) {
     74         mExecutor.execute(new Runnable() {
     75             @Override
     76             public void run() {
     77                 HierarchyViewerApplication.getMainWindow().startTask(taskName);
     78                 task.run();
     79                 HierarchyViewerApplication.getMainWindow().endTask();
     80             }
     81         });
     82     }
     83 
     84 }
     85