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

Commit e0facd90 authored by Julia Reynolds's avatar Julia Reynolds Committed by Android (Google) Code Review
Browse files

Merge "Refactor code to allow B&R logging" into main

parents 425080ab 5c239c72
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -272,3 +272,10 @@ flag {
  description: "[RONs] Guards new promotion logic and UI, including AOD notification and Colorization"
  bug: "367705002"
}

flag {
  name: "backup_restore_logging"
  namespace: "systemui"
  description: "Adds logging for notification/modes backup and restore events"
  bug: "289524803"
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.os.UserManager;
import android.util.Slog;

import com.android.server.backup.Flags;
import com.android.server.notification.NotificationBackupHelper;

import com.google.android.collect.Sets;

+21 −7
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 */

package com.android.server.backup;
package com.android.server.notification;

import android.app.INotificationManager;
import android.app.backup.BlobBackupHelper;
@@ -22,6 +22,8 @@ import android.os.ServiceManager;
import android.util.Log;
import android.util.Slog;

import com.android.server.LocalServices;

public class NotificationBackupHelper extends BlobBackupHelper {
    static final String TAG = "NotifBackupHelper";   // must be < 23 chars
    static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
@@ -34,9 +36,13 @@ public class NotificationBackupHelper extends BlobBackupHelper {

    private final int mUserId;

    private final NotificationManagerInternal mNm;

    public NotificationBackupHelper(int userId) {
        super(BLOB_VERSION, KEY_NOTIFICATIONS);
        mUserId = userId;

        mNm = LocalServices.getService(NotificationManagerInternal.class);
    }

    @Override
@@ -44,9 +50,13 @@ public class NotificationBackupHelper extends BlobBackupHelper {
        byte[] newPayload = null;
        if (KEY_NOTIFICATIONS.equals(key)) {
            try {
                if (android.app.Flags.backupRestoreLogging()) {
                    newPayload = mNm.getBackupPayload(mUserId, getLogger());
                } else {
                    INotificationManager nm = INotificationManager.Stub.asInterface(
                            ServiceManager.getService("notification"));
                    newPayload = nm.getBackupPayload(mUserId);
                }
            } catch (Exception e) {
                // Treat as no data
                Slog.e(TAG, "Couldn't communicate with notification manager", e);
@@ -64,9 +74,13 @@ public class NotificationBackupHelper extends BlobBackupHelper {

        if (KEY_NOTIFICATIONS.equals(key)) {
            try {
                if (android.app.Flags.backupRestoreLogging()) {
                    mNm.applyRestore(payload, mUserId, getLogger());
                } else {
                    INotificationManager nm = INotificationManager.Stub.asInterface(
                            ServiceManager.getService("notification"));
                    nm.applyRestore(payload, mUserId);
                }
            } catch (Exception e) {
                Slog.e(TAG, "Couldn't communicate with notification manager", e);
            }
+6 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.notification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.backup.BackupRestoreEventLogger;
import android.service.notification.DeviceEffectsApplier;

import java.util.Set;
@@ -73,4 +74,9 @@ public interface NotificationManagerInternal {
     * Otherwise an {@link IllegalStateException} will be thrown.
     */
    void setDeviceEffectsApplier(DeviceEffectsApplier applier);

    // Backup/restore interface
    byte[] getBackupPayload(int user, BackupRestoreEventLogger logger);

    void applyRestore(byte[] payload, int user, BackupRestoreEventLogger logger);
}
+40 −7
Original line number Diff line number Diff line
@@ -216,6 +216,7 @@ import android.app.StatsManager;
import android.app.UriGrantsManager;
import android.app.admin.DevicePolicyManagerInternal;
import android.app.backup.BackupManager;
import android.app.backup.BackupRestoreEventLogger;
import android.app.compat.CompatChanges;
import android.app.role.OnRoleHoldersChangedListener;
import android.app.role.RoleManager;
@@ -1102,7 +1103,8 @@ public class NotificationManagerService extends SystemService {
        return false;
    }
    void readPolicyXml(InputStream stream, boolean forRestore, int userId)
    void readPolicyXml(InputStream stream, boolean forRestore, int userId,
            BackupRestoreEventLogger logger)
            throws XmlPullParserException, NumberFormatException, IOException {
        final TypedXmlPullParser parser;
        if (forRestore) {
@@ -1189,7 +1191,7 @@ public class NotificationManagerService extends SystemService {
            InputStream infile = null;
            try {
                infile = mPolicyFile.openRead();
                readPolicyXml(infile, false /*forRestore*/, USER_ALL);
                readPolicyXml(infile, false /*forRestore*/, USER_ALL, null);
                // We re-load the default dnd packages to allow the newly added and denined.
                final boolean isWatch = mPackageManagerClient.hasSystemFeature(
@@ -1239,7 +1241,7 @@ public class NotificationManagerService extends SystemService {
                }
                try {
                    writePolicyXml(stream, false /*forBackup*/, USER_ALL);
                    writePolicyXml(stream, false /*forBackup*/, USER_ALL, null);
                    mPolicyFile.finishWrite(stream);
                } catch (IOException e) {
                    Slog.w(TAG, "Failed to save policy file, restoring backup", e);
@@ -1250,8 +1252,8 @@ public class NotificationManagerService extends SystemService {
        }
    }
    private void writePolicyXml(OutputStream stream, boolean forBackup, int userId)
            throws IOException {
    private void writePolicyXml(OutputStream stream, boolean forBackup, int userId,
            BackupRestoreEventLogger logger)  throws IOException {
        final TypedXmlSerializer out;
        if (forBackup) {
            out = Xml.newFastSerializer();
@@ -6171,7 +6173,7 @@ public class NotificationManagerService extends SystemService {
            if (DBG) Slog.d(TAG, "getBackupPayload u=" + user);
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                writePolicyXml(baos, true /*forBackup*/, user);
                writePolicyXml(baos, true /*forBackup*/, user, null);
                return baos.toByteArray();
            } catch (IOException e) {
                Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e);
@@ -6190,7 +6192,7 @@ public class NotificationManagerService extends SystemService {
            }
            final ByteArrayInputStream bais = new ByteArrayInputStream(payload);
            try {
                readPolicyXml(bais, true /*forRestore*/, user);
                readPolicyXml(bais, true /*forRestore*/, user, null);
                handleSavePolicyFile();
            } catch (NumberFormatException | XmlPullParserException | IOException e) {
                Slog.w(TAG, "applyRestore: error reading payload", e);
@@ -7392,6 +7394,37 @@ public class NotificationManagerService extends SystemService {
     */
    private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() {
        public byte[] getBackupPayload(int user, BackupRestoreEventLogger logger) {
            checkCallerIsSystem();
            if (DBG) Slog.d(TAG, "getBackupPayload u=" + user);
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                writePolicyXml(baos, true /*forBackup*/, user, logger);
                return baos.toByteArray();
            } catch (IOException e) {
                Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e);
            }
            return null;
        }
        @Override
        public void applyRestore(byte[] payload, int user, BackupRestoreEventLogger logger) {
            checkCallerIsSystem();
            if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload="
                    + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null));
            if (payload == null) {
                Slog.w(TAG, "applyRestore: no payload to restore for user " + user);
                return;
            }
            final ByteArrayInputStream bais = new ByteArrayInputStream(payload);
            try {
                readPolicyXml(bais, true /*forRestore*/, user, logger);
                handleSavePolicyFile();
            } catch (NumberFormatException | XmlPullParserException | IOException e) {
                Slog.w(TAG, "applyRestore: error reading payload", e);
            }
        }
        @Override
        public NotificationChannel getNotificationChannel(String pkg, int uid, String
                channelId) {
Loading