1 /* 2 * Copyright (C) 2016 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 com.android.documentsui.sorting; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.view.View; 22 23 import androidx.annotation.IntDef; 24 import androidx.annotation.StringRes; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 29 /** 30 * A model class that describes a sort dimension and its sort state. 31 */ 32 public class SortDimension implements Parcelable { 33 34 /** 35 * This enum is defined as flag because it's also used to denote whether a column can be sorted 36 * in a certain direction. 37 */ 38 @IntDef(flag = true, value = { 39 SORT_DIRECTION_NONE, 40 SORT_DIRECTION_ASCENDING, 41 SORT_DIRECTION_DESCENDING 42 }) 43 @Retention(RetentionPolicy.SOURCE) 44 public @interface SortDirection {} 45 public static final int SORT_DIRECTION_NONE = 0; 46 public static final int SORT_DIRECTION_ASCENDING = 1; 47 public static final int SORT_DIRECTION_DESCENDING = 2; 48 49 @IntDef({ 50 SORT_CAPABILITY_NONE, 51 SORT_CAPABILITY_BOTH_DIRECTION 52 }) 53 @Retention(RetentionPolicy.SOURCE) 54 @interface SortCapability {} 55 public static final int SORT_CAPABILITY_NONE = 0; 56 public static final int SORT_CAPABILITY_BOTH_DIRECTION = 57 SORT_DIRECTION_ASCENDING | SORT_DIRECTION_DESCENDING; 58 59 @IntDef({ 60 DATA_TYPE_STRING, 61 DATA_TYPE_NUMBER 62 }) 63 @Retention(RetentionPolicy.SOURCE) 64 public @interface DataType {} 65 public static final int DATA_TYPE_STRING = 0; 66 public static final int DATA_TYPE_NUMBER = 1; 67 68 private final int mId; 69 private final @StringRes int mLabelId; 70 private final @DataType int mDataType; 71 private final @SortCapability int mSortCapability; 72 private final @SortDirection int mDefaultSortDirection; 73 74 @SortDirection int mSortDirection = SORT_DIRECTION_NONE; 75 int mVisibility; 76 SortDimension(int id, @StringRes int labelId, @DataType int dataType, @SortCapability int sortCapability, @SortDirection int defaultSortDirection)77 private SortDimension(int id, @StringRes int labelId, @DataType int dataType, 78 @SortCapability int sortCapability, @SortDirection int defaultSortDirection) { 79 mId = id; 80 mLabelId = labelId; 81 mDataType = dataType; 82 mSortCapability = sortCapability; 83 mDefaultSortDirection = defaultSortDirection; 84 } 85 getId()86 public int getId() { 87 return mId; 88 } 89 getLabelId()90 public @StringRes int getLabelId() { 91 return mLabelId; 92 } 93 getDataType()94 public @DataType int getDataType() { 95 return mDataType; 96 } 97 getSortCapability()98 public @SortCapability int getSortCapability() { 99 return mSortCapability; 100 } 101 getDefaultSortDirection()102 public @SortDirection int getDefaultSortDirection() { 103 return mDefaultSortDirection; 104 } 105 getNextDirection()106 public @SortDirection int getNextDirection() { 107 @SortDimension.SortDirection int alternativeDirection = 108 (mDefaultSortDirection == SortDimension.SORT_DIRECTION_ASCENDING) 109 ? SortDimension.SORT_DIRECTION_DESCENDING 110 : SortDimension.SORT_DIRECTION_ASCENDING; 111 @SortDimension.SortDirection int direction = 112 (mSortDirection == mDefaultSortDirection) 113 ? alternativeDirection 114 : mDefaultSortDirection; 115 116 return direction; 117 } 118 getSortDirection()119 public @SortDirection int getSortDirection() { 120 return mSortDirection; 121 } 122 getVisibility()123 public int getVisibility() { 124 return mVisibility; 125 } 126 127 @Override hashCode()128 public int hashCode() { 129 return mId; 130 } 131 132 @Override equals(Object o)133 public boolean equals(Object o) { 134 if (o == null || !(o instanceof SortDimension)) { 135 return false; 136 } 137 138 if (this == o) { 139 return true; 140 } 141 142 SortDimension other = (SortDimension) o; 143 144 return mId == other.mId 145 && mLabelId == other.mLabelId 146 && mDataType == other.mDataType 147 && mSortCapability == other.mSortCapability 148 && mDefaultSortDirection == other.mDefaultSortDirection 149 && mSortDirection == other.mSortDirection 150 && mVisibility == other.mVisibility; 151 } 152 153 @Override toString()154 public String toString() { 155 return new StringBuilder().append("SortDimension{") 156 .append("id=").append(mId) 157 .append(", labelId=").append(mLabelId) 158 .append(", dataType=").append(mDataType) 159 .append(", sortCapability=").append(mSortCapability) 160 .append(", defaultSortDirection=").append(mDefaultSortDirection) 161 .append(", sortDirection=").append(mSortDirection) 162 .append(", visibility=").append(mVisibility) 163 .append("}") 164 .toString(); 165 } 166 167 @Override describeContents()168 public int describeContents() { 169 return 0; 170 } 171 172 @Override writeToParcel(Parcel out, int flag)173 public void writeToParcel(Parcel out, int flag) { 174 out.writeInt(mId); 175 out.writeInt(mLabelId); 176 out.writeInt(mDataType); 177 out.writeInt(mSortCapability); 178 out.writeInt(mDefaultSortDirection); 179 out.writeInt(mSortDirection); 180 out.writeInt(mVisibility); 181 } 182 183 public static final Parcelable.Creator<SortDimension> CREATOR = 184 new Parcelable.Creator<SortDimension>() { 185 186 @Override 187 public SortDimension createFromParcel(Parcel in) { 188 int id = in.readInt(); 189 @StringRes int labelId = in.readInt(); 190 @DataType int dataType = in.readInt(); 191 int sortCapability = in.readInt(); 192 int defaultSortDirection = in.readInt(); 193 194 SortDimension column = 195 new SortDimension(id, labelId, dataType, sortCapability, defaultSortDirection); 196 197 column.mSortDirection = in.readInt(); 198 column.mVisibility = in.readInt(); 199 200 return column; 201 } 202 203 @Override 204 public SortDimension[] newArray(int size) { 205 return new SortDimension[size]; 206 } 207 }; 208 209 static class Builder { 210 private int mId; 211 private @StringRes int mLabelId; 212 private @DataType int mDataType = DATA_TYPE_STRING; 213 private @SortCapability int mSortCapability = SORT_CAPABILITY_BOTH_DIRECTION; 214 private @SortDirection int mDefaultSortDirection = SORT_DIRECTION_ASCENDING; 215 private int mVisibility = View.VISIBLE; 216 withId(int id)217 Builder withId(int id) { 218 mId = id; 219 return this; 220 } 221 withLabelId(@tringRes int labelId)222 Builder withLabelId(@StringRes int labelId) { 223 mLabelId = labelId; 224 return this; 225 } 226 withDataType(@ataType int dataType)227 Builder withDataType(@DataType int dataType) { 228 mDataType = dataType; 229 return this; 230 } 231 withSortCapability(@ortCapability int sortCapability)232 Builder withSortCapability(@SortCapability int sortCapability) { 233 mSortCapability = sortCapability; 234 return this; 235 } 236 withVisibility(int visibility)237 Builder withVisibility(int visibility) { 238 mVisibility = visibility; 239 return this; 240 } 241 withDefaultSortDirection(@ortDirection int defaultSortDirection)242 Builder withDefaultSortDirection(@SortDirection int defaultSortDirection) { 243 mDefaultSortDirection = defaultSortDirection; 244 return this; 245 } 246 build()247 SortDimension build() { 248 if (mLabelId == 0) { 249 throw new IllegalStateException("Must set labelId."); 250 } 251 252 SortDimension dimension = new SortDimension( 253 mId, mLabelId, mDataType, mSortCapability, mDefaultSortDirection); 254 dimension.mVisibility = mVisibility; 255 return dimension; 256 } 257 } 258 } 259