@@ -734,8 +734,11 @@ ComponentName startServiceLocked(IApplicationThread caller, Intent service, Stri
734734 }
735735 ComponentName cmp = startServiceInnerLocked (smap , service , r , callerFg , addToStarting );
736736
737- setFgsRestrictionLocked (callingPackage , callingPid , callingUid , r ,
738- allowBackgroundActivityStarts );
737+ if (!r .mAllowWhileInUsePermissionInFgs ) {
738+ r .mAllowWhileInUsePermissionInFgs =
739+ shouldAllowWhileInUsePermissionInFgsLocked (callingPackage , callingPid ,
740+ callingUid , service , r , allowBackgroundActivityStarts );
741+ }
739742
740743 return cmp ;
741744 }
@@ -1408,6 +1411,14 @@ private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
14081411 + String .format ("0x%08X" , manifestType )
14091412 + " in service element of manifest file" );
14101413 }
1414+ // If the foreground service is not started from TOP process, do not allow it to
1415+ // have while-in-use location/camera/microphone access.
1416+ if (!r .mAllowWhileInUsePermissionInFgs ) {
1417+ Slog .w (TAG ,
1418+ "Foreground service started from background can not have "
1419+ + "location/camera/microphone access: service "
1420+ + r .shortInstanceName );
1421+ }
14111422 }
14121423 boolean alreadyStartedOp = false ;
14131424 boolean stopProcStatsOp = false ;
@@ -1455,57 +1466,6 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14551466 ignoreForeground = true ;
14561467 }
14571468
1458- if (!ignoreForeground ) {
1459- if (r .mStartForegroundCount == 0 ) {
1460- /*
1461- If the service was started with startService(), not
1462- startForegroundService(), and if startForeground() isn't called within
1463- mFgsStartForegroundTimeoutMs, then we check the state of the app
1464- (who owns the service, which is the app that called startForeground())
1465- again. If the app is in the foreground, or in any other cases where
1466- FGS-starts are allowed, then we still allow the FGS to be started.
1467- Otherwise, startForeground() would fail.
1468-
1469- If the service was started with startForegroundService(), then the service
1470- must call startForeground() within a timeout anyway, so we don't need this
1471- check.
1472- */
1473- if (!r .fgRequired ) {
1474- final long delayMs = SystemClock .elapsedRealtime () - r .createRealTime ;
1475- if (delayMs > mAm .mConstants .mFgsStartForegroundTimeoutMs ) {
1476- resetFgsRestrictionLocked (r );
1477- setFgsRestrictionLocked (r .serviceInfo .packageName , r .app .pid ,
1478- r .appInfo .uid , r , false );
1479- EventLog .writeEvent (0x534e4554 , "183147114" ,
1480- r .appInfo .uid ,
1481- "call setFgsRestrictionLocked again due to "
1482- + "startForegroundTimeout" );
1483- }
1484- }
1485- } else if (r .mStartForegroundCount >= 1 ) {
1486- // The second or later time startForeground() is called after service is
1487- // started. Check for app state again.
1488- final long delayMs = SystemClock .elapsedRealtime () -
1489- r .mLastSetFgsRestrictionTime ;
1490- if (delayMs > mAm .mConstants .mFgsStartForegroundTimeoutMs ) {
1491- resetFgsRestrictionLocked (r );
1492- setFgsRestrictionLocked (r .serviceInfo .packageName , r .app .pid ,
1493- r .appInfo .uid , r , false );
1494- EventLog .writeEvent (0x534e4554 , "183147114" , r .appInfo .uid ,
1495- "call setFgsRestrictionLocked for "
1496- + (r .mStartForegroundCount + 1 ) + "th startForeground" );
1497- }
1498- }
1499- // If the foreground service is not started from TOP process, do not allow it to
1500- // have while-in-use location/camera/microphone access.
1501- if (!r .mAllowWhileInUsePermissionInFgs ) {
1502- Slog .w (TAG ,
1503- "Foreground service started from background can not have "
1504- + "location/camera/microphone access: service "
1505- + r .shortInstanceName );
1506- }
1507- }
1508-
15091469 // Apps under strict background restrictions simply don't get to have foreground
15101470 // services, so now that we've enforced the startForegroundService() contract
15111471 // we only do the machinery of making the service foreground when the app
@@ -1541,7 +1501,6 @@ must call startForeground() within a timeout anyway, so we don't need this
15411501 active .mNumActive ++;
15421502 }
15431503 r .isForeground = true ;
1544- r .mStartForegroundCount ++;
15451504 if (!stopProcStatsOp ) {
15461505 ServiceState stracker = r .getTracker ();
15471506 if (stracker != null ) {
@@ -1600,7 +1559,6 @@ must call startForeground() within a timeout anyway, so we don't need this
16001559 decActiveForegroundAppLocked (smap , r );
16011560 }
16021561 r .isForeground = false ;
1603- resetFgsRestrictionLocked (r );
16041562 ServiceState stracker = r .getTracker ();
16051563 if (stracker != null ) {
16061564 stracker .setForeground (false , mAm .mProcessStats .getMemFactorLocked (),
@@ -2160,7 +2118,12 @@ public void run() {
21602118 }
21612119 }
21622120
2163- setFgsRestrictionLocked (callingPackage , callingPid , callingUid , s , false );
2121+ if (!s .mAllowWhileInUsePermissionInFgs ) {
2122+ s .mAllowWhileInUsePermissionInFgs =
2123+ shouldAllowWhileInUsePermissionInFgsLocked (callingPackage ,
2124+ callingPid , callingUid ,
2125+ service , s , false );
2126+ }
21642127
21652128 if (s .app != null ) {
21662129 if ((flags &Context .BIND_TREAT_LIKE_ACTIVITY ) != 0 ) {
@@ -3456,7 +3419,7 @@ private final void bringDownServiceLocked(ServiceRecord r) {
34563419 r .isForeground = false ;
34573420 r .foregroundId = 0 ;
34583421 r .foregroundNoti = null ;
3459- resetFgsRestrictionLocked ( r ) ;
3422+ r . mAllowWhileInUsePermissionInFgs = false ;
34603423
34613424 // Clear start entries.
34623425 r .clearDeliveredStartsLocked ();
@@ -4937,7 +4900,7 @@ private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
49374900 * @return true if allow, false otherwise.
49384901 */
49394902 private boolean shouldAllowWhileInUsePermissionInFgsLocked (String callingPackage ,
4940- int callingPid , int callingUid , ServiceRecord r ,
4903+ int callingPid , int callingUid , Intent intent , ServiceRecord r ,
49414904 boolean allowBackgroundActivityStarts ) {
49424905 // Is the background FGS start restriction turned on?
49434906 if (!mAm .mConstants .mFlagBackgroundFgsStartRestrictionEnabled ) {
@@ -5019,32 +4982,4 @@ private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage
50194982 }
50204983 return false ;
50214984 }
5022-
5023- boolean canAllowWhileInUsePermissionInFgsLocked (int callingPid , int callingUid ,
5024- String callingPackage ) {
5025- return shouldAllowWhileInUsePermissionInFgsLocked (
5026- callingPackage , callingPid , callingUid , null , false );
5027- }
5028-
5029- /**
5030- * In R, mAllowWhileInUsePermissionInFgs is to allow while-in-use permissions in foreground
5031- * service or not. while-in-use permissions in FGS started from background might be restricted.
5032- * @param callingPackage caller app's package name.
5033- * @param callingUid caller app's uid.
5034- * @param r the service to start.
5035- * @return true if allow, false otherwise.
5036- */
5037- private void setFgsRestrictionLocked (String callingPackage ,
5038- int callingPid , int callingUid , ServiceRecord r ,
5039- boolean allowBackgroundActivityStarts ) {
5040- r .mLastSetFgsRestrictionTime = SystemClock .elapsedRealtime ();
5041- if (!r .mAllowWhileInUsePermissionInFgs ) {
5042- r .mAllowWhileInUsePermissionInFgs = shouldAllowWhileInUsePermissionInFgsLocked (
5043- callingPackage , callingPid , callingUid , r , allowBackgroundActivityStarts );
5044- }
5045- }
5046-
5047- private void resetFgsRestrictionLocked (ServiceRecord r ) {
5048- r .mAllowWhileInUsePermissionInFgs = false ;
5049- }
50504985}
0 commit comments