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

AdsRemovalPurchaseDialog.java « purchase « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd39b19aae9bdddddd5f661234750fa8958c67fa (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.mapswithme.maps.purchase;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.SkuDetails;
import com.mapswithme.maps.PrivateVariables;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmDialogFragment;
import com.mapswithme.maps.dialog.AlertDialogCallback;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.Statistics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AdsRemovalPurchaseDialog extends BaseMwmDialogFragment
    implements AlertDialogCallback, PurchaseStateActivator<AdsRemovalPaymentState>
{
  private final static Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.BILLING);
  private final static String TAG = AdsRemovalPurchaseDialog.class.getSimpleName();
  private final static String EXTRA_CURRENT_STATE = "extra_current_state";
  private final static String EXTRA_PRODUCT_DETAILS = "extra_product_details";
  private final static String EXTRA_ACTIVATION_RESULT = "extra_activation_result";

  private boolean mActivationResult;
  @Nullable
  private ProductDetails[] mProductDetails;
  @NonNull
  private AdsRemovalPaymentState mState = AdsRemovalPaymentState.NONE;
  @Nullable
  private AdsRemovalPurchaseControllerProvider mControllerProvider;
  @NonNull
  private AdsRemovalPurchaseCallback mPurchaseCallback = new AdsRemovalPurchaseCallback();
  @NonNull
  private List<AdsRemovalActivationCallback> mActivationCallbacks = new ArrayList<>();
  @SuppressWarnings("NullableProblems")
  @NonNull
  private View mYearlyButton;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mMonthlyButton;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private TextView mWeeklyButton;
  @SuppressWarnings("NullableProblems")
  @NonNull
  private CompoundButton mOptionsButton;

  public static void show(@NonNull FragmentActivity context)
  {
    show(context.getSupportFragmentManager());
  }

  public static void show(@NonNull Fragment parent)
  {
    show(parent.getChildFragmentManager());
  }

  private static void show(@NonNull FragmentManager manager)
  {
    DialogFragment fragment = new AdsRemovalPurchaseDialog();
    fragment.show(manager, AdsRemovalPurchaseDialog.class.getName());
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    LOGGER.d(TAG, "onCreate savedInstanceState = " + savedInstanceState);
  }

  @Override
  public void onAttach(Context context)
  {
    super.onAttach(context);
    LOGGER.d(TAG, "onAttach");
    if (context instanceof AdsRemovalPurchaseControllerProvider)
      mControllerProvider = (AdsRemovalPurchaseControllerProvider) context;
    if (getParentFragment() instanceof  AdsRemovalPurchaseControllerProvider)
      mControllerProvider = (AdsRemovalPurchaseControllerProvider) getParentFragment();
    if (context instanceof AdsRemovalActivationCallback)
      mActivationCallbacks.add((AdsRemovalActivationCallback) context);
    if (getParentFragment() instanceof AdsRemovalActivationCallback)
      mActivationCallbacks.add((AdsRemovalActivationCallback) getParentFragment());
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState)
  {
    LOGGER.d(TAG, "onCreateView savedInstanceState = " + savedInstanceState + "this " + this);
    View view = inflater.inflate(R.layout.fragment_ads_removal_purchase_dialog, container, false);
    View payButtonContainer = view.findViewById(R.id.pay_button_container);
    mYearlyButton = payButtonContainer.findViewById(R.id.yearly_button);
    mYearlyButton.setOnClickListener(v -> onYearlyProductClicked());
    mMonthlyButton = payButtonContainer.findViewById(R.id.monthly_button);
    mMonthlyButton.setOnClickListener(v -> onMonthlyProductClicked());
    mWeeklyButton = payButtonContainer.findViewById(R.id.weekly_button);
    mWeeklyButton.setOnClickListener(v -> onWeeklyProductClicked());
    mOptionsButton = payButtonContainer.findViewById(R.id.options);
    mOptionsButton.setCompoundDrawablesWithIntrinsicBounds(null, null, getOptionsToggle(), null);
    mOptionsButton.setOnCheckedChangeListener((buttonView, isChecked) -> onOptionsClicked());
    view.findViewById(R.id.explanation).setOnClickListener(v -> onExplanationClick());
    return view;
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
  {
    super.onViewCreated(view, savedInstanceState);
    LOGGER.d(TAG, "onViewCreated savedInstanceState = " + savedInstanceState);
    if (savedInstanceState != null)
    {
      AdsRemovalPaymentState savedState
          = AdsRemovalPaymentState.values()[savedInstanceState.getInt(EXTRA_CURRENT_STATE)];
      ProductDetails[] productDetails
          = (ProductDetails[]) savedInstanceState.getParcelableArray(EXTRA_PRODUCT_DETAILS);
      if (productDetails != null)
        mProductDetails = productDetails;
      mActivationResult = savedInstanceState.getBoolean(EXTRA_ACTIVATION_RESULT);

      activateState(savedState);
      return;
    }

    activateState(AdsRemovalPaymentState.LOADING);
  }

  @NonNull
  private Drawable getOptionsToggle()
  {
    return Graphics.tint(getContext(), mOptionsButton.isChecked() ? R.drawable.ic_expand_less
                                                                  : R.drawable.ic_expand_more,
                         android.R.attr.textColorSecondary);
  }

  @Override
  public void onStart()
  {
    super.onStart();
    getControllerOrThrow().addCallback(mPurchaseCallback);
    mPurchaseCallback.attach(this);
  }

  @Override
  public void onStop()
  {
    super.onStop();
    getControllerOrThrow().removeCallback();
    mPurchaseCallback.detach();
  }

  void queryPurchaseDetails()
  {
    getControllerOrThrow().queryProductDetails();
  }

  void onYearlyProductClicked()
  {
    launchPurchaseFlowForPeriod(PurchaseUtils.Period.P1Y);
  }

  void onMonthlyProductClicked()
  {
    launchPurchaseFlowForPeriod(PurchaseUtils.Period.P1M);
  }

  void onWeeklyProductClicked()
  {
    launchPurchaseFlowForPeriod(PurchaseUtils.Period.P1W);
  }

  void onOptionsClicked()
  {
    mOptionsButton.setCompoundDrawablesWithIntrinsicBounds(null, null, getOptionsToggle(), null);
    View payContainer = getViewOrThrow().findViewById(R.id.pay_button_container);
    UiUtils.showIf(mOptionsButton.isChecked(), payContainer, R.id.monthly_button,
                   R.id.weekly_button);
  }

  private void launchPurchaseFlowForPeriod(@NonNull PurchaseUtils.Period period)
  {
    ProductDetails details = getProductDetailsForPeriod(period);
    getControllerOrThrow().launchPurchaseFlow(details.getProductId());
    String purchaseId = PrivateVariables.adsRemovalServerId();
    Statistics.INSTANCE.trackPurchasePreviewSelect(PrivateVariables.adsRemovalServerId(),
                                                   details.getProductId());
    Statistics.INSTANCE.trackPurchaseEvent(Statistics.EventName.INAPP_PURCHASE_PREVIEW_PAY,
                                           purchaseId, Statistics.STATISTICS_CHANNEL_REALTIME);
  }

  void onExplanationClick()
  {
    activateState(AdsRemovalPaymentState.EXPLANATION);
  }

  @Override
  public void activateState(@NonNull AdsRemovalPaymentState state)
  {
    if (state == mState)
      return;

    LOGGER.i(TAG, "Activate state: " + state);
    mState = state;
    mState.activate(this);
  }

  @NonNull
  private PurchaseController<PurchaseCallback> getControllerOrThrow()
  {
    if (mControllerProvider == null)
      throw new IllegalStateException("Controller provider must be non-null at this point!");

    PurchaseController<PurchaseCallback> controller
        = mControllerProvider.getAdsRemovalPurchaseController();
    if (controller == null)
      throw new IllegalStateException("Controller must be non-null at this point!");

    return controller;
  }

  @Override
  public void onSaveInstanceState(Bundle outState)
  {
    super.onSaveInstanceState(outState);
    LOGGER.d(TAG, "onSaveInstanceState");
    outState.putInt(EXTRA_CURRENT_STATE, mState.ordinal());
    outState.putParcelableArray(EXTRA_PRODUCT_DETAILS, mProductDetails);
    outState.putBoolean(EXTRA_ACTIVATION_RESULT, mActivationResult);
  }

  @Override
  public void onCancel(DialogInterface dialog)
  {
    super.onCancel(dialog);
    Statistics.INSTANCE.trackPurchaseEvent(Statistics.EventName.INAPP_PURCHASE_PREVIEW_CANCEL,
                                           PrivateVariables.adsRemovalServerId());
  }

  @Override
  public void onDetach()
  {
    LOGGER.d(TAG, "onDetach");
    mControllerProvider = null;
    mActivationCallbacks.clear();
    super.onDetach();
  }

  void finishValidation()
  {
    if (mActivationResult)
    {
      for (AdsRemovalActivationCallback callback : mActivationCallbacks)
        callback.onAdsRemovalActivation();
    }

    dismissAllowingStateLoss();
  }

  void updatePaymentButtons()
  {
    updateYearlyButton();
    updateMonthlyButton();
    updateWeeklyButton();
  }

  private void updateYearlyButton()
  {
    ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1Y);
    String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
    TextView priceView = mYearlyButton.findViewById(R.id.price);
    priceView.setText(getString(R.string.paybtn_title, price));
    TextView savingView = mYearlyButton.findViewById(R.id.saving);
    String saving = Utils.formatCurrencyString(calculateYearlySaving(), details.getCurrencyCode());
    savingView.setText(getString(R.string.paybtn_subtitle, saving));
  }

  private void updateMonthlyButton()
  {
    ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1M);
    String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
    String saving = Utils.formatCurrencyString(calculateMonthlySaving(), details.getCurrencyCode());
    mMonthlyButton.setText(getString(R.string.options_dropdown_item1, price, saving));
  }

  private void updateWeeklyButton()
  {
    ProductDetails details = getProductDetailsForPeriod(PurchaseUtils.Period.P1W);
    String price = Utils.formatCurrencyString(details.getPrice(), details.getCurrencyCode());
    mWeeklyButton.setText(getString(R.string.options_dropdown_item2, price));
  }

  @NonNull
  private ProductDetails getProductDetailsForPeriod(@NonNull PurchaseUtils.Period period)
  {
    if (mProductDetails == null)
      throw new AssertionError("Product details must be exist at this moment!");
    return mProductDetails[period.ordinal()];
  }

  private float calculateYearlySaving()
  {
    float pricePerWeek = getProductDetailsForPeriod(PurchaseUtils.Period.P1W).getPrice();
    float pricePerYear = getProductDetailsForPeriod(PurchaseUtils.Period.P1Y).getPrice();
    return pricePerWeek * PurchaseUtils.WEEKS_IN_YEAR - pricePerYear;
  }

  private float calculateMonthlySaving()
  {
    float pricePerWeek = getProductDetailsForPeriod(PurchaseUtils.Period.P1W).getPrice();
    float pricePerMonth = getProductDetailsForPeriod(PurchaseUtils.Period.P1M).getPrice();
    return pricePerWeek * PurchaseUtils.WEEKS_IN_YEAR - pricePerMonth * PurchaseUtils.MONTHS_IN_YEAR;
  }

  @Override
  public void onAlertDialogPositiveClick(int requestCode, int which)
  {
    handleErrorDialogEvent(requestCode);
  }

  @Override
  public void onAlertDialogNegativeClick(int requestCode, int which)
  {
     // Do nothing by default.
  }

  @Override
  public void onAlertDialogCancel(int requestCode)
  {
    handleErrorDialogEvent(requestCode);
  }

  private void handleErrorDialogEvent(int requestCode)
  {
    switch (requestCode)
    {
      case PurchaseUtils.REQ_CODE_PRODUCT_DETAILS_FAILURE:
      case PurchaseUtils.REQ_CODE_VALIDATION_SERVER_ERROR:
        dismissAllowingStateLoss();
        break;
      case PurchaseUtils.REQ_CODE_PAYMENT_FAILURE:
        activateState(AdsRemovalPaymentState.PRICE_SELECTION);
        break;
    }
  }

  private void handleProductDetails(@NonNull List<SkuDetails> details)
  {
    mProductDetails = new ProductDetails[PurchaseUtils.Period.values().length];
    for (SkuDetails sku: details)
    {
      PurchaseUtils.Period period = PurchaseUtils.Period.valueOf(sku.getSubscriptionPeriod());
      mProductDetails[period.ordinal()] = PurchaseUtils.toProductDetails(sku);
    }
  }

  private void handleActivationResult(boolean result)
  {
    mActivationResult = result;
  }

  private static class AdsRemovalPurchaseCallback
      extends StatefulPurchaseCallback<AdsRemovalPaymentState, AdsRemovalPurchaseDialog>
      implements PurchaseCallback
  {
    @Nullable
    private List<SkuDetails> mPendingDetails;
    private Boolean mPendingActivationResult;

    @Override
    public void onProductDetailsLoaded(@NonNull List<SkuDetails> details)
    {
      if (PurchaseUtils.hasIncorrectSkuDetails(details))
      {
        activateStateSafely(AdsRemovalPaymentState.PRODUCT_DETAILS_FAILURE);
        return;
      }

      if (getUiObject() == null)
        mPendingDetails = Collections.unmodifiableList(details);
      else
        getUiObject().handleProductDetails(details);
      activateStateSafely(AdsRemovalPaymentState.PRICE_SELECTION);
    }

    @Override
    public void onPaymentFailure(@BillingClient.BillingResponse int error)
    {
      Statistics.INSTANCE.trackPurchaseStoreError(PrivateVariables.adsRemovalServerId(), error);
      activateStateSafely(AdsRemovalPaymentState.PAYMENT_FAILURE);
    }

    @Override
    public void onProductDetailsFailure()
    {
      activateStateSafely(AdsRemovalPaymentState.PRODUCT_DETAILS_FAILURE);
    }

    @Override
    public void onStoreConnectionFailed()
    {
      activateStateSafely(AdsRemovalPaymentState.PRODUCT_DETAILS_FAILURE);
    }

    @Override
    public void onValidationStarted()
    {
      Statistics.INSTANCE.trackPurchaseEvent(Statistics.EventName.INAPP_PURCHASE_STORE_SUCCESS,
                                             PrivateVariables.adsRemovalServerId());
      activateStateSafely(AdsRemovalPaymentState.VALIDATION);
    }

    @Override
    public void onValidationFinish(boolean success)
    {
      if (getUiObject() == null)
        mPendingActivationResult = success;
      else
        getUiObject().handleActivationResult(success);

      activateStateSafely(AdsRemovalPaymentState.VALIDATION_FINISH);
    }

    @Override
    void onAttach(@NonNull AdsRemovalPurchaseDialog uiObject)
    {
      if (mPendingDetails != null)
      {
        uiObject.handleProductDetails(mPendingDetails);
        mPendingDetails = null;
      }

      if (mPendingActivationResult != null)
      {
        uiObject.handleActivationResult(mPendingActivationResult);
        mPendingActivationResult = null;
      }
    }
  }
}