Guest User

Untitled

a guest
Nov 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. private void checkPurchaseHistory() {
  2. Purchase.PurchasesResult purchasesResult=mBillingClient.queryPurchases(BillingClient.SkuType.SUBS); //we want to check subscription
  3. if (purchasesResult!=null && purchasesResult.getPurchasesList()!=null && purchasesResult.getPurchasesList().size()>0){
  4. //so minimum one purchase was actually made
  5. for (Purchase purchase:purchasesResult.getPurchasesList()) {
  6. if (monthlySku.equals(purchase.getSku())){
  7. //we know monthly sub last 30 days and api gives us purchase time.
  8. long purchaseTime=purchase.getPurchaseTime();
  9. Calendar purchaseDate=Calendar.getInstance();
  10. purchaseDate.setTimeInMillis(purchaseTime);
  11. Calendar currentDate=Calendar.getInstance();
  12. long days=calendarDaysBetween(purchaseDate,currentDate);
  13. if (days>30){
  14. //monthly expired
  15. }
  16. Toast.makeText(this,"Remaining days: "
  17. +days,Toast.LENGTH_LONG).show();
  18.  
  19. }
  20. }
  21. }
  22. }
  23. public static long calendarDaysBetween(Calendar startCal, Calendar endCal) {
  24.  
  25. // Create copies so we don't update the original calendars.
  26.  
  27. Calendar start = Calendar.getInstance();
  28. start.setTimeZone(startCal.getTimeZone());
  29. start.setTimeInMillis(startCal.getTimeInMillis());
  30.  
  31. Calendar end = Calendar.getInstance();
  32. end.setTimeZone(endCal.getTimeZone());
  33. end.setTimeInMillis(endCal.getTimeInMillis());
  34.  
  35. // Set the copies to be at midnight, but keep the day information.
  36.  
  37. start.set(Calendar.HOUR_OF_DAY, 0);
  38. start.set(Calendar.MINUTE, 0);
  39. start.set(Calendar.SECOND, 0);
  40. start.set(Calendar.MILLISECOND, 0);
  41.  
  42. end.set(Calendar.HOUR_OF_DAY, 0);
  43. end.set(Calendar.MINUTE, 0);
  44. end.set(Calendar.SECOND, 0);
  45. end.set(Calendar.MILLISECOND, 0);
  46.  
  47. // At this point, each calendar is set to midnight on
  48. // their respective days. Now use TimeUnit.MILLISECONDS to
  49. // compute the number of full days between the two of them.
  50.  
  51. return TimeUnit.MILLISECONDS.toDays(
  52. Math.abs(end.getTimeInMillis() - start.getTimeInMillis()));
  53. }
Add Comment
Please, Sign In to add comment