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.ddmuilib; 18 19 import com.android.ddmlib.SyncException; 20 import com.android.ddmlib.SyncService; 21 import com.android.ddmlib.SyncService.ISyncProgressMonitor; 22 import com.android.ddmlib.TimeoutException; 23 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 26 import org.eclipse.jface.operation.IRunnableWithProgress; 27 import org.eclipse.swt.widgets.Shell; 28 29 import java.io.IOException; 30 import java.lang.reflect.InvocationTargetException; 31 32 /** 33 * Helper class to run a Sync in a {@link ProgressMonitorDialog}. 34 */ 35 public class SyncProgressHelper { 36 37 /** 38 * a runnable class run with an {@link ISyncProgressMonitor}. 39 */ 40 public interface SyncRunnable { 41 /** Runs the sync action */ run(ISyncProgressMonitor monitor)42 void run(ISyncProgressMonitor monitor) throws SyncException, IOException, TimeoutException; 43 /** close the {@link SyncService} */ close()44 void close(); 45 } 46 47 /** 48 * Runs a {@link SyncRunnable} in a {@link ProgressMonitorDialog}. 49 * @param runnable The {@link SyncRunnable} to run. 50 * @param progressMessage the message to display in the progress dialog 51 * @param parentShell the parent shell for the progress dialog. 52 * 53 * @throws InvocationTargetException 54 * @throws InterruptedException 55 * @throws SyncException if an error happens during the push of the package on the device. 56 * @throws IOException 57 * @throws TimeoutException 58 */ run(final SyncRunnable runnable, final String progressMessage, final Shell parentShell)59 public static void run(final SyncRunnable runnable, final String progressMessage, 60 final Shell parentShell) 61 throws InvocationTargetException, InterruptedException, SyncException, IOException, 62 TimeoutException { 63 64 final Exception[] result = new Exception[1]; 65 new ProgressMonitorDialog(parentShell).run(true, true, new IRunnableWithProgress() { 66 @Override 67 public void run(IProgressMonitor monitor) { 68 try { 69 runnable.run(new SyncProgressMonitor(monitor, progressMessage)); 70 } catch (Exception e) { 71 result[0] = e; 72 } finally { 73 runnable.close(); 74 } 75 } 76 }); 77 78 if (result[0] instanceof SyncException) { 79 SyncException se = (SyncException)result[0]; 80 if (se.wasCanceled()) { 81 // no need to throw this 82 return; 83 } 84 throw se; 85 } 86 87 // just do some casting so that the method declaration matches what's thrown. 88 if (result[0] instanceof TimeoutException) { 89 throw (TimeoutException)result[0]; 90 } 91 92 if (result[0] instanceof IOException) { 93 throw (IOException)result[0]; 94 } 95 96 if (result[0] instanceof RuntimeException) { 97 throw (RuntimeException)result[0]; 98 } 99 } 100 } 101