1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.broadcastradio; 18 19 import android.app.ActivityManager; 20 import android.os.Binder; 21 import android.os.UserHandle; 22 23 /** 24 * Controller to handle users in {@link com.android.server.broadcastradio.BroadcastRadioService} 25 */ 26 public final class RadioServiceUserController { 27 RadioServiceUserController()28 private RadioServiceUserController() { 29 throw new UnsupportedOperationException( 30 "RadioServiceUserController class is noninstantiable"); 31 } 32 33 /** 34 * Check if the user calling the method in Broadcast Radio Service is the current user or the 35 * system user. 36 * 37 * @return {@code true} if the user calling this method is the current user of system user, 38 * {@code false} otherwise. 39 */ isCurrentOrSystemUser()40 public static boolean isCurrentOrSystemUser() { 41 int callingUser = Binder.getCallingUserHandle().getIdentifier(); 42 return callingUser == getCurrentUser() || callingUser == UserHandle.USER_SYSTEM; 43 } 44 45 /** 46 * Get current foreground user for Broadcast Radio Service 47 * 48 * @return foreground user id. 49 */ getCurrentUser()50 public static int getCurrentUser() { 51 int userId = UserHandle.USER_NULL; 52 final long identity = Binder.clearCallingIdentity(); 53 try { 54 userId = ActivityManager.getCurrentUser(); 55 } catch (RuntimeException e) { 56 // Activity manager not running, nothing we can do assume user 0. 57 } finally { 58 Binder.restoreCallingIdentity(identity); 59 } 60 return userId; 61 } 62 } 63