Xposed - How to get the package name of a process - android

I'm building an xposed module and i want to hook a method only if certain process (app) called this method.
I can get the process pid and uid using Binder, but I can't find a way to get the package name without having a context (i'm running my code in a class that can't get any Context as parameter).
How can I get it?
Thanks,
Gidi

There is a solution working for me in the case you don't have Context:
BufferedReader cmdlineReader = null;
try {
cmdlineReader = new BufferedReader(new InputStreamReader(
new FileInputStream(
"/proc/" + android.os.Process.myPid() + "/cmdline"),
"iso-8859-1"));
int c;
StringBuilder processName = new StringBuilder();
while ((c = cmdlineReader.read()) > 0) {
processName.append((char) c);
}
return processName.toString();
} finally {
if (cmdlineReader != null) {
cmdlineReader.close();
}
}
Source: Is there a way to get current process name in Android

Related

Read logs from code programmatically and matching with a string in android app

How can we read logs(verbose,debug etc) programmatically from android class and then search for a string or matching with a provided string in android.
Sometimes we need to handle some system or kernel layer related event. But as we have limited access of those code we can't handle them. We can see the logcat via adb and also see some log comes from kernel/framework layer.
Now the question is, How we can override or handling some event in our app based on those logs?
Here is a solution to this from any android app:
We need to make a class with some code like below:
public class LogsUtil {
private static final String processId = Integer.toString(android.os.Process
.myPid());
public static StringBuilder readLogs() {
StringBuilder logBuilder = new StringBuilder();
try {
String[] command = new String[] { "logcat", "-d", "threadtime" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(processId)) {
logBuilder.append(line);
//Code here
}
}
} catch (IOException e) {
}
return logBuilder;
}
}
Then need to write below code in our activity from where we need to check the logs string:
//read the logs
StringBuilder logs = LogsUtil.readLogs();
if(logs.toString().contains("your_text"))
//your code
else //your code

Android: Logcat string for Bluetooth ON

My application will switch on Bluetooth. I want to wait till bluetooth is switched on.
I will look for string
MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1
in logcat and then proceed.
I want to know if this method is correct or i should be looking for some other string. What is the best way to know whether i am looking for right string in logcat. Is there any collection/document to learn what all info can be gathered using logcat
You have to be careful with below as logcat may prevent your app from responding. You should either run this piece of code or your own app in a seperate thread to keep it responsive. Below code asks logcat to send logs to your app and you can do investigate the logs as you see fit.
private static final String SEARCH_STRING = "MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1";
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
boolean didFind = false;
while ((line = bufferedReader.readLine()) != null && !didFind) {
log.append(line);
didFind = line.toUpperCase().contains(SEARCH_STRING))
}
}
catch (IOException e) {}
I hope it helps.

Self updating app

TL:DR; version ;)
my app should run without user interaction (autostart etc works)
it should update itself (via apk) without any user interaction
rooted devices are possible
.
problem:
querying a newer apk from a server works
when starting the apk with a (view?) intent, the "install app" prompt pops and needs a user confirmation
How do I solve this without any user interaction?
http://code.google.com/p/auto-update-apk-client/
This seems to be a solution, but there must be better approach.
I already found this: Install Application programmatically on Android
but that doesn't solve my problem.
Solved it! :D
It just works in rooted devices but works perfectly.
Using the unix cmd "pm" (packageManager) allows you to install apks from sdcard, when executing it as root.
Hope this could help some people in the future.
public static void installNewApk()
{
try
{
Runtime.getRuntime().exec(new String[] {"su", "-c", "pm install -r /mnt/internal/Download/fp.apk"});
}
catch (IOException e)
{
System.out.println(e.toString());
System.out.println("no root");
}
}
Required permissions:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
My suggestion is to use plugin mechanism instad of updating the app. You can dynamically load classes from the Web and run them inside your app without any user interaction. There is a lot of resources spread across the Internet:
How to load a Java class dynamically on android/dalvik?
http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
If su -c doesn't work, try su 0 (only rooted devices can do su!)
The full answer looks like this:
private void installNewApk()
{
String path = mContext.getFilesDir().getAbsolutePath() + "/" + LOCAL_FILENAME;
mQuickLog.logD("Install at: " + path);
ProcessUtils.runProcessNoException(mQuickLog, "su", "0", "pm", "install", "-r", path);
}
With this class defined:
public class ProcessUtils {
Process process;
int errCode;
public ProcessUtils(String ...command) throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder(command);
this.process = pb.start();
this.errCode = this.process.waitFor();
}
public int getErrCode() {
return errCode;
}
public String getOutput() throws IOException {
InputStream inputStream = process.getInputStream();
InputStream errStream = process.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
br = new BufferedReader(new InputStreamReader(errStream));
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
return sb.toString();
}
public static String runProcess(String ...command) throws IOException, InterruptedException {
ProcessUtils p = new ProcessUtils(command);
if (p.getErrCode() != 0) {
// err
}
return p.getOutput();
}
public static void runProcessNoException(String ...command) {
try {
runProcess(command);
} catch (InterruptedException | IOException e) {
// err
}
}
}

Reading Logcat within the app returns null

I read the other posts and can't figure out the "trick".
I looked at Log Collector but can't use a separate APK. I'm basically using the same approach and I consistently get nothing back on the processes inputstream.
I have READ_LOGS in the manifest.
From within my default activity, I'm able to get the log, but if I move the logic to another activity or utilize an asynctask, no output is returned.
this code is from my default activity... inline, i dump it to the log
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
Log.d(LOGTAG, "Logcat: " +log.toString());
} catch (IOException e) {}
if i wrap it in an asynctask or just inline it in another activity, it returns nothing
ArrayList<String> commandLine = new ArrayList<String>();
//terminate on completion and suppress everything except the filter
commandLine.add("logcat -d -s");
...
//replace asynctask with inline (could not get log in asynctask)
showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
final StringBuilder log = new StringBuilder();
BufferedReader bufferedReader = null;
try{
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(MangoApp.LINE_SEPARATOR);
}
sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
dismissProgressDialog();
dismissMainDialog();
finish();
}
catch (IOException e){
dismissProgressDialog();
showErrorDialog(getString(R.string.failed_to_get_log_message));
Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ignore) {}
}
}
Can anyone spot the diff or explain the magic? I'm pretty sure the commandline is right in the second version so scratching my head. I'm using 2.1 SDK 7 on the emulator.
Thanks
Hope this will be helpful, you don't have to create file by your self just execute the below command, to get the error info.
Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");
Logcat parameters options:
-r <size in kilobytes> -> for specifying the size of file
-f <filename> -> file to which you want to write the logs.
Can you try it without the ArrayList. Just pass the command String
I have implemented it in the following way (without the ArrayList). It works for me.
String baseCommand = "logcat -v time";
baseCommand += " MyApp:I "; // Info for my app
baseCommand += " *:S "; // Silence others
ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);

Android Runtime.getRuntime().exec() to nav through directories

So I want to be able to write an app that can turn on and display logcat messages, dmesg, and also be able to run commands like 'ls' 'cat' 'echo' 'cd.'
If I do the following:
nativeProc = Runtime.getRuntime().exec("ls\n");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
full = full + "\n" + line;
}
I can put the text "full" to a Text View and see the root directory.
However, that's about all I can do. Let's say I want to find a directory, and change to it, I'm having trouble.
So if I do this:
nativeProc = Runtime.getRuntime().exec("ls\n");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
full = full + "\n" + line;
}
/* Code here to confirm the existing of a directory*/
nativeProc = Runtime.getRuntime().exec("cd tmp\n");
BufferedReader in2 = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
line = null;
String test = "\nStart1:\n";
while ((line = in2.readLine()) != null) {
test = test + "\n" + line;
}
I get nothing for both "full" and "text"
Any ideas?
You can execute ls with provided path for which folders should be listed
Runtime.getRuntime().exec(new String[] { "ls", "\\tmp"});
The current working directory is associated with the current process. When you exec("cd tmp") you are creating a process, changing its directory to "tmp", and then the process exits. The parent process' working directory doesn't change.
See this for a more general discussion of changing the current working directory in Java.
How about writing a shell file which checks for existence of the file and all other implementaion in this file and added it to sdcard and trigger the shell file from java using getRuntimr.exec().you can also pass parameters to it.

Categories

Resources