• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.ddmuilib.handler;
18 
19 import com.android.ddmlib.SyncService;
20 import com.android.ddmlib.ClientData.IHprofDumpHandler;
21 import com.android.ddmlib.ClientData.IMethodProfilingHandler;
22 import com.android.ddmlib.SyncService.SyncResult;
23 import com.android.ddmuilib.SyncProgressMonitor;
24 
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Shell;
31 
32 import java.lang.reflect.InvocationTargetException;
33 
34 /**
35  * Base handler class for handler dealing with files located on a device.
36  *
37  * @see IHprofDumpHandler
38  * @see IMethodProfilingHandler
39  */
40 public class BaseFileHandler {
41 
42     protected final Shell mParentShell;
43 
BaseFileHandler(Shell parentShell)44     public BaseFileHandler(Shell parentShell) {
45         mParentShell = parentShell;
46     }
47 
48     /**
49      * Prompts the user for a save location and pulls the remote files into this location.
50      * <p/>This <strong>must</strong> be called from the UI Thread.
51      * @param sync the {@link SyncService} to use to pull the file from the device
52      * @param localFileName The default local name
53      * @param remoteFilePath The name of the file to pull off of the device
54      * @param title The title of the File Save dialog.
55      * @return The result of the pull as a {@link SyncResult} object, or null if the sync
56      * didn't happen (canceled by the user).
57      * @throws InvocationTargetException
58      * @throws InterruptedException
59      */
promptAndPull(SyncService sync, String localFileName, String remoteFilePath, String title)60     protected SyncResult promptAndPull(SyncService sync,
61             String localFileName, String remoteFilePath, String title)
62             throws InvocationTargetException, InterruptedException {
63         FileDialog fileDialog = new FileDialog(mParentShell, SWT.SAVE);
64 
65         fileDialog.setText(title);
66         fileDialog.setFileName(localFileName);
67 
68         String localFilePath = fileDialog.open();
69         if (localFilePath != null) {
70             return pull(sync, localFilePath, remoteFilePath);
71         }
72 
73         return null;
74     }
75 
76     /**
77      * Pulls a file off of a device
78      * @param sync the {@link SyncService} to use to pull the file.
79      * @param localFilePath the path of the local file to create
80      * @param remoteFilePath the path of the remote file to pull
81      * @return the result of the sync as an instance of {@link SyncResult}
82      * @throws InvocationTargetException
83      * @throws InterruptedException
84      */
pull(final SyncService sync, final String localFilePath, final String remoteFilePath)85     protected SyncResult pull(final SyncService sync, final String localFilePath,
86             final String remoteFilePath)
87             throws InvocationTargetException, InterruptedException {
88         final SyncResult[] res = new SyncResult[1];
89         new ProgressMonitorDialog(mParentShell).run(true, true, new IRunnableWithProgress() {
90             public void run(IProgressMonitor monitor) {
91                 try {
92                     res[0] = sync.pullFile(remoteFilePath, localFilePath,
93                             new SyncProgressMonitor(monitor, String.format(
94                                     "Pulling %1$s from the device", remoteFilePath)));
95                 } finally {
96                     sync.close();
97                 }
98             }
99         });
100 
101         return res[0];
102     }
103 }
104