How to change permissions on database

anaylor01

Well-Known Member
Licensed User
Longtime User
I was able to use root explorer to go into the settings.db under /data/data/com.android.providers.settings/databases to change the permissions to get access. Now my question is how to I change permissions to the database through code?
 

anaylor01

Well-Known Member
Licensed User
Longtime User
I manually changed the permissions with root explorer. Now I need to figure out how to do it in my app. I have no idea how I would do it. I am pretty new to Android development and really new to B4A.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Ok. I ran the following code and went back into the database with root explorer but the permissions didn't change. Any help would be appreciated.
B4X:
Dim p As Phone
Dim sb As StringBuilder
sb.Initialize
p.Shell("chmod 999", Null, sb, Null)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I guess it would help if I told it what folder/file to change permissions on. Not sure how to do that. Tried the below but it didn't work either.
Dim p As Phone
Dim sb As StringBuilder
sb.Initialize
p.Shell("chmod 999 /data/data/com.android.providers.settings/databases/settings.db", Null, sb, Null)
 
Last edited:
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I think this should be the right code but it doesn't work. Please a little help here.
B4X:
Dim p As Phone
Dim sb As StringBuilder
sb.Initialize
p.Shell("chmod 777 /data/data/com.android.providers.settings/databases/settings.db", Null, sb, Null)
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
In what way does it not work? Why not get the return value and see what it says the problem is.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
From what I read by searching Google, Shell commands are not part of the SDK and thus what you are doing can't be relied upon to work.

However, I found a post saying:

Java doesn't have native support for platform dependent operations like chmod. However, Android provides utilities for some of these operations via android.os.FileUtils. The FileUtils class is not part of the public SDK and is therefore not supported. So, use this at your own risk:

You could try it by wrapping the following code into a B4A-library:

B4X:
public int chmod(File path, int mode) throws Exception {
  Class fileUtils = Class.forName("android.os.FileUtils");
  Method setPermissions =
      fileUtils.getMethod("setPermissions", String.class, int.class, int.class, int.class);
  return (Integer) setPermissions.invoke(null, path.getAbsolutePath(), mode, -1, -1);
}

source: http://www.android10.org/index.php/forums/14-tipstricks/570-using-chmod-in-android
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Really? WOW I thought this would be a no brainer for you guys. I am pretty sure this will work. I am just missing something simple. From ADB I typed "ADB SHELL" then enter. Which gave me the $ prompt and then typed "SU" and then enter. Which gave me the # prompt.
Then typed "chmod 777 /data/data/com.android.providers.settings/databases/settings.db". And it worked fine. Since my app already has SU access I would think that just sending the shell command "chmod 777 /data/data/com.android.providers.settings/databases/settings.db" would work.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
I am using the B4A Help viewer and didn't see anything at first but from one of the links I saw another link. It usually is pretty good at showing the links.
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Ok. I followed the tutorial. I used Eclipse to create a new library. I called it chmod. I added to the B4A library. When it tries to compile I get a undeclared variable error 'chmod'. I am not sure if the code below is the problem or not.
B4X:
package chmod.chmod;

import java.io.File;
import java.lang.reflect.Method;

public class Chmod {
    public int chmod(File path, int mode) throws Exception {
          Class fileUtils = Class.forName("android.os.FileUtils");
          Method setPermissions =
              fileUtils.getMethod("setPermissions", String.class, int.class, int.class, int.class);
          return (Integer) setPermissions.invoke(null, path.getAbsolutePath(), mode, -1, -1);
        }
}
Here is the code I have in B4A
B4X:
chmod("/data/data/com.android.providers.settings/databases/settings.db", 0777)
 
Upvote 0
Top