Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 23ecae3b authored by Joe Onorato's avatar Joe Onorato
Browse files

Fix SharedPrefsBackupHelper so it doesn't hard code the paths to the files.

This took quite a bit of refactoring.
parent 0b774530
Loading
Loading
Loading
Loading
+26 −0
Original line number Original line Diff line number Diff line
@@ -29035,6 +29035,19 @@
<parameter name="mode" type="int">
<parameter name="mode" type="int">
</parameter>
</parameter>
</method>
</method>
<method name="getSharedPrefsFile"
 return="java.io.File"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="getSystemService"
<method name="getSystemService"
 return="java.lang.Object"
 return="java.lang.Object"
 abstract="false"
 abstract="false"
@@ -115907,6 +115920,19 @@
<parameter name="mode" type="int">
<parameter name="mode" type="int">
</parameter>
</parameter>
</method>
</method>
<method name="getSharedPrefsFile"
 return="java.io.File"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="name" type="java.lang.String">
</parameter>
</method>
<method name="getSystemService"
<method name="getSystemService"
 return="java.lang.Object"
 return="java.lang.Object"
 abstract="false"
 abstract="false"
+5 −1
Original line number Original line Diff line number Diff line
@@ -300,10 +300,14 @@ class ApplicationContext extends Context {
        return new File(prefsFile.getPath() + ".bak");
        return new File(prefsFile.getPath() + ".bak");
    }
    }


    public File getSharedPrefsFile(String name) {
        return makeFilename(getPreferencesDir(), name + ".xml");
    }

    @Override
    @Override
    public SharedPreferences getSharedPreferences(String name, int mode) {
    public SharedPreferences getSharedPreferences(String name, int mode) {
        SharedPreferencesImpl sp;
        SharedPreferencesImpl sp;
        File f = makeFilename(getPreferencesDir(), name + ".xml");
        File f = getSharedPrefsFile(name);
        synchronized (sSharedPrefs) {
        synchronized (sSharedPrefs) {
            sp = sSharedPrefs.get(f);
            sp = sSharedPrefs.get(f);
            if (sp != null && !sp.hasFileChanged()) {
            if (sp != null && !sp.hasFileChanged()) {
+25 −11
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.util.Log;


import java.io.File;
import java.io.FileDescriptor;
import java.io.FileDescriptor;


/** @hide */
/** @hide */
@@ -34,22 +35,34 @@ public class FileBackupHelper {
    public static void performBackup(Context context,
    public static void performBackup(Context context,
            ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState, String[] files) {
            ParcelFileDescriptor newState, String[] files) {
        String basePath = context.getFilesDir().getAbsolutePath();
        File base = context.getFilesDir();
        performBackup_checked(basePath, oldState, data, newState, files);
        final int N = files.length;
        String[] fullPaths = new String[N];
        for (int i=0; i<N; i++) {
            fullPaths[i] = (new File(base, files[i])).getAbsolutePath();
        }
        performBackup_checked(oldState, data, newState, fullPaths, files);
    }
    }


    /**
    /**
     * Check the parameters so the native code doens't have to throw all the exceptions
     * Check the parameters so the native code doens't have to throw all the exceptions
     * since it's easier to do that from java.
     * since it's easier to do that from java.
     */
     */
    static void performBackup_checked(String basePath,
    static void performBackup_checked(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState, String[] files, String[] keys) {
            ParcelFileDescriptor newState, String[] files) {
        if (files.length == 0) {
        if (files.length == 0) {
            return;
            return;
        }
        }
        if (basePath == null) {
        // files must be all absolute paths
            throw new NullPointerException();
        for (String f: files) {
            if (f.charAt(0) != '/') {
                throw new RuntimeException("files must have all absolute paths: " + f);
            }
        }
        // the length of files and keys must be the same
        if (files.length != keys.length) {
            throw new RuntimeException("files.length=" + files.length
                    + " keys.length=" + keys.length);
        }
        }
        // oldStateFd can be null
        // oldStateFd can be null
        FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
        FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
@@ -58,13 +71,14 @@ public class FileBackupHelper {
            throw new NullPointerException();
            throw new NullPointerException();
        }
        }


        int err = performBackup_native(basePath, oldStateFd, data.mBackupWriter, newStateFd, files);
        int err = performBackup_native(oldStateFd, data.mBackupWriter, newStateFd, files, keys);


        if (err != 0) {
        if (err != 0) {
            throw new RuntimeException("Backup failed"); // TODO: more here
            // TODO: more here
            throw new RuntimeException("Backup failed 0x" + Integer.toHexString(err));
        }
        }
    }
    }


    native private static int performBackup_native(String basePath, FileDescriptor oldState,
    native private static int performBackup_native(FileDescriptor oldState,
            int data, FileDescriptor newState, String[] files);
            int data, FileDescriptor newState, String[] files, String[] keys);
}
}
+2 −4
Original line number Original line Diff line number Diff line
@@ -26,16 +26,14 @@ public class SharedPreferencesBackupHelper {
    public static void performBackup(Context context,
    public static void performBackup(Context context,
            ParcelFileDescriptor oldSnapshot, ParcelFileDescriptor newSnapshot,
            ParcelFileDescriptor oldSnapshot, ParcelFileDescriptor newSnapshot,
            BackupDataOutput data, String[] prefGroups) {
            BackupDataOutput data, String[] prefGroups) {
        String basePath = "/xxx"; //context.getPreferencesDir();

        // make filenames for the prefGroups
        // make filenames for the prefGroups
        final int N = prefGroups.length;
        final int N = prefGroups.length;
        String[] files = new String[N];
        String[] files = new String[N];
        for (int i=0; i<N; i++) {
        for (int i=0; i<N; i++) {
            files[i] = prefGroups[i] + ".xml";
            files[i] = context.getSharedPrefsFile(prefGroups[i]).toString();
        }
        }


        FileBackupHelper.performBackup_checked(basePath, oldSnapshot, data, newSnapshot, files);
        FileBackupHelper.performBackup_checked(oldSnapshot, data, newSnapshot, files, prefGroups);
    }
    }
}
}
+9 −1
Original line number Original line Diff line number Diff line
@@ -254,11 +254,19 @@ public abstract class Context {
     * <p>Note: this is not generally useful for applications, since they should
     * <p>Note: this is not generally useful for applications, since they should
     * not be directly accessing the file system.
     * not be directly accessing the file system.
     *
     *
     *
     * @return String Path to the code and assets.
     * @return String Path to the code and assets.
     */
     */
    public abstract String getPackageCodePath();
    public abstract String getPackageCodePath();


    /**
     * {@hide}
     * Return the full path to the shared prefs file for the given prefs group name.
     *
     * <p>Note: this is not generally useful for applications, since they should
     * not be directly accessing the file system.
     */
    public abstract File getSharedPrefsFile(String name);

    /**
    /**
     * Retrieve and hold the contents of the preferences file 'name', returning
     * Retrieve and hold the contents of the preferences file 'name', returning
     * a SharedPreferences through which you can retrieve and modify its
     * a SharedPreferences through which you can retrieve and modify its
Loading