Java Code Examples for com.android.ddmlib.IDevice#executeShellCommand()

The following examples show how to use com.android.ddmlib.IDevice#executeShellCommand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventLogParser.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Inits the parser for a specific Device.
 * <p/>
 * This methods reads the event-log-tags located on the device to find out
 * what tags are being written to the event log and what their format is.
 * @param device The device.
 * @return <code>true</code> if success, <code>false</code> if failure or cancellation.
 */
public boolean init(IDevice device) {
    // read the event tag map file on the device.
    try {
        device.executeShellCommand("cat " + EVENT_TAG_MAP_FILE, //$NON-NLS-1$
                new MultiLineReceiver() {
            @Override
            public void processNewLines(String[] lines) {
                for (String line : lines) {
                    processTagLine(line);
                }
            }
            @Override
            public boolean isCancelled() {
                return false;
            }
        });
    } catch (Exception e) {
        // catch all possible exceptions and return false.
        return false;
    }

    return true;
}
 
Example 2
Source File: HJAdb.java    From HJMirror with MIT License 5 votes vote down vote up
/**
 * Send an ADB Shell Command to Device
 */
private void sendCmd(IDevice device, String cmd, Callback callback) throws Exception {
    // 启动命令
    HJLog.i("[RUN]"+cmd);
    device.executeShellCommand(cmd, new IShellOutputReceiver() {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        @Override
        public void addOutput(byte[] data, int offset, int length) {
            bos.write(data, offset, length);
        }
        @Override
        public void flush() {
            try {
                bos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String msg = new String(bos.toByteArray());
            if (callback != null) {
                callback.onCallback(msg);
            }
        }
        @Override
        public boolean isCancelled() {
            return false;
        }
    }, 1, TimeUnit.DAYS);
}
 
Example 3
Source File: StartActivityEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	String shellCmd = String.format("am start -n %s/.%s", packageName, activityName);
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.START_ACTIVITY, String.format("started activity %s/%s", packageName, activityName));
	} catch (Exception e) {
		LoggerHelper.logEvent(MyLevel.EXCEPTION_ANALYSIS, String.format("not able to start activity %s/%s: ", packageName, activityName, e.getMessage()));
		e.printStackTrace();
	}		
	return null;
}
 
Example 4
Source File: AddContactEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	String shellCmd = String.format("am startservice --es \"task\" \"addContact\" -n %s/%s", 
			packageName, UtilInstrumenter.COMPONENT_CALLER_SERVICE_HELPER);		
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), "contact added..."));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;
}
 
Example 5
Source File: OutgoingCallEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	String numberToCall = "";
	for(int i = 0; i < 8; i++)
		numberToCall += ThreadLocalRandom.current().nextInt(0, 9 + 1);
	String shellCmd = String.format("am broadcast -a android.intent.action.NEW_OUTGOING_CALL --es PHONE_NUMBER %s", numberToCall);		
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), shellCmd));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;

}
 
Example 6
Source File: RealBroadcastEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	String shellCmd = String.format("am broadcast %s -a %s %s", prepareExtras(extras), action, prepareMimeType(mimeType));		
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), shellCmd));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;
}
 
Example 7
Source File: FakeBroadcastEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
	public Object onEventReceived(IDevice device) {		
		String shellCmd = prepareStartService();
		try {
			device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
			LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), shellCmd));
		} catch (Exception e) {
//			LoggerHelper.logEvent(MyLevel.EXCEPTION_ANALYSIS, "not able to send a broadcast: " + e.getMessage());
			e.printStackTrace();
		}		
		return null;
	}
 
Example 8
Source File: OnClickEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {		
	String shellCmd = String.format("am startservice --es \"className\" \"%s\""
			+ " --es \"task\" \"onClick\" -n %s/%s", 
			onClickListenerClass, packageName, UtilInstrumenter.COMPONENT_CALLER_SERVICE_HELPER);		
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), onClickListenerClass));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;
}
 
Example 9
Source File: ServiceEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	//just start the onCreate for now; we do not care whether the bundle is null
	String shellCmd = String.format("am startservice %s/%s", packageName, servicePath);		
	try {			
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), shellCmd));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;
}
 
Example 10
Source File: StartApkEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
	public Object onEventReceived(IDevice device) {
//		String shellCmd = String.format("am start -n %s/.%s", packageName, launchableActivity);
		String shellCmd = String.format("monkey -p %s -c android.intent.category.LAUNCHER 1", packageName);
		try {
			device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
			LoggerHelper.logEvent(MyLevel.OPEN_APK, "APK opened");
		} catch (Exception e) {
			LoggerHelper.logEvent(MyLevel.EXCEPTION_ANALYSIS, "not able to start apk: " + e.getMessage());
			e.printStackTrace();
		}		
		return null;
	}
 
Example 11
Source File: ActivityEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	//just start the onCreate for now; we do not care whether the bundle is null
	String shellCmd = String.format("am start -W -n %s/%s", packageName, activityPath);		
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
		LoggerHelper.logEvent(MyLevel.ADB_EVENT, adbEventFormat(toString(), shellCmd));
	} catch (Exception e) {
		e.printStackTrace();
	}		
	return null;
}
 
Example 12
Source File: KillAppProcessEvent.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
public Object onEventReceived(IDevice device) {
	String shellCmd = String.format("am force-stop %s", packageName);
	try {
		device.executeShellCommand(shellCmd, new GenericReceiver(), 10000, TimeUnit.MILLISECONDS);
	} catch (Exception e) {
		LoggerHelper.logEvent(MyLevel.EXCEPTION_ANALYSIS, "not able to kill app: " + e.getMessage());
		e.printStackTrace();
	}		
	return null;
}
 
Example 13
Source File: UserIdHelper.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Integer getWorkProfileId(IDevice device)
    throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
        IOException {
  CollectingOutputReceiver receiver = new CollectingOutputReceiver();
  device.executeShellCommand("pm list users", receiver);
  String result = receiver.getOutput();
  Matcher matcher = USER_ID_REGEX.matcher(result);
  if (matcher.find()) {
    return Integer.parseInt(matcher.group(1));
  }
  return null;
}
 
Example 14
Source File: AdbUtil.java    From ADBWIFI with Apache License 2.0 5 votes vote down vote up
public static boolean isAppInstalled(IDevice device, String packageName) throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
    GenericReceiver receiver = new GenericReceiver();
    // "pm list packages com.my.package" will return one line per package installed that corresponds to this package.
    // if this list is empty, we know for sure that the app is not installed
    device.executeShellCommand("pm list packages " + packageName, receiver, 15L, TimeUnit.SECONDS);

    //TODO make sure that it is the exact package name and not a subset.
    // e.g. if our app is called com.example but there is another app called com.example.another.app, it will match and return a false positive
    return !receiver.getAdbOutputLines().isEmpty();
}