diff --git a/app/src/google_donation/java/app/fedilab/fedilabtube/DonationActivity.java b/app/src/google_donation/java/app/fedilab/fedilabtube/DonationActivity.java index d0c9dd2..7a3a2ff 100644 --- a/app/src/google_donation/java/app/fedilab/fedilabtube/DonationActivity.java +++ b/app/src/google_donation/java/app/fedilab/fedilabtube/DonationActivity.java @@ -27,6 +27,7 @@ import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.android.billingclient.api.AcknowledgePurchaseParams; import com.android.billingclient.api.BillingClient; import com.android.billingclient.api.BillingClientStateListener; import com.android.billingclient.api.BillingResult; @@ -43,12 +44,16 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import app.fedilab.fedilabtube.databinding.ActivityDonationBinding; import app.fedilab.fedilabtube.fragment.MySubscriptionDonationsFragment; import app.fedilab.fedilabtube.fragment.DonationsFragment; +import app.fedilab.fedilabtube.helper.Helper; +import es.dmoral.toasty.Toasty; public class DonationActivity extends AppCompatActivity implements PurchasesUpdatedListener { @@ -79,30 +84,7 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda donationsFragment.initialized(billingClient); subscriptionDonationsFragment.initialized(billingClient); - Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS); - List purchases = purchasesResult.getPurchasesList(); - List isSubscriptions = new ArrayList<>(); - if (purchases != null) { - for (Purchase purchase : purchases) { - try { - JSONObject purchaseJson = new JSONObject(purchase.getOriginalJson()); - String productId = purchaseJson.getString("productId"); - isSubscriptions.add(productId); - } catch (JSONException e) { - e.printStackTrace(); - } - } - SkuDetailsParams.Builder paramsSub = SkuDetailsParams.newBuilder(); - paramsSub.setSkusList(isSubscriptions).setType(BillingClient.SkuType.SUBS); - billingClient.querySkuDetailsAsync(paramsSub.build(), - (billingResult2, skuDetailsList) -> mySubscriptionDonationsFragment.initialized(skuDetailsList, billingClient)); - } else { - mySubscriptionDonationsFragment.initialized(new ArrayList<>(), billingClient); - } - - - purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP); - purchases = purchasesResult.getPurchasesList(); + List purchases = queryPurchases(); if (purchases != null) { for (Purchase purchase : purchases) { if (!purchase.isAutoRenewing()) { @@ -188,6 +170,34 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda } + private List queryPurchases() { + Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS); + List purchases = purchasesResult.getPurchasesList(); + List isSubscriptions = new ArrayList<>(); + HashMap map = new HashMap<>(); + if (purchases != null) { + for (Purchase purchase : purchases) { + try { + if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { + JSONObject purchaseJson = new JSONObject(purchase.getOriginalJson()); + String productId = purchaseJson.getString("productId"); + isSubscriptions.add(productId); + map.put(productId, purchase); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + SkuDetailsParams.Builder paramsSub = SkuDetailsParams.newBuilder(); + paramsSub.setSkusList(isSubscriptions).setType(BillingClient.SkuType.SUBS); + billingClient.querySkuDetailsAsync(paramsSub.build(), + (billingResult2, skuDetailsList) -> mySubscriptionDonationsFragment.initialized(skuDetailsList, map, billingClient)); + } else { + mySubscriptionDonationsFragment.initialized(new ArrayList<>(), map, billingClient); + } + return purchases; + } + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { @@ -210,11 +220,21 @@ public class DonationActivity extends AppCompatActivity implements PurchasesUpda .build(); ConsumeResponseListener listener = (billingResult1, purchaseToken) -> { - if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) { - // Handle the success of the consume operation. - } }; billingClient.consumeAsync(consumeParams, listener); + } else { + if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { + if (!purchase.isAcknowledged()) { + AcknowledgePurchaseParams acknowledgePurchaseParams = + AcknowledgePurchaseParams.newBuilder() + .setPurchaseToken(purchase.getPurchaseToken()) + .build(); + billingClient.acknowledgePurchase(acknowledgePurchaseParams, b -> { + }); + } + } + + queryPurchases(); } } message = getString(R.string.donation_succeeded_null); diff --git a/app/src/google_donation/java/app/fedilab/fedilabtube/drawable/DonationHistoryAdapter.java b/app/src/google_donation/java/app/fedilab/fedilabtube/drawable/DonationHistoryAdapter.java index d2cea87..ebb0511 100644 --- a/app/src/google_donation/java/app/fedilab/fedilabtube/drawable/DonationHistoryAdapter.java +++ b/app/src/google_donation/java/app/fedilab/fedilabtube/drawable/DonationHistoryAdapter.java @@ -19,15 +19,26 @@ import android.view.LayoutInflater; import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.recyclerview.widget.RecyclerView; import com.android.billingclient.api.BillingClient; +import com.android.billingclient.api.ConsumeParams; +import com.android.billingclient.api.ConsumeResponseListener; +import com.android.billingclient.api.Purchase; import com.android.billingclient.api.SkuDetails; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; import java.util.List; +import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.databinding.DrawerMyDonationBinding; +import app.fedilab.fedilabtube.helper.Helper; +import es.dmoral.toasty.Toasty; public class DonationHistoryAdapter extends RecyclerView.Adapter { @@ -35,10 +46,12 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter skuDetailsList; private final BillingClient billingClient; private Context context; + private final HashMap map; - public DonationHistoryAdapter(List SkuDetailsList, BillingClient billingClient) { + public DonationHistoryAdapter(List SkuDetailsList, HashMap map, BillingClient billingClient) { this.skuDetailsList = SkuDetailsList; this.billingClient = billingClient; + this.map = map; } @@ -51,6 +64,7 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); + dialogBuilder.setMessage(R.string.cancel_subscription_confirm); + + dialogBuilder.setPositiveButton(R.string.cancel_subscription, (dialog, id) -> { + JSONObject skudetailsJson; + try { + skudetailsJson = new JSONObject(skuDetails.getOriginalJson()); + String productId = skudetailsJson.getString("productId"); + if (map.containsKey(productId)) { + Purchase purchase = map.get(productId); + if (purchase != null) { + ConsumeParams consumeParams = + ConsumeParams.newBuilder() + .setPurchaseToken(purchase.getPurchaseToken()) + .build(); + if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { + ConsumeResponseListener listener = (billingResult1, purchaseToken) -> { + if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) { + Toasty.success(context, context.getString(R.string.subscription_cancelled), Toasty.LENGTH_LONG).show(); + } + skuDetailsList.remove(skuDetails); + notifyDataSetChanged(); + }; + billingClient.consumeAsync(consumeParams, listener); + } + + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + dialog.dismiss(); + }); + dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); + AlertDialog alertDialogLogoutAccount = dialogBuilder.create(); + alertDialogLogoutAccount.show(); }); } @@ -69,6 +120,7 @@ public class DonationHistoryAdapter extends RecyclerView.Adapter { binding.loader.setVisibility(View.GONE); binding.lvProducts.setVisibility(View.VISIBLE); + if (skuDetailsList != null) { + Collections.sort(skuDetailsList, (obj1, obj2) -> obj1.getPrice().compareTo(obj2.getPrice())); + } DonationButtonAdapter donationButtonAdapter = new DonationButtonAdapter(skuDetailsList, bc, isSubscriptions); binding.lvProducts.setAdapter(donationButtonAdapter); binding.lvProducts.setLayoutManager(new LinearLayoutManager(context)); diff --git a/app/src/google_donation/java/app/fedilab/fedilabtube/fragment/MySubscriptionDonationsFragment.java b/app/src/google_donation/java/app/fedilab/fedilabtube/fragment/MySubscriptionDonationsFragment.java index cc34b80..0300157 100644 --- a/app/src/google_donation/java/app/fedilab/fedilabtube/fragment/MySubscriptionDonationsFragment.java +++ b/app/src/google_donation/java/app/fedilab/fedilabtube/fragment/MySubscriptionDonationsFragment.java @@ -28,15 +28,11 @@ import com.android.billingclient.api.BillingClient; import com.android.billingclient.api.Purchase; import com.android.billingclient.api.SkuDetails; -import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import app.fedilab.fedilabtube.databinding.FragmentMyDonationsBinding; import app.fedilab.fedilabtube.drawable.DonationHistoryAdapter; -import app.fedilab.fedilabtube.helper.Helper; - -import static app.fedilab.fedilabtube.fragment.DonationsFragment.donations; - public class MySubscriptionDonationsFragment extends Fragment { @@ -55,11 +51,11 @@ public class MySubscriptionDonationsFragment extends Fragment { return rootView; } - public void initialized(List skuDetailsList, BillingClient bc) { + public void initialized(List skuDetailsList, HashMap map, BillingClient bc) { binding.loader.setVisibility(View.GONE); binding.lvPurchases.setVisibility(View.VISIBLE); - DonationHistoryAdapter donationHistoryAdapter = new DonationHistoryAdapter(skuDetailsList, bc); + DonationHistoryAdapter donationHistoryAdapter = new DonationHistoryAdapter(skuDetailsList, map, bc); binding.lvPurchases.setAdapter(donationHistoryAdapter); binding.lvPurchases.setLayoutManager(new LinearLayoutManager(context)); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ea369b7..ac2cb02 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -468,5 +468,8 @@ One time My subscription Month + Subscription cancelled! + Cancel subscription + Are you sure, you want to cancel that subscription? \ No newline at end of file