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

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

import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClient.BillingResponse;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

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

class PlayStoreBillingManager implements BillingManager<PlayStoreBillingCallback>,
                                                PurchasesUpdatedListener,
                                                PlayStoreBillingConnection.ConnectionListener
{
  final static Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.BILLING);
  final static String TAG = PlayStoreBillingManager.class.getSimpleName();
  @Nullable
  private Activity mActivity;
  @Nullable
  private BillingClient mBillingClient;
  @Nullable
  private PlayStoreBillingCallback mCallback;
  @NonNull
  @BillingClient.SkuType
  private final String mProductType;
  @SuppressWarnings({ "NullableProblems" })
  @NonNull
  private BillingConnection mConnection;
  @NonNull
  private final List<BillingRequest> mPendingRequests = new ArrayList<>();
  
  PlayStoreBillingManager(@NonNull @BillingClient.SkuType String productType)
  {
    mProductType = productType;
  }

  @Override
  public void initialize(@NonNull Activity context)
  {
    LOGGER.i(TAG, "Creating play store billing client...");
    mActivity = context;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
    mConnection = new PlayStoreBillingConnection(mBillingClient, this);
    mConnection.open();
  }

  @Override
  public void destroy()
  {
    mActivity = null;
    mConnection.close();
    mPendingRequests.clear();
  }

  @Override
  public void queryProductDetails(@NonNull List<String> productIds)
  {
    executeBillingRequest(new QueryProductDetailsRequest(getClientOrThrow(), mProductType,
                                                         mCallback, productIds));
  }

  @Override
  public void queryExistingPurchases()
  {
    executeBillingRequest(new QueryExistingPurchases(getClientOrThrow(), mProductType, mCallback));
  }

  @Override
  public void consumePurchase(@NonNull String purchaseToken)
  {
    executeBillingRequest(new ConsumePurchaseRequest(getClientOrThrow(), mProductType, mCallback,
                                                     purchaseToken));
  }

  private void executeBillingRequest(@NonNull BillingRequest request)
  {
    switch (mConnection.getState())
    {
      case CONNECTING:
        mPendingRequests.add(request);
        break;
      case CONNECTED:
        request.execute();
        break;
      case DISCONNECTED:
        mPendingRequests.add(request);
        mConnection.open();
        break;
      case CLOSED:
        throw new IllegalStateException("Billing service connection already closed, " +
                                        "please initialize it again.");
    }
  }

  @Override
  public void launchBillingFlowForProduct(@NonNull String productId)
  {
    if (!isBillingSupported())
    {
      LOGGER.w(TAG, "Purchase type '" + mProductType + "' is not supported by this device!");
      return;
    }

    executeBillingRequest(new LaunchBillingFlowRequest(getActivityOrThrow(), getClientOrThrow(),
                                                       mProductType, productId));
  }

  @Override
  public boolean isBillingSupported()
  {
    if (BillingClient.SkuType.SUBS.equals(mProductType))
    {
      @BillingResponse
      int result = getClientOrThrow().isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS);
      return result != BillingResponse.FEATURE_NOT_SUPPORTED;
    }

    return true;
  }

  @Override
  public void addCallback(@NonNull PlayStoreBillingCallback callback)
  {
    mCallback = callback;
  }

  @Override
  public void removeCallback(@NonNull PlayStoreBillingCallback callback)
  {
    mCallback = null;
  }

  @Override
  public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases)
  {
    if (responseCode == BillingResponse.USER_CANCELED)
    {
      LOGGER.i(TAG, "Billing cancelled by user.");
      return;
    }

    if (responseCode == BillingResponse.ITEM_ALREADY_OWNED)
    {
      LOGGER.i(TAG, "Billing already done before.");
      return;
    }

    if (responseCode != BillingResponse.OK || purchases == null || purchases.isEmpty())
    {
      LOGGER.e(TAG, "Billing failed. Response code: " + responseCode);
      if (mCallback != null)
        mCallback.onPurchaseFailure(responseCode);
      return;
    }

    LOGGER.i(TAG, "Purchase process successful. Count of purchases: " + purchases.size());
    if (mCallback != null)
      mCallback.onPurchaseSuccessful(purchases);
  }

  @NonNull
  private BillingClient getClientOrThrow()
  {
    if (mBillingClient == null)
      throw new IllegalStateException("Manager must be initialized! Call 'initialize' method first.");

    return mBillingClient;
  }

  @NonNull
  private Activity getActivityOrThrow()
  {
    if (mActivity == null)
      throw new IllegalStateException("Manager must be initialized! Call 'initialize' method first.");

    return mActivity;
  }

  @Override
  public void onConnected()
  {
    for(@NonNull BillingRequest request: mPendingRequests)
      request.execute();

    mPendingRequests.clear();
  }

  @Override
  public void onDisconnected()
  {
    LOGGER.w(TAG, "Play store connection failed.");
    if (mPendingRequests.isEmpty())
      return;

    mPendingRequests.clear();
    if (mCallback != null)
      mCallback.onStoreConnectionFailed();
  }
}