Skip to content

Commit

Permalink
use application context for billing cache (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
serggl authored Nov 11, 2016
1 parent 1e017c8 commit 4f9c2db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
39 changes: 16 additions & 23 deletions library/src/main/java/com/anjlab/android/iab/v3/BillingBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,30 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.lang.ref.WeakReference;

class BillingBase {

private WeakReference<Context> contextReference;
private Context context;

public BillingBase(Context context) {
contextReference = new WeakReference<Context>(context);
BillingBase(Context context) {
this.context = context;
}

public Context getContext() {
return contextReference.get();
Context getContext() {
return context;
}

protected String getPreferencesBaseKey() {
return contextReference.get().getPackageName() + "_preferences";
String getPreferencesBaseKey() {
return getContext().getPackageName() + "_preferences";
}

private SharedPreferences getPreferences() {
if (contextReference.get() != null)
return PreferenceManager.getDefaultSharedPreferences(contextReference.get());
return null;
}

public void release() {
if (contextReference != null)
contextReference.clear();
return PreferenceManager.getDefaultSharedPreferences(getContext());
}

protected boolean saveString(String key, String value) {
boolean saveString(String key, String value) {
SharedPreferences sp = getPreferences();
if (sp != null) {
if (sp != null)
{
SharedPreferences.Editor spe = sp.edit();
spe.putString(key, value);
spe.commit();
Expand All @@ -59,16 +51,17 @@ protected boolean saveString(String key, String value) {
return false;
}

protected String loadString(String key, String defValue) {
String loadString(String key, String defValue) {
SharedPreferences sp = getPreferences();
if (sp != null)
return sp.getString(key, defValue);
return defValue;
}

protected boolean saveBoolean(String key, Boolean value) {
boolean saveBoolean(String key, Boolean value) {
SharedPreferences sp = getPreferences();
if (sp != null) {
if (sp != null)
{
SharedPreferences.Editor spe = sp.edit();
spe.putBoolean(key, value);
spe.commit();
Expand All @@ -77,7 +70,7 @@ protected boolean saveBoolean(String key, Boolean value) {
return false;
}

protected boolean loadBoolean(String key, boolean defValue) {
boolean loadBoolean(String key, boolean defValue) {
SharedPreferences sp = getPreferences();
if (sp != null)
return sp.getBoolean(key, defValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ public BillingProcessor(Context context, String licenseKey, IBillingHandler hand
}

public BillingProcessor(Context context, String licenseKey, String merchantId, IBillingHandler handler) {
super(context);
super(context.getApplicationContext());
signatureBase64 = licenseKey;
eventHandler = handler;
contextPackageName = context.getApplicationContext().getPackageName();
cachedProducts = new BillingCache(context, MANAGED_PRODUCTS_CACHE_KEY);
cachedSubscriptions = new BillingCache(context, SUBSCRIPTIONS_CACHE_KEY);
contextPackageName = getContext().getPackageName();
cachedProducts = new BillingCache(getContext(), MANAGED_PRODUCTS_CACHE_KEY);
cachedSubscriptions = new BillingCache(getContext(), SUBSCRIPTIONS_CACHE_KEY);
developerMerchantId = merchantId;
bindPlayServices();
}
Expand All @@ -151,18 +151,15 @@ public static boolean isIabServiceAvailable(Context context) {
return list.size() > 0;
}

@Override
public void release() {
if (serviceConnection != null && getContext() != null) {
if (isInitialized() && serviceConnection != null) {
try {
getContext().unbindService(serviceConnection);
} catch (Exception e) {
Log.e(LOG_TAG, "Error in release", e);
}
billingService = null;
}
cachedProducts.release();
super.release();
}

public boolean isInitialized() {
Expand Down

0 comments on commit 4f9c2db

Please sign in to comment.