Bug 1357579 - Correctly copy the sparse Boolean array when clearing Site Settings. r?ahunt
The checked items are stored in a *sparse* Boolean array, which we want to transform into an array (list) of the checked indices for transmission to Gecko.
The current approach doesn't do this correctly, as it iterates over all (sparse and non-sparse) items, but uses SparseBooleanArray.size() (which only counts non-sparse items) as its iteration limit. This means that we only copy the checked state of the first n items, where n is the total count of checked items.
For correctly iterating over the array to retrieve all indices that are true, we'd either have to use the largest available key (if we'd want to iterate over everything, including the sparse indices), or else use the approach chosen in this patch, namely using valueAt/keyAt in order to iterate over the internal array that's storing the values for all non-sparse indices.
MozReview-Commit-ID: FRGI4Rr0uCb
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -881,21 +881,21 @@ public abstract class GeckoApp
});
builder.setPositiveButton(R.string.site_settings_clear, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
ListView listView = ((AlertDialog) dialog).getListView();
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
- // An array of the indices of the permissions we want to clear
+ // An array of the indices of the permissions we want to clear.
final ArrayList<Integer> permissionsToClear = new ArrayList<>();
for (int i = 0; i < checkedItemPositions.size(); i++) {
- if (checkedItemPositions.get(i)) {
- permissionsToClear.add(i);
+ if (checkedItemPositions.valueAt(i)) {
+ permissionsToClear.add(checkedItemPositions.keyAt(i));
}
}
final GeckoBundle data = new GeckoBundle(1);
data.putIntArray("permissions", permissionsToClear);
EventDispatcher.getInstance().dispatch("Permissions:Clear", data);
}
});