Changing Android Device orientation with ADB - android

I'm using Android 4.4 on a real device and I want to set the device orientation via adb. I don't want it done with uiautomator since it won't last after the termination of the uiautomator code.
How can I do this?

Instead of using "adb shell content", there's a more clean way by using "adb shell settings". They are doing the same thing, put value to settings provider.
adb shell settings put system accelerometer_rotation 0 #disable auto-rotate
adb shell settings put system user_rotation 3 #270° clockwise
accelerometer_rotation: auto-rotation, 0 disable, 1 enable
user_rotation: actual rotation, clockwise, 0 0°, 1 90°, 2 180°, 3 270°

You may first need to turn off the automatic rotation:
adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0
Rotate to landscape:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:1
Rotate portrait:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0

Disable accelerometer_rotation and set the user_rotation
user_rotation Values:
0 # Protrait
1 # Landscape
2 # Protrait Reversed
3 # Landscape Reversed
accelerometer_rotation Values:
0 # Stay in the current rotation
1 # Rotate the content of the screen
Example using adb:
adb shell settings put system accelerometer_rotation 0
adb shell settings put system user_rotation 3
Example programmatically:
import android.provider.Settings;
// You can get ContentResolver from the Context
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, 3);

wm cmd can be used to set the user rotation on adb shell
wm help
set-user-rotation [free|lock] [-d DISPLAY_ID] [rotation]
Set user rotation mode and user rotation.
Example:
wm set-user-rotation lock 0
wm set-user-rotation lock 1

Related

Adb sleep function working on adb but not in a .sh file

i am trying to make a simple "tap and wait" script to run on my Android device through adb.
Opening adb on Powershell with the command ./adb shell works, running input tap 300 300 works, and running sleep 1does sleep for a second.
However, if I run a test.sh script with the Powershell command ./adb shell "sh /sdcard/Download/test.sh" the tapping part still works, but the sleep call gives me this instead: 'sdcard/Download/MIOPROGRAMMA.sh[2]: sleep: syntax error: Invalid argument '1 .
Any idea why this happen?
Just add ; after the int, i.e.:
input tap 250 250
sleep 3;
input tap 350 250
sleep 3;
input tap 450 250
sleep 3;
input tap 550 250
sleep 3;
input tap 650 250
sleep 3;
Why not use powershell's Start-Sleep cmdlet ? Have all your logic in the powershell script.
$sendTapCmd = "adb shell `"input tap 250 250`" "
$numberOfTaps = 5
for ($i=0;$i -lt $numberOfTaps;$i++) {
$output = Invoke-Expression $sendTapCmd
Start-Sleep -Milliseconds 3
}

How to set brightness through ADB on nexus 7

I found some info on the web:
echo 1 /sys/devices/platform/flashlight.0/leds/flashlight/brightness
But on my Nexus 7 (flashed an AOSP), I couldn't find that directory.
Any idea about which file should I write to? Is this doable?
After ls /sys/devices/platform, I got:
LID
alarm
arm-pmu.0
bcm4330_rfkill
bcmdhd_wlan.1
bluesleep
fiq_debugger.0
fsl-tegra-udc
gpio-keys.0
grouper_misc
leds-gpio
oprofile-perf.0
power
power.0
pwm-backlight
ram_console
reg-dummy
reg-fixed-voltage.1
reg-fixed-voltage.10
reg-fixed-voltage.11
reg-fixed-voltage.2
reg-fixed-voltage.3
reg-fixed-voltage.4
reg-fixed-voltage.6
reg-fixed-voltage.8
regulatory.0
sdhci-tegra.2
sdhci-tegra.3
serial8250
snd-soc-dummy
spdif-dit.0
spdif-dit.1
spi_tegra.0
spi_tegra.3
tegra-ehci.1
tegra-i2c.0
tegra-i2c.1
tegra-i2c.2
tegra-i2c.3
tegra-i2c.4
tegra-nvmap
tegra-otg
tegra-pcm-audio
tegra-se
tegra-snd-rt5640.0
tegra30-ahub
tegra30-dam.0
tegra30-dam.1
tegra30-dam.2
tegra30-hda
tegra30-i2s.1
tegra30-i2s.3
tegra30-i2s.4
tegra30-spdif
tegra_camera
tegra_pwm.0
tegra_rtc
tegra_smmu
tegra_uart.1
tegra_uart.2
tegra_uart.3
tegra_uart.4
tegra_wdt
uevent
There is a new binary shipping with Android Jellybean 4.2, which can be used to directly read/write to the system settings provider, accessible via command line.
For example: in order to increase brightness of the screen, use below command:
adb shell settings put system screen_brightness 200
Read more about SCREEN_BRIGHTNESS Note that the range of values is [0 - 255]
The range of values is not necessarily from 0 - 255. On my OnePlus for example
it ranges from 0-2047. If you want to know yours just set the brightness slider to max and
then type: adb shell settings get system screen_brightness.
You should use the pwm-backlight!
You can use this adb command to set screen brightness
adb shell settings put system screen_brightness 255
and if your device has auto brightness setting then use this command first
adb shell settings put system screen_brightness_mode 0
This did not work for the latest Nexus 7 (2013) for me. But this answer explains the way to find what you are looking for on any version: https://stackoverflow.com/a/13492336
However we need to note that in order to change any of this you need to have root access.

How to lock Android screen via ADB?

Is there a way to lock the Android screen via the ADB?
I find ways to lock the display in an apk, but I want to lock the screen from the PC via ADB, to simulate a display timeout, without having to wait for the timeout.
Is it possible to do this?
Thanks,
Diane
Cool, I just found KEYCODE_POWER which is 26.
so it works by sending:
adb shell input keyevent 26
which locks the screen if the screen is unlocked. If the screen is already locked, it wakes up the device.
My guess is that the only way to ensure that the screen is locked (off), is to unlock (we use keyevent 82 (menu), then lock it with the power button keyevent. Does anyone have any idea if this is true?
Michael R. Hines gave the what is arguably the easiest solution. However, the following line is not useful in later versions of Android.
adb shell input keyevent 82 # unlock
I've updated the shell script using coordinates for the individual device I want to wake (Tablet). My tablet does not support orientation changes for lockscreen events, so the values always work as the lockscreen is always in landscape. Should you require orientation change detection, a simple if/then/else would suffice in picking the correct coordinates to use for the orientation.
#!/bin/bash
if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
echo "Screen is off. Turning on."
adb shell input keyevent 26 # wakeup
adb shell input touchscreen swipe 930 380 1080 380 # unlock
echo "OK, should be on now."
else
echo "Screen is already on."
echo "Turning off."
adb shell input keyevent 26 # sleep
fi
Here's the whole thing in one single bash script which checks if the screen is actually on or not and then wakes up and unlocks the screen in one shot:
if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
echo "Screen is off. Turning on."
adb shell input keyevent 26 # wakeup
adb shell input keyevent 82 # unlock
echo "OK, should be on now."
else
echo "Screen is already on."
fi
You've already found a solution, but I'll put this code here for reference anyway.
What you could do is to inject event to "press" the power button twice. If you don't know the status of the device (display on/off), check whether the screen is currently on or off and press the power button accordingly.
Here's a simple monkeyrunner script:
import re
from java.util import *
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection() # connect to a device
device.shell("input keyevent KEYCODE_POWER") # turn screen off (or on?)
res = device.shell("dumpsys power") # fetch power state
m = re.search(r'.*mPowerState=([0-9]+).*', res) # parse the string
if m and int(m.group(1)) == 0: # screen is off
device.shell("input keyevent KEYCODE_POWER") # turn the screen on
In addition to the answers before, here's what I do to lock / unlock my screen using adb:
adb shell input keyevent 26 will lock the screen.
So, if you execute that command again, while the screen is turned off / locked, it will be turned on / unlocked.
adb shell input keyevent 26 will also unlock the screen (if the screen is locked).
Furthermore, I have also tested all commands, such as adb shell input keyevent number, and found out that adb shell input keyevent 3 also unlock the device.
I had also found out (by testing) that key 3 is the home button. So , if you have a physical home button (not the soft home button on the screen), you can also use this to unlock your device.
For those using earlier versions of android (4.2+ at least), dumpsys power has a different output.
Instead of using mPowerState= as the answer given by #Jakub Czaplicki, I used mScreenOn=.
p = Runtime.getRuntime().exec("su", null, null);
OutputStream o = p.getOutputStream();
o.write(("dumpsys power").getBytes("ASCII"));
o.flush();
o.close();
p.waitFor();
boolean screenState;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((res = in.readLine()) != null) dump += res;
screenState = dump.charAt( dump.indexOf("mScreenOn=") + 10 ) == 't';
screenState is true (screen on), or false (screen off).
You can use following command to trigger display ON.
adb shell input keyevent POWER
I tried and am using in my project, Hope it will work for you.
And here is the ruby code I used:
def ScreenCheck()
system("adb shell dumpsys power > c:/interact.log")
File.open("C:\\interact.log").each do |line|
if line[/mScreenOn/]
if line.strip == "mScreenOn=true"
p "Screen is On, Starting execution.."
else
p "Screen is Off, starting screen.."
system("adb shell input keyevent = POWER")
p "Starting execution.."
end
end
end
end
Here is a script to turn on/off the screen for every connected device including any pre-lollipop devices. I use this on my Jenkins server right before running any connected Android tests to make sure the devices are ready to go. Hope someone finds this useful!
#!/bin/sh
# Returns the power state of the screen 1 = on, 0 = off
getDisplayState() {
state=$(adb -s $1 shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')
# If we didn't get anything it might be a pre-lollipop device
if [ "$state" = "" ]; then
state=$(adb -s $1 shell dumpsys power | grep 'Display Power' | grep -oE '(ON|OFF)')
fi
if [ "$state" = "ON" ] || [ "$state" = "true" ]; then
return 1;
else
return 0;
fi
}
echo "Turning on screen on all connected devices..."
for device in `adb devices | grep device$ | cut -f1`
do
echo -n "Found device: $device ... "
getDisplayState $device
state=$?
# If the display is off, turn it on
if [ $state -eq 0 ]; then
echo "display was off, turning on"
adb -s $device shell input keyevent 26
else
echo "display was on"
fi
done

Auto screen orientation changes with respect to time intervals

I wanted to rotate the screen orientations i.e from landscape -> patriot and vice verse for every 500 ms(This is on real device(not on emulator)).
Is there any shell command where we can rotate the current screen orientation? This is not corresponding to any of the app. I just want to rotate the screen in all available directions with irrespective to current activity
I've checked with adb shell to change the screen to landscape:
service call window 18 i32 1
change the screen to portrait:
service call window 18 i32 0
But these are not working on real device.. Can any one please provide a better solution to do this, would really helpful for me.
Atleast share/point me to any available scripts/apks that will do this auto orientations.
Thanks in advance
Well adb shell is still a shell (if a bit cut down)
You can do this:
sw=1;
while true; do
[[ "$sw" = 1 ]] && sw=0 || sw=1;
service call window 18 i32 $sw;
sleep 1
done;
And this is the one line version of it (in case you can not write it in file)
sw=1; while true; do [[ "$sw" = 1 ]] && sw=0 || sw=1; service call window 18 i32 $sw; sleep 1; done;
This will switch orientation every second (I don't think sleep works with less than seconds)
I came across the same problem, and found a solution in here.
briefly, that's what you have to do:
First disable the auto rotation by using the following command:
adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0
and to rotate:
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:x
for landscape replace x (in the end of the line) with 1 and for portrait with 0, and the code from the previous comment should work just fine :)

How to get the Android device screen size from the adb command line?

I need a way to detect device screen size and density with adb.
If there is no solution, where can I get the complete list of all existing android device with their screen size and density ?
You can also access the WindowManager through ADB:
$ adb shell wm
usage: wm [subcommand] [options]
wm size [reset|WxH]
wm density [reset|DENSITY]
wm overscan [reset|LEFT,TOP,RIGHT,BOTTOM]
To get the screen resolution:
$ adb shell wm size
Physical size: 2880x1600
To get the screen the density:
$ adb shell wm density
Physical density: 320
You can also override the density by adding the new density:
$ adb shell wm density 160
LCD density is in the build.prop:
adb shell getprop ro.sf.lcd_density
And the resolution is availble in the dumpsys of the input activity:
# windows
adb shell dumpsys window | find "DisplayWidth"
# linux
adb shell dumpsys window | grep DisplayWidth
It works on all the devices I've tested with (2.2, 2.3.3, 2.3.4, 4.0.3; Acer Liquid E, HTC Wildfire S, HTC Incredible S, Motorola Atrix 4G, Samsung Galaxy Note, Samsung Galaxy Nexus), as well as the emulator, although the emulator's outputs are too clean to serve as a good example for parsing.
Using dumpsys
dumpsys window displays
shows something like this:
Display: mDisplayId=0
init=1080x1920 480dpi cur=1080x1920 app=1080x1920 rng=1080x1005-1920x1845
layoutNeeded=false
another way:
dumpsys display
also shows some interesting stuff like:
mDefaultViewport=DisplayViewport{valid=true, displayId=0, orientation=0, logicalFrame=Rect(0, 0 - 1080, 1920), physicalFrame=Rect(0, 0 - 1080, 1920), deviceWidth=1080, deviceHeight=1920}
and last but not least:
dumpsys power
will display something like
Electron Beam State:
mPrepared=false
mMode=2
mDisplayLayerStack=0
mDisplayWidth=1080
mDisplayHeight=1920
mSurfaceVisible=false
mSurfaceAlpha=0.0
that you could easily use to grep for mDisplayWidth and mDisplayHeight
To get required info from ADB, the following command executed from the command line will return a lot of useful properties about the connected devices
> adb shell getprop
To filter through these properties
on Unix use grep like
> adb shell getprop | grep density
on Windows use find like
> adb shell getprop | findstr "density"
Returned value looks like
[ro.sf.lcd_density]: [240]
for screen size put display instead of density
Work is Good:
dumpsys window | grep Display
return: Display: init=320x480 cur=320x480 app=320x480 rng=320x295-480x455
ANDROID:/ # dumpsys window | grep mGlobalConfiguration
mGlobalConfiguration={1.0 ?mcc?mnc [en_US] ldltr sw720dp w1920dp h532dp 160dpi
So resolution is 1920x720
You can get screen dimensions with this code:
public int getScreenHeight() {
return getDisplay().getHeight();
}
private Display getDisplay() {
return ((WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay();
}
public int getScreenWidth() {
return getDisplay().getWidth();
}
Once you have the Display in the code above you can use DisplayMetrics to get the density. DisplayMetrics will also give you absolute display with and height.
Look at the output of adb shell dumpsys. The screen size shows up there several times, along with lots of other information.
... although now I'm in the office, while it works on my phone, it's absent from the Galaxy tablet. Darn.
If you need to get the current status of range of Android device available in the market with it Screen Sizes and Densities Click here
This data is based on the number of Android devices that have accessed Android Market within a 7-day period ending on the data collection date

Categories

Resources