Delete contact via adb - android

How can I delete a contact using the adb shell? I know the raw_contact_id of the contact.
I tried different ways but none with success.
To edit a contact I used the command
am start -a android.intent.action.EDIT content://contacts/people/8
and it worked.
But deleting it with
am start -a android.intent.action.DELETE content://contacts/people/8
does not work and showed me this error message:
Activity not started, unable to resolve Intent { act=android.intent.action.DELETE dat=content://contacts/people/8 flg=0x10000000 }
(flg=0x10000000 means FLAG_RECEIVER_FOREGROUND, I think it is set automatically.)
Do I have to set some flags? Or why does this not work?
A more fancy way is to simulate the process, how a normal user deletes the contact.
am start -a android.intent.action.VIEW content://contacts/people/8
input keyevent 22 # right button
input keyevent 22 # reach field in the right corner
input keyevent 66 # enter
input keyevent 20 # go down
input keyevent 20 # go down --> reached "Delete"
input keyevent 66 # enter
input keyevent 22 # button "Ok"
input keyevent 66 # enter
After clicking on the "Delete" button, the contact app always crashes (Contacts has been closed). Using these key events never worked on my emulator -- if I click on the buttons by hand (with my mouse), everything works…
Third way could be deleting the entries in the databases. This does not make sense for me, because then they are completely deleted -- if they are deleted the "normal" way, the contacts are still stored in some tables (like "deleted contacts").

The content://contacts provider has been deprecated. You should be using content://com.android.contacts instead:
$ adb shell content query --uri content://com.android.contacts/contacts
To remove contact (where's id = 1)
$ adb shell content delete --uri content://com.android.contacts/contacts/1
To remove all contacts at once
$ adb shell pm clear com.android.providers.contacts

Related

ADB Shell Android how to delete group of contacts?

I am using adb shell to delete contacts from phone. For example if I have 50 contacts, how to delete first 10?
I use this to delete one by one, is there a way to delete more contacts at once, by one command, to make process faster?
adb shell content delete --uri content://com.android.contacts/contacts/1
I am not looking to clear all contacts, but to delete some and to leave the rest of the contacts.
First, you can query all your contacts:
$ adb shell content query --uri content://com.android.contacts/contacts
Row: 0 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Doe, John, sort_key_alt=Doe, John, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=S, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r100-4D5331434D4F31394337453333, phonebook_label_alt=S, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=19, contact_status=NULL, display_name=John Doe, sort_key=John Doe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=100, name_raw_contact_id=100, phonebook_bucket_alt=19
Row: 1 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Doe, Jane, sort_key_alt=Doe, Jane, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=C, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r101-2D374B394D4F3929432B512B312D3D, phonebook_label_alt=B, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=3, contact_status=NULL, display_name=Jane Doe, sort_key=Jane Doe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=101, name_raw_contact_id=101, phonebook_bucket_alt=2
Row: 2 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Roe, Jane, sort_key_alt=Roe, Jane, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=A, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r102-29432F4B31294D55393F3F, phonebook_label_alt=W, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=1, contact_status=NULL, display_name=Jane Roe, sort_key=Jane Roe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=102, name_raw_contact_id=102, phonebook_bucket_alt=23
Row: 3 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Roe, Richard, sort_key_alt=Roe, Richard, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=U, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r103-51432B313D2943434F2B4B31315B31, phonebook_label_alt=B, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=21, contact_status=NULL, display_name=Richard Roe, sort_key=Richard Roe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=103, name_raw_contact_id=103, phonebook_bucket_alt=2
You can use sed to, e.g., select the first three rows and trim them to their ids (i.e., the _id attribute:
$ adb shell content query --uri content://com.android.contacts/contacts | sed -n '1,3 s/^.* _id=\([[:digit:]]\+\),.*$/\1/p'
100
101
102
This sed command says not to print anything by default (the -n switch), but for lines 1 to 3 substitute the lines with the value of the _id and print the result (the p flag at the end of the substitution).
I now tried to loop over this output and issue an adb shell delete for every id, but this does not work. It will only issue a command for the first id. I don’t know exactly why this is, but it must have something to do with what the adb shell command does.
As a workaround, we can do all this directly in the adb shell. So, first execute adb shell and then in that shell you can directly use the content command. For better readability I first save the ids in a file and then loop over its lines. Of course, it’s perfectly possible to just pipe the output of sed into the while loop.
mycomputer$ adb shell
android$ content query --uri content://com.android.contacts/contacts | sed -n '1,3 s/^.* _id=\([[:digit:]]\+\),.*$/\1/p' >ids
android$ while read -r id; do content delete --uri content://com.android.contacts/contacts/"$id"; done <ids

adb command for call state details

I would like to get an adb command with a response of a code that map to current call state
the call state I mean are those in following link
https://developer.android.com/reference/android/telecom/Call.html#STATE_ACTIVE
those values are more representative and getting those values in command shell upon executing the adb command will be very helpful for me
I have only managed to get them on a log as per following command
adb logcat -d | findstr -i InCallFragment.setCallState
but I couldnot get the state value as a response of any adb command
Any help will be much appreciated
Thanks
for more illustration
please connect a phone to the PC , do a phone call and end it
use the above command to dump the buffer
refer to the state value
You can use adb shell service call telecom [code] command. The codes for getCallState() will be different depending on the Android version:
6.0.1: 26
7.0.0: 27
7.1.0: 27
7.1.2: 27
8.0.0: 29
8.1.0: 29
I have achieved what you want to do by modifying a custom ROM (LineageOS) and adding an android.util.Log line to print every state.
In my case I modified class:
Call
frameworks/opt/telephony/src/java/com/android/internal/telephony/Call.java
And what I did is inside getState(...) method, adding this line:
Log.i(myTAG, "getState state->" + mState.name());
With this what I have to do is search for myTAG in adb logcat.
I think otherwise you wont be able to do it...
You can dumpsys telecomm service:
adb shell dumpsys telecom
CallsManager:
mCalls:
[TC#7, ACTIVE, com.android.phone/com.android.services.telephony.TelephonyConnectionService, tel:***, A, childs(0), has_parent(false), [Capabilities: CAPABILITY_HOLD CAPABILITY_SUPPORT_HOLD CAPABILITY_MUTE CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO], [Properties:]]
mCallAudioManager:
All calls:
TC#7
Active dialing, or connecting calls:
TC#7
Ringing calls:
Holding calls:
Foreground call:
[TC#7, ACTIVE, com.android.phone/com.android.services.telephony.TelephonyConnectionService, tel:***, A, childs(0), has_parent(false), [Capabilities: CAPABILITY_HOLD CAPABILITY_SUPPORT_HOLD CAPABILITY_MUTE CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO], [Properties:]]
mTtyManager:
mCurrentTtyMode: 0
mInCallController:
mInCallServices (InCalls registered):
.
.
Call TC#7 [2018-06-05 14:38:41.505](MO - outgoing)
To address: tel:***
14:38:41.508 - CREATED:PCR.oR#DMA
14:38:41.511 - SET_CONNECTING (ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}, [8c3d1caa626a79d75b154221ea94852a62fee7b3], UserHandle{0}):PCR.oR#DMA
14:38:41.847 - AUDIO_ROUTE (Leaving state QuiescentEarpieceRoute):PCR.oR->CAMSM.pM_2001->CARSM.pM_SWITCH_FOCUS#DMA_2_2
14:38:41.847 - AUDIO_ROUTE (Entering state ActiveEarpieceRoute):PCR.oR->CAMSM.pM_2001->CARSM.pM_SWITCH_FOCUS#DMA_2_2
14:38:43.442 - BIND_CS (ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}):NOCBIR.oR#DMU
14:38:43.519 - CS_BOUND (ComponentInfo{com.android.phone/com.android.services.telephony.TelephonyConnectionService}):SBC.oSC#DMY
14:38:43.519 - START_CONNECTION (tel:***):SBC.oSC#DMY
14:38:43.703 - CAPABILITY_CHANGE (Current: [[ sup_hld mut !v2a]], Removed [[]], Added [[ sup_hld mut !v2a]]):CSW.hCCC#DMg
14:38:43.706 - SET_DIALING (successful outgoing call):CSW.hCCC#DMg
14:38:47.560 - SET_ACTIVE (active set explicitly):CSW.sA#DNM
14:38:47.639 - CAPABILITY_CHANGE (Current: [[ hld sup_hld mut !v2a]], Removed [[]], Added [[ hld]]):CSW.sCC#DNY
Timings (average for this call, milliseconds):
bind_cs: 77.00
outgoing_time_to_dialing: 187.00

Finding right Android touchscreen driver file

My goal is to add a single printk command to one of the driver files for my phone's touchscreen. I would like to run this printk command everytime the screen recieves touch input. I found the list of touchscreen driver files shown below. I'm just hoping someone with some experience might be able to point me to the correct file to place this printk command.
ad7877.c atmel_224e.c cy8ctmg110_ts.c gunze.c intel-mid-touch.c
max11801_ts.c pcap_ts.c touchit213.c ucb1400_ts.c wm9712.c
ad7879.c atmel_mxt_ts.c da9034-ts.c h3600_ts_input.c jornada720_ts.c
mc13783_ts.c penmount.c touchright.c usbtouchscreen.c wm9713.c
ad7879.h atmel_tsadcc.c dynapro.c hampshire.c Kconfig
mcs5000_ts.c s3c2410_ts.c touchwin.c w90p910_ts.c wm97xx-core.c
ad7879-i2c.c atmel-wm97xx.c eeti_ts.c hp680_ts_input.c lpc32xx_ts.c
migor_ts.c st1232.c tps6507x-ts.c wacom_w8001.c zylonite-wm97xx.c
ad7879-spi.c bu21013_ts.c elo.c htcpen.c mainstone-wm97xx.c
mk712.c stmpe-ts.c tsc2005.c wm831x-ts.c
First Check the Driver file by using the following command on terminal:
$logcat | grep EventHub
You will find all the input devices (Including your touch driver) in the prints.
Go to that file and add the print command in interrupt handler.
Try running the getevent command in a shell on the Android device. The touchscreen should be listed, and the name should be the driver being used.
Here's what came up on my phone:
add device 7: /dev/input/event1
name: "synaptics_dsx"
This means my phone uses the synaptics_dsx touchscreen driver.

How to launch android app through python

I want to run any app (say Settings) after rebooting tablet. Can I use os.system or do I have to use other methods.
import os,time
for i in range(0,3):
os.system("adb reboot")
time.sleep(60)
Yes, you can use os.system to execute ADB commands. If you want to validate the command executed successfully, take a look at the check_output(...) function which is apart of the subprocess library. This code snipet is how I choose to implement the check_output function. For the full code look here.
def _run_command(self, cmd):
"""
Execute an adb command via the subprocess module. If the process exits with
a exit status of zero, the output is encapsulated into a ADBCommandResult and
returned. Otherwise, an ADBExecutionError is thrown.
"""
try:
output = check_output(cmd, stderr=subprocess.STDOUT)
return ADBCommandResult(0,output)
except CalledProcessError as e:
raise ADBProcessError(e.cmd, e.returncode, e.output)
To launch an application you can use the command am start -n yourpackagename/.activityname. To launch the Settings App, run adb shell am start -n com.android.settings/com.android.settings.Settings. This stackoverflow question shows you in detail the options you can use to start the application via a command line intent.
Other tips:
I created an ADB wrapper written in python along with a few other python utilities that may aid in what you are trying to accomplish. For example, instead of calling time.sleep(60) to wait for the reboot, you use adb to poll the status of the property sys.boot_completed and once the property is set the device has finished booting and you can launch any application. Below is a reference implementation you can use.
def wait_boot_complete(self, encryption='off'):
"""
When data at rest encryption is turned on, there needs to be a waiting period
during boot up for the user to enter the DAR password. This function will wait
till the password has been entered and the phone has finished booting up.
OR
Wait for the BOOT_COMPLETED intent to be broadcast by check the system
property 'sys.boot_completed'. A ADBProcessError is thrown if there is an
error communicating with the device.
This method assumes the phone will eventually reach the boot completed state.
A check is needed to see if the output length is zero because the property
is not initialized with a 0 value. It is created once the intent is broadcast.
"""
if encryption is 'on':
decrypted = None
target = 'trigger_restart_framework'
print 'waiting for framework restart'
while decrypted is None:
status = self.adb.adb_shell(self.serial, "getprop vold.decrypt")
if status.output.strip() == 'trigger_restart_framework':
decrypted = 'true'
#Wait for boot to complete. The boot completed intent is broadcast before
#boot is actually completed when encryption is enabled. So 'key' off the
#animation.
status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
print 'wait for animation to start'
while status == 'stopped':
status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
print 'waiting for animation to finish'
while status == 'running':
status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
else:
boot = False
while(not boot):
self.adb.adb_wait_for_device(self.serial)
res = self.adb.adb_shell(self.serial, "getprop sys.boot_completed")
if len(res.output.strip()) != 0 and int(res.output.strip()) is 1:
boot = True

adb shell input unicode character

Knowing the basic key mappings described in ADB Shell Input Events I get the emulation of text input and special keys working quite well. But what about Unicode characters? For instance I want to use umlauts from the German QWERTZ keyboard layout.
This gets me:
$ adb shell input text ö
Killed
So it seems to crash and
adb shell input text \xFC
prints xFC on the input. I have tried to the the events with getevent but I haven't found a direct mapping, I've also looked into the keymapping file /system/usr/keylayout/Qwerty.kl
I believe the only possibility is via the clipboard, but as pointed out in the question Pasting text into Android emulator clipboard using adb shell it seems to be unknown how to use it for Android Ice Cream Sandwich or later..
I wrote a virtual keyboard that accept broadcast intent, so you can send unicode characters to the editText view via adb.
for e.g.
adb shell am broadcast -a ADB_INPUT_TEXT --es msg "你好嗎! Hello!"
Here is the github project:
https://github.com/senzhk/ADBKeyBoard
Hope this little project would help.
Actually ADBKeyBoard is very good! Thanks for Eric Tang !
Some useful extensions for comfortable usage:
Switch to ADBKeyBoard from adb:
adb shell ime set com.android.adbkeyboard/.AdbIME
Check your available le virtual keyboards:
ime list -a
Use simple quote characters -not double as in example above- if your shell not accepts "!" (explanation sign)
adb shell am broadcast -a ADB_INPUT_TEXT --es msg 'Accented characters here'
Switch back to original virtual keyboard: (swype in my case...)
adb shell ime set com.nuance.swype.dtc/com.nuance.swype.input.IME
Use adb over wifi to simplify your life... :)
input won't work because it can only send single key event through the virtual keyboard (check the source code if you don't know what I mean).
I think the only way left is using Instrumentation. I guess you can create a test for your Activity and then do something like this:
final Instrumentation instrumentation = getInstrumentation();
final long downTime = SystemClock.uptimeMillis();
final long eventTime = SystemClock.uptimeMillis();
final KeyEvent altDown = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_GRAVE, 1, KeyEvent.META_ALT_LEFT_ON);
final KeyEvent altUp = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_GRAVE, 1, KeyEvent.META_ALT_LEFT_ON);
instrumentation.sendKeySync(altDown);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_A);
instrumentation.sendKeySync(altUp);
instrumentation.sendKeySync(altDown);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
instrumentation.sendKeySync(altUp);
instrumentation.sendKeySync(altDown);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
instrumentation.sendKeySync(altUp);
instrumentation.sendKeySync(altDown);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O);
instrumentation.sendKeySync(altUp);
instrumentation.sendKeySync(altDown);
instrumentation.sendCharacterSync(KeyEvent.KEYCODE_U);
instrumentation.sendKeySync(altUp);
This will send the modified keys: àèìòù
update 2022
https://stackoverflow.com/a/71367206/236465 shows another solution using AndroidViewClient/culebra and CulebraTester2-public backend.

Categories

Resources