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

Commit a5287dc1 authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Write policy file on background thread

Change-Id: I10d2fab26d2efcdc51086be7962dd04f72540933
Fixes: 66333007
Test: device boot, change notification settings, very settings on reboot
parent 47fd15fa
Loading
Loading
Loading
Loading
+46 −51
Original line number Original line Diff line number Diff line
@@ -206,6 +206,7 @@ import com.android.internal.util.Preconditions;
import com.android.internal.util.XmlUtils;
import com.android.internal.util.XmlUtils;
import com.android.server.DeviceIdleController;
import com.android.server.DeviceIdleController;
import com.android.server.EventLogTags;
import com.android.server.EventLogTags;
import com.android.server.IoThread;
import com.android.server.LocalServices;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.SystemService;
import com.android.server.lights.Light;
import com.android.server.lights.Light;
@@ -264,7 +265,7 @@ public class NotificationManagerService extends SystemService {


    // message codes
    // message codes
    static final int MESSAGE_DURATION_REACHED = 2;
    static final int MESSAGE_DURATION_REACHED = 2;
    static final int MESSAGE_SAVE_POLICY_FILE = 3;
    // 3: removed to a different handler
    static final int MESSAGE_SEND_RANKING_UPDATE = 4;
    static final int MESSAGE_SEND_RANKING_UPDATE = 4;
    static final int MESSAGE_LISTENER_HINTS_CHANGED = 5;
    static final int MESSAGE_LISTENER_HINTS_CHANGED = 5;
    static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6;
    static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6;
@@ -570,7 +571,7 @@ public class NotificationManagerService extends SystemService {
            mListeners.migrateToXml();
            mListeners.migrateToXml();
            mAssistants.migrateToXml();
            mAssistants.migrateToXml();
            mConditionProviders.migrateToXml();
            mConditionProviders.migrateToXml();
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        mAssistants.ensureAssistant();
        mAssistants.ensureAssistant();
@@ -600,12 +601,8 @@ public class NotificationManagerService extends SystemService {
        }
        }
    }
    }


    public void savePolicyFile() {
        mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE);
        mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE);
    }

    private void handleSavePolicyFile() {
    private void handleSavePolicyFile() {
        IoThread.getHandler().post(() -> {
            if (DBG) Slog.d(TAG, "handleSavePolicyFile");
            if (DBG) Slog.d(TAG, "handleSavePolicyFile");
            synchronized (mPolicyFile) {
            synchronized (mPolicyFile) {
                final FileOutputStream stream;
                final FileOutputStream stream;
@@ -625,6 +622,7 @@ public class NotificationManagerService extends SystemService {
                }
                }
            }
            }
            BackupManager.dataChanged(getContext().getPackageName());
            BackupManager.dataChanged(getContext().getPackageName());
        });
    }
    }


    private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException {
    private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException {
@@ -1138,7 +1136,7 @@ public class NotificationManagerService extends SystemService {
                mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList);
                mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList);
                mPreferencesHelper.onPackagesChanged(
                mPreferencesHelper.onPackagesChanged(
                        removingPackage, changeUserId, pkgList, uidList);
                        removingPackage, changeUserId, pkgList, uidList);
                savePolicyFile();
                handleSavePolicyFile();
            }
            }
        }
        }
    };
    };
@@ -1205,7 +1203,7 @@ public class NotificationManagerService extends SystemService {
                mListeners.onUserRemoved(userId);
                mListeners.onUserRemoved(userId);
                mConditionProviders.onUserRemoved(userId);
                mConditionProviders.onUserRemoved(userId);
                mAssistants.onUserRemoved(userId);
                mAssistants.onUserRemoved(userId);
                savePolicyFile();
                handleSavePolicyFile();
            } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
            } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
                mUserProfiles.updateCache(context);
                mUserProfiles.updateCache(context);
@@ -1456,7 +1454,7 @@ public class NotificationManagerService extends SystemService {
        mZenModeHelper.addCallback(new ZenModeHelper.Callback() {
        mZenModeHelper.addCallback(new ZenModeHelper.Callback() {
            @Override
            @Override
            public void onConfigChanged() {
            public void onConfigChanged() {
                savePolicyFile();
                handleSavePolicyFile();
            }
            }


            @Override
            @Override
@@ -1755,7 +1753,7 @@ public class NotificationManagerService extends SystemService {
                    modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED);
                    modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED);
        }
        }


        savePolicyFile();
        handleSavePolicyFile();
    }
    }


    private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate,
    private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate,
@@ -2223,7 +2221,7 @@ public class NotificationManagerService extends SystemService {
                Slog.w(TAG, "Can't notify app about app block change", e);
                Slog.w(TAG, "Can't notify app about app block change", e);
            }
            }


            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        /**
        /**
@@ -2280,7 +2278,7 @@ public class NotificationManagerService extends SystemService {
        public void setShowBadge(String pkg, int uid, boolean showBadge) {
        public void setShowBadge(String pkg, int uid, boolean showBadge) {
            checkCallerIsSystem();
            checkCallerIsSystem();
            mPreferencesHelper.setShowBadge(pkg, uid, showBadge);
            mPreferencesHelper.setShowBadge(pkg, uid, showBadge);
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -2296,7 +2294,7 @@ public class NotificationManagerService extends SystemService {
                if (info != null) {
                if (info != null) {
                    mPreferencesHelper.setNotificationDelegate(
                    mPreferencesHelper.setNotificationDelegate(
                            callingPkg, callingUid, delegate, info.uid);
                            callingPkg, callingUid, delegate, info.uid);
                    savePolicyFile();
                    handleSavePolicyFile();
                }
                }
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                // :(
                // :(
@@ -2307,7 +2305,7 @@ public class NotificationManagerService extends SystemService {
        public void revokeNotificationDelegate(String callingPkg) {
        public void revokeNotificationDelegate(String callingPkg) {
            checkCallerIsSameApp(callingPkg);
            checkCallerIsSameApp(callingPkg);
            mPreferencesHelper.revokeNotificationDelegate(callingPkg, Binder.getCallingUid());
            mPreferencesHelper.revokeNotificationDelegate(callingPkg, Binder.getCallingUid());
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -2342,7 +2340,7 @@ public class NotificationManagerService extends SystemService {
                NotificationChannelGroup group) throws RemoteException {
                NotificationChannelGroup group) throws RemoteException {
            enforceSystemOrSystemUI("Caller not system or systemui");
            enforceSystemOrSystemUI("Caller not system or systemui");
            createNotificationChannelGroup(pkg, uid, group, false, false);
            createNotificationChannelGroup(pkg, uid, group, false, false);
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -2355,7 +2353,7 @@ public class NotificationManagerService extends SystemService {
                final NotificationChannelGroup group = groups.get(i);
                final NotificationChannelGroup group = groups.get(i);
                createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false);
                createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false);
            }
            }
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        private void createNotificationChannelsImpl(String pkg, int uid,
        private void createNotificationChannelsImpl(String pkg, int uid,
@@ -2373,7 +2371,7 @@ public class NotificationManagerService extends SystemService {
                        mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false),
                        mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false),
                        NOTIFICATION_CHANNEL_OR_GROUP_ADDED);
                        NOTIFICATION_CHANNEL_OR_GROUP_ADDED);
            }
            }
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -2418,7 +2416,7 @@ public class NotificationManagerService extends SystemService {
                    UserHandle.getUserHandleForUid(callingUid),
                    UserHandle.getUserHandleForUid(callingUid),
                    mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true),
                    mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true),
                    NOTIFICATION_CHANNEL_OR_GROUP_DELETED);
                    NOTIFICATION_CHANNEL_OR_GROUP_DELETED);
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -2460,7 +2458,7 @@ public class NotificationManagerService extends SystemService {
                mListeners.notifyNotificationChannelGroupChanged(
                mListeners.notifyNotificationChannelGroupChanged(
                        pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete,
                        pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete,
                        NOTIFICATION_CHANNEL_OR_GROUP_DELETED);
                        NOTIFICATION_CHANNEL_OR_GROUP_DELETED);
                savePolicyFile();
                handleSavePolicyFile();
            }
            }
        }
        }


@@ -2593,7 +2591,7 @@ public class NotificationManagerService extends SystemService {
                        true, UserHandle.getCallingUserId(), packages, uids);
                        true, UserHandle.getCallingUserId(), packages, uids);
            }
            }


            savePolicyFile();
            handleSavePolicyFile();
        }
        }




@@ -3381,7 +3379,7 @@ public class NotificationManagerService extends SystemService {
                final ByteArrayInputStream bais = new ByteArrayInputStream(payload);
                final ByteArrayInputStream bais = new ByteArrayInputStream(payload);
                try {
                try {
                    readPolicyXml(bais, true /*forRestore*/);
                    readPolicyXml(bais, true /*forRestore*/);
                    savePolicyFile();
                    handleSavePolicyFile();
                } catch (NumberFormatException | XmlPullParserException | IOException e) {
                } catch (NumberFormatException | XmlPullParserException | IOException e) {
                    Slog.w(TAG, "applyRestore: error reading payload", e);
                    Slog.w(TAG, "applyRestore: error reading payload", e);
                }
                }
@@ -3422,7 +3420,7 @@ public class NotificationManagerService extends SystemService {
                                    .setPackage(pkg)
                                    .setPackage(pkg)
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                            UserHandle.of(userId), null);
                            UserHandle.of(userId), null);
                    savePolicyFile();
                    handleSavePolicyFile();
                }
                }
            } finally {
            } finally {
                Binder.restoreCallingIdentity(identity);
                Binder.restoreCallingIdentity(identity);
@@ -3566,7 +3564,7 @@ public class NotificationManagerService extends SystemService {
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                            UserHandle.of(userId), null);
                            UserHandle.of(userId), null);


                    savePolicyFile();
                    handleSavePolicyFile();
                }
                }
            } finally {
            } finally {
                Binder.restoreCallingIdentity(identity);
                Binder.restoreCallingIdentity(identity);
@@ -3592,7 +3590,7 @@ public class NotificationManagerService extends SystemService {
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                                    .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY),
                            UserHandle.of(userId), null);
                            UserHandle.of(userId), null);


                    savePolicyFile();
                    handleSavePolicyFile();
                }
                }
            } finally {
            } finally {
                Binder.restoreCallingIdentity(identity);
                Binder.restoreCallingIdentity(identity);
@@ -3675,7 +3673,7 @@ public class NotificationManagerService extends SystemService {
            verifyPrivilegedListener(token, user, false);
            verifyPrivilegedListener(token, user, false);
            createNotificationChannelGroup(
            createNotificationChannelGroup(
                    pkg, getUidForPackageAndUser(pkg, user), group, false, true);
                    pkg, getUidForPackageAndUser(pkg, user), group, false, true);
            savePolicyFile();
            handleSavePolicyFile();
        }
        }


        @Override
        @Override
@@ -3724,7 +3722,7 @@ public class NotificationManagerService extends SystemService {
            }
            }
            if (allow != mLockScreenAllowSecureNotifications) {
            if (allow != mLockScreenAllowSecureNotifications) {
                mLockScreenAllowSecureNotifications = allow;
                mLockScreenAllowSecureNotifications = allow;
                savePolicyFile();
                handleSavePolicyFile();
            }
            }
        }
        }


@@ -4483,7 +4481,7 @@ public class NotificationManagerService extends SystemService {
                Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey());
                Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey());
            }
            }
            mSnoozeHelper.update(userId, r);
            mSnoozeHelper.update(userId, r);
            savePolicyFile();
            handleSavePolicyFile();
            return false;
            return false;
        }
        }


@@ -4614,7 +4612,7 @@ public class NotificationManagerService extends SystemService {
                mSnoozeHelper.snooze(r, mDuration);
                mSnoozeHelper.snooze(r, mDuration);
            }
            }
            r.recordSnoozed();
            r.recordSnoozed();
            savePolicyFile();
            handleSavePolicyFile();
        }
        }
    }
    }


@@ -4692,7 +4690,7 @@ public class NotificationManagerService extends SystemService {
                    if (mReason != REASON_SNOOZED) {
                    if (mReason != REASON_SNOOZED) {
                        final boolean wasSnoozed = mSnoozeHelper.cancel(mUserId, mPkg, mTag, mId);
                        final boolean wasSnoozed = mSnoozeHelper.cancel(mUserId, mPkg, mTag, mId);
                        if (wasSnoozed) {
                        if (wasSnoozed) {
                            savePolicyFile();
                            handleSavePolicyFile();
                        }
                        }
                    }
                    }
                }
                }
@@ -5724,9 +5722,6 @@ public class NotificationManagerService extends SystemService {
                case MESSAGE_FINISH_TOKEN_TIMEOUT:
                case MESSAGE_FINISH_TOKEN_TIMEOUT:
                    handleKillTokenTimeout((ToastRecord)msg.obj);
                    handleKillTokenTimeout((ToastRecord)msg.obj);
                    break;
                    break;
                case MESSAGE_SAVE_POLICY_FILE:
                    handleSavePolicyFile();
                    break;
                case MESSAGE_SEND_RANKING_UPDATE:
                case MESSAGE_SEND_RANKING_UPDATE:
                    handleSendRankingUpdate();
                    handleSendRankingUpdate();
                    break;
                    break;
@@ -6228,7 +6223,7 @@ public class NotificationManagerService extends SystemService {
            Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName));
            Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName));
        }
        }
        mSnoozeHelper.repost(key);
        mSnoozeHelper.repost(key);
        savePolicyFile();
        handleSavePolicyFile();
    }
    }


    @GuardedBy("mNotificationLock")
    @GuardedBy("mNotificationLock")