From c005610cb5f127ea18e37f4363f4827a0861994a Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sat, 14 Jan 2012 20:37:21 +0400 Subject: [PATCH] android_calculator-56: Billing doesn't work if internet was off on the start of application --- res/layout/admob_pref.xml | 13 +++++ res/layout/help_faq.xml | 23 +++++---- res/layout/help_hints.xml | 21 ++++++--- res/layout/help_screens.xml | 22 ++++++--- res/xml/main_preferences.xml | 8 ++++ .../org/solovyev/android/AndroidUtils.java | 23 ++++++++- .../calculator/CalculatorActivity.java | 20 +------- .../calculator/CalculatorApplication.java | 7 +-- .../CalculatorPreferencesActivity.java | 13 +++-- .../calculator/help/AbstractHelpActivity.java | 3 +- .../android/view/prefs/AdViewPreference.java | 47 +++++++++++++++++++ 11 files changed, 148 insertions(+), 52 deletions(-) create mode 100644 res/layout/admob_pref.xml create mode 100644 src/main/java/org/solovyev/android/view/prefs/AdViewPreference.java diff --git a/res/layout/admob_pref.xml b/res/layout/admob_pref.xml new file mode 100644 index 00000000..9260c564 --- /dev/null +++ b/res/layout/admob_pref.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/res/layout/help_faq.xml b/res/layout/help_faq.xml index 4c86e954..ae5d5f32 100644 --- a/res/layout/help_faq.xml +++ b/res/layout/help_faq.xml @@ -6,19 +6,26 @@ ~ or visit http://se.solovyev.org --> - - + a:layout_height="fill_parent"> + + + + diff --git a/res/layout/help_hints.xml b/res/layout/help_hints.xml index f2449c53..0a0b60e9 100644 --- a/res/layout/help_hints.xml +++ b/res/layout/help_hints.xml @@ -9,12 +9,19 @@ a:layout_width="fill_parent" a:layout_height="fill_parent"> - + + + + \ No newline at end of file diff --git a/res/layout/help_screens.xml b/res/layout/help_screens.xml index 4190a3e1..ee4dab4a 100644 --- a/res/layout/help_screens.xml +++ b/res/layout/help_screens.xml @@ -9,12 +9,20 @@ a:layout_width="fill_parent" a:layout_height="fill_parent"> - + + + + + \ No newline at end of file diff --git a/res/xml/main_preferences.xml b/res/xml/main_preferences.xml index 6502fac8..90061590 100644 --- a/res/xml/main_preferences.xml +++ b/res/xml/main_preferences.xml @@ -12,6 +12,10 @@ + + + + keywords) { + final ViewGroup layout = parentView != null ? parentView : (ViewGroup) activity.findViewById(layoutId); + // Create the adView final AdView adView = new AdView(activity, AdSize.BANNER, admobAccountId); - final ViewGroup layout = (ViewGroup) activity.findViewById(layoutId); - // Add the adView to it layout.addView(adView); @@ -184,5 +185,23 @@ public final class AndroidUtils { void process(@NotNull V view); } + public static void restartActivity(@NotNull Activity activity) { + final Intent intent = activity.getIntent(); + /* + for compatibility with android_1.6_compatibility + overridePendingTransition(0, 0); + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);*/ + + Log.d(activity.getClass().getName(), "Finishing current activity!"); + activity.finish(); + + /* + for compatibility with android_1.6_compatibility + + overridePendingTransition(0, 0);*/ + Log.d(activity.getClass().getName(), "Starting new activity!"); + activity.startActivity(intent); + } + } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index e4be1534..4d50d318 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -599,24 +599,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio); } - public void restart() { - final Intent intent = getIntent(); - /* - for compatibility with android_1.6_compatibility - overridePendingTransition(0, 0); - intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);*/ - - Log.d(this.getClass().getName(), "Finishing current activity!"); - finish(); - - /* - for compatibility with android_1.6_compatibility - - overridePendingTransition(0, 0);*/ - Log.d(this.getClass().getName(), "Starting new activity!"); - startActivity(intent); - } - @Override protected void onResume() { super.onResume(); @@ -626,7 +608,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh final String newLayoutName = preferences.getString(getString(R.string.p_calc_layout_key), getString(R.string.p_calc_layout)); final String newThemeName = preferences.getString(getString(R.string.p_calc_theme_key), getString(R.string.p_calc_theme)); if (!themeName.equals(newThemeName) || !layoutName.equals(newLayoutName)) { - restart(); + AndroidUtils.restartActivity(this); } calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance); diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index 87813353..40fe143c 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -3,6 +3,7 @@ package org.solovyev.android.calculator; import android.app.Activity; import android.content.Context; import android.util.Log; +import android.view.ViewGroup; import com.google.ads.AdView; import net.robotmedia.billing.BillingController; import org.jetbrains.annotations.NotNull; @@ -60,16 +61,16 @@ public class CalculatorApplication extends android.app.Application { @Nullable public static AdView inflateAd(@NotNull Activity activity) { - return inflateAd(activity, R.id.ad_parent_view); + return inflateAd(activity, null, R.id.ad_parent_view); } @Nullable - public static AdView inflateAd(@NotNull Activity activity, int parentViewId) { + public static AdView inflateAd(@NotNull Activity activity, @Nullable ViewGroup parentView, int parentViewId) { AdView result = null; if ( !isAdFree(activity) ) { Log.d(activity.getClass().getName(), "Application is not ad free - inflating ad!"); final List keywords = Collections.emptyList(); - result = AndroidUtils.createAndInflateAdView(activity, ADMOB_USER_ID, parentViewId, keywords); + result = AndroidUtils.createAndInflateAdView(activity, ADMOB_USER_ID, parentView, parentViewId, keywords); } else { Log.d(activity.getClass().getName(), "Application is ad free - no ads!"); } diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java index d63122a3..63034af4 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorPreferencesActivity.java @@ -17,6 +17,7 @@ import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingRequest; import net.robotmedia.billing.IBillingObserver; import net.robotmedia.billing.model.Transaction; +import org.solovyev.android.AndroidUtils; import org.solovyev.android.calculator.model.CalculatorEngine; import org.solovyev.android.view.widgets.VibratorContainer; @@ -135,17 +136,19 @@ public class CalculatorPreferencesActivity extends PreferenceActivity implements @Override public void onPurchaseStateChanged(String itemId, Transaction.PurchaseState state) { if (CalculatorApplication.AD_FREE_PRODUCT_ID.equals(itemId)) { - final Preference addFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY); - if (addFreePreference != null) { + final Preference adFreePreference = findPreference(CalculatorApplication.AD_FREE_P_KEY); + if (adFreePreference != null) { switch (state) { case PURCHASED: - addFreePreference.setEnabled(false); + adFreePreference.setEnabled(false); + // restart activity to disable ads + AndroidUtils.restartActivity(this); break; case CANCELLED: - addFreePreference.setEnabled(true); + adFreePreference.setEnabled(true); break; case REFUNDED: - addFreePreference.setEnabled(true); + adFreePreference.setEnabled(true); break; } } else { diff --git a/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java b/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java index 44badb56..c0ccd8ff 100644 --- a/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java +++ b/src/main/java/org/solovyev/android/calculator/help/AbstractHelpActivity.java @@ -10,6 +10,7 @@ import android.app.Activity; import android.os.Bundle; import com.google.ads.AdView; import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorApplication; /** * User: serso @@ -34,7 +35,7 @@ public class AbstractHelpActivity extends Activity { setContentView(layoutId); // do not inflate ad in help (as some problems were encountered dut to ScrollView - no space for ad banner) - //adView = CalculatorApplication.inflateAd(this); + adView = CalculatorApplication.inflateAd(this); } @Override diff --git a/src/main/java/org/solovyev/android/view/prefs/AdViewPreference.java b/src/main/java/org/solovyev/android/view/prefs/AdViewPreference.java new file mode 100644 index 00000000..bb5ae999 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/prefs/AdViewPreference.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2009-2012. Created by serso aka se.solovyev. + * For more information, please, contact se.solovyev@gmail.com + * or visit http://se.solovyev.org + */ + +package org.solovyev.android.view.prefs; + +import android.app.Activity; +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import com.google.ads.AdView; +import org.jetbrains.annotations.Nullable; +import org.solovyev.android.calculator.CalculatorApplication; + +/** + * User: serso + * Date: 1/14/12 + * Time: 6:47 PM + */ +public class AdViewPreference extends android.preference.Preference { + + @Nullable + private AdView adView; + + public AdViewPreference(Context context) { + super(context, null); + } + + public AdViewPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected View onCreateView(ViewGroup parent) { + // this will create the linear layout defined in ads_layout.xml + View view = super.onCreateView(parent); + + if (view instanceof ViewGroup) { + adView = CalculatorApplication.inflateAd((Activity) getContext(), ((ViewGroup) view), 0); + } + + return view; + } +} \ No newline at end of file