1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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.ide.eclipse.adt.internal.launch; 18 19 import com.android.ddmlib.AdbCommandRejectedException; 20 import com.android.ddmlib.IDevice; 21 import com.android.ddmlib.ShellCommandUnresponsiveException; 22 import com.android.ddmlib.TimeoutException; 23 import com.android.ide.eclipse.adt.AdtPlugin; 24 25 import java.io.IOException; 26 27 /** 28 * Launches the given activity 29 */ 30 public class ActivityLaunchAction implements IAndroidLaunchAction { 31 32 private final String mActivity; 33 private final ILaunchController mLaunchController; 34 35 /** 36 * Creates a ActivityLaunchAction 37 * 38 * @param activity fully qualified activity name to launch 39 * @param controller the {@link ILaunchController} that performs launch 40 */ 41 public ActivityLaunchAction(String activity, ILaunchController controller) { 42 mActivity = activity; 43 mLaunchController = controller; 44 } 45 46 /** 47 * Launches the activity on targeted device 48 * 49 * @param info the {@link DelayedLaunchInfo} that contains launch details 50 * @param device the Android device to perform action on 51 * 52 * @see IAndroidLaunchAction#doLaunchAction(DelayedLaunchInfo, IDevice) 53 */ 54 @Override 55 public boolean doLaunchAction(DelayedLaunchInfo info, IDevice device) { 56 String command = "am start" //$NON-NLS-1$ 57 + (info.isDebugMode() ? " -D" //$NON-NLS-1$ 58 : "") //$NON-NLS-1$ 59 + " -n " //$NON-NLS-1$ 60 + info.getPackageName() + "/" //$NON-NLS-1$ 61 + mActivity.replaceAll("\\$", "\\\\\\$") //$NON-NLS-1$ //$NON-NLS-2$ 62 + " -a android.intent.action.MAIN" //$NON-NLS-1$ 63 + " -c android.intent.category.LAUNCHER"; 64 try { 65 String msg = String.format("Starting activity %1$s on device %2$s", mActivity, 66 device); 67 AdtPlugin.printToConsole(info.getProject(), msg); 68 69 // In debug mode, we need to add the info to the list of application monitoring 70 // client changes. 71 // increment launch attempt count, to handle retries and timeouts 72 info.incrementAttemptCount(); 73 74 // now we actually launch the app. 75 device.executeShellCommand(command, new AMReceiver(info, device, mLaunchController)); 76 77 // if the app is not a debug app, we need to do some clean up, as 78 // the process is done! 79 if (info.isDebugMode() == false) { 80 // stop the launch object, since there's no debug, and it can't 81 // provide any control over the app 82 return false; 83 } 84 } catch (TimeoutException e) { 85 AdtPlugin.printErrorToConsole(info.getProject(), "Launch error: timeout"); 86 return false; 87 } catch (AdbCommandRejectedException e) { 88 AdtPlugin.printErrorToConsole(info.getProject(), String.format( 89 "Launch error: adb rejected command: %1$s", e.getMessage())); 90 return false; 91 } catch (ShellCommandUnresponsiveException e) { 92 // we didn't get the output but that's ok, just log it 93 AdtPlugin.log(e, "No command output when running: '%1$s' on device %2$s", command, 94 device); 95 } catch (IOException e) { 96 // something went wrong trying to launch the app. 97 // lets stop the Launch 98 AdtPlugin.printErrorToConsole(info.getProject(), 99 String.format("Launch error: %s", e.getMessage())); 100 return false; 101 } 102 return true; 103 } 104 105 /** 106 * Returns a description of the activity being launched 107 * 108 * @see IAndroidLaunchAction#getLaunchDescription() 109 */ 110 @Override 111 public String getLaunchDescription() { 112 return String.format("%1$s activity launch", mActivity); 113 } 114 115 } 116