Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void checkPurchaseHistory() {
- Purchase.PurchasesResult purchasesResult=mBillingClient.queryPurchases(BillingClient.SkuType.SUBS); //we want to check subscription
- if (purchasesResult!=null && purchasesResult.getPurchasesList()!=null && purchasesResult.getPurchasesList().size()>0){
- //so minimum one purchase was actually made
- for (Purchase purchase:purchasesResult.getPurchasesList()) {
- if (monthlySku.equals(purchase.getSku())){
- //we know monthly sub last 30 days and api gives us purchase time.
- long purchaseTime=purchase.getPurchaseTime();
- Calendar purchaseDate=Calendar.getInstance();
- purchaseDate.setTimeInMillis(purchaseTime);
- Calendar currentDate=Calendar.getInstance();
- long days=calendarDaysBetween(purchaseDate,currentDate);
- if (days>30){
- //monthly expired
- }
- Toast.makeText(this,"Remaining days: "
- +days,Toast.LENGTH_LONG).show();
- }
- }
- }
- }
- public static long calendarDaysBetween(Calendar startCal, Calendar endCal) {
- // Create copies so we don't update the original calendars.
- Calendar start = Calendar.getInstance();
- start.setTimeZone(startCal.getTimeZone());
- start.setTimeInMillis(startCal.getTimeInMillis());
- Calendar end = Calendar.getInstance();
- end.setTimeZone(endCal.getTimeZone());
- end.setTimeInMillis(endCal.getTimeInMillis());
- // Set the copies to be at midnight, but keep the day information.
- start.set(Calendar.HOUR_OF_DAY, 0);
- start.set(Calendar.MINUTE, 0);
- start.set(Calendar.SECOND, 0);
- start.set(Calendar.MILLISECOND, 0);
- end.set(Calendar.HOUR_OF_DAY, 0);
- end.set(Calendar.MINUTE, 0);
- end.set(Calendar.SECOND, 0);
- end.set(Calendar.MILLISECOND, 0);
- // At this point, each calendar is set to midnight on
- // their respective days. Now use TimeUnit.MILLISECONDS to
- // compute the number of full days between the two of them.
- return TimeUnit.MILLISECONDS.toDays(
- Math.abs(end.getTimeInMillis() - start.getTimeInMillis()));
- }
Add Comment
Please, Sign In to add comment