Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BookmarkCategory.java « data « bookmarks « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17e721abb3c07e723efae124831afe1ad5f83d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package com.mapswithme.maps.bookmarks.data;

import android.content.Context;
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.PluralsRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.BookmarksPageFactory;
import com.mapswithme.util.TypeConverter;
import com.mapswithme.util.UiUtils;

public class BookmarkCategory implements Parcelable
{
  private final long mId;
  @NonNull
  private final String mName;
  @Nullable
  private final Author mAuthor;
  @NonNull
  private final String mAnnotation;
  @NonNull
  private final String mDescription;
  private final int mTracksCount;
  private final int mBookmarksCount;
  private final int mTypeIndex;
  private final int mAccessRulesIndex;
  private final boolean mIsMyCategory;
  private final boolean mIsVisible;
  @NonNull
  private final String mServerId;


  public BookmarkCategory(long id, @NonNull String name, @NonNull String authorId,
                          @NonNull String authorName, @NonNull String annotation,
                          @NonNull String description, int tracksCount, int bookmarksCount,
                          boolean fromCatalog, boolean isMyCategory, boolean isVisible,
                          int accessRulesIndex, @NonNull String serverId)
  {
    mId = id;
    mName = name;
    mAnnotation = annotation;
    mDescription = description;
    mTracksCount = tracksCount;
    mBookmarksCount = bookmarksCount;
    mIsMyCategory = isMyCategory;
    mServerId = serverId;
    mTypeIndex = fromCatalog && !isMyCategory ? Type.DOWNLOADED.ordinal() : Type.PRIVATE.ordinal();
    mIsVisible = isVisible;
    mAuthor = TextUtils.isEmpty(authorId) || TextUtils.isEmpty(authorName)
              ? null
              : new Author(authorId, authorName);
    mAccessRulesIndex = accessRulesIndex;
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    BookmarkCategory that = (BookmarkCategory) o;
    return mId == that.mId;
  }

  @Override
  public int hashCode()
  {
    return (int)(mId ^ (mId >>> 32));
  }

  public long getId()
  {
    return mId;
  }

  @NonNull
  public String getName()
  {
    return mName;
  }

  @Nullable
  public Author getAuthor()
  {
    return mAuthor;
  }

  public int getTracksCount()
  {
    return mTracksCount;
  }

  public int getBookmarksCount()
  {
    return mBookmarksCount;
  }

  @NonNull
  public Type getType()
  {
    return Type.values()[mTypeIndex];
  }

  @NonNull
  public AccessRules getAccessRules()
  {
    return AccessRules.values()[mAccessRulesIndex];
  }

  public boolean isFromCatalog()
  {
    return Type.values()[mTypeIndex] == Type.DOWNLOADED;
  }

  public boolean isVisible()
  {
    return mIsVisible;
  }

  public boolean isMyCategory()
  {
    return mIsMyCategory;
  }

  public int size()
  {
    return getBookmarksCount() + getTracksCount();
  }

  @NonNull
  public String getAnnotation()
  {
    return mAnnotation;
  }

  @NonNull
  public String getDescription()
  {
    return mDescription;
  }

  @NonNull
  public String getServerId()
  {
    return mServerId;
  }

  @NonNull
  public CountAndPlurals getPluralsCountTemplate()
  {
    if (size() == 0)
      return new CountAndPlurals(0, R.plurals.objects);

    if (getBookmarksCount() == 0)
      return new CountAndPlurals(getTracksCount(), R.plurals.tracks);

    if (getTracksCount() == 0)
      return new CountAndPlurals(getBookmarksCount(), R.plurals.places);

    return new CountAndPlurals(size(), R.plurals.objects);
  }

  public boolean isSharingOptionsAllowed()
  {
    BookmarkCategory.AccessRules rules = getAccessRules();
    return rules != BookmarkCategory.AccessRules.ACCESS_RULES_PAID
           && rules != BookmarkCategory.AccessRules.ACCESS_RULES_P2P
           && size() > 0;
  }

  public boolean isExportAllowed()
  {
    BookmarkCategory.AccessRules rules = getAccessRules();
    boolean isLocal = rules == BookmarkCategory.AccessRules.ACCESS_RULES_LOCAL;
    return isLocal && size() > 0;
  }

  public static class CountAndPlurals {
    private final int mCount;
    @PluralsRes
    private final int mPlurals;

    public CountAndPlurals(int count, int plurals)
    {
      mCount = count;
      mPlurals = plurals;
    }

    public int getCount()
    {
      return mCount;
    }

    public int getPlurals()
    {
      return mPlurals;
    }
  }

  public static class Author implements Parcelable
  {
    @NonNull
    private final String mId;
    @NonNull
    private final String mName;

    public Author(@NonNull String id, @NonNull String name)
    {
      mId = id;
      mName = name;
    }

    @NonNull
    public String getId()
    {
      return mId;
    }

    @NonNull
    public String getName()
    {
      return mName;
    }

    @Override
    public String toString()
    {
      final StringBuilder sb = new StringBuilder("Author{");
      sb.append("mId='").append(mId).append('\'');
      sb.append(", mName='").append(mName).append('\'');
      sb.append('}');
      return sb.toString();
    }

    @Override
    public boolean equals(Object o)
    {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      Author author = (Author) o;
      return mId.equals(author.mId);
    }

    @Override
    public int hashCode()
    {
      return mId.hashCode();
    }

    @Override
    public int describeContents()
    {
      return 0;
    }

    public static String getRepresentation(@NonNull Context context, @NonNull Author author)
    {
      Resources res = context.getResources();
      return String.format(res.getString(R.string.author_name_by_prefix), author.getName());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
      dest.writeString(this.mId);
      dest.writeString(this.mName);
    }

    protected Author(Parcel in)
    {
      this.mId = in.readString();
      this.mName = in.readString();
    }

    public static final Creator<Author> CREATOR = new Creator<Author>()
    {
      @Override
      public Author createFromParcel(Parcel source)
      {
        return new Author(source);
      }

      @Override
      public Author[] newArray(int size)
      {
        return new Author[size];
      }
    };
  }

  @Override
  public String toString()
  {
    final StringBuilder sb = new StringBuilder("BookmarkCategory{");
    sb.append("mId=").append(mId);
    sb.append(", mName='").append(mName).append('\'');
    sb.append(", mAuthor=").append(mAuthor);
    sb.append(", mAnnotation='").append(mAnnotation).append('\'');
    sb.append(", mDescription='").append(mDescription).append('\'');
    sb.append(", mTracksCount=").append(mTracksCount);
    sb.append(", mBookmarksCount=").append(mBookmarksCount);
    sb.append(", mType=").append(Type.values()[mTypeIndex]);
    sb.append(", mIsMyCategory=").append(mIsMyCategory);
    sb.append(", mIsVisible=").append(mIsVisible);
    sb.append(", mAccessRules=").append(getAccessRules());
    sb.append(", mServerId=").append(mServerId);
    sb.append('}');
    return sb.toString();
  }

  public static class Downloaded implements TypeConverter<BookmarkCategory, Boolean>
  {
    @Override
    public Boolean convert(@NonNull BookmarkCategory data)
    {
      return data.getType() == Type.DOWNLOADED;
    }
  }

  public enum Type
  {
    PRIVATE(BookmarksPageFactory.PRIVATE, FilterStrategy.PredicativeStrategy.makePrivateInstance()),
    DOWNLOADED(BookmarksPageFactory.DOWNLOADED, FilterStrategy.PredicativeStrategy.makeDownloadedInstance());

    @NonNull
    private final BookmarksPageFactory mFactory;
    @NonNull
    private final FilterStrategy mFilterStrategy;

    Type(@NonNull BookmarksPageFactory pageFactory, @NonNull FilterStrategy filterStrategy)
    {
      mFactory = pageFactory;
      mFilterStrategy = filterStrategy;
    }

    @NonNull
    public BookmarksPageFactory getFactory()
    {
      return mFactory;
    }

    @NonNull
    public FilterStrategy getFilterStrategy()
    {
      return mFilterStrategy;
    }
  }

  @Override
  public int describeContents()
  {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags)
  {
    dest.writeLong(this.mId);
    dest.writeString(this.mName);
    dest.writeParcelable(this.mAuthor, flags);
    dest.writeString(this.mAnnotation);
    dest.writeString(this.mDescription);
    dest.writeInt(this.mTracksCount);
    dest.writeInt(this.mBookmarksCount);
    dest.writeInt(this.mTypeIndex);
    dest.writeByte(this.mIsMyCategory ? (byte) 1 : (byte) 0);
    dest.writeByte(this.mIsVisible ? (byte) 1 : (byte) 0);
    dest.writeInt(this.mAccessRulesIndex);
    dest.writeString(this.mServerId);
  }

  protected BookmarkCategory(Parcel in)
  {
    this.mId = in.readLong();
    this.mName = in.readString();
    this.mAuthor = in.readParcelable(Author.class.getClassLoader());
    this.mAnnotation = in.readString();
    this.mDescription = in.readString();
    this.mTracksCount = in.readInt();
    this.mBookmarksCount = in.readInt();
    this.mTypeIndex = in.readInt();
    this.mIsMyCategory = in.readByte() != 0;
    this.mIsVisible = in.readByte() != 0;
    this.mAccessRulesIndex = in.readInt();
    this.mServerId = in.readString();
  }

  public static final Creator<BookmarkCategory> CREATOR = new Creator<BookmarkCategory>()
  {
    @Override
    public BookmarkCategory createFromParcel(Parcel source)
    {
      return new BookmarkCategory(source);
    }

    @Override
    public BookmarkCategory[] newArray(int size)
    {
      return new BookmarkCategory[size];
    }
  };

  public enum AccessRules
  {
    ACCESS_RULES_LOCAL(R.string.not_shared, R.drawable.ic_lock),
    ACCESS_RULES_PUBLIC(R.string.public_access, R.drawable.ic_public_inline),
    ACCESS_RULES_DIRECT_LINK(R.string.limited_access, R.drawable.ic_link_inline),
    ACCESS_RULES_P2P(R.string.access_rules_p_to_p, R.drawable.ic_public_inline),
    ACCESS_RULES_PAID(R.string.access_rules_paid, R.drawable.ic_public_inline),
    ACCESS_RULES_AUTHOR_ONLY(R.string.access_rules_author_only, R.drawable.ic_lock);

    private final int mResId;
    private final int mDrawableResId;

    AccessRules(int resId, int drawableResId)
    {
      mResId = resId;
      mDrawableResId = drawableResId;
    }

    @DrawableRes
    public int getDrawableResId()
    {
      return mDrawableResId;
    }

    @StringRes
    public int getNameResId()
    {
      return mResId;
    }
  }
}