| /** |
| * 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); |
| } |
| } |
| } |