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

UGC.java « ugc « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0d381c0950f6367bfb2a592432b38a90ecacb54a (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
package com.mapswithme.maps.ugc;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.background.AppBackgroundTracker;
import com.mapswithme.maps.background.WorkerService;
import com.mapswithme.maps.bookmarks.data.FeatureId;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.List;

public class UGC
{
  @Retention(RetentionPolicy.SOURCE)
  @IntDef({ RATING_NONE, RATING_HORRIBLE, RATING_BAD, RATING_NORMAL, RATING_GOOD,
            RATING_EXCELLENT, RATING_COMING_SOON })

  public @interface Impress
  {}

  public static final int RATING_NONE = 0;
  public static final int RATING_HORRIBLE = 1;
  public static final int RATING_BAD = 2;
  public static final int RATING_NORMAL = 3;
  public static final int RATING_GOOD = 4;
  public static final int RATING_EXCELLENT = 5;
  public static final int RATING_COMING_SOON = 6;

  @NonNull
  private final Rating[] mRatings;
  @Nullable
  private final Review[] mReviews;
  private final int mBasedOnCount;
  private final float mAverageRating;
  @Nullable
  private static ReceiveUGCListener mReceiveListener;
  @Nullable
  private static SaveUGCListener mSaveListener;

  public static void init(final @NonNull Context context)
  {
    final AppBackgroundTracker.OnTransitionListener listener = new UploadUgcTransitionListener(context);
    MwmApplication.backgroundTracker(context).addListener(listener);
  }

  private UGC(@NonNull Rating[] ratings, float averageRating, @Nullable Review[] reviews,
              int basedOnCount)
  {
    mRatings = ratings;
    mReviews = reviews;
    mAverageRating = averageRating;
    mBasedOnCount = basedOnCount;
  }

  int getBasedOnCount()
  {
    return mBasedOnCount;
  }

  @NonNull
  List<Rating> getRatings()
  {
    return Arrays.asList(mRatings);
  }

  @Nullable
  public List<Review> getReviews()
  {
    if (mReviews == null)
      return null;

    return Arrays.asList(mReviews);
  }

  public static void setReceiveListener(@Nullable ReceiveUGCListener listener)
  {
    mReceiveListener = listener;
  }

  public static void setSaveListener(@Nullable SaveUGCListener listener)
  {
    mSaveListener = listener;
  }

  public static void setUGCUpdate(@NonNull FeatureId fid, UGCUpdate update)
  {
    nativeSetUGCUpdate(fid, update);
  }

  public static native void nativeRequestUGC(@NonNull FeatureId fid);

  public static native void nativeSetUGCUpdate(@NonNull FeatureId fid, UGCUpdate update);

  public static native void nativeUploadUGC();

  public static native int nativeToImpress(float rating);

  @NonNull
  public static native String nativeFormatRating(float rating);

  // Called from JNI.
  @SuppressWarnings("unused")
  public static void onUGCReceived(@Nullable UGC ugc, @Nullable UGCUpdate ugcUpdate,
                                   @Impress int impress, @NonNull String rating)
  {
    if (mReceiveListener == null)
      return;

    if (ugc == null && ugcUpdate != null)
      impress = UGC.RATING_COMING_SOON;
    mReceiveListener.onUGCReceived(ugc, ugcUpdate, impress, rating);
  }

  // Called from JNI.
  @SuppressWarnings("unused")
  public static void onUGCSaved(boolean result)
  {
    if (mSaveListener == null)
      return;

    mSaveListener.onUGCSaved(result);
  }

  public static class Rating implements Parcelable
  {
    public static final Creator<Rating> CREATOR = new Creator<Rating>()
    {
      @Override
      public Rating createFromParcel(Parcel in)
      {
        return new Rating(in);
      }

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

    @NonNull
    private final String mName;
    private float mValue;

    Rating(@NonNull String name, float value)
    {
      mName = name;
      mValue = value;
    }

    protected Rating(Parcel in)
    {
      mName = in.readString();
      mValue = in.readFloat();
    }

    public float getValue()
    {
      return mValue;
    }

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

    public void setValue(float value)
    {
      mValue = value;
    }

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

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

  public static class Review implements Parcelable
  {
    public static final Creator<Review> CREATOR = new Creator<Review>()
    {
      @Override
      public Review createFromParcel(Parcel in)
      {
        return new Review(in);
      }

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

    @NonNull
    private final String mText;
    @NonNull
    private final String mAuthor;
    private final long mTimeMillis;
    private final float mRating;
    @Impress
    private final int mImpress;

    private Review(@NonNull String text, @NonNull String author, long timeMillis,
                   float rating, @Impress int impress)
    {
      mText = text;
      mAuthor = author;
      mTimeMillis = timeMillis;
      mRating = rating;
      mImpress = impress;
    }

    protected Review(Parcel in)
    {
      mText = in.readString();
      mAuthor = in.readString();
      mTimeMillis = in.readLong();
      mRating = in.readFloat();
      //noinspection WrongConstant
      mImpress = in.readInt();
    }

    @NonNull
    public String getText()
    {
      return mText;
    }

    @NonNull
    String getAuthor()
    {
      return mAuthor;
    }

    long getTime()
    {
      return mTimeMillis;
    }

    public float getRating()
    {
      return mRating;
    }

    int getImpress()
    {
      return mImpress;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
      dest.writeString(mText);
      dest.writeString(mAuthor);
      dest.writeLong(mTimeMillis);
      dest.writeFloat(mRating);
      dest.writeInt(mImpress);
    }
  }

  interface ReceiveUGCListener
  {
    void onUGCReceived(@Nullable UGC ugc, @Nullable UGCUpdate ugcUpdate, @Impress int impress,
                       @NonNull String rating);
  }

  interface SaveUGCListener
  {
    void onUGCSaved(boolean result);
  }

  private static class UploadUgcTransitionListener implements AppBackgroundTracker.OnTransitionListener
  {
    @NonNull
    private final Context mContext;

    UploadUgcTransitionListener(@NonNull Context context)
    {
      mContext = context;
    }

    @Override
    public void onTransit(boolean foreground)
    {
      if (foreground)
        return;

      WorkerService.startActionUploadUGC(mContext);
    }
  }
}