1 /** 2 * Copyright (C) 2024 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 * Implementation for the controller to handle users in 25 * {@link com.android.server.broadcastradio.BroadcastRadioService} 26 */ 27 public final class RadioServiceUserControllerImpl implements RadioServiceUserController { 28 29 /** 30 * @see RadioServiceUserController#isCurrentOrSystemUser() 31 */ 32 @Override isCurrentOrSystemUser()33 public boolean isCurrentOrSystemUser() { 34 int callingUser = getCallingUserId(); 35 return callingUser == getCurrentUser() || callingUser == UserHandle.USER_SYSTEM; 36 } 37 38 /** 39 * @see RadioServiceUserController#getCurrentUser() 40 */ 41 @Override getCurrentUser()42 public int getCurrentUser() { 43 int userId = UserHandle.USER_NULL; 44 final long identity = Binder.clearCallingIdentity(); 45 try { 46 userId = ActivityManager.getCurrentUser(); 47 } catch (RuntimeException e) { 48 // Activity manager not running, nothing we can do assume user 0. 49 } finally { 50 Binder.restoreCallingIdentity(identity); 51 } 52 return userId; 53 } 54 55 /** 56 * @see RadioServiceUserController#getCallingUserId() 57 */ 58 @Override getCallingUserId()59 public int getCallingUserId() { 60 return Binder.getCallingUserHandle().getIdentifier(); 61 } 62 } 63