@@ -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 }
@@ -1397,6 +1400,14 @@ private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
13971400 + String .format ("0x%08X" , manifestType )
13981401 + " in service element of manifest file" );
13991402 }
1403+ // If the foreground service is not started from TOP process, do not allow it to
1404+ // have while-in-use location/camera/microphone access.
1405+ if (!r .mAllowWhileInUsePermissionInFgs ) {
1406+ Slog .w (TAG ,
1407+ "Foreground service started from background can not have "
1408+ + "location/camera/microphone access: service "
1409+ + r .shortInstanceName );
1410+ }
14001411 }
14011412 boolean alreadyStartedOp = false ;
14021413 boolean stopProcStatsOp = false ;
@@ -1444,57 +1455,6 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14441455 ignoreForeground = true ;
14451456 }
14461457
1447- if (!ignoreForeground ) {
1448- if (r .mStartForegroundCount == 0 ) {
1449- /*
1450- If the service was started with startService(), not
1451- startForegroundService(), and if startForeground() isn't called within
1452- mFgsStartForegroundTimeoutMs, then we check the state of the app
1453- (who owns the service, which is the app that called startForeground())
1454- again. If the app is in the foreground, or in any other cases where
1455- FGS-starts are allowed, then we still allow the FGS to be started.
1456- Otherwise, startForeground() would fail.
1457-
1458- If the service was started with startForegroundService(), then the service
1459- must call startForeground() within a timeout anyway, so we don't need this
1460- check.
1461- */
1462- if (!r .fgRequired ) {
1463- final long delayMs = SystemClock .elapsedRealtime () - r .createRealTime ;
1464- if (delayMs > mAm .mConstants .mFgsStartForegroundTimeoutMs ) {
1465- resetFgsRestrictionLocked (r );
1466- setFgsRestrictionLocked (r .serviceInfo .packageName , r .app .pid ,
1467- r .appInfo .uid , r , false );
1468- EventLog .writeEvent (0x534e4554 , "183147114" ,
1469- r .appInfo .uid ,
1470- "call setFgsRestrictionLocked again due to "
1471- + "startForegroundTimeout" );
1472- }
1473- }
1474- } else if (r .mStartForegroundCount >= 1 ) {
1475- // The second or later time startForeground() is called after service is
1476- // started. Check for app state again.
1477- final long delayMs = SystemClock .elapsedRealtime () -
1478- r .mLastSetFgsRestrictionTime ;
1479- if (delayMs > mAm .mConstants .mFgsStartForegroundTimeoutMs ) {
1480- resetFgsRestrictionLocked (r );
1481- setFgsRestrictionLocked (r .serviceInfo .packageName , r .app .pid ,
1482- r .appInfo .uid , r , false );
1483- EventLog .writeEvent (0x534e4554 , "183147114" , r .appInfo .uid ,
1484- "call setFgsRestrictionLocked for "
1485- + (r .mStartForegroundCount + 1 ) + "th startForeground" );
1486- }
1487- }
1488- // If the foreground service is not started from TOP process, do not allow it to
1489- // have while-in-use location/camera/microphone access.
1490- if (!r .mAllowWhileInUsePermissionInFgs ) {
1491- Slog .w (TAG ,
1492- "Foreground service started from background can not have "
1493- + "location/camera/microphone access: service "
1494- + r .shortInstanceName );
1495- }
1496- }
1497-
14981458 // Apps under strict background restrictions simply don't get to have foreground
14991459 // services, so now that we've enforced the startForegroundService() contract
15001460 // we only do the machinery of making the service foreground when the app
@@ -1530,7 +1490,6 @@ must call startForeground() within a timeout anyway, so we don't need this
15301490 active .mNumActive ++;
15311491 }
15321492 r .isForeground = true ;
1533- r .mStartForegroundCount ++;
15341493 if (!stopProcStatsOp ) {
15351494 ServiceState stracker = r .getTracker ();
15361495 if (stracker != null ) {
@@ -1589,7 +1548,6 @@ must call startForeground() within a timeout anyway, so we don't need this
15891548 decActiveForegroundAppLocked (smap , r );
15901549 }
15911550 r .isForeground = false ;
1592- resetFgsRestrictionLocked (r );
15931551 ServiceState stracker = r .getTracker ();
15941552 if (stracker != null ) {
15951553 stracker .setForeground (false , mAm .mProcessStats .getMemFactorLocked (),
@@ -2149,7 +2107,12 @@ public void run() {
21492107 }
21502108 }
21512109
2152- setFgsRestrictionLocked (callingPackage , callingPid , callingUid , s , false );
2110+ if (!s .mAllowWhileInUsePermissionInFgs ) {
2111+ s .mAllowWhileInUsePermissionInFgs =
2112+ shouldAllowWhileInUsePermissionInFgsLocked (callingPackage ,
2113+ callingPid , callingUid ,
2114+ service , s , false );
2115+ }
21532116
21542117 if (s .app != null ) {
21552118 if ((flags &Context .BIND_TREAT_LIKE_ACTIVITY ) != 0 ) {
@@ -3445,7 +3408,7 @@ private final void bringDownServiceLocked(ServiceRecord r) {
34453408 r .isForeground = false ;
34463409 r .foregroundId = 0 ;
34473410 r .foregroundNoti = null ;
3448- resetFgsRestrictionLocked ( r ) ;
3411+ r . mAllowWhileInUsePermissionInFgs = false ;
34493412
34503413 // Clear start entries.
34513414 r .clearDeliveredStartsLocked ();
@@ -4926,7 +4889,7 @@ private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
49264889 * @return true if allow, false otherwise.
49274890 */
49284891 private boolean shouldAllowWhileInUsePermissionInFgsLocked (String callingPackage ,
4929- int callingPid , int callingUid , ServiceRecord r ,
4892+ int callingPid , int callingUid , Intent intent , ServiceRecord r ,
49304893 boolean allowBackgroundActivityStarts ) {
49314894 // Is the background FGS start restriction turned on?
49324895 if (!mAm .mConstants .mFlagBackgroundFgsStartRestrictionEnabled ) {
@@ -4997,32 +4960,4 @@ private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage
49974960 }
49984961 return false ;
49994962 }
5000-
5001- boolean canAllowWhileInUsePermissionInFgsLocked (int callingPid , int callingUid ,
5002- String callingPackage ) {
5003- return shouldAllowWhileInUsePermissionInFgsLocked (
5004- callingPackage , callingPid , callingUid , null , false );
5005- }
5006-
5007- /**
5008- * In R, mAllowWhileInUsePermissionInFgs is to allow while-in-use permissions in foreground
5009- * service or not. while-in-use permissions in FGS started from background might be restricted.
5010- * @param callingPackage caller app's package name.
5011- * @param callingUid caller app's uid.
5012- * @param r the service to start.
5013- * @return true if allow, false otherwise.
5014- */
5015- private void setFgsRestrictionLocked (String callingPackage ,
5016- int callingPid , int callingUid , ServiceRecord r ,
5017- boolean allowBackgroundActivityStarts ) {
5018- r .mLastSetFgsRestrictionTime = SystemClock .elapsedRealtime ();
5019- if (!r .mAllowWhileInUsePermissionInFgs ) {
5020- r .mAllowWhileInUsePermissionInFgs = shouldAllowWhileInUsePermissionInFgsLocked (
5021- callingPackage , callingPid , callingUid , r , allowBackgroundActivityStarts );
5022- }
5023- }
5024-
5025- private void resetFgsRestrictionLocked (ServiceRecord r ) {
5026- r .mAllowWhileInUsePermissionInFgs = false ;
5027- }
50284963}
0 commit comments