Skip to content

Commit fa01cd0

Browse files
simonschillercopybara-github
authored andcommitted
[GH] Avoid exception when calling Activity.reportFullyDrawn()
## Proposed Changes - Avoid a bug on API 19 when calling `Activity.reportFullyDrawn()` I'm not quite sure if this class is the right place for this workaround, the code could also live in other places, e.g.: - `androidx.core.app.ComponentActivity` - `androidx.activity.ComponentActivity` - `androidx.appcompat.app.AppCompatActivity` ## Testing Test: Manually tested. If your test harness is also executed on devices with API 19, I could write an automated test for this change if you want. ## Issues Fixed Fixes: 163239764 This is an imported pull request from #103. Resolves #103 Github-Pr-Head-Sha: 78894ac GitOrigin-RevId: 9ef8e3e Change-Id: Ie4cd0f421b4ac69a3420a553f930fde6253392f0
1 parent a756115 commit fa01cd0

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

activity/activity/src/androidTest/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<activity android:name="androidx.activity.LifecycleComponentActivity"/>
2424
<activity android:name="androidx.activity.EagerOverrideLifecycleComponentActivity"/>
2525
<activity android:name="androidx.activity.LazyOverrideLifecycleComponentActivity"/>
26+
<activity android:name="androidx.activity.ReportFullyDrawnActivity"/>
2627
<activity android:name="androidx.activity.ViewModelActivity"/>
2728
<activity android:name="androidx.activity.SavedStateActivity"/>
2829
<activity android:name="androidx.activity.ContentViewActivity"/>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.activity
18+
19+
import androidx.test.core.app.ActivityScenario
20+
import androidx.test.ext.junit.runners.AndroidJUnit4
21+
import androidx.test.filters.LargeTest
22+
import androidx.testutils.withActivity
23+
import org.junit.Test
24+
import org.junit.runner.RunWith
25+
26+
@LargeTest
27+
@RunWith(AndroidJUnit4::class)
28+
class ComponentActivityReportFullyDrawnTest {
29+
30+
@Test
31+
fun testReportFullyDrawn() {
32+
with(ActivityScenario.launch(ReportFullyDrawnActivity::class.java)) {
33+
withActivity {
34+
// This test makes sure that this method does not throw an exception on devices
35+
// running API 19 (without UPDATE_DEVICE_STATS permission) and earlier
36+
// (regardless or permissions).
37+
reportFullyDrawn()
38+
}
39+
}
40+
}
41+
}
42+
43+
class ReportFullyDrawnActivity : ComponentActivity()

activity/activity/src/main/java/androidx/activity/ComponentActivity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static androidx.activity.result.contract.ActivityResultContracts.StartIntentSenderForResult.EXTRA_INTENT_SENDER_REQUEST;
3030
import static androidx.activity.result.contract.ActivityResultContracts.StartIntentSenderForResult.EXTRA_SEND_INTENT_EXCEPTION;
3131

32+
import android.Manifest;
3233
import android.annotation.SuppressLint;
3334
import android.app.Activity;
3435
import android.content.Context;
@@ -62,6 +63,7 @@
6263
import androidx.annotation.Nullable;
6364
import androidx.core.app.ActivityCompat;
6465
import androidx.core.app.ActivityOptionsCompat;
66+
import androidx.core.content.ContextCompat;
6567
import androidx.lifecycle.HasDefaultViewModelProviderFactory;
6668
import androidx.lifecycle.Lifecycle;
6769
import androidx.lifecycle.LifecycleEventObserver;
@@ -685,4 +687,19 @@ public final <I, O> ActivityResultLauncher<I> registerForActivityResult(
685687
public final ActivityResultRegistry getActivityResultRegistry() {
686688
return mActivityResultRegistry;
687689
}
690+
691+
@Override
692+
public void reportFullyDrawn() {
693+
if (Build.VERSION.SDK_INT > 19) {
694+
super.reportFullyDrawn();
695+
} else if (Build.VERSION.SDK_INT == 19 && ContextCompat.checkSelfPermission(this,
696+
Manifest.permission.UPDATE_DEVICE_STATS) == PackageManager.PERMISSION_GRANTED) {
697+
// On API 19, the Activity.reportFullyDrawn() method requires the UPDATE_DEVICE_STATS
698+
// permission, otherwise it throws an exception. Instead of throwing, we fall back to
699+
// a no-op call.
700+
super.reportFullyDrawn();
701+
}
702+
// The Activity.reportFullyDrawn() got added in API 19, fall back to a no-op call if this
703+
// method gets called on devices with an earlier version.
704+
}
688705
}

0 commit comments

Comments
 (0)