@Override public void dispose () { // dispose the purchase system PurchaseSystem.dispose(); } }
/** Returns true if there is a purchase manager available. */ public static boolean hasManager () { return getManager() != null; }
/** @see #install(PurchaseObserver, PurchaseManagerConfig, boolean) */ public static void install (PurchaseObserver observer, PurchaseManagerConfig config) { install(observer, config, true); }
/** * @param activity The AndroidApplication activity. * @param requestCode The request code to use in case they are needed (not all stores need them). */ public IAP (Activity activity, int requestCode) { // are we on GooglePlay? try { Class<?> googlePlayClazz = Class.forName("com.badlogic.gdx.pay.android.googleplay.AndroidGooglePlayPurchaseManager"); Method method = googlePlayClazz.getMethod("isRunningViaGooglePlay", Activity.class); if ((Boolean)method.invoke(googlePlayClazz, activity)) { // we are running on GooglePlay: let's set the purchase manager and be done with it! PurchaseSystem.setManager((PurchaseManager)googlePlayClazz.getConstructor(Activity.class, int.class).newInstance(activity, requestCode)); return; } } catch (Exception e) { Log.d(TAG, "Failed to locate purchase manager for GooglePlay (gdx-pay-android-googleplay.jar file not installed)", e); } }
/** Asks to restore previous purchases. Results are returned to the observer. */ public static void purchaseRestore () { if (hasManager()) { manager.purchaseRestore(); } else { throw new RuntimeException("No purchase manager was found."); } }
@Test public void shouldDisposeManagerOnAppRestart() throws Exception { PurchaseSystem.onAppRestarted(); verify(purchaseManager).dispose(); } }
/** * @param activity The AndroidApplication activity. * @param requestCode The request code to use in case they are needed (not all stores need them). */ public IAP (Activity activity, int requestCode) { // are we on GooglePlay? try { Class<?> googlePlayClazz = Class.forName("com.badlogic.gdx.pay.android.googleplay.AndroidGooglePlayPurchaseManager"); Method method = googlePlayClazz.getMethod("isRunningViaGooglePlay", Activity.class); if ((Boolean)method.invoke(googlePlayClazz, activity)) { // we are running on GooglePlay: let's set the purchase manager and be done with it! PurchaseSystem.setManager((PurchaseManager)googlePlayClazz.getConstructor(Activity.class, int.class).newInstance(activity, requestCode)); return; } } catch (Exception e) { Log.d(TAG, "Failed to locate purchase manager for GooglePlay (gdx-pay-android-googleplay.jar file not installed)", e); } }
/** Returns true if the purchase system is installed and ready to go. */ public static boolean installed () { if (hasManager()) { return manager.installed(); } else { return false; } }
@Before public void setUp() throws Exception { PurchaseSystem.setManager(purchaseManager); }
/** * Disposes static instances in case JVM is re-used on restarts. */ public static void onAppRestarted() { if (manager != null) { dispose(); } }
@Override public void onActivityResult (int requestCode, int resultCode, Intent data) { // forward to corresponding Android IAP-system PurchaseManager manager = PurchaseSystem.getManager(); if (manager != null) { try { // this might fail which is OK! --> some implementations will not require this... Method method = manager.getClass().getMethod("onActivityResult", int.class, int.class, Intent.class); method.invoke(manager, requestCode, resultCode, data); } catch (Exception e) { Log.d(TAG, "Failed to invoke onActivityResult(...) on purchase manager.", e); } } }
/** Executes a purchase. */ public static void purchase (String identifier) { if (hasManager()) { manager.purchase(identifier); } else { throw new RuntimeException("No purchase manager was found."); } }
@SuppressWarnings("unused") // Unit tested with reflection. (as in IAP.java) public AndroidGooglePlayPurchaseManager(Activity activity, AndroidFragmentApplication application, int activityRequestCode) { PurchaseResponseActivityResultConverter converter = new PurchaseResponseActivityResultConverter(this); AsyncExecutor executor = new NewThreadSleepAsyncExecutor(); ApplicationProxy.FragmentProxy proxy = new ApplicationProxy.FragmentProxy(activity, application); googleInAppBillingService = new V3GoogleInAppBillingService(proxy, activityRequestCode, converter, executor); PurchaseSystem.setManager(this); }
@Override public void dispose () { // dispose the purchase system PurchaseSystem.dispose(); } }
@Override public void onActivityResult (int requestCode, int resultCode, Intent data) { // forward to corresponding Android IAP-system PurchaseManager manager = PurchaseSystem.getManager(); if (manager != null) { try { // this might fail which is OK! --> some implementations will not require this... Method method = manager.getClass().getMethod("onActivityResult", int.class, int.class, Intent.class); method.invoke(manager, requestCode, resultCode, data); } catch (Exception e) { Log.d(TAG, "Failed to invoke onActivityResult(...) on purchase manager.", e); } } }
/** Returns the store name or null for none. */ public static String storeName () { if (hasManager()) { return manager.storeName(); } else { return null; } }
PurchaseSystem.setManager((PurchaseManager) ClassReflection.newInstance(iapClazz));
/** Returns information about a product provided by the purchase manager. * * Note, you should set autoFetchInformation to true in {@link PurchaseSystem#install} to true to make this method work for all * PurchaseManager implementations * * @return {@link Information#UNAVAILABLE} if the product is not available or information was not previously fetched */ public static Information getInformation(String identifier) { if (hasManager()) { return manager.getInformation(identifier); } else { throw new RuntimeException("No purchase manager was found."); } } }
/** Installs a purchase observer. * * @param autoFetchInformation tells PurchaseManager to automatically fetch offer details on setup to make * {@link PurchaseSystem#getInformation(String)} work properly **/ public static void install (PurchaseObserver observer, PurchaseManagerConfig config, boolean autoFetchInformation) { if (hasManager()) { manager.install(observer, config, autoFetchInformation); } else { observer.handleInstallError(new RuntimeException("No purchase manager was available.")); } }