Fix a bug in RegisterStatusBarResult parceling
This is a follow up CL to my previous CL [1], which introduced
RegisterStatusBarResult to consolidate return values from
StatusBarManagerService#registerStatusBar().
It turns out that the following fields are not correctly unmarshalled.
- RegisterStatusBarResult#mFullscreenStackBounds
- RegisterStatusBarResult#mDockedStackBounds
This is because those two fields were written with
Parcel#writeParcelable() while we were reading them directly with
Rect.CREATOR#createFromParcel(). Parcel#writeParcelable() needs to be
used with Parcel#readParcelable() for null handling and custom
class-loader handling.
With this CL, we now read/write these two fields with the following
methods, which can also be used to write/read Parcelable objects in a
bit more simpler style unless as long as we can rely on the default
class-loader.
- Parcel#writeTypedObject()
- Parcel#readTypedObject()
This CL also adds unit tests for StatusBarIcon and
RegisterStatusBarResult to make sure that these classes can be sent
over Parcel.
[1]: Ib1c0ae8f591ca09d0bce7a39f85ba57aad386e47
49efa718a8
Bug: 122439339
Test: atest FrameworksCoreTests:StatusBarIconTest
Test: atest FrameworksCoreTests:RegisterStatusBarResultTest
Change-Id: I0f41cbcb4c2f1222f18e1e34e3cf40bb0dfdfdb3
This commit is contained in:
@@ -78,8 +78,8 @@ public final class RegisterStatusBarResult implements Parcelable {
|
||||
dest.writeInt(mFullscreenStackSysUiVisibility);
|
||||
dest.writeInt(mDockedStackSysUiVisibility);
|
||||
dest.writeStrongBinder(mImeToken);
|
||||
dest.writeParcelable(mFullscreenStackBounds, flags);
|
||||
dest.writeParcelable(mDockedStackBounds, flags);
|
||||
dest.writeTypedObject(mFullscreenStackBounds, flags);
|
||||
dest.writeTypedObject(mDockedStackBounds, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,8 +101,8 @@ public final class RegisterStatusBarResult implements Parcelable {
|
||||
final int fullscreenStackSysUiVisibility = source.readInt();
|
||||
final int dockedStackSysUiVisibility = source.readInt();
|
||||
final IBinder imeToken = source.readStrongBinder();
|
||||
final Rect fullscreenStackBounds = Rect.CREATOR.createFromParcel(source);
|
||||
final Rect dockedStackBounds = Rect.CREATOR.createFromParcel(source);
|
||||
final Rect fullscreenStackBounds = source.readTypedObject(Rect.CREATOR);
|
||||
final Rect dockedStackBounds = source.readTypedObject(Rect.CREATOR);
|
||||
return new RegisterStatusBarResult(icons, disabledFlags1, systemUiVisibility,
|
||||
menuVisible, imeWindowVis, imeBackDisposition, showImeSwitcher,
|
||||
disabledFlags2, fullscreenStackSysUiVisibility,
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.internal.statusbar;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.os.Binder;
|
||||
import android.os.Parcel;
|
||||
import android.os.UserHandle;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class RegisterStatusBarResultTest {
|
||||
|
||||
/**
|
||||
* Test {@link RegisterStatusBarResult} can be stored then restored with {@link Parcel}.
|
||||
*/
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
final String dumyIconKey = "dummyIcon1";
|
||||
final ArrayMap<String, StatusBarIcon> iconMap = new ArrayMap<>();
|
||||
iconMap.put(dumyIconKey, new StatusBarIcon("com.android.internal.statusbar.test",
|
||||
UserHandle.of(100), 123, 1, 2, "dummyIconDescription"));
|
||||
|
||||
final RegisterStatusBarResult original = new RegisterStatusBarResult(iconMap,
|
||||
0x2 /* disabledFlags1 */,
|
||||
0x4 /* systemUiVisibility */,
|
||||
true /* menuVisible */,
|
||||
0x8 /* imeWindowVis */,
|
||||
0x10 /* imeBackDisposition */,
|
||||
false /* showImeSwitcher */,
|
||||
0x20 /* disabledFlags2 */,
|
||||
0x40 /* fullscreenStackSysUiVisibility */,
|
||||
0x80 /* dockedStackSysUiVisibility */,
|
||||
new Binder() /* imeToken */,
|
||||
new Rect(0x100, 0x200, 0x400, 0x800) /* fullscreenStackBounds */,
|
||||
new Rect(0x1000, 0x2000, 0x4000, 0x8000) /* dockedStackBounds */);
|
||||
|
||||
final RegisterStatusBarResult copy = clone(original);
|
||||
|
||||
assertThat(copy.mIcons).hasSize(original.mIcons.size());
|
||||
// We already test that StatusBarIcon is Parcelable. Only check StatusBarIcon.user here.
|
||||
assertThat(copy.mIcons.get(dumyIconKey).user)
|
||||
.isEqualTo(original.mIcons.get(dumyIconKey).user);
|
||||
|
||||
assertThat(copy.mDisabledFlags1).isEqualTo(original.mDisabledFlags1);
|
||||
assertThat(copy.mSystemUiVisibility).isEqualTo(original.mSystemUiVisibility);
|
||||
assertThat(copy.mMenuVisible).isEqualTo(original.mMenuVisible);
|
||||
assertThat(copy.mImeWindowVis).isEqualTo(original.mImeWindowVis);
|
||||
assertThat(copy.mImeBackDisposition).isEqualTo(original.mImeBackDisposition);
|
||||
assertThat(copy.mShowImeSwitcher).isEqualTo(original.mShowImeSwitcher);
|
||||
assertThat(copy.mDisabledFlags2).isEqualTo(original.mDisabledFlags2);
|
||||
assertThat(copy.mFullscreenStackSysUiVisibility)
|
||||
.isEqualTo(original.mFullscreenStackSysUiVisibility);
|
||||
assertThat(copy.mDockedStackSysUiVisibility)
|
||||
.isEqualTo(original.mDockedStackSysUiVisibility);
|
||||
assertThat(copy.mImeToken).isSameAs(original.mImeToken);
|
||||
assertThat(copy.mFullscreenStackBounds).isEqualTo(original.mFullscreenStackBounds);
|
||||
assertThat(copy.mDockedStackBounds).isEqualTo(original.mDockedStackBounds);
|
||||
}
|
||||
|
||||
private RegisterStatusBarResult clone(RegisterStatusBarResult original) {
|
||||
Parcel parcel = null;
|
||||
try {
|
||||
parcel = Parcel.obtain();
|
||||
original.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return RegisterStatusBarResult.CREATOR.createFromParcel(parcel);
|
||||
} finally {
|
||||
if (parcel != null) {
|
||||
parcel.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.internal.statusbar;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.UserHandle;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class StatusBarIconTest {
|
||||
|
||||
/**
|
||||
* Test {@link StatusBarIcon} can be stored then restored with {@link Parcel}.
|
||||
*/
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
final UserHandle dummyUserHandle = UserHandle.of(100);
|
||||
final String dummyIconPackageName = "com.android.internal.statusbar.test";
|
||||
final int dummyIconId = 123;
|
||||
final int dummyIconLevel = 1;
|
||||
final int dummyIconNumber = 2;
|
||||
final CharSequence dummyIconContentDescription = "dummyIcon";
|
||||
final StatusBarIcon original = new StatusBarIcon(dummyIconPackageName, dummyUserHandle,
|
||||
dummyIconId, dummyIconLevel, dummyIconNumber, dummyIconContentDescription);
|
||||
|
||||
final StatusBarIcon copy = clone(original);
|
||||
|
||||
assertThat(copy.user).isEqualTo(original.user);
|
||||
assertThat(copy.pkg).isEqualTo(original.pkg);
|
||||
assertThat(copy.icon.sameAs(original.icon)).isTrue();
|
||||
assertThat(copy.iconLevel).isEqualTo(original.iconLevel);
|
||||
assertThat(copy.visible).isEqualTo(original.visible);
|
||||
assertThat(copy.number).isEqualTo(original.number);
|
||||
assertThat(copy.contentDescription).isEqualTo(original.contentDescription);
|
||||
}
|
||||
|
||||
private StatusBarIcon clone(StatusBarIcon original) {
|
||||
Parcel parcel = null;
|
||||
try {
|
||||
parcel = Parcel.obtain();
|
||||
original.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return StatusBarIcon.CREATOR.createFromParcel(parcel);
|
||||
} finally {
|
||||
if (parcel != null) {
|
||||
parcel.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user