WIP: Implement Google Play Billing

This commit is contained in:
Billy Brawner 2019-02-10 13:15:47 -06:00 committed by William Brawner
parent 45f1c54eb1
commit 96478b7748
7 changed files with 52 additions and 9 deletions

3
app/.gitignore vendored
View file

@ -1,3 +1,4 @@
/build
crashlytics.properties
*.apk
*.apk
google-services.json

View file

@ -73,7 +73,7 @@ android {
}
ext {
acraVersion = '5.1.3'
acraVersion = '5.2.0'
support_version = "28.0.0"
}

View file

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name=".MarkdownApplication"

View file

@ -1,5 +1,7 @@
package com.wbrawner.simplemarkdown;
import android.content.Context;
import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenterImpl;
@ -15,6 +17,11 @@ import dagger.Provides;
@Module
public class AppModule {
private final Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
public MarkdownFile provideMarkdownFile() {
return new MarkdownFile();
@ -22,6 +29,6 @@ public class AppModule {
@Provides @Singleton
public MarkdownPresenter provideMarkdownPresenter(MarkdownFile file) {
return new MarkdownPresenterImpl(file);
return new MarkdownPresenterImpl(context.getApplicationContext(), file);
}
}

View file

@ -67,7 +67,7 @@ public class MarkdownApplication extends Application {
public void onCreate() {
super.onCreate();
component = DaggerAppComponent.builder()
.appModule(new AppModule())
.appModule(new AppModule(this))
.build();
}
@ -78,8 +78,6 @@ public class MarkdownApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// The following line triggers the initialization of ACRA
ACRA.init(this);
}
}

View file

@ -3,13 +3,15 @@ package com.wbrawner.simplemarkdown.presentation;
import android.content.Context;
import android.net.Uri;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.wbrawner.simplemarkdown.view.MarkdownEditView;
import com.wbrawner.simplemarkdown.view.MarkdownPreviewView;
import java.io.File;
import java.io.InputStream;
public interface MarkdownPresenter {
public interface MarkdownPresenter extends PurchasesUpdatedListener, BillingClientStateListener {
File getFile();
void loadMarkdown();

View file

@ -5,7 +5,12 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.OpenableColumns;
import android.support.annotation.Nullable;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.commonsware.cwac.anddown.AndDown;
import com.wbrawner.simplemarkdown.model.MarkdownFile;
import com.wbrawner.simplemarkdown.utility.Utils;
@ -18,18 +23,27 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
public class MarkdownPresenterImpl implements MarkdownPresenter {
public class MarkdownPresenterImpl
implements MarkdownPresenter, BillingClientStateListener, PurchasesUpdatedListener {
private final Object fileLock = new Object();
private MarkdownFile file;
private volatile MarkdownEditView editView;
private volatile MarkdownPreviewView previewView;
private Handler fileHandler = new Handler();
private BillingClient billingClient;
public MarkdownPresenterImpl(MarkdownFile file) {
public MarkdownPresenterImpl(Context context, MarkdownFile file) {
synchronized (fileLock) {
this.file = file;
}
billingClient = BillingClient.newBuilder(context.getApplicationContext())
.setListener(this)
.build();
billingClient.startConnection(this);
}
@Override
@ -242,4 +256,24 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
}
}
}
@Override
public void onBillingSetupFinished(int responseCode) {
if (responseCode != BillingClient.BillingResponse.OK) {
return;
}
// The billing client is ready. You can query purchases here.
}
@Override
public void onBillingServiceDisconnected() {
// TODO: Set a flag and just try again later
billingClient.startConnection(this);
}
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
}
}