Android get GPU model - android

When running the following command from termial:
adb shell dumpsys | grep GLES
The output is:
GLES: Qualcomm, Adreno (TM) 330, OpenGL ES 3.0 V#53.0 AU# (CL#)
However I am unable to get the output when running programatically.
String GPUModel = "";
String command = "adb shell dumpsys | grep GLES";
try {
InputStream inputStream = Runtime.getRuntime()
.exec(command)
.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
GPUModel = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
GPUModel is null.

You can't run the dumpsys command from your app. It'd require the DUMP permission which only system apps and apps signed with the same key as the system are granted.

You should use glGetString to get GPU type:
String renderer = GLES20.glGetString(GLES20.GL_RENDERER);
However, if you need to check for certain feature of GPU you better not to check GPU name but check if necessary GL extension is available instead. You can retrieve all of them by requesting GL_EXTENSIONS:
String extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);

Related

Can't run exec("su") on a rooted Android Marshmallow device, other commands run fine

I have gone over all the possible answers to this questions, and none of them has worked for me. Seems like Android has very tightly blocked the running of the shell commands from within an app, even when the device is rooted.
I can do su and run commands perfectly fine from adb, but I cannot run them from the app itself. It fails with error:
Error running exec(). Command: [su] Working Directory: null Environment: null
I have searched for these errors and the solutions work for older versions of Android, not Marshmallow.
I have also tried libsuperuser (SuperSu), but it also fails on Marshmallow.
Other things I tried were like, e.g. for the device reboot:
Process p = Runtime.getRuntime().exec("su");
Process proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
or
PowerManager pm = (PowerManager)...
Same with other operations. The commands fail right at su. I can't issue an adb command either from the app. But I can do both perfectly fine otherwise from the command line via adb shell.
The example which Android has on its website doesn't work either.
I read about disabling SELinux security and setting it to permissive mode. But that also require to run su first, where the app fails.
Now I am totally lost and don't know what to do next. I am really looking for a working solution since for this particular project, which requires the app to interact with the underlying hardware.
You need to first ensure you have busybox installed as that would install the list of most commonly used shell commands and then use the following code to run the command.
Runtime.getRuntime().exec("ls");
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Try this
Process command = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes("-c reboot\n");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();
It can take time to execute so u might wanna run this on worker thread
There is nothing that blocks shell commands in "Marshmallow" . I am using it inside my app and all commands work flawlessly. There is something else you are missing. First confirm that in your "/system/bin/" directory all files are present. Alternatively check in other device.
Try to add "sh -c " as the beginning of your shell command. Otherwise you will get the error message about null directory and environment.
Example: sh -c su

Trigger UIAutomator Test Script programmatically

We are using UIAutomator for automation.
To run UIAutomator test scripts we used to compile the scripts from command prompt by using either USB or Wifi adb and it will run on the android device.
UiAutomator scripts will come in jar format after compilation.
We will push the jar in the device and we have trigger the test scripts.
The command to start the test script in “adb shell uiautomator runtest TestPackage.jar”.
It is working with normal adb connection.
But in our case we have to initiate the test scripts without any adb connection.
So we tried by passing the commands programmatically in the device using this java code
try {
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
After the execution we are getting the following exception:
java.lang.securityException : longMsg = You do not have android.permission.RETRIEVE_WINDOW_CONTENT required to call registerUITestAutomationService
If we run scripts from command prompt with adb connection it is working. But if we tried the command programmatically we are getting this exception.
We came to know that the above mentioned issue can be overcome in a rooted android device by enabling the RETRIEVE_WINDOW_CONTENT permission. But we want this scripts to run on a non-rooted setup.
Can anyone help me in explaining this security permission issue?
How can I resolve it?

Can not find Remote Shell in eclipse

I want to do some work for which I require shell in eclipse but I can't get it anywhere.
I have tried :
Window > Show View > other
but i didn't get any option over there.
Shall I download something for the shell, or do any settings to get the shell ?
Give any link.
Thank you.
You need Eclipse RSE (Remote System Explorer) installed for this support.
Go to Help > Install New Software, choose your Eclipse release to work with. RSE is in the Mobile and Device Development section.
You can also access using java code
Process p;
p = Runtime.getRuntime().exec("//enter Your shell Command here");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
sb.append(line);
while (line != null) {
line = reader.readLine();
sb.append(line);
}
System.out.println(sb.toString());//ur shell command result

Is it possible to execute adb commands through my android app?

Can anyone say, whether adb commands can be executed through my android application. If it is possible to execute, how it can be implemented?
You can do it with this:
Process process = Runtime.getRuntime().exec("your command");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Don't forget to surround it with a try and catch statement.
Edit:
#Phix is right, ProcessBuilder would be better to use.
Normal Android apps have different privileges to processes started via adb, e.g., processes started via adb are allowed to the capture the screen whereas normal apps aren't. So, you can execute commands from your app via Runtime.getRuntime().exec(), but they won't have the same privileges as if you had executed from an adb shell.
i came across this post looking for a different query, but i have worked specifically with input on android before, so I'd just like to put some clarity on the matter.
The reason why
Runtime.getRuntime().exec("adb shell input keyevent 120");
Is not working, is because you are not removing
adb shell
The ADB part is only for use on your computer, if you have incorrectly installed ADB, the command would actually be a path to the adb.exe file on your computer, like this
C:\XXXX\ADB Files\adb.exe shell
or
C:\XXXX\ADB Files\adb shell
The shell part tells the ADB program on your computer to access the devices shell, so your device will not know what shell is either...
Using sh /path/to/commandList.sh will execute the commads listed in commandList.sh as it is a shell script (a .batch file on windows is similar )
The command you want to use is
Runtime.getRuntime().exec("input keyevent 120");
However this will cause Environment null and working directory null, you can bypass this by writing the commands to a shell script ( .sh file ) and then running the script with
Runtime.getRuntime().exec("sh path/to/shellScript.sh");
Sometimes the sh is not needed, but i use it just incase.
I hope this clears at least something up :)
adb shell invoked in Runtime.getRuntime().exec is not running under shell user. It provide shell but with same process owner user (like u0_a44). That's the reason all command did not work.
This is what I do in Kotlin, I also get command responses too
fun runShellCommand(command: String) {
// Run the command
val process = Runtime.getRuntime().exec(command)
val bufferedReader = BufferedReader(
InputStreamReader(process.inputStream)
)
// Grab the results
val log = StringBuilder()
var line: String?
line = bufferedReader.readLine()
while (line != null) {
log.append(line + "\n")
line = bufferedReader.readLine()
}
val Reader = BufferedReader(
InputStreamReader(process.errorStream)
)
// if we had an error during ex we get here
val error_log = StringBuilder()
var error_line: String?
error_line = Reader.readLine()
while (error_line != null) {
error_log.append(error_line + "\n")
error_line = Reader.readLine()
}
if (error_log.toString() != "")
Log.info("ADB_COMMAND", "command : $command $log error $error_log")
else
Log.info("ADB_COMMAND", "command : $command $log")
}
Executing
Runtime.getRuntime().exec("adb shell input keyevent 120");
I got the following error:
java.io.IOException: Cannot run program "adb": error=13, Permission denied.
Executing
Runtime.getRuntime().exec("adb shell input keyevent 120");
There is no error but at the same time, my request is not processed to take the screenshot.
I found out this was working in earlier versions of android but later it was removed. Though I'm not able to provide the source here why it is not working.
Hope this helps someone like me who is trying to use this approach to take the screenshot when the app is not in the foreground.
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
string cmd = "/system/bin/input keyevent 23\n";
os.writeBytes(cmd);
the phone must be rooted. here I have executed adb command "input keyevent 23".
remember when you execute adb command through su you does not need to add "adb shell input keyevent 23"

How can I run Linux commands on an Android device?

On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:
tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk
Or the command cp, it returns:
sh: tar: not found
Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/data folder to SD card. I got su and I got the following code:
int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
+ packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
process.getInputStream())), 64);
String inLine;
try {
StringBuilder sbCommand = new StringBuilder();
sbCommand.append(command).append(" ");
sbCommand.append("\n");
os.writeBytes(command.toString());
if (is != null) {
for (int i = 0; i < timeout; i++) {
if (is.ready())
break;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (is.ready()) {
inLine = is.readLine();
} else {
}
}
} catch (IOException e) {
e.printStackTrace();
}
It always stops in is.ready(), and when I changed it to process.waitfor() it also stopped. Why?
As far as i know, the only way to run shell commands is:
Process proc = Runtime.getRuntime().exec("your command");
You can run Linux commands on Android. But there are usually just very few pre-installed.
If you want to add more commands you might want to root your device and install busybox on it.
This is not for productive use within an application but can help you to work with your device.
If you have the binaries for your system, you can run anything on your system.
Saying that you have to understand that you have to find the binaries for tar.
Look here http://forum.xda-developers.com/showthread.php?t=872438
And possibly other places..
You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.
You can install Termux app on your android device and run Linux command by using that app
Install busybox, then type the command in the following format:
busybox [linux command]
You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.
FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:
ls -a /system/bin/sh
Hope this helps.
In reply to Igor Ganapolsky, You would have to have a database set up for locate.
Probably find would be adequate for your needs.
example:
find -name *.apk

Categories

Resources