How to set On/Off Timers on Android 9 using ADB? - android

I am trying to set an automatic on/off timer for a device that is running Android 9 (For examples sake let's say I want it to turn on at 9 AM and turn off at 5PM) This must happen daily.
I want this to be done using adb commands so that it can be automated down the line.
If I go into settings I can navigate as so: Settings > Accessibility > Scheduled Power on and off
once in here I can set the Power off time, Power on time and the 2 relative Repeats
Any changes I make (physically or via adb) are then required to be confirmed by pressing the Save Settings button at the bottom of the screen.
Pressing the Save Settings button triggers the introduction of multiple variables:
close_machine_time_hour
close_machine_time_mins
open_machine_time_hour
open_machine_time_mins
power_off_cycle_mode
power_on_cycle_mode
power_off_date
power_on_date
power_way
machine_time_secs
Of which the 2 I am most concerned about are the last 2 as these seem like odd variables to only introduce when activating a niche setting (I feel as though they should either be there always)
Question 1: would be is this a standard Android feature? Or is this something that has been done for custom ROM?
I have tried setting all the settings as they appear in settings grabbed using command adb shell settings list system
They are set individually using the commands below:
adb shell settings put system timer_power_switch_settings 1
adb shell settings put system close_machine_time_hour 09
adb shell settings put system close_machine_time_mins 00
adb shell settings put system open_machine_time_hour 17
adb shell settings put system open_machine_time_mins 00
adb shell settings put system power_off_time 9:00:16
adb shell settings put system power_on_time 17:00:16
adb shell settings put system power_off_cycle_mode 0-1-2-3-4-5-6-
adb shell settings put system power_on_cycle_mode 0-1-2-3-4-5-6-
You'll note that in essence I am setting the time twice, which is quite odd.
The key thing is that none of this works unless I physically press the save button (despite showing visual feedback on the device screen that values change as I alter them via adb). Which is fine for 1 device but will become quite laborious if it needs to be done for 30+ devices at a time.
Question 2: Is there to simulate this Save Settings button click without simulating a tap on screen? Or to bypass this setting entirely?
Question 2.A: Is there a way to continuously monitor activity using an adb command? so I can run it, then click the button, and it will show me what's going on in the background?

Related

adb - Script to add PIN but not secure start-up

I'm trying to write a shellscript to create android emulators programatically (OS version >= 7.1.1), and I want those emulators to have a PIN but not "secure start-up".
Note that "secure start-up" is not the "lock screen" and also not the screen to unlock an encrypted device.
The secure start prevents android from booting without first inserting the PIN; lock screen is what appears after the boot up has finished.
Here's the description that shows up on Android 8.1 when you set the PIN manually on the emulator:
And what shows up when the emulator starts with the secure start-up activated:
I only managed to insert the PIN using the command:
adb -s <emulator_name> shell locksettings set-pin 1111
but this also activates the secure-start and I didn't find a way to deactivate it.
Do you know a command to deactivate only the secure-start and not the PIN? Thanks!

Automatically configure Android settings?

At work, we have an Android-based infotainment system that we're constantly deploying new versions to, on a half-dozen different test benches. The deployment script does the moral equivalent of:
for apk in ${apk_files}; do
adb install -r ${apk]
done
After this, we need to manually execute the following steps:
Set the home app to be one of our just-installed applications (Always, not Just Once)
Become a developer, and enable the Stay Awake option
Select the Google TTS engine for text-to-speech functionality rather than Pico
Executing these steps after each deploy is a giant PITA. People often forget one or more steps, and leave the test bench in a non-working state. This results in a bunch of 'bogus' bug reports that waste everbody's time.
Is there some way (using adb, perhaps) that we can automate these steps?
You can disable other home apps with adb shell pm disable .... I don't think there's a command line option to set apps as default. I remember looking into this before and there was a "preferred application" XML file where this was stored. If you want to look into it, the magic happens in PackageManagerService.addPreferredActivityInternal(). Looks like it writes the data to a file on disk: package-restrictions.xml. I suppose it's possible you could figure out the format thereof and write the file (you'd need root).
This is controlled by a system settings, "stay_on_while_plugged_in". You can set it using adb shell settings system put ....
The TTS engine is stored in a secure setting, "tts_default_synth". You can see the value like,
$ adb shell settings get secure tts_default_synth com.svox.pico
com.svox.pico
And you can set it with adb shell settings put secure "tts_default_synth" <the value>.
I noticed that if the value was not been previously set, when you get the value using the settings command you get null and it's not listed in settings list, even though there is a default value. As of Android 6 (I think), settings are no longer in a DB but rather are stored in XML files in /data/system/users/0/settings_*.xml. You can see the values therein.

Programmatically enable/disable "Battery Saver" mode

I am trying to find if I can enable and/or disable Android's built-in "Battery Saver" mode programmatically.
Any official approaches, or trickery, are welcome.
For reference, here is how to do it following Android's standard UI in 5.0:
http://www.androidcentral.com/android-50-lollipop-basics-how-get-more-life-between-charges-battery-saver
I am aware you can detect it -- that is not what I am after.
Thanks all.
You can enable/disable Battery Saver programmatically on rooted devices. You have to edit the low_power value in global table, in /data/data/com.android.providers.settings/databases/settings.db file.
If your device supports settings command, you can execute (as root):
settings put global low_power 1
to enable Energy Saver and
settings put global low_power 0
to disable it.
If it doesn't, use sqlite3 command:
sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='1' where name='low_power';"
sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='0' where name='low_power';"
Remember that you have to unplug your phone from PC first, otherwise system will disable Energy Saver. Use ADB over WiFi or Android Terminal (Emulator).
UPDATE:
The sqlite3 method doesn't seem to be reliable anymore.
I'm not sure if android.os.action.POWER_SAVE_MODE_CHANGED broadcast gets send. Maybe you have to send it manually, like in code from here:
private static String COMMAND_ENABLE = "settings put global low_power 1\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode true\n";
private static String COMMAND_DISABLE = "settings put global low_power 0\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode false\n";
Also, it's been reported that a new power_saving entry appeared in settings database, however in Android 6.0.1 I haven't managed to find it. In Android 7.1.1_r13 low_power is still used internally (e.g. here), however it may get changed in some Android release. You may want to keep checking changes in e.g. this and this file.
You cannot without rooting your phone. I am not sure why this is the case, especially where location services are required now for viewing scan results since SdkVersion 23+ .
This issue is very revealing.
https://code.google.com/p/android/issues/detail?id=185370
The best answer is application developers are being forced to crowd source network location for their google overlords. Notice, the OS itself has no problem displaying scan results without location services on.
Revealing that there is no way to turn on location services without GPS where location services are inactive. This two step shuffle is a major quality of experience issue for users. Turn location services on, then turn GPS off.
Watch this space, more lock downs on the way.

Wake and unlock Android phone screen when compile and run project?

When build and run iOS app using Xcode, the phone becomes awake and app runs. Is there a way to wake and unlock Android phone (or tablet) screen then run installed Android app (Eclipse, Android Studio)?
One solution: set the following flags in your activity class(es):
if (BuildConfig.DEBUG) {
// These flags cause the device screen to turn on (and bypass screen guard if possible) when launching.
// This makes it easy for developers to test the app launch without needing to turn on the device
// each time and without needing to enable the "Stay awake" option.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
These flags will:
Turn on the screen
Bypass the lock-screen
Allow showing the activity even if the device is locked
By setting those flags in your base activity, you'll be able to continue using your application even if the device was off and/or locked at the time of running. If you try to leave your application's process (i.e., hitting the home button, or switching to another app), the lock screen will appear, and you'll have to manually unlock to continue using the device.
Warning: this should only be used while developing/debugging your application, so I recommend keeping the if (BuildConfig.DEBUG) check, as it is in this example
Better and easiest solution is use the option (in development section) that the screen never goes off I thing the name is "Stay awake". This option prevent your phone to get locked when the usb cable is connected.
Another way is to write a script which runs before the app deploys and unlocks the device. This requires no app code change and will not change screen timeout which can have security implications.
You can find the full setup here
#!/bin/bash
# When a device is attached there will be atleast 3 lines -> heading, device details, an empty new line
if adb devices | wc -l | grep "3"; then
# Check if device locked, this may differ on some OEMs
if adb shell dumpsys window | grep "mInputRestricted=true"; then
echo "Device is Locked"
adb shell input keyevent KEYCODE_WAKEUP # wakeup device
adb shell input touchscreen swipe 530 1420 530 1120 # swipe up gesture
adb shell input text "000000" # <- Change to the your device PIN/Password
#adb shell input keyevent 66 # simulate press enter, if your keyguard requires it
else
echo "Device already unLocked"
fi
# 2 = Stay awake on USB, 0 = reset
adb shell settings put global stay_on_while_plugged_in 2
#adb shell settings put system screen_brightness 700
adb shell input keyevent KEYCODE_WAKEUP
adb shell input touchscreen tap 0 0 # this will wake up the screen and won't have any unwanted touches
else
echo "There should be only one device connected at a time"
fi
return 0
You cant unlock and wake the phone as that would not be very secure, however you can enable "Stay awake" in developer options.

How to simulate sleep mode on Android emulator

Some users of my Android application report bugs when the mobile enters sleep/power saving mode.
I would like to test that issue on the Android Virtual Device. Is it possible to simulate the mobile entering in sleep/power saving mode on the AVD ?
Thanks in advance.
Try the following on emulator
Go to Settings
Go to Developer options
Un-check Stay awake (3rd option from the top)
The emulator will go to sleep after 1 min (default setting) if not changed.
Press the power button to wake up the emulator.
Developer options is not enabled by default, to enable it, do the following:
Go to Settings
Click About emulated device
Click Build number 7 times, you should be notified that Developer options is now enabled
Go back and you should see Developer options
On emulator, go Settings/Security/Screen lock change from 'None' to 'Swipe'
Then, click 'power' button on right control bar to turn off screen, and press again to turn on screen. Now the lock screen will show up.
The Power Button on the sidebar next to emulator will do it.
The hotkey on my Mac is ⌘ P
Note: You will need to set Lock Screen using Swipe instead of None
To put the device to sleep using command line, run:
adb shell input keyevent 223
To wake the device from sleep using command line, run:
adb shell input keyevent 224
For more info about key events you can send with ADB, check out the KEYCODE_... constants for KeyEvent, e.g.:
/** Key code constant: Sleep key.
* Puts the device to sleep. Behaves somewhat like {#link #KEYCODE_POWER} but it
* has no effect if the device is already asleep. */
public static final int KEYCODE_SLEEP = 223;
/** Key code constant: Wakeup key.
* Wakes up the device. Behaves somewhat like {#link #KEYCODE_POWER} but it
* has no effect if the device is already awake. */
public static final int KEYCODE_WAKEUP = 224;
Somehow fn + F7 doesn't work on my mac. So what I use instead is:
adb shell input keyevent 26
This sends the POWER KEY event and will turn off the screen. Note: It will not show that the screen is off. Image wills tay. But you can't interact with it. As soon as you do adb shell input keyevent 26 again, you will see the lock screen indicating, that the device was off before.
By pressing F7 you can emulate sleep mode in your emulator.

Categories

Resources