Skip to content

Commit

Permalink
Merge branch 'hotfix/3.6.6' of ssh://github.com/TeamAmaze/AmazeFileMa…
Browse files Browse the repository at this point in the history
…nager into hotfix/3.6.6
  • Loading branch information
VishalNehra committed Nov 8, 2021
2 parents 0da4228 + cfada8e commit cf73f3a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
19 changes: 15 additions & 4 deletions app/src/main/java/com/amaze/filemanager/application/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.amaze.filemanager.application;

import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;

import org.acra.ACRA;
import org.acra.annotation.AcraCore;
Expand Down Expand Up @@ -65,7 +66,7 @@
reportSenderFactoryClasses = AcraReportSenderFactory.class)
public class AppConfig extends GlideApplication {

public static final String TAG = AppConfig.class.getSimpleName();
private static final String TAG = AppConfig.class.getSimpleName();

private UtilitiesProvider utilsProvider;
private RequestQueue requestQueue;
Expand Down Expand Up @@ -175,14 +176,24 @@ public static void toast(Context context, String message) {
}

/**
* Run a runnable in the main application thread
* Run a {@link Runnable} in the main application thread
*
* @param r Runnable to run
* @param r {@link Runnable} to run
*/
public void runInApplicationThread(Runnable r) {
public void runInApplicationThread(@NonNull Runnable r) {
Completable.fromRunnable(r).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
}

/**
* Convenience method to run a {@link Callable} in the main application thread. Use when the
* callable's return value is not processed.
*
* @param c {@link Callable} to run
*/
public void runInApplicationThread(@NonNull Callable<Void> c) {
Completable.fromCallable(c).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
}

public static synchronized AppConfig getInstance() {
return instance;
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -728,5 +728,6 @@ You only need to do this once, until the next time you select a new location for
<string name="select_by_type">Select by type</string>
<string name="select_by_date">Select by date</string>
<string name="select_similar">Select similar</string>
<string name="error_fetching_google_play_product_list">Error fetching product list from Google Play.</string>
</resources>

59 changes: 39 additions & 20 deletions app/src/play/java/com/amaze/filemanager/utils/Billing.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.util.List;

import com.afollestad.materialdialogs.MaterialDialog;
import com.amaze.filemanager.BuildConfig;
import com.amaze.filemanager.R;
import com.amaze.filemanager.adapters.holders.DonationViewHolder;
import com.amaze.filemanager.application.AppConfig;
import com.amaze.filemanager.databinding.AdapterDonationBinding;
import com.amaze.filemanager.ui.activities.superclasses.BasicActivity;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
Expand All @@ -38,7 +41,6 @@
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -52,17 +54,18 @@
public class Billing extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements PurchasesUpdatedListener {

private static final String TAG = Billing.class.getSimpleName();

private BasicActivity activity;
private List<String> skuList;
private LayoutInflater layoutInflater;
private List<SkuDetails> skuDetails;

// create new donations client
private BillingClient billingClient;
/** True if billing service is connected now. */
private boolean isServiceConnected;

public Billing(BasicActivity activity) {
public Billing(@NonNull BasicActivity activity) {
this.activity = activity;

skuList = new ArrayList<>();
Expand All @@ -71,8 +74,6 @@ public Billing(BasicActivity activity) {
skuList.add("donations_3");
skuList.add("donations_4");

layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

billingClient =
BillingClient.newBuilder(activity).setListener(this).enablePendingPurchases().build();
initiatePurchaseFlow();
Expand Down Expand Up @@ -103,9 +104,18 @@ private void initiatePurchaseFlow() {
billingClient.querySkuDetailsAsync(
params.build(),
(responseCode, skuDetailsList) -> {
// Successfully fetched product details
skuDetails = skuDetailsList;
popProductsList(responseCode, skuDetailsList);
if (skuDetailsList != null && skuDetailsList.size() > 0) {
// Successfully fetched product details
skuDetails = skuDetailsList;
popProductsList(responseCode, skuDetailsList);
} else {
AppConfig.toast(activity, R.string.error_fetching_google_play_product_list);
if (BuildConfig.DEBUG) {
Log.w(
TAG,
"Error fetching product list - looks like you are running a DEBUG build.");
}
}
});
};

Expand All @@ -128,13 +138,14 @@ private void popProductsList(BillingResult response, List<SkuDetails> skuDetails
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View rootView = layoutInflater.inflate(R.layout.adapter_donation, parent, false);
View rootView =
AdapterDonationBinding.inflate(LayoutInflater.from(this.activity), parent, false).getRoot();
return new DonationViewHolder(rootView);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DonationViewHolder) {
if (holder instanceof DonationViewHolder && skuDetails.size() > 0) {
String titleRaw = skuDetails.get(position).getTitle();
((DonationViewHolder) holder)
.TITLE.setText(titleRaw.subSequence(0, titleRaw.lastIndexOf("(")));
Expand Down Expand Up @@ -199,10 +210,7 @@ private void startServiceConnection(final Runnable executeOnSuccess) {
new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResponse) {
Log.d(
Billing.this.getClass().getSimpleName(),
"Setup finished. Response code: " + billingResponse);

Log.d(TAG, "Setup finished. Response code: " + billingResponse.getResponseCode());
if (billingResponse.getResponseCode() == BillingClient.BillingResponseCode.OK) {
isServiceConnected = true;
if (executeOnSuccess != null) {
Expand All @@ -226,11 +234,22 @@ public void destroyBillingInstance() {
}

private void showPaymentsDialog(final BasicActivity context) {
final MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
builder.title(R.string.donate);
builder.adapter(this, null);
builder.theme(context.getAppTheme().getMaterialDialogTheme());
builder.cancelListener(dialog -> purchaseProduct.purchaseCancel());
builder.show();
/*
* As of Billing library 4.0, all callbacks are running on background thread.
* Need to use AppConfig.runInApplicationThread() for UI interactions
*
*
*/
AppConfig.getInstance()
.runInApplicationThread(
() -> {
final MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
builder.title(R.string.donate);
builder.adapter(this, null);
builder.theme(context.getAppTheme().getMaterialDialogTheme());
builder.cancelListener(dialog -> purchaseProduct.purchaseCancel());
builder.show();
return null;
});
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
slf4jVersion = "1.7.25"
mockitoVersion = "3.9.0"
mockitoKotlinVersion = "3.2.0"
androidBillingVersion = "2.1.0"
androidBillingVersion = "4.0.0"
junrarVersion = "7.4.0"
zip4jVersion = "2.6.4"
espressoVersion = "3.3.0"
Expand Down

0 comments on commit cf73f3a

Please sign in to comment.