[Miscellaneous Android Development] Bexton:
Yeah, recognized already that I completely forgot to mention that at all (to run the script as root). :rolleyes: I updated the inline instructions accordingly.

Sheesh. Okay, I think I got what's wrong here.

As far as I understood, you didn't even got to the point where the file

/data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml

stored the modified value, correct?

When I was looking at your manual procedure I recognized a small but probably crucial difference! Let's have a look at it. My initial, manual approach was:

# Login as root

bexton@pc> [B]adb shell[/B]
shell@phone:/ $ [B]su[/B]

# Copy the original prefs xml file to /sdcard/.

# NOTE: We use cp as root user, but with the --preserve switch to copy a file owned by Whatapp's OS user.

root@phone:/ # [B]cp -p /data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml /sdcard/[/B]

# So, at this point, the copied file /sdcard/com.whatsapp_preferences.xml is still owned by Whatapp's OS user.

# Now, my last 2 steps were to modify the read-receipts settings value in /sdcard/com.whatsapp_preferences.xml and copy back the modified file to its original location, which I did as follows:

root@phone:/ # [B]sed -i'.bak' 's/^.*\bread_receipts\b.*$/ <long name="read_receipts" value="0" \/>/g' /sdcard/com.whatsapp_preferences.xml[/B]
root@phone:/ # [B]cp -p /sdcard/com.whatsapp_preferences.xml /data/data/com.whatsapp/shared_prefs/[/B]

So all together, this was:

bexton@pc> [B]adb shell[/B]
shell@phone:/ $ [B]su[/B]
root@phone:/ # [B]cp -p /data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml /sdcard/[/B]
root@phone:/ # [B]sed -i'.bak' 's/^.*\bread_receipts\b.*$/ <long name="read_receipts" value="0" \/>/g' /sdcard/com.whatsapp_preferences.xml[/B]
root@phone:/ # [B]cp -fp /sdcard/com.whatsapp_preferences.xml /data/data/com.whatsapp/shared_prefs/[/B]

So what happened with the last 2 commands that made it work for me, but breaks in the script version?

The core problem here is, as I just learned, that GNU sed's -i extension does not actually edit files in place (--in-place is a misnomer, in my opinion); it creates a temp file, deletes the original file, then renames the temp to the name of the original. The result is a new file - much possibly with a different owner.

So in my manual procedure, the result of the sed command worked fine except for the fact that it changed ownership on all the files it went through. The only problem is that these files (or at least the backup file) were owned by the root user - the user I run the command as. However, then I used the -f switch (to force overwrite) and the -p switch (to preserve permission, ownership and timestamps) to copy back the prefs file from /sdcard/ back to its original location in the Whatsapp data folder. That means, as a result, in the Whatsapp data folder there was a) no new file from another user (the backup file) and b) the modified prefs xml file still had its original ownership information. Basically, this kind of "fixed" sed's -i mode behaviour on the prefs file plus didn't created a new file in Whatsapp's data folder.

The last step to solve the puzzle is fairly simple. I just tried the procedure manually - as defined upthread - with all my friends' phones. Thus, I didn't noticed the sed behaviour. Plus, the friend Iinitially wrote the script for didn't told me that it wasn't working for him.

Anyway. Let's finally come to how to fix.

A quick look into the sed manual unveils that -c switch should do the trick:

-c, --copy

use copy instead of rename when shuffling files in -i mode
(avoids change of input file ownership)

Unfortunately, this switch is not enabled in all the busybox sed's for Android. Also, this would still leave us with a new file in Whatsapp's data directory. Even if all ownership information of existing files can be preserved, we should also not create any files in the folder that are not known to the Whatsapp app.

So basically my manual approach is the way to go:

a) Save the backup of the original prefs file somewhere on /sdcard/

b) Preserve ownership and permissions for /data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml

NOTE: Even if you got the value in the prefs xml saved to "0" and with no changes to ownership and permissions, you still need to restart any running Whatsapp process. Otherwise the change will have no effect! And, rebooting the device, resets the prefs xml file!

I'll post an updated version later. Until then, probably the easiest way to test this, is the manual way using a text editor app on your device.

- Open any text editor app with root capabilities (I used https://play.google.com/store/apps/details?id=com.maskyn.fileeditor )

- From the menu choose "Open file", navigate to /data/data/com.whatsapp/shared_prefs/ and open the file com.whatsapp_preferences.xml

- Find the line that reads <long name="read_receipts" value="SOMENUMBER" /> (SOMENUMBER is a placeholder, of course)

- Replace SOMENUMBER with 0 (zero), so the line reads <long name="read_receipts" value="0" />

- Save the file

- Now, close Whatsapp from the recent apps view and restart it.

- You can verify the change by running the following command (as root) from a terminal on your phone:

cat /data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml |grep read

- You can verify the ownership and permissions by running the following command (as root) from a terminal on your phone:

ls -l /data/data/com.whatsapp/shared_prefs/com.whatsapp_preferences.xml

Sure, feel free to share any suggestions! :)