Issue #8 paid apps support

This commit is contained in:
Sergey Eremin
2017-01-18 14:21:40 +03:00
parent 6da1a6baf5
commit 20b8fee0fc
6 changed files with 94 additions and 20 deletions

View File

@@ -24,5 +24,5 @@ android {
}
dependencies {
compile 'com.github.yeriomin:play-store-api:0.3'
compile 'com.github.yeriomin:play-store-api:cfd47603fa'
}

View File

@@ -3,9 +3,12 @@ package com.github.yeriomin.yalpstore;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -52,6 +55,23 @@ public class DetailsActivity extends Activity {
private List<Review> reviews = new ArrayList<>();
private Menu menu;
private App app;
private long downloadId;
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (null != extras) {
long id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
System.out.println("downloadId=" + downloadId + " id=" + id);
if (downloadId == id) {
Button button = (Button) findViewById(R.id.download);
button.setText(R.string.details_install);
button.setEnabled(true);
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@@ -158,6 +178,20 @@ public class DetailsActivity extends Activity {
task.execute();
}
@Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
@Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(receiver, filter);
super.onResume();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -282,13 +316,14 @@ public class DetailsActivity extends Activity {
setText(R.id.permissions, TextUtils.join("\n", localizedPermissions));
Button downloadButton = (Button) findViewById(R.id.download);
if (!app.isFree()) {
downloadButton.setText(getString(R.string.details_download_nonfree));
downloadButton.setEnabled(false);
} else if (app.getVersionCode() == 0) {
if (app.getVersionCode() == 0) {
downloadButton.setText(getString(R.string.details_download_impossible));
downloadButton.setEnabled(false);
} else {
final boolean exists = PlayStoreApiWrapper.getApkPath(app).exists();
if (exists) {
downloadButton.setText(R.string.details_install);
}
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@@ -297,12 +332,24 @@ public class DetailsActivity extends Activity {
protected Throwable doInBackground(Void... params) {
PlayStoreApiWrapper wrapper = new PlayStoreApiWrapper(DetailsActivity.this);
try {
wrapper.download(app);
downloadId = wrapper.download(app);
} catch (Throwable e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Throwable e) {
super.onPostExecute(e);
if (null == e && !exists) {
Button button = (Button) findViewById(R.id.download);
button.setText(R.string.details_downloading);
button.setEnabled(false);
} else if (e instanceof NotPurchasedException) {
Toast.makeText(getApplicationContext(), getString(R.string.error_not_purchased), Toast.LENGTH_LONG).show();
}
}
};
task.setContext(v.getContext());
task.prepareDialog(

View File

@@ -0,0 +1,4 @@
package com.github.yeriomin.yalpstore;
public class NotPurchasedException extends Exception {
}

View File

@@ -14,6 +14,7 @@ import com.github.yeriomin.playstoreapi.AndroidAppDeliveryData;
import com.github.yeriomin.playstoreapi.AppDetails;
import com.github.yeriomin.playstoreapi.BulkDetailsEntry;
import com.github.yeriomin.playstoreapi.BuyResponse;
import com.github.yeriomin.playstoreapi.DeliveryResponse;
import com.github.yeriomin.playstoreapi.DocV2;
import com.github.yeriomin.playstoreapi.GooglePlayAPI;
import com.github.yeriomin.playstoreapi.HttpCookie;
@@ -250,30 +251,48 @@ public class PlayStoreApiWrapper {
return suggestions;
}
public void download(App app) throws IOException {
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String filename = app.getPackageName() + "." + String.valueOf(app.getVersionCode()) + ".apk";
File fullPath = new File(downloadsDir, filename);
public long download(App app) throws IOException, NotPurchasedException {
File path = getApkPath(app);
if (fullPath.exists()) {
Log.i(this.getClass().getName(), filename + " exists. No download needed.");
context.startActivity(getOpenApkIntent(context, fullPath));
if (path.exists()) {
Log.i(this.getClass().getName(), path.getName() + " exists. No download needed.");
context.startActivity(getOpenApkIntent(context, path));
} else {
Log.i(this.getClass().getName(), "Downloading apk to " + filename);
BuyResponse response = getApi().purchase(app.getPackageName(), app.getVersionCode(), app.getOfferType());
AndroidAppDeliveryData appDeliveryData = response.getPurchaseStatusResponse().getAppDeliveryData();
Log.i(this.getClass().getName(), "Downloading apk to " + path.getName());
AndroidAppDeliveryData appDeliveryData;
if (app.isFree()) {
BuyResponse response = getApi().purchase(app.getPackageName(), app.getVersionCode(), app.getOfferType());
appDeliveryData = response.getPurchaseStatusResponse().getAppDeliveryData();
} else {
DeliveryResponse response = getApi().delivery(app.getPackageName(), app.getVersionCode(), app.getOfferType());
if (response.hasAppDeliveryData()) {
appDeliveryData = response.getAppDeliveryData();
} else {
throw new NotPurchasedException();
}
}
// Download manager cannot download https on old android versions
String downloadUrl = appDeliveryData.getDownloadUrl().replace("https", "http");
HttpCookie downloadAuthCookie = appDeliveryData.getDownloadAuthCookie(0);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
request.addRequestHeader("Cookie", downloadAuthCookie.getName() + "=" + downloadAuthCookie.getValue());
Uri uri = Uri.withAppendedPath(Uri.fromFile(downloadsDir), filename);
Uri uri = Uri.withAppendedPath(
Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)),
path.getName()
);
request.setDestinationUri(uri);
request.setTitle(app.getDisplayName());
((DownloadManager) this.context.getSystemService(DOWNLOAD_SERVICE)).enqueue(request);
return ((DownloadManager) this.context.getSystemService(DOWNLOAD_SERVICE)).enqueue(request);
}
return 0L;
}
static public File getApkPath(App app) {
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String filename = app.getPackageName() + "." + String.valueOf(app.getVersionCode()) + ".apk";
return new File(downloadsDir, filename);
}
static public Intent getOpenApkIntent(Context context, File file) {

View File

@@ -27,6 +27,7 @@
<string name="error_credentials_empty">Введите данные для доступа к аккаунту google</string>
<string name="error_no_network">Нет соединения с сетью.</string>
<string name="error_network_other" formatted="false">Сетевая ошибка: %s</string>
<string name="error_not_purchased">Похоже, это приложение не было куплено.</string>
<string name="details_installs" formatted="false">%s ↓</string>
<string name="details_rating" formatted="false">%.1f★</string>
<string name="details_rating_specific" formatted="false">%d★ %d</string>
@@ -41,7 +42,8 @@
<string name="details_versionName" formatted="false">Версия %s</string>
<string name="details_versionName_updatable" formatted="false">Версия %s → %s</string>
<string name="details_download">Скачать</string>
<string name="details_download_nonfree">Платное приложение - скачать не получится</string>
<string name="details_install">Установить</string>
<string name="details_downloading">Скачиваем…</string>
<string name="details_download_impossible">Ошибка. Не удалось получить версию приложения</string>
<string name="suffix_million">млн</string>
<string name="suffix_billion">млрд</string>

View File

@@ -27,6 +27,7 @@
<string name="error_credentials_empty">Provide google account credentials, please.</string>
<string name="error_no_network">No network connection.</string>
<string name="error_network_other" formatted="false">Network error: %s</string>
<string name="error_not_purchased">The app does not seem to be purchased.</string>
<string name="details_installs" formatted="false">%s ↓</string>
<string name="details_rating" formatted="false">%.1f★</string>
<string name="details_rating_specific" formatted="false">%d★ %d</string>
@@ -41,7 +42,8 @@
<string name="details_versionName" formatted="false">Version %s</string>
<string name="details_versionName_updatable" formatted="false">Version %s → %s</string>
<string name="details_download">Download</string>
<string name="details_download_nonfree">App is not free - download disabled</string>
<string name="details_install">Install</string>
<string name="details_downloading">Downloading…</string>
<string name="details_download_impossible">Error. Could not get app version.</string>
<string name="suffix_million">mil</string>
<string name="suffix_billion">bil</string>