Skip to content

Commit 2384fb3

Browse files
author
Android Build Coastguard Worker
committed
Merge cherrypicks of [15164514, 15164515, 15164096, 15164078, 15163495, 15164068, 15164480, 15164097, 15164098, 15164482, 15164698, 15164375, 15164485, 15164469, 15164730, 15164731, 15164732, 15164733, 15164734, 15164735, 15164736, 15164737, 15164738, 15163662, 15164486, 15164519, 15164699, 15164700, 15164487, 15164099] into rvc-qpr3-release
Change-Id: I4020f23d9d9b511acc588327bc22d6ccd34a22b5
2 parents 50be4f5 + f1c159e commit 2384fb3

14 files changed

Lines changed: 300 additions & 59 deletions

File tree

core/java/android/content/pm/parsing/component/ParsedIntentInfo.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.annotation.Nullable;
2020
import android.content.IntentFilter;
2121
import android.os.Parcel;
22-
import android.os.Parcelable;
2322
import android.util.Pair;
2423

2524
import com.android.internal.util.DataClass;
@@ -168,19 +167,6 @@ public String toString() {
168167
+ '}';
169168
}
170169

171-
public static final Parcelable.Creator<ParsedIntentInfo> CREATOR =
172-
new Parcelable.Creator<ParsedIntentInfo>() {
173-
@Override
174-
public ParsedIntentInfo createFromParcel(Parcel source) {
175-
return new ParsedIntentInfo(source);
176-
}
177-
178-
@Override
179-
public ParsedIntentInfo[] newArray(int size) {
180-
return new ParsedIntentInfo[size];
181-
}
182-
};
183-
184170
public boolean isHasDefault() {
185171
return hasDefault;
186172
}

core/java/android/text/Layout.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,10 @@ private void ellipsize(int start, int end, int line,
23502350
final int ellipsisStringLen = ellipsisString.length();
23512351
// Use the ellipsis string only if there are that at least as many characters to replace.
23522352
final boolean useEllipsisString = ellipsisCount >= ellipsisStringLen;
2353-
for (int i = 0; i < ellipsisCount; i++) {
2353+
final int min = Math.max(0, start - ellipsisStart - lineStart);
2354+
final int max = Math.min(ellipsisCount, end - ellipsisStart - lineStart);
2355+
2356+
for (int i = min; i < max; i++) {
23542357
final char c;
23552358
if (useEllipsisString && i < ellipsisStringLen) {
23562359
c = ellipsisString.charAt(i);
@@ -2359,9 +2362,7 @@ private void ellipsize(int start, int end, int line,
23592362
}
23602363

23612364
final int a = i + ellipsisStart + lineStart;
2362-
if (start <= a && a < end) {
2363-
dest[destoff + a - start] = c;
2364-
}
2365+
dest[destoff + a - start] = c;
23652366
}
23662367
}
23672368

packages/Shell/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
<provider android:name=".HeapDumpProvider"
347347
android:authorities="com.android.shell.heapdump"
348348
android:grantUriPermissions="true"
349-
android:exported="true" />
349+
android:exported="false" />
350350

351351
<activity
352352
android:name=".BugreportWarningActivity"

services/core/java/com/android/server/am/ActiveServices.java

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,8 @@ ComponentName startServiceLocked(IApplicationThread caller, Intent service, Stri
734734
}
735735
ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
736736

737-
if (!r.mAllowWhileInUsePermissionInFgs) {
738-
r.mAllowWhileInUsePermissionInFgs =
739-
shouldAllowWhileInUsePermissionInFgsLocked(callingPackage, callingPid,
740-
callingUid, service, r, allowBackgroundActivityStarts);
741-
}
737+
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, r,
738+
allowBackgroundActivityStarts);
742739

743740
return cmp;
744741
}
@@ -1411,14 +1408,6 @@ private void setServiceForegroundInnerLocked(final ServiceRecord r, int id,
14111408
+ String.format("0x%08X", manifestType)
14121409
+ " in service element of manifest file");
14131410
}
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-
}
14221411
}
14231412
boolean alreadyStartedOp = false;
14241413
boolean stopProcStatsOp = false;
@@ -1466,6 +1455,57 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
14661455
ignoreForeground = true;
14671456
}
14681457

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+
14691509
// Apps under strict background restrictions simply don't get to have foreground
14701510
// services, so now that we've enforced the startForegroundService() contract
14711511
// we only do the machinery of making the service foreground when the app
@@ -1501,6 +1541,7 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
15011541
active.mNumActive++;
15021542
}
15031543
r.isForeground = true;
1544+
r.mStartForegroundCount++;
15041545
if (!stopProcStatsOp) {
15051546
ServiceState stracker = r.getTracker();
15061547
if (stracker != null) {
@@ -1559,6 +1600,7 @@ && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {
15591600
decActiveForegroundAppLocked(smap, r);
15601601
}
15611602
r.isForeground = false;
1603+
resetFgsRestrictionLocked(r);
15621604
ServiceState stracker = r.getTracker();
15631605
if (stracker != null) {
15641606
stracker.setForeground(false, mAm.mProcessStats.getMemFactorLocked(),
@@ -2118,12 +2160,7 @@ public void run() {
21182160
}
21192161
}
21202162

2121-
if (!s.mAllowWhileInUsePermissionInFgs) {
2122-
s.mAllowWhileInUsePermissionInFgs =
2123-
shouldAllowWhileInUsePermissionInFgsLocked(callingPackage,
2124-
callingPid, callingUid,
2125-
service, s, false);
2126-
}
2163+
setFgsRestrictionLocked(callingPackage, callingPid, callingUid, s, false);
21272164

21282165
if (s.app != null) {
21292166
if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
@@ -3419,7 +3456,7 @@ private final void bringDownServiceLocked(ServiceRecord r) {
34193456
r.isForeground = false;
34203457
r.foregroundId = 0;
34213458
r.foregroundNoti = null;
3422-
r.mAllowWhileInUsePermissionInFgs = false;
3459+
resetFgsRestrictionLocked(r);
34233460

34243461
// Clear start entries.
34253462
r.clearDeliveredStartsLocked();
@@ -4900,7 +4937,7 @@ private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
49004937
* @return true if allow, false otherwise.
49014938
*/
49024939
private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage,
4903-
int callingPid, int callingUid, Intent intent, ServiceRecord r,
4940+
int callingPid, int callingUid, ServiceRecord r,
49044941
boolean allowBackgroundActivityStarts) {
49054942
// Is the background FGS start restriction turned on?
49064943
if (!mAm.mConstants.mFlagBackgroundFgsStartRestrictionEnabled) {
@@ -4986,6 +5023,28 @@ private boolean shouldAllowWhileInUsePermissionInFgsLocked(String callingPackage
49865023
boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid,
49875024
String callingPackage) {
49885025
return shouldAllowWhileInUsePermissionInFgsLocked(
4989-
callingPackage, callingPid, callingUid, null, null, false);
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;
49905049
}
49915050
}

services/core/java/com/android/server/am/ActivityManagerConstants.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ final class ActivityManagerConstants extends ContentObserver {
8888
static final String KEY_PROCESS_START_ASYNC = "process_start_async";
8989
static final String KEY_MEMORY_INFO_THROTTLE_TIME = "memory_info_throttle_time";
9090
static final String KEY_TOP_TO_FGS_GRACE_DURATION = "top_to_fgs_grace_duration";
91+
static final String KEY_FGS_START_FOREGROUND_TIMEOUT = "fgs_start_foreground_timeout";
9192
static final String KEY_PENDINGINTENT_WARNING_THRESHOLD = "pendingintent_warning_threshold";
9293

9394
private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
@@ -121,6 +122,7 @@ final class ActivityManagerConstants extends ContentObserver {
121122
private static final boolean DEFAULT_PROCESS_START_ASYNC = true;
122123
private static final long DEFAULT_MEMORY_INFO_THROTTLE_TIME = 5*60*1000;
123124
private static final long DEFAULT_TOP_TO_FGS_GRACE_DURATION = 15 * 1000;
125+
private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
124126
private static final int DEFAULT_PENDINGINTENT_WARNING_THRESHOLD = 2000;
125127

126128
// Flag stored in the DeviceConfig API.
@@ -273,6 +275,12 @@ final class ActivityManagerConstants extends ContentObserver {
273275
// this long.
274276
public long TOP_TO_FGS_GRACE_DURATION = DEFAULT_TOP_TO_FGS_GRACE_DURATION;
275277

278+
/**
279+
* When service started from background, before the timeout it can be promoted to FGS by calling
280+
* Service.startForeground().
281+
*/
282+
volatile long mFgsStartForegroundTimeoutMs = DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS;
283+
276284
// Indicates whether the activity starts logging is enabled.
277285
// Controlled by Settings.Global.ACTIVITY_STARTS_LOGGING_ENABLED
278286
volatile boolean mFlagActivityStartsLoggingEnabled;
@@ -421,6 +429,9 @@ public void onPropertiesChanged(Properties properties) {
421429
case KEY_MIN_ASSOC_LOG_DURATION:
422430
updateMinAssocLogDuration();
423431
break;
432+
case KEY_FGS_START_FOREGROUND_TIMEOUT:
433+
updateFgsStartForegroundTimeout();
434+
break;
424435
default:
425436
break;
426437
}
@@ -697,6 +708,13 @@ private void updateMinAssocLogDuration() {
697708
/* defaultValue */ DEFAULT_MIN_ASSOC_LOG_DURATION);
698709
}
699710

711+
private void updateFgsStartForegroundTimeout() {
712+
mFgsStartForegroundTimeoutMs = DeviceConfig.getLong(
713+
DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
714+
KEY_FGS_START_FOREGROUND_TIMEOUT,
715+
DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
716+
}
717+
700718
void dump(PrintWriter pw) {
701719
pw.println("ACTIVITY MANAGER SETTINGS (dumpsys activity settings) "
702720
+ Settings.Global.ACTIVITY_MANAGER_CONSTANTS + ":");
@@ -769,6 +787,8 @@ void dump(PrintWriter pw) {
769787
pw.println(Arrays.toString(IMPERCEPTIBLE_KILL_EXEMPT_PACKAGES.toArray()));
770788
pw.print(" "); pw.print(KEY_MIN_ASSOC_LOG_DURATION); pw.print("=");
771789
pw.println(MIN_ASSOC_LOG_DURATION);
790+
pw.print(" "); pw.print(KEY_FGS_START_FOREGROUND_TIMEOUT); pw.print("=");
791+
pw.println(mFgsStartForegroundTimeoutMs);
772792

773793
pw.println();
774794
if (mOverrideMaxCachedProcesses >= 0) {

services/core/java/com/android/server/am/ActivityManagerShellCommand.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import com.android.server.compat.PlatformCompat;
9595

9696
import java.io.BufferedReader;
97-
import java.io.File;
9897
import java.io.IOException;
9998
import java.io.InputStream;
10099
import java.io.InputStreamReader;
@@ -787,8 +786,7 @@ int runTraceIpcStop(PrintWriter pw) throws RemoteException {
787786
return -1;
788787
}
789788

790-
File file = new File(filename);
791-
file.delete();
789+
// Writes an error message to stderr on failure
792790
ParcelFileDescriptor fd = openFileForSystem(filename, "w");
793791
if (fd == null) {
794792
return -1;
@@ -942,16 +940,16 @@ int runDumpHeap(PrintWriter pw) throws RemoteException {
942940
String logNameTimeString = LOG_NAME_TIME_FORMATTER.format(localDateTime);
943941
heapFile = "/data/local/tmp/heapdump-" + logNameTimeString + ".prof";
944942
}
945-
pw.println("File: " + heapFile);
946-
pw.flush();
947943

948-
File file = new File(heapFile);
949-
file.delete();
944+
// Writes an error message to stderr on failure
950945
ParcelFileDescriptor fd = openFileForSystem(heapFile, "w");
951946
if (fd == null) {
952947
return -1;
953948
}
954949

950+
pw.println("File: " + heapFile);
951+
pw.flush();
952+
955953
final CountDownLatch latch = new CountDownLatch(1);
956954

957955
final RemoteCallback finishCallback = new RemoteCallback(new OnResultListener() {

services/core/java/com/android/server/am/ServiceRecord.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
142142
// allow while-in-use permissions in foreground service or not.
143143
// while-in-use permissions in FGS started from background might be restricted.
144144
boolean mAllowWhileInUsePermissionInFgs;
145+
// The number of times Service.startForeground() is called;
146+
int mStartForegroundCount;
147+
// Last time mAllowWhileInUsePermissionInFgs is set.
148+
long mLastSetFgsRestrictionTime;
145149

146150
// the most recent package that start/bind this service.
147151
String mRecentCallingPackage;
@@ -406,6 +410,8 @@ void dump(PrintWriter pw, String prefix) {
406410
}
407411
pw.print(prefix); pw.print("allowWhileInUsePermissionInFgs=");
408412
pw.println(mAllowWhileInUsePermissionInFgs);
413+
pw.print(prefix); pw.print("startForegroundCount=");
414+
pw.println(mStartForegroundCount);
409415
pw.print(prefix); pw.print("recentCallingPackage=");
410416
pw.println(mRecentCallingPackage);
411417
if (delayed) {

0 commit comments

Comments
 (0)