android_calculator-56: Billing doesn't work if internet was off on the start of application

This commit is contained in:
Sergey Solovyev 2012-01-14 20:37:21 +04:00
parent 20392b00ad
commit c005610cb5
11 changed files with 148 additions and 52 deletions

13
res/layout/admob_pref.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
</LinearLayout>

View File

@ -6,11 +6,16 @@
~ or visit http://se.solovyev.org
-->
<ScrollView xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<LinearLayout
a:orientation="vertical"
a:id="@+id/ad_parent_view"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView
a:text="@string/c_faq_content"
a:layout_width="fill_parent"
@ -20,5 +25,7 @@
a:scrollbars="vertical"
style="@style/about_style"/>
</LinearLayout>
</ScrollView>

View File

@ -9,6 +9,12 @@
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<LinearLayout
a:orientation="vertical"
a:id="@+id/ad_parent_view"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView
xmlns:a="http://schemas.android.com/apk/res/android"
a:text="@string/c_hints_content"
@ -16,5 +22,6 @@
a:layout_weight="1"
a:scrollbars="vertical"
style="@style/about_style"/>
</LinearLayout>
</ScrollView>

View File

@ -9,6 +9,12 @@
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<LinearLayout
a:orientation="vertical"
a:id="@+id/ad_parent_view"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView
xmlns:a="http://schemas.android.com/apk/res/android"
a:text="@string/c_screens_content"
@ -17,4 +23,6 @@
a:scrollbars="vertical"
style="@style/about_style"/>
</LinearLayout>
</ScrollView>

View File

@ -12,6 +12,10 @@
<PreferenceCategory a:title="@string/c_prefs_calculations_category">
<org.solovyev.android.view.prefs.AdViewPreference
a:key="admob_01"
a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference
a:key="@string/p_calc_round_result_key"
a:summary="@string/c_calc_round_result_summary"
@ -54,6 +58,10 @@
<PreferenceCategory a:title="@string/c_prefs_appearance_category">
<org.solovyev.android.view.prefs.AdViewPreference
a:key="admob_02"
a:layout="@layout/admob_pref"/>
<android.preference.CheckBoxPreference
a:key="@string/p_calc_color_display_key"
a:summary="@string/c_calc_color_display_summary"

View File

@ -129,13 +129,14 @@ public final class AndroidUtils {
@NotNull
public static AdView createAndInflateAdView(@NotNull Activity activity,
@NotNull String admobAccountId,
@Nullable ViewGroup parentView,
int layoutId,
@NotNull List<String> 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);
}
}

View File

@ -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);

View File

@ -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<String> 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!");
}

View File

@ -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 {

View File

@ -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

View File

@ -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;
}
}