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.TimeoutException; 22 import com.android.ddmlib.SyncService.ISyncProgressMonitor; 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 public void run(IProgressMonitor monitor) { 67 try { 68 runnable.run(new SyncProgressMonitor(monitor, progressMessage)); 69 } catch (Exception e) { 70 result[0] = e; 71 } finally { 72 runnable.close(); 73 } 74 } 75 }); 76 77 if (result[0] instanceof SyncException) { 78 SyncException se = (SyncException)result[0]; 79 if (se.wasCanceled()) { 80 // no need to throw this 81 return; 82 } 83 throw se; 84 } 85 86 // just do some casting so that the method declaration matches what's thrown. 87 if (result[0] instanceof TimeoutException) { 88 throw (TimeoutException)result[0]; 89 } 90 91 if (result[0] instanceof IOException) { 92 throw (IOException)result[0]; 93 } 94 95 if (result[0] instanceof RuntimeException) { 96 throw (RuntimeException)result[0]; 97 } 98 } 99 } 100