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

Commit b408e8ec authored by John Spurlock's avatar John Spurlock
Browse files

Move notification listener management into helper.

Part of a larger effort to reuse service management code.  This is
mostly algebraic, simply move code related to listener management
into a new helper, and cut the backreference to NoMan.  Put
user-profile code into reusable inner helper, and common uid-checking
code into a new static util helper.

Resist the urge to rewrite / reformat, but replace HashSet with
ArraySet.

Bug:13743109
Change-Id: I59e0131f632c3bbf555b5276dc5092422113cdc3
parent 25a13518
Loading
Loading
Loading
Loading
+608 −0

File added.

Preview size limit exceeded, changes collapsed.

+78 −599

File changed.

Preview size limit exceeded, changes collapsed.

+63 −0
Original line number Original line Diff line number Diff line
/**
 * Copyright (c) 2014, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.notification;

import android.app.AppGlobals;
import android.content.pm.ApplicationInfo;
import android.os.Binder;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;

public class NotificationUtil {

    // Return true if the UID is a system or phone UID and therefore should not have
    // any notifications or toasts blocked.
    public static boolean isUidSystem(int uid) {
        final int appid = UserHandle.getAppId(uid);
        return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0);
    }

    // same as isUidSystem(int, int) for the Binder caller's UID.
    public static boolean isCallerSystem() {
        return isUidSystem(Binder.getCallingUid());
    }

    public static void checkCallerIsSystem() {
        if (isCallerSystem()) {
            return;
        }
        throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid());
    }

    public static void checkCallerIsSystemOrSameApp(String pkg) {
        if (isCallerSystem()) {
            return;
        }
        final int uid = Binder.getCallingUid();
        try {
            ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
                    pkg, 0, UserHandle.getCallingUserId());
            if (!UserHandle.isSameApp(ai.uid, uid)) {
                throw new SecurityException("Calling uid " + uid + " gave package"
                        + pkg + " which is owned by uid " + ai.uid);
            }
        } catch (RemoteException re) {
            throw new SecurityException("Unknown package " + pkg + "\n" + re);
        }
    }
}