[StartForFutureActivity] Mitigate CompletableFuture leak
Instead of blocking concurrent requests to StartForFutureActivity,
allow them. Actually, we don't *really* need to support concurrent
requests: but, by writing our code this way, we mitigate the issue
of a leaked global variable (StartForFutureActivity.future) blocking
subsequent requests.
In order to support concurrent invocations of StartForFutureActivity,
we need to perform the tricky task of correlating activity results
(i.e., resultCode and data) back to their origin (i.e., a
CompletableFuture, a non-serializable object) without leaking. But:
1. we can't use class instance variables, since this is an Activity,
and we need to anticipate configuration changes, and
2. we can't save non-serializable objects into savedInstanceState, and
3. This leaves us with static/global variables, which lead to memory
leak issues, because we can't always guarantee a cleanup method
will be invoked after each request.
The tradeoff made here is to use a static/global variable to hold a
collection of CompletableFutures (StartForFutureActivity.futures), but
do it in a way that mitigates leaks. Here, we do that by using an
LruCache, which conventially does what we want: it acts as a Map that
automatically drops old entries once the Map reaches a certain
capacity. (The capacity of 10 here was chosen arbitrarily. It's
possible 2 is enough.)
(This is a short-term "fix". Ideally, what we should do is avoid
global variables altogether. We can do this by invoking
StartForFutureActivity with startActivityForResult. But,
ActivityScenario, which we're using to host the activity, has some
bugs that we'll need to work around.)
LOW_COVERAGE_REASON=TEST_ONLY
Bug: 333609580
Test: atest CtsPermissionUiTestCases
Merged-In: I058717b47dd0dedeb98dcc625cfbe091f532cd7e
Merged-In: Id3befa381a08cc65ce6ce8aa37b2296b4bfba325
Change-Id: I058717b47dd0dedeb98dcc625cfbe091f532cd7e
(cherry picked from commit 518a7a1c03b0499aab02d94d54c6a575b7e4f3e9)
1 file changed