Skip to content

Commit

Permalink
Issue #396 non-purchased apps have "buy" button now, purchased and fr…
Browse files Browse the repository at this point in the history
…ee apps have "download" button
  • Loading branch information
yeriomin committed May 14, 2018
1 parent 83a6eb1 commit 841ea32
Show file tree
Hide file tree
Showing 19 changed files with 292 additions and 21 deletions.
14 changes: 14 additions & 0 deletions app/src/contemporary/res/layout/details_button_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@
android:background="@drawable/button_border"
android:text="@string/details_download" />

<Button
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/buy"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:visibility="gone"
android:maxLines="1"
android:background="@drawable/button_border" />

<ImageButton
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/cancel"
Expand Down
8 changes: 8 additions & 0 deletions app/src/legacy/res/layout/details_activity_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
android:maxLines="1"
android:text="@string/details_download" />

<Button
android:id="@+id/buy"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:visibility="gone"
android:maxLines="1" />

<ImageButton
android:id="@+id/cancel"
android:layout_width="48dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class AppListIterator implements Iterator {

protected Filter filter = new Filter();
private Filter filter;
protected com.github.yeriomin.playstoreapi.AppListIterator iterator;

public AppListIterator(com.github.yeriomin.playstoreapi.AppListIterator iterator) {
Expand Down Expand Up @@ -73,7 +73,7 @@ protected boolean shouldSkip(App app) {
}

protected void addApp(List<App> apps, App app) {
if (shouldSkip(app)) {
if (null != filter && shouldSkip(app)) {
Log.i(getClass().getSimpleName(), "Filtering out " + app.getPackageName());
} else {
apps.add(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
import com.github.yeriomin.playstoreapi.PropertiesDeviceInfoProvider;
import com.github.yeriomin.playstoreapi.TokenDispenserException;
import com.github.yeriomin.yalpstore.model.LoginInfo;
import com.github.yeriomin.yalpstore.task.playstore.PlayStorePayloadTask;
import com.github.yeriomin.yalpstore.task.playstore.PlayStoreTask;
import com.github.yeriomin.yalpstore.task.playstore.PurchasedAppsTask;

import java.io.IOException;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class PlayStoreApiAuthenticator {

Expand All @@ -49,12 +53,14 @@ public class PlayStoreApiAuthenticator {
static private final int RETRIES = 5;

private Context context;
private Set<PlayStorePayloadTask> onLoginTasks = new HashSet<>();

private static GooglePlayAPI api;
private static TokenDispenserMirrors tokenDispenserMirrors = new TokenDispenserMirrors();

public PlayStoreApiAuthenticator(Context context) {
this.context = context;
onLoginTasks.add(new PurchasedAppsTask());
}

public GooglePlayAPI getApi() throws IOException {
Expand Down Expand Up @@ -128,6 +134,7 @@ private GooglePlayAPI build(LoginInfo loginInfo) throws IOException {
loginInfo.setGsfId(api.getGsfId());
loginInfo.setToken(api.getToken());
save(loginInfo);
runOnLoginTasks();
return api;
}

Expand Down Expand Up @@ -213,4 +220,11 @@ private void save(LoginInfo loginInfo) {
.commit()
;
}

private void runOnLoginTasks() {
for (PlayStorePayloadTask task: onLoginTasks) {
task.setContext(context);
task.executeOnExecutorIfPossible();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ static public boolean canInstallInBackground(Context context) {
}

static public Set<String> getStringSet(Context context, String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return getStringSet(PreferenceManager.getDefaultSharedPreferences(context), key);
}

static public Set<String> getStringSet(SharedPreferences preferences, String key) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
try {
return preferences.getStringSet(key, new HashSet<String>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Yalp Store
* Copyright (C) 2018 Sergey Yeriomin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package com.github.yeriomin.yalpstore;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Collection;
import java.util.HashSet;

public class SharedPreferencesCachedSet extends HashSet<String> {

static private final long DEFAULT_VALID_TIME = 1000*60*60*24;

static private final String PREFERENCE_PREFIX = "PREFERENCE_";
static private final String _UPDATE_TIME = "_UPDATE_TIME";
static private final String _NOT_LOGGED_IN = "_NOT_LOGGED_IN";

private String name;
private SharedPreferences preferences;

public SharedPreferencesCachedSet(String name, Context context) {
this(name, PreferenceManager.getDefaultSharedPreferences(context));
}

public SharedPreferencesCachedSet(String name, SharedPreferences preferences) {
this(name);
setPreferences(preferences);
}

public SharedPreferencesCachedSet(String name) {
this.name = name;
}

public void setPreferences(SharedPreferences preferences) {
this.preferences = preferences;
addAll(PreferenceUtil.getStringSet(preferences, getStorageKey()));
}

public boolean timeToUpdate() {
return preferences.getLong(getLastUpdateTimeKey(), 0) + getValidTime() < System.currentTimeMillis();
}

@Override
public boolean addAll(Collection<? extends String> c) {
boolean modified = super.addAll(c);
if (modified) {
save();
}
return modified;
}

@Override
public void clear() {
super.clear();
save();
}

public void save() {
PreferenceUtil.putStringSet(preferences, getStorageKey(), this);
preferences.edit().putLong(getLastUpdateTimeKey(), System.currentTimeMillis()).commit();
}

protected String getStorageKey() {
return PREFERENCE_PREFIX + name + (preferences.getBoolean(PlayStoreApiAuthenticator.PREFERENCE_APP_PROVIDED_EMAIL, true) ? _NOT_LOGGED_IN : preferences.getString(PlayStoreApiAuthenticator.PREFERENCE_EMAIL, ""));
}

protected String getLastUpdateTimeKey() {
return getStorageKey() + _UPDATE_TIME;
}

protected long getValidTime() {
return DEFAULT_VALID_TIME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
public class YalpStoreApplication extends Application {

public static final Set<String> fdroidPackageNames = new HashSet<>();
public static final SharedPreferencesCachedSet purchasedPackageNames = new SharedPreferencesCachedSet("purchasedPackageNames");

private boolean isBackgroundUpdating = false;
private List<String> pendingUpdates = new ArrayList<>();
Expand Down Expand Up @@ -111,6 +112,7 @@ public void onCreate() {
} else {
new FdroidListTask(this.getApplicationContext()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
purchasedPackageNames.setPreferences(PreferenceManager.getDefaultSharedPreferences(this));
}

private void registerDownloadReceiver() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Yalp Store
* Copyright (C) 2018 Sergey Yeriomin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package com.github.yeriomin.yalpstore.fragment.details;

import android.view.View;

import com.github.yeriomin.yalpstore.R;
import com.github.yeriomin.yalpstore.YalpStoreActivity;
import com.github.yeriomin.yalpstore.YalpStoreApplication;
import com.github.yeriomin.yalpstore.model.App;
import com.github.yeriomin.yalpstore.view.PurchaseDialogBuilder;
import com.github.yeriomin.yalpstore.view.UriOnClickListener;

class ButtonBuy extends Button {

public ButtonBuy(YalpStoreActivity activity, App app) {
super(activity, app);
}

@Override
protected View getButton() {
return activity.findViewById(R.id.buy);
}

@Override
protected boolean shouldBeVisible() {
return !YalpStoreApplication.purchasedPackageNames.contains(app.getPackageName()) && !app.isInstalled() && !app.isFree();
}

@Override
public void draw() {
super.draw();
android.widget.Button button = (android.widget.Button) getButton();
if (null != button) {
button.setText(app.getPrice());
}
}

@Override
protected void onButtonClick(View v) {
new UriOnClickListener(activity, PurchaseDialogBuilder.URL_PURCHASE + app.getPackageName()).onClick(v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.github.yeriomin.yalpstore.Paths;
import com.github.yeriomin.yalpstore.R;
import com.github.yeriomin.yalpstore.YalpStoreActivity;
import com.github.yeriomin.yalpstore.YalpStoreApplication;
import com.github.yeriomin.yalpstore.YalpStorePermissionManager;
import com.github.yeriomin.yalpstore.model.App;
import com.github.yeriomin.yalpstore.selfupdate.UpdaterFactory;
Expand Down Expand Up @@ -62,6 +63,7 @@ public boolean shouldBeVisible() {
|| apk.length() != app.getSize()
|| !DownloadState.get(app.getPackageName()).isEverythingSuccessful()
)
&& (app.isFree() || YalpStoreApplication.purchasedPackageNames.contains(app.getPackageName()))
&& (app.isInPlayStore() || app.getPackageName().equals(BuildConfig.APPLICATION_ID))
&& (getInstalledVersionCode() != app.getVersionCode() || activity instanceof ManualDownloadActivity)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.yeriomin.yalpstore.DetailsActivity;
import com.github.yeriomin.yalpstore.DownloadState;
import com.github.yeriomin.yalpstore.InstallationState;
import com.github.yeriomin.yalpstore.InstallerFactory;
import com.github.yeriomin.yalpstore.Paths;
import com.github.yeriomin.yalpstore.R;
import com.github.yeriomin.yalpstore.model.App;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public DownloadOrInstall(DetailsActivity activity, App app) {
public void draw() {
new ButtonUninstall(activity, app).draw();
new ButtonDownload(activity, app).draw();
new ButtonCancel((DetailsActivity) activity, app).draw();
new ButtonBuy(activity, app).draw();
new ButtonCancel(activity, app).draw();
new ButtonInstall((DetailsActivity) activity, app).draw();
new ButtonRun((DetailsActivity) activity, app).draw();
new DownloadProgressBarUpdater(app.getPackageName(), (ProgressBar) activity.findViewById(R.id.download_progress)).execute(UPDATE_INTERVAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.yeriomin.yalpstore.YalpStoreActivity;
import com.github.yeriomin.yalpstore.YalpStoreApplication;
import com.github.yeriomin.yalpstore.model.App;
import com.github.yeriomin.yalpstore.task.playstore.PurchaseTask;
import com.github.yeriomin.yalpstore.view.UriOnClickListener;

public class Fdroid extends Abstract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.github.yeriomin.yalpstore.BuildConfig;
import com.github.yeriomin.yalpstore.InstallerAbstract;
import com.github.yeriomin.yalpstore.InstallerFactory;
import com.github.yeriomin.yalpstore.model.App;

import java.util.List;

import eu.chainfire.libsuperuser.Shell;

public class InstallTask extends AsyncTask<Void, Void, Void> {

private App app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.view.View;

import com.github.yeriomin.yalpstore.ContextUtil;
Expand Down Expand Up @@ -73,4 +74,12 @@ protected void onPostExecute(T result) {
progressIndicator.setVisibility(View.GONE);
}
}

public AsyncTask<String, Void, T> executeOnExecutorIfPossible() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return this.execute();
} else {
return this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
package com.github.yeriomin.yalpstore.task.playstore;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.WindowManager;

Expand All @@ -38,8 +36,6 @@
import com.github.yeriomin.yalpstore.NotPurchasedException;
import com.github.yeriomin.yalpstore.R;
import com.github.yeriomin.yalpstore.YalpStoreActivity;
import com.github.yeriomin.yalpstore.view.DialogWrapper;
import com.github.yeriomin.yalpstore.view.DialogWrapperAbstract;
import com.github.yeriomin.yalpstore.view.PurchaseDialogBuilder;

import java.io.IOException;
Expand Down
Loading

0 comments on commit 841ea32

Please sign in to comment.