1 /* 2 * Copyright (C) 2019 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.systemcaptions; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.UserIdInt; 22 import android.app.AppGlobals; 23 import android.content.ComponentName; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ServiceInfo; 26 import android.os.RemoteException; 27 import android.util.Slog; 28 29 import com.android.internal.annotations.GuardedBy; 30 import com.android.server.infra.AbstractPerUserSystemService; 31 32 /** Manages the captions manager service on a per-user basis. */ 33 final class SystemCaptionsManagerPerUserService extends 34 AbstractPerUserSystemService<SystemCaptionsManagerPerUserService, 35 SystemCaptionsManagerService> { 36 37 private static final String TAG = SystemCaptionsManagerPerUserService.class.getSimpleName(); 38 39 @Nullable 40 @GuardedBy("mLock") 41 private RemoteSystemCaptionsManagerService mRemoteService; 42 SystemCaptionsManagerPerUserService( @onNull SystemCaptionsManagerService master, @NonNull Object lock, boolean disabled, @UserIdInt int userId)43 SystemCaptionsManagerPerUserService( 44 @NonNull SystemCaptionsManagerService master, 45 @NonNull Object lock, boolean disabled, @UserIdInt int userId) { 46 super(master, lock, userId); 47 } 48 49 @Override 50 @NonNull newServiceInfoLocked( @uppressWarningsR) @onNull ComponentName serviceComponent)51 protected ServiceInfo newServiceInfoLocked( 52 @SuppressWarnings("unused") @NonNull ComponentName serviceComponent) 53 throws PackageManager.NameNotFoundException { 54 try { 55 return AppGlobals.getPackageManager().getServiceInfo(serviceComponent, 56 PackageManager.GET_META_DATA, mUserId); 57 } catch (RemoteException e) { 58 throw new PackageManager.NameNotFoundException( 59 "Could not get service for " + serviceComponent); 60 } 61 } 62 63 @GuardedBy("mLock") initializeLocked()64 void initializeLocked() { 65 if (mMaster.verbose) { 66 Slog.v(TAG, "initialize()"); 67 } 68 69 RemoteSystemCaptionsManagerService service = getRemoteServiceLocked(); 70 if (service == null && mMaster.verbose) { 71 Slog.v(TAG, "initialize(): Failed to init remote server"); 72 } 73 } 74 75 @GuardedBy("mLock") destroyLocked()76 void destroyLocked() { 77 if (mMaster.verbose) { 78 Slog.v(TAG, "destroyLocked()"); 79 } 80 81 if (mRemoteService != null) { 82 mRemoteService.destroy(); 83 mRemoteService = null; 84 } 85 } 86 87 @GuardedBy("mLock") 88 @Nullable getRemoteServiceLocked()89 private RemoteSystemCaptionsManagerService getRemoteServiceLocked() { 90 if (mRemoteService == null) { 91 String serviceName = getComponentNameLocked(); 92 if (serviceName == null) { 93 if (mMaster.verbose) { 94 Slog.v(TAG, "getRemoteServiceLocked(): Not set"); 95 } 96 return null; 97 } 98 99 ComponentName serviceComponent = ComponentName.unflattenFromString(serviceName); 100 mRemoteService = new RemoteSystemCaptionsManagerService( 101 getContext(), 102 serviceComponent, 103 mUserId, 104 mMaster.verbose); 105 if (mMaster.verbose) { 106 Slog.v(TAG, "getRemoteServiceLocked(): initialize for user " + mUserId); 107 } 108 mRemoteService.initialize(); 109 } 110 111 return mRemoteService; 112 } 113 } 114