Skip to content

Commit

Permalink
Issue #281 Download/more button in app lists
Browse files Browse the repository at this point in the history
  • Loading branch information
yeriomin committed Mar 21, 2018
1 parent 42f5144 commit 6cc8b97
Show file tree
Hide file tree
Showing 31 changed files with 468 additions and 143 deletions.
Expand Up @@ -4,7 +4,6 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.ListAdapter;

Expand Down
@@ -1,7 +1,6 @@
package com.github.yeriomin.yalpstore;

import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -24,9 +23,14 @@ abstract public class AppListActivity extends YalpStoreActivity {

protected ListView listView;
protected Map<String, ListItem> listItems = new HashMap<>();
protected AppListDownloadReceiver appListDownloadReceiver;

abstract public void loadApps();
abstract protected ListItem getListItem(App app);
abstract protected ListItem buildListItem(App app);

public ListItem getListItem(String packageName) {
return listItems.get(packageName);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,6 +42,18 @@ protected void onCreate(Bundle savedInstanceState) {
registerForContextMenu(getListView());
}

@Override
protected void onPause() {
unregisterReceiver(appListDownloadReceiver);
super.onPause();
}

@Override
protected void onResume() {
appListDownloadReceiver = new AppListDownloadReceiver(this);
super.onResume();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Expand Down Expand Up @@ -74,7 +90,6 @@ public boolean onContextItemSelected(MenuItem item) {
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (YalpStorePermissionManager.isGranted(requestCode, permissions, grantResults)) {
Log.i(getClass().getSimpleName(), "User granted the write permission");
new ButtonDownload(this, DetailsActivity.app).download();
}
}
Expand Down Expand Up @@ -111,7 +126,7 @@ public void addApps(List<App> appsToAdd, boolean update) {
AppListAdapter adapter = (AppListAdapter) getListView().getAdapter();
adapter.setNotifyOnChange(false);
for (App app: appsToAdd) {
ListItem listItem = getListItem(app);
ListItem listItem = buildListItem(app);
listItems.put(app.getPackageName(), listItem);
adapter.add(listItem);
}
Expand Down
@@ -0,0 +1,42 @@
package com.github.yeriomin.yalpstore;

import android.content.Context;
import android.content.Intent;

import com.github.yeriomin.yalpstore.view.AppBadge;

public class AppListDownloadReceiver extends ForegroundDownloadReceiver {

public AppListDownloadReceiver(AppListActivity activity) {
super(activity);
}

@Override
protected void cleanup() {
draw();
}

@Override
protected void draw() {
AppBadge appBadge = getAppBadge();
if (null != appBadge) {
appBadge.redrawMoreButton();
}
}

@Override
protected void process(Context context, Intent intent) {
AppListActivity activity = (AppListActivity) activityRef.get();
if (!activity.getListedPackageNames().contains(state.getApp().getPackageName())) {
return;
}
super.process(context, intent);
}

private AppBadge getAppBadge() {
if (null == activityRef.get() || null == state || null == state.getApp()) {
return null;
}
return (AppBadge) ((AppListActivity) activityRef.get()).getListItem(state.getApp().getPackageName());
}
}
Expand Up @@ -95,7 +95,6 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (YalpStorePermissionManager.isGranted(requestCode, permissions, grantResults)) {
Log.i(getClass().getSimpleName(), "User granted the write permission");
if (null == downloadOrInstallFragment && null != app) {
downloadOrInstallFragment = new DownloadOrInstall(this, app);
redrawButtons();
Expand Down
Expand Up @@ -2,78 +2,43 @@

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import java.lang.ref.WeakReference;
public class DetailsDownloadReceiver extends ForegroundDownloadReceiver {

public class DetailsDownloadReceiver extends DownloadReceiver {

private WeakReference<DetailsActivity> activityRef = new WeakReference<>(null);
private String packageName;

public DetailsDownloadReceiver(DetailsActivity activity, String packageName) {
activityRef = new WeakReference<>(activity);
super(activity);
this.packageName = packageName;
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_DELTA_PATCHING_COMPLETE);
filter.addAction(DownloadManagerInterface.ACTION_DOWNLOAD_COMPLETE);
filter.addAction(DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED);
activity.registerReceiver(this, filter);
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
DetailsActivity activity = activityRef.get();
if (null == activity || !ContextUtil.isAlive(activity)) {
return;
}
if (null == state) {
if (actionIs(intent, DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED)) {
cleanup();
}
}
}

@Override
protected void process(Context context, Intent intent) {
if (!state.getApp().getPackageName().equals(packageName)) {
return;
}
if (actionIs(intent, DownloadManagerInterface.ACTION_DOWNLOAD_COMPLETE) && isDelta(state.getApp())) {
return;
}
state.setFinished(downloadId);
if (DownloadManagerFactory.get(context).success(downloadId) && !actionIs(intent, DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED)) {
state.setSuccessful(downloadId);
}
if (!state.isEverythingFinished()) {
return;
}
draw(context, state);
super.process(context, intent);
}

private void draw(Context context, DownloadState state) {
protected void draw() {
cleanup();
if (!state.isEverythingSuccessful()) {
return;
}
View buttonDownload = activityRef.get().findViewById(R.id.download);
buttonDownload.setVisibility(View.GONE);
View buttonInstall = activityRef.get().findViewById(R.id.install);
buttonInstall.setVisibility(View.VISIBLE);
activityRef.get().findViewById(R.id.download).setVisibility(View.GONE);
activityRef.get().findViewById(R.id.install).setVisibility(View.VISIBLE);
boolean installing = !state.getTriggeredBy().equals(DownloadState.TriggeredBy.MANUAL_DOWNLOAD_BUTTON)
&& (PreferenceUtil.getBoolean(context, PreferenceUtil.PREFERENCE_AUTO_INSTALL)
|| PreferenceUtil.getBoolean(context, PreferenceUtil.PREFERENCE_DOWNLOAD_INTERNAL_STORAGE)
&& (PreferenceUtil.getBoolean(activityRef.get(), PreferenceUtil.PREFERENCE_AUTO_INSTALL)
|| PreferenceUtil.getBoolean(activityRef.get(), PreferenceUtil.PREFERENCE_DOWNLOAD_INTERNAL_STORAGE)
)
;
toggle(R.id.install, installing ? R.string.details_installing : R.string.details_install, !installing);
}

private void cleanup() {
protected void cleanup() {
ProgressBar progressBar = activityRef.get().findViewById(R.id.download_progress);
if (null != progressBar) {
progressBar.setVisibility(View.INVISIBLE);
Expand Down
Expand Up @@ -49,6 +49,7 @@ public long enqueue(App app, AndroidAppDeliveryData deliveryData, Type type) {
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
}
long downloadId = dm.enqueue(request);
Log.i(getClass().getSimpleName(), "Download id " + downloadId);
new DownloadManagerProgressUpdater(downloadId, this).execute(PROGRESS_INTERVAL);
return downloadId;
}
Expand Down
Expand Up @@ -29,6 +29,7 @@ public long enqueue(App app, AndroidAppDeliveryData deliveryData, Type type) {
Log.i(getClass().getSimpleName(), "Downloading " + type.name() + " for " + app.getPackageName());
String url = getUrl(deliveryData, type);
long downloadId = url.hashCode();
Log.i(getClass().getSimpleName(), "Download id " + downloadId);
statuses.put(downloadId, DownloadManagerInterface.IN_PROGRESS);
DownloadState.get(app.getPackageName()).setStarted(downloadId);

Expand Down
@@ -1,43 +1,38 @@
package com.github.yeriomin.yalpstore;

import android.util.Pair;
import android.view.View;
import android.widget.ProgressBar;

import java.lang.ref.WeakReference;

public class DownloadProgressBarUpdater extends RepeatingTask {
public class DownloadProgressBarUpdater extends DownloadProgressUpdater {

private String packageName;
private WeakReference<ProgressBar> progressBarRef = new WeakReference<>(null);

public DownloadProgressBarUpdater(String packageName, ProgressBar progressBar) {
this.packageName = packageName;
super(packageName);
progressBarRef = new WeakReference<>(progressBar);
}

@Override
protected boolean shouldRunAgain() {
DownloadState state = DownloadState.get(packageName);
return null != state && !state.isEverythingFinished();
protected void setProgress(int progress, int max) {
ProgressBar progressBar = progressBarRef.get();
if (null == progressBar) {
return;
}
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
progressBar.setMax(max);
}

@Override
protected void payload() {
protected void finish() {
ProgressBar progressBar = progressBarRef.get();
if (null == progressBar) {
return;
}
DownloadState state = DownloadState.get(packageName);
if (null == state || state.isEverythingFinished()) {
progressBar.setVisibility(View.INVISIBLE);
progressBar.setIndeterminate(true);
return;
}
Pair<Integer, Integer> progress = state.getProgress();
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(progress.first);
progressBar.setMax(progress.second);
progressBar.setVisibility(View.INVISIBLE);
progressBar.setIndeterminate(true);
}
}
@@ -0,0 +1,32 @@
package com.github.yeriomin.yalpstore;

import android.util.Pair;

abstract public class DownloadProgressUpdater extends RepeatingTask {

private String packageName;

abstract protected void setProgress(int progress, int max);
abstract protected void finish();

public DownloadProgressUpdater(String packageName) {
this.packageName = packageName;
}

@Override
protected boolean shouldRunAgain() {
DownloadState state = DownloadState.get(packageName);
return null != state && !state.isEverythingFinished();
}

@Override
protected void payload() {
DownloadState state = DownloadState.get(packageName);
if (null == state || state.isEverythingFinished()) {
finish();
} else {
Pair<Integer, Integer> progress = state.getProgress();
setProgress(progress.first, progress.second);
}
}
}
@@ -0,0 +1,25 @@
package com.github.yeriomin.yalpstore;

import android.widget.ProgressBar;

import com.github.yeriomin.yalpstore.view.AppBadge;

public class DownloadProgressUpdaterFactory {

static public DownloadProgressUpdater get(YalpStoreActivity activity, String packageName) {
if (activity instanceof DetailsActivity) {
return get((DetailsActivity) activity, packageName);
} else if (activity instanceof AppListActivity) {
return get((AppListActivity) activity, packageName);
}
return null;
}

static private DownloadProgressUpdater get(DetailsActivity activity, String packageName) {
return new DownloadProgressBarUpdater(packageName, (ProgressBar) activity.findViewById(R.id.download_progress));
}

static private DownloadProgressUpdater get(AppListActivity activity, String packageName) {
return new ListItemDownloadProgressUpdater(packageName, (AppBadge) activity.getListItem(packageName));
}
}
Expand Up @@ -146,7 +146,9 @@ public void setProgress(long downloadId, int complete, int total) {

public void reset() {
progress = new HashMap<>();
status = new HashMap<>();
for (Long downloadId: status.keySet()) {
setCancelled(downloadId);
}
apkChecksum = null;
}
}
Expand Up @@ -37,7 +37,7 @@ protected void loadMore() {
}

@Override
protected ListItem getListItem(App app) {
protected ListItem buildListItem(App app) {
SearchResultAppBadge appBadge = new SearchResultAppBadge();
appBadge.setApp(app);
return appBadge;
Expand Down

0 comments on commit 6cc8b97

Please sign in to comment.