500865
500865

Reputation: 7120

android - changing the system settings of a rooted device from inside the app

I am trying to change the sms limit described here from inside the app. The app is assumed to be running on a rooted device. I'm using the RootTools to check if the device is rooted or not. I'm trying to figure out how to write the actual setting on to the settings.db. Any suggestion which could help me in this is highly appreciated.

Thanks in advance.

Upvotes: 4

Views: 27688

Answers (3)

Upendra Shah
Upendra Shah

Reputation: 2301

Yes, You can do this if you have root access. Its a lengthy process but you can do this :

Step 1: copy settings.db from /data/data/com.android.providers.settings/databases/ using RootBrowser from the device and than change the value using sqliteBrowser which you want to update than update settings.db and put in assets package of your project

Step 2: Copy this file into your application folder (anywhere where you can access) using asset manager for that you need

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

this function will copy file from assets

public static void copyAssets(Context context, String assetPath, String outFilename) {
        AssetManager assetManager = context.getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(assetPath);
            File outFile = new File(context.getExternalFilesDir(null), outFilename);
              
            out = new FileOutputStream(outFile);
            copyFile(in, out);
        } catch (IOException e) {
            Log.e(TAG, "Failed to copy asset: " + outFilename, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

Step 3: overwrite the system settings.db file system path (destPath) is /data/data/com.android.providers.settings/databases/

public static void copyToSystem(final String sourceFilePath, final String destPath) {
        Thread background = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream os = new DataOutputStream(process.getOutputStream());
//                    
                    os.writeBytes("cp -f " + sourceFilePath + " " + destPath + "\n");
                    os.flush();
                    os.writeBytes("exit\n");
                    os.flush();
                    process.waitFor();
                    process.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                }
            }
        });
        background.start();
    }

Step 4: Reboot device

That's all done.

Upvotes: 0

Tomek
Tomek

Reputation: 556

Late answer, but you don't need to copy settings.db, you can update it with RootTools like

CommandCapture commandLogcat = new CommandCapture(0, "sqlite3 /data/data/com.android.providers.settings/databases/settings.db \"UPDATE secure SET value = '192.168.2.140' WHERE name = 'eth_ip';\"");
RootTools.getShell(true).add(commandLogcat).getExitCode();

Upvotes: 0

500865
500865

Reputation: 7120

I got this solved by doing this:

  • copy the /data/data/com.android.providers.settings/databases/settings.db to my application folder /data/data/my_app_directory/.
  • update the table in the database using the SQLiteDatabase class.
  • copy back the database from /data/data/my_app_directory/settings.db to /data/data/com.android.providers.settings/databases/

The copy operations were done by using RootTools.sendShell

Upvotes: 7

Related Questions