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

Commit c9141ca2 authored by David van Tonder's avatar David van Tonder Committed by Gerrit Code Review
Browse files

Merge "Also backup profiles file via backup manager." into cm-10.1

parents d029fe6e 416f0d44
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
@@ -613,6 +613,13 @@ public final class Profile implements Parcelable, Comparable {
        }
        }
    }
    }


    /** @hide */
    public void validateRingtones(Context context) {
        for (ProfileGroup pg : profileGroups.values()) {
            pg.validateOverrideUris(context);
        }
    }

    /** @hide */
    /** @hide */
    public static Profile fromXml(XmlPullParser xpp, Context context)
    public static Profile fromXml(XmlPullParser xpp, Context context)
            throws XmlPullParserException, IOException {
            throws XmlPullParserException, IOException {
+28 −0
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserException;


import android.content.Context;
import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcel;
@@ -218,6 +219,33 @@ public final class ProfileGroup implements Parcelable {
        return notification;
        return notification;
    }
    }


    private boolean validateOverrideUri(Context context, Uri uri) {
        if (RingtoneManager.isDefault(uri)) {
            return true;
        }
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        boolean valid = false;

        if (cursor != null) {
            valid = cursor.moveToFirst();
            cursor.close();
        }
        return valid;
    }

    void validateOverrideUris(Context context) {
        if (!validateOverrideUri(context, mSoundOverride)) {
            mSoundOverride = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mSoundMode = Mode.DEFAULT;
            mDirty = true;
        }
        if (!validateOverrideUri(context, mRingerOverride)) {
            mRingerOverride = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            mRingerMode = Mode.DEFAULT;
            mDirty = true;
        }
    }

    private void silenceNotification(Notification notification) {
    private void silenceNotification(Notification notification) {
        notification.defaults &= (~Notification.DEFAULT_SOUND);
        notification.defaults &= (~Notification.DEFAULT_SOUND);
        notification.sound = null;
        notification.sound = null;
+25 −7
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.app.IProfileManager;
import android.app.NotificationGroup;
import android.app.NotificationGroup;
import android.app.Profile;
import android.app.Profile;
import android.app.ProfileGroup;
import android.app.ProfileGroup;
import android.app.backup.BackupManager;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
@@ -34,12 +35,14 @@ import android.content.res.XmlResourceParser;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiSsid;
import android.net.wifi.WifiSsid;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiInfo;
import android.os.Environment;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserHandle;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.Log;
import android.util.Log;
import android.os.ParcelUuid;
import android.os.ParcelUuid;


import java.io.File;
import java.io.FileReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.IOException;
@@ -70,7 +73,8 @@ public class ProfileManagerService extends IProfileManager.Stub {


    public static final String PERMISSION_CHANGE_SETTINGS = "android.permission.WRITE_SETTINGS";
    public static final String PERMISSION_CHANGE_SETTINGS = "android.permission.WRITE_SETTINGS";


    private static final String PROFILE_FILENAME = "/data/system/profiles.xml";
    /* package */ static final File PROFILE_FILE =
            new File(Environment.getSystemSecureDirectory(), "profiles.xml");


    private static final String TAG = "ProfileService";
    private static final String TAG = "ProfileService";


@@ -89,7 +93,7 @@ public class ProfileManagerService extends IProfileManager.Stub {


    private Context mContext;
    private Context mContext;
    private boolean mDirty;
    private boolean mDirty;

    private BackupManager mBackupManager;
    private WifiManager mWifiManager;
    private WifiManager mWifiManager;
    private String mLastConnectedSSID;
    private String mLastConnectedSSID;


@@ -142,6 +146,7 @@ public class ProfileManagerService extends IProfileManager.Stub {


    public ProfileManagerService(Context context) {
    public ProfileManagerService(Context context) {
        mContext = context;
        mContext = context;
        mBackupManager = new BackupManager(mContext);
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mLastConnectedSSID = getActiveSSID();
        mLastConnectedSSID = getActiveSSID();


@@ -498,10 +503,19 @@ public class ProfileManagerService extends IProfileManager.Stub {
        return null;
        return null;
    }
    }


    // Called by SystemBackupAgent after files are restored to disk.
    void settingsRestored() {
        initialize();
        for (Profile p : mProfiles.values()) {
            p.validateRingtones(mContext);
        }
        persistIfDirty();
    }

    private void loadFromFile() throws RemoteException, XmlPullParserException, IOException {
    private void loadFromFile() throws RemoteException, XmlPullParserException, IOException {
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = xppf.newPullParser();
        XmlPullParser xpp = xppf.newPullParser();
        FileReader fr = new FileReader(PROFILE_FILENAME);
        FileReader fr = new FileReader(PROFILE_FILE);
        xpp.setInput(fr);
        xpp.setInput(fr);
        loadXml(xpp, mContext);
        loadXml(xpp, mContext);
        fr.close();
        fr.close();
@@ -530,7 +544,7 @@ public class ProfileManagerService extends IProfileManager.Stub {
                    addNotificationGroupInternal(ng);
                    addNotificationGroupInternal(ng);
                }
                }
            } else if (event == XmlPullParser.END_DOCUMENT) {
            } else if (event == XmlPullParser.END_DOCUMENT) {
                throw new IOException("Premature end of file while reading " + PROFILE_FILENAME);
                throw new IOException("Premature end of file while reading " + PROFILE_FILE);
            }
            }
            event = xpp.next();
            event = xpp.next();
        }
        }
@@ -609,11 +623,15 @@ public class ProfileManagerService extends IProfileManager.Stub {
        if (dirty) {
        if (dirty) {
            try {
            try {
                Log.d(TAG, "Saving profile data...");
                Log.d(TAG, "Saving profile data...");
                FileWriter fw = new FileWriter(PROFILE_FILENAME);
                FileWriter fw = new FileWriter(PROFILE_FILE);
                fw.write(getXmlString());
                fw.write(getXmlString());
                fw.close();
                fw.close();
                Log.d(TAG, "Save completed.");
                Log.d(TAG, "Save completed.");
                mDirty = false;
                mDirty = false;

                long token = clearCallingIdentity();
                mBackupManager.dataChanged();
                restoreCallingIdentity(token);
            } catch (Throwable e) {
            } catch (Throwable e) {
                e.printStackTrace();
                e.printStackTrace();
            }
            }
+30 −1
Original line number Original line Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server;
package com.android.server;




import android.app.backup.AbsoluteFileBackupHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.BackupDataOutput;
import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupAgentHelper;
@@ -46,6 +47,11 @@ public class SystemBackupAgent extends BackupAgentHelper {
    private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
    private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
    private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
    private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";


    private static final String PROFILES_FILENAME =
            ProfileManagerService.PROFILE_FILE.getName();
    private static final String PROFILES_FILE_DIRECTORY =
            ProfileManagerService.PROFILE_FILE.getParentFile().getAbsolutePath();

    // TODO: Will need to change if backing up non-primary user's wallpaper
    // TODO: Will need to change if backing up non-primary user's wallpaper
    private static final String WALLPAPER_IMAGE_DIR =
    private static final String WALLPAPER_IMAGE_DIR =
            Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
            Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
@@ -75,13 +81,16 @@ public class SystemBackupAgent extends BackupAgentHelper {
            keys = new String[] { WALLPAPER_INFO_KEY };
            keys = new String[] { WALLPAPER_INFO_KEY };
        }
        }
        addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files, keys));
        addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files, keys));
        addHelper("profiles", new AbsoluteFileBackupHelper(SystemBackupAgent.this,
                    ProfileManagerService.PROFILE_FILE.getAbsolutePath()));
        super.onBackup(oldState, data, newState);
        super.onBackup(oldState, data, newState);
    }
    }


    @Override
    @Override
    public void onFullBackup(FullBackupDataOutput data) throws IOException {
    public void onFullBackup(FullBackupDataOutput data) throws IOException {
        // At present we back up only the wallpaper
        // At present we back up only the wallpaper and profiles
        fullWallpaperBackup(data);
        fullWallpaperBackup(data);
        fullProfilesBackup(data);
    }
    }


    private void fullWallpaperBackup(FullBackupDataOutput output) {
    private void fullWallpaperBackup(FullBackupDataOutput output) {
@@ -95,6 +104,11 @@ public class SystemBackupAgent extends BackupAgentHelper {
                WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output.getData());
                WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output.getData());
    }
    }


    private void fullProfilesBackup(FullBackupDataOutput output) {
        FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
                PROFILES_FILE_DIRECTORY, PROFILES_FILENAME, output.getData());
    }

    @Override
    @Override
    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
            throws IOException {
            throws IOException {
@@ -105,13 +119,19 @@ public class SystemBackupAgent extends BackupAgentHelper {
        addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
        addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
                new String[] { WALLPAPER_IMAGE },
                new String[] { WALLPAPER_IMAGE },
                new String[] { WALLPAPER_IMAGE_KEY} ));
                new String[] { WALLPAPER_IMAGE_KEY} ));
        addHelper("profiles", new AbsoluteFileBackupHelper(SystemBackupAgent.this,
                ProfileManagerService.PROFILE_FILE.getAbsolutePath()));


        try {
        try {
            super.onRestore(data, appVersionCode, newState);
            super.onRestore(data, appVersionCode, newState);


            WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
            WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
                    Context.WALLPAPER_SERVICE);
                    Context.WALLPAPER_SERVICE);
            ProfileManagerService profiles = (ProfileManagerService)ServiceManager.getService(
                    Context.PROFILE_SERVICE);

            wallpaper.settingsRestored();
            wallpaper.settingsRestored();
            profiles.settingsRestored();
        } catch (IOException ex) {
        } catch (IOException ex) {
            // If there was a failure, delete everything for the wallpaper, this is too aggressive,
            // If there was a failure, delete everything for the wallpaper, this is too aggressive,
            // but this is hopefully a rare failure.
            // but this is hopefully a rare failure.
@@ -129,6 +149,7 @@ public class SystemBackupAgent extends BackupAgentHelper {


        // Bits to indicate postprocessing we may need to perform
        // Bits to indicate postprocessing we may need to perform
        boolean restoredWallpaper = false;
        boolean restoredWallpaper = false;
        boolean restoredProfiles = false;


        File outFile = null;
        File outFile = null;
        // Various domain+files we understand a priori
        // Various domain+files we understand a priori
@@ -139,6 +160,9 @@ public class SystemBackupAgent extends BackupAgentHelper {
            } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
            } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
                outFile = new File(WALLPAPER_IMAGE);
                outFile = new File(WALLPAPER_IMAGE);
                restoredWallpaper = true;
                restoredWallpaper = true;
            } else if (path.equals(PROFILES_FILENAME)) {
                outFile = ProfileManagerService.PROFILE_FILE;
                restoredProfiles = true;
            }
            }
        }
        }


@@ -154,6 +178,11 @@ public class SystemBackupAgent extends BackupAgentHelper {
                        Context.WALLPAPER_SERVICE);
                        Context.WALLPAPER_SERVICE);
                wallpaper.settingsRestored();
                wallpaper.settingsRestored();
            }
            }
            if (restoredProfiles) {
                ProfileManagerService profiles = (ProfileManagerService)
                        ServiceManager.getService(Context.PROFILE_SERVICE);
                profiles.settingsRestored();
            }
        } catch (IOException e) {
        } catch (IOException e) {
            if (restoredWallpaper) {
            if (restoredWallpaper) {
                // Make sure we wind up in a good state
                // Make sure we wind up in a good state