diff options
3 files changed, 55 insertions, 10 deletions
diff --git a/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java b/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java index edf007d428c1..cad8100c88db 100644 --- a/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java +++ b/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java @@ -50,6 +50,8 @@ public final class ClientProfile { */ private final int mProcessId; + private boolean mIsForeground; + /** * All the clients that share the same resource would be under the same group id. * @@ -113,6 +115,20 @@ public final class ClientProfile { return mProcessId; } + /** + * Set the current isForeground status. + */ + public void setForeground(boolean isForeground) { + mIsForeground = isForeground; + } + + /** + * Get the previous recorded isForeground status. + */ + public boolean isForeground() { + return mIsForeground; + } + public int getGroupId() { return mGroupId; } diff --git a/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java b/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java index ff49ad9bdf6a..5f885c94fd02 100644 --- a/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java +++ b/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java @@ -460,7 +460,9 @@ public class TunerResourceManagerService extends SystemService implements IBinde .useCase(profile.useCase) .processId(pid) .build(); - clientProfile.setPriority(getClientPriority(profile.useCase, pid)); + clientProfile.setForeground(checkIsForeground(pid)); + clientProfile.setPriority( + getClientPriority(profile.useCase, clientProfile.isForeground())); addClientProfile(clientId[0], clientProfile, listener); } @@ -498,6 +500,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde return false; } + profile.setForeground(checkIsForeground(profile.getProcessId())); profile.setPriority(priority); profile.setNiceValue(niceValue); @@ -611,6 +614,10 @@ public class TunerResourceManagerService extends SystemService implements IBinde frontendHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE; ClientProfile requestClient = getClientProfile(request.clientId); + if (requestClient == null) { + return false; + } + clientPriorityUpdateOnRequest(requestClient); int grantingFrontendHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE; int inUseLowestPriorityFrHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE; // Priority max value is 1000 @@ -684,6 +691,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde lnbHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE; ClientProfile requestClient = getClientProfile(request.clientId); + clientPriorityUpdateOnRequest(requestClient); int grantingLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE; int inUseLowestPriorityLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE; // Priority max value is 1000 @@ -742,6 +750,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde } casSessionHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE; ClientProfile requestClient = getClientProfile(request.clientId); + clientPriorityUpdateOnRequest(requestClient); int lowestPriorityOwnerId = -1; // Priority max value is 1000 int currentLowestPriority = MAX_CLIENT_PRIORITY + 1; @@ -797,8 +806,9 @@ public class TunerResourceManagerService extends SystemService implements IBinde ? Binder.getCallingPid() /*callingPid*/ : mTvInputManager.getClientPid(holderProfile.tvInputSessionId); /*tvAppId*/ - int challengerPriority = getClientPriority(challengerProfile.useCase, challengerPid); - int holderPriority = getClientPriority(holderProfile.useCase, holderPid); + int challengerPriority = getClientPriority( + challengerProfile.useCase, checkIsForeground(challengerPid)); + int holderPriority = getClientPriority(holderProfile.useCase, checkIsForeground(holderPid)); return challengerPriority > holderPriority; } @@ -843,6 +853,21 @@ public class TunerResourceManagerService extends SystemService implements IBinde } @VisibleForTesting + // This mothod is to sync up the request client's foreground/background status and update + // the client priority accordingly whenever new resource request comes in. + protected void clientPriorityUpdateOnRequest(ClientProfile requestProfile) { + int pid = requestProfile.getProcessId(); + boolean currentIsForeground = checkIsForeground(pid); + if (requestProfile.isForeground() == currentIsForeground) { + // To avoid overriding the priority set through updateClientPriority API. + return; + } + requestProfile.setForeground(currentIsForeground); + requestProfile.setPriority( + getClientPriority(requestProfile.getUseCase(), currentIsForeground)); + } + + @VisibleForTesting protected boolean requestDescramblerInternal( TunerDescramblerRequest request, int[] descramblerHandle) { if (DEBUG) { @@ -933,20 +958,20 @@ public class TunerResourceManagerService extends SystemService implements IBinde } @VisibleForTesting - protected int getClientPriority(int useCase, int pid) { + protected int getClientPriority(int useCase, boolean isForeground) { if (DEBUG) { Slog.d(TAG, "getClientPriority useCase=" + useCase - + ", pid=" + pid + ")"); + + ", isForeground=" + isForeground + ")"); } - if (isForeground(pid)) { + if (isForeground) { return mPriorityCongfig.getForegroundPriority(useCase); } return mPriorityCongfig.getBackgroundPriority(useCase); } @VisibleForTesting - protected boolean isForeground(int pid) { + protected boolean checkIsForeground(int pid) { if (mActivityManager == null) { return false; } diff --git a/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java index 83ce9f559a48..e8479a8f2790 100644 --- a/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java @@ -99,7 +99,7 @@ public class TunerResourceManagerServiceTest { when(mContextSpy.getSystemService(Context.TV_INPUT_SERVICE)).thenReturn(tvInputManager); mTunerResourceManagerService = new TunerResourceManagerService(mContextSpy) { @Override - protected boolean isForeground(int pid) { + protected boolean checkIsForeground(int pid) { return mIsForeground; } }; @@ -231,6 +231,10 @@ public class TunerResourceManagerServiceTest { @Test public void requestFrontendTest_ClientNotRegistered() { + TunerFrontendInfo[] infos0 = new TunerFrontendInfo[1]; + infos0[0] = + tunerFrontendInfo(0 /*id*/, FrontendSettings.TYPE_DVBT, 0 /*exclusiveGroupId*/); + mTunerResourceManagerService.setFrontendInfoListInternal(infos0); TunerFrontendRequest request = tunerFrontendRequest(0 /*clientId*/, FrontendSettings.TYPE_DVBT); int[] frontendHandle = new int[1]; @@ -752,9 +756,9 @@ public class TunerResourceManagerServiceTest { resourceClientProfile(null /*sessionId*/, TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD); int backgroundPlaybackPriority = mTunerResourceManagerService.getClientPriority( - TvInputService.PRIORITY_HINT_USE_CASE_TYPE_PLAYBACK, 0); + TvInputService.PRIORITY_HINT_USE_CASE_TYPE_PLAYBACK, mIsForeground); int backgroundRecordPriority = mTunerResourceManagerService.getClientPriority( - TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD, 0); + TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD, mIsForeground); assertThat(mTunerResourceManagerService.isHigherPriorityInternal(backgroundPlaybackProfile, backgroundRecordProfile)).isEqualTo( (backgroundPlaybackPriority > backgroundRecordPriority)); |