pull/156/head
M66B 6 years ago
parent 137053a865
commit ada668da2a

@ -34,6 +34,9 @@ import android.view.View;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
@ -52,6 +55,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -60,17 +64,19 @@ import java.util.Map;
abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedListener {
private BillingClient billingClient = null;
private Map<String, SkuDetails> skuDetails = new HashMap<>();
private List<IBillingListener> listeners = new ArrayList<>();
static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE";
static final String ACTION_ACTIVATE_PRO = BuildConfig.APPLICATION_ID + ".ACTIVATE_PRO";
private static final String SKU_PRO = BuildConfig.APPLICATION_ID + ".pro";
static final String SKU_PRO = BuildConfig.APPLICATION_ID + ".pro";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Helper.isPlayStoreInstall(this)) {
Log.i("IAB start");
billingClient = BillingClient.newBuilder(this).setListener(this).build();
billingClient.startConnection(billingClientStateListener);
}
@ -265,12 +271,34 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
for (SkuDetails skuDetail : skuDetailsList) {
Log.i("IAB SKU detail=" + skuDetail);
skuDetails.put(skuDetail.getSku(), skuDetail);
for (IBillingListener listener : listeners)
listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice());
}
}
}
});
}
interface IBillingListener {
void onSkuDetails(String sku, String price);
}
void addBillingListener(final IBillingListener listener, LifecycleOwner owner) {
Log.i("Adding billing listener=" + listener);
listeners.add(listener);
for (SkuDetails skuDetail : skuDetails.values())
listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice());
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
Log.i("Removing billing listener=" + listener);
listeners.remove(listener);
}
});
}
private void checkPurchases(List<Purchase> purchases) {
if (purchases != null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

@ -152,4 +152,8 @@ public class FragmentBase extends Fragment {
void addBackPressedListener(ActivityBase.IBackPressedListener listener) {
((ActivityBase) getActivity()).addBackPressedListener(listener, getViewLifecycleOwner());
}
void addBillingListener(ActivityBilling.IBillingListener listener) {
((ActivityBilling) getActivity()).addBillingListener(listener, getViewLifecycleOwner());
}
}

@ -40,6 +40,7 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
private TextView tvList;
private Button btnPurchase;
private TextView tvPrice;
private TextView tvPriceHint;
@Override
@Nullable
@ -52,11 +53,14 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
tvList = view.findViewById(R.id.tvList);
btnPurchase = view.findViewById(R.id.btnPurchase);
tvPrice = view.findViewById(R.id.tvPrice);
tvPriceHint = view.findViewById(R.id.tvPriceHint);
tvList.setText(HtmlHelper.fromHtml(
"<a href=\"" + BuildConfig.PRO_FEATURES_URI + "\">" + Html.escapeHtml(getString(R.string.title_pro_list)) + "</a>"));
tvList.setMovementMethod(LinkMovementMethod.getInstance());
tvPrice.setText(null);
btnPurchase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@ -65,11 +69,24 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
}
});
tvPrice.setMovementMethod(LinkMovementMethod.getInstance());
tvPriceHint.setMovementMethod(LinkMovementMethod.getInstance());
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
addBillingListener(new ActivityBilling.IBillingListener() {
@Override
public void onSkuDetails(String sku, String price) {
if (ActivityBilling.SKU_PRO.equals(sku))
tvPrice.setText(price);
}
});
}
@Override
public void onResume() {
super.onResume();

@ -46,6 +46,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvList" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="5 euro"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toBottomOf="@+id/btnPurchase"
app:layout_constraintStart_toEndOf="@+id/btnPurchase"
app:layout_constraintTop_toTopOf="@+id/btnPurchase" />
<TextView
android:id="@+id/tvHint"
android:layout_width="wrap_content"
@ -57,7 +68,7 @@
app:layout_constraintTop_toBottomOf="@id/btnPurchase" />
<TextView
android:id="@+id/tvPrice"
android:id="@+id/tvPriceHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"

Loading…
Cancel
Save