rename User from Shell - Android Android OS

Android examples for Android OS:Shell

Description

rename User from Shell

Demo Code


//package com.java2s;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    public static String TAG = "UM";

    public static String[] renameUser(String id, String newName) {
        Process p;/* www. ja  va 2  s  . c o  m*/
        try {
            p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            os.writeBytes("cat /data/system/users/" + id + ".xml\n");
            os.writeBytes("exit\n");
            ArrayList<String> users = new ArrayList<String>();
            String test;
            int ctr = 1;
            while ((test = bf.readLine()) != null) {

                Log.i(TAG, test);
                users.add(test);
                Log.i(TAG, "" + (ctr++));

            }
            Log.i(TAG, "Loop done.");

            String[] content = (String[]) users.toArray(new String[users
                    .size()]);

            for (ctr = 0; ctr < content.length; ctr++) {
                int begin = content[ctr].indexOf("<name>");
                if (begin != -1) {
                    int end = content[ctr].indexOf("</name>");
                    begin = begin + 6;
                    String oldName = content[ctr].substring(begin, end);
                    content[ctr] = content[ctr]
                            .replaceAll(oldName, newName);
                    Log.i(TAG, content[ctr]);
                }
            }

            String savedFile = Environment.getExternalStorageDirectory()
                    .getAbsoluteFile() + "/" + id + ".xml";

            FileWriter fw = new FileWriter(new File(savedFile));
            BufferedWriter bw = new BufferedWriter(fw);

            for (ctr = 0; ctr < content.length; ctr++) {
                bw.write(content[ctr]);
                bw.write("\n");
            }

            bw.close();

            copyFile(id, savedFile);

            os.flush();
            return content;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void copyFile(String id, String savedFile) {
        Process p;
        try {
            p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());

            os.writeBytes("cat " + savedFile + " > /data/system/users/"
                    + id + ".xml\n");
            os.writeBytes("rm -f " + savedFile);
            os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials