SparseBooleanArrayParcelable.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud GmbH.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.helpers;
  21. import android.os.Parcel;
  22. import android.os.Parcelable;
  23. import android.util.SparseBooleanArray;
  24. /**
  25. * Wraps a SparseBooleanArrayParcelable to allow its serialization and desearialization
  26. * through {@link Parcelable} interface.
  27. */
  28. public class SparseBooleanArrayParcelable implements Parcelable {
  29. public static Parcelable.Creator<SparseBooleanArrayParcelable> CREATOR =
  30. new Parcelable.Creator<SparseBooleanArrayParcelable>() {
  31. @Override
  32. public SparseBooleanArrayParcelable createFromParcel(Parcel source) {
  33. // read size of array from source
  34. int size = source.readInt();
  35. // then pairs of (key, value)s, in the object to wrap
  36. SparseBooleanArray sba = new SparseBooleanArray();
  37. int key;
  38. boolean value;
  39. for (int i = 0; i < size; i++) {
  40. key = source.readInt();
  41. value = (source.readInt() != 0);
  42. sba.put(key, value);
  43. }
  44. // wrap SparseBooleanArray
  45. return new SparseBooleanArrayParcelable(sba);
  46. }
  47. @Override
  48. public SparseBooleanArrayParcelable[] newArray(int size) {
  49. return new SparseBooleanArrayParcelable[size];
  50. }
  51. };
  52. private final SparseBooleanArray mSba;
  53. public SparseBooleanArrayParcelable(SparseBooleanArray sba) {
  54. if (sba == null) {
  55. throw new IllegalArgumentException("Cannot wrap a null SparseBooleanArray");
  56. }
  57. mSba = sba;
  58. }
  59. public SparseBooleanArray getSparseBooleanArray() {
  60. return mSba;
  61. }
  62. @Override
  63. public int describeContents() {
  64. return 0;
  65. }
  66. @Override
  67. public void writeToParcel(Parcel dest, int flags) {
  68. // first, size of the array
  69. dest.writeInt(mSba.size());
  70. // then, pairs of (key, value)
  71. for (int i = 0; i < mSba.size(); i++) {
  72. dest.writeInt(mSba.keyAt(i));
  73. dest.writeInt(mSba.valueAt(i) ? 1 : 0);
  74. }
  75. }
  76. }