Skip to content

Commit

Permalink
Better display of download hint
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantner committed Sep 24, 2017
1 parent 40c64af commit 43a5ff7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class MainActivity extends AppCompatActivity
private String cameraPictureFilepath = "";
String[] _tagKeys, _tagValues;
private int _currentMainMode = 0;
private long _lastInfoBarTextShownAt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -502,14 +503,19 @@ public boolean dispatchTouchEvent(MotionEvent event) {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case AppCast.DOWNLOAD_REQUEST_RESULT.ACTION: {
switch (intent.getIntExtra(AppCast.DOWNLOAD_REQUEST_RESULT.EXTRA_RESULT, AssetUpdater.UpdateThread.DOWNLOAD_REQUEST_RESULT__FAILED)) {
case AssetUpdater.UpdateThread.DOWNLOAD_REQUEST_RESULT__FAILED: {
updateInfoBar(0, R.string.downloading_failed, R.drawable.ic_file_download_white_32dp, 1000);
case AppCast.ASSET_DOWNLOAD_REQUEST.ACTION: {

switch (intent.getIntExtra(AppCast.ASSET_DOWNLOAD_REQUEST.EXTRA_RESULT, AssetUpdater.UpdateThread.ASSET_DOWNLOAD_REQUEST__FAILED)) {
case AssetUpdater.UpdateThread.ASSET_DOWNLOAD_REQUEST__CHECKING: {
updateInfoBar(0, R.string.checking_assets_for_update, R.drawable.ic_file_download_white_32dp, false);
break;
}
case AssetUpdater.UpdateThread.ASSET_DOWNLOAD_REQUEST__FAILED: {
updateInfoBar(0, R.string.downloading_failed, R.drawable.ic_file_download_white_32dp, false);
break;
}
case AssetUpdater.UpdateThread.DOWNLOAD_REQUEST_RESULT__DO_DOWNLOAD_ASK: {
updateInfoBar(0, R.string.downloading, R.drawable.ic_file_download_white_32dp, 1);
case AssetUpdater.UpdateThread.ASSET_DOWNLOAD_REQUEST__DO_DOWNLOAD_ASK: {
updateInfoBar(0, R.string.checking_assets_for_update, R.drawable.ic_file_download_white_32dp, false);
showDownloadDialog();
break;
}
Expand All @@ -520,19 +526,19 @@ public void onReceive(Context context, Intent intent) {
int percent = intent.getIntExtra(AppCast.DOWNLOAD_STATUS.EXTRA_PERCENT, 100);
switch (intent.getIntExtra(AppCast.DOWNLOAD_STATUS.EXTRA_STATUS, AssetUpdater.UpdateThread.DOWNLOAD_STATUS__FAILED)) {
case AssetUpdater.UpdateThread.DOWNLOAD_STATUS__DOWNLOADING: {
updateInfoBar(percent, R.string.downloading, R.drawable.ic_file_download_white_32dp, -1);
updateInfoBar(percent, R.string.downloading, R.drawable.ic_file_download_white_32dp, true);
break;
}
case AssetUpdater.UpdateThread.DOWNLOAD_STATUS__FAILED: {
updateInfoBar(percent, R.string.downloading_failed, R.drawable.ic_mood_bad_black_256dp, 2000);
updateInfoBar(percent, R.string.downloading_failed, R.drawable.ic_mood_bad_black_256dp, false);
break;
}
case AssetUpdater.UpdateThread.DOWNLOAD_STATUS__UNZIPPING: {
updateInfoBar(percent, R.string.unzipping, R.drawable.ic_file_download_white_32dp, -1);
updateInfoBar(percent, R.string.unzipping, R.drawable.ic_file_download_white_32dp, true);
break;
}
case AssetUpdater.UpdateThread.DOWNLOAD_STATUS__FINISHED: {
updateInfoBar(percent, R.string.downloading_success, R.drawable.ic_gavel_white_48px, 2000);
updateInfoBar(percent, R.string.downloading_success, R.drawable.ic_gavel_white_48px, false);
break;
}
}
Expand Down Expand Up @@ -560,17 +566,18 @@ public void onClick(DialogInterface dialogInterface, int i) {
dialog.show();
}

public void updateInfoBar(Integer percent, @StringRes Integer textResId, @DrawableRes Integer image, int hideInMillis) {
public void updateInfoBar(Integer percent, @StringRes Integer textResId, @DrawableRes Integer image, final boolean showlong) {
_lastInfoBarTextShownAt = System.currentTimeMillis();
_infoBar.setVisibility(View.VISIBLE);
if (hideInMillis >= 0) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if ((System.currentTimeMillis() - _lastInfoBarTextShownAt) > (showlong ? 20 : 2) * 1000) {
_infoBar.setVisibility(View.GONE);
}
}, hideInMillis);
}
}
}, (showlong ? 20 : 2) * 1000 + 100);
if (percent != null) {
_infoBarProgressBar.setProgress(percent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public static File getMemesDir(AppSettings appSettings) {
}

public static class UpdateThread extends Thread {
public static final int DOWNLOAD_REQUEST_RESULT__FAILED = -1;
public static final int DOWNLOAD_REQUEST_RESULT__DO_DOWNLOAD_ASK = 1;
public static final int ASSET_DOWNLOAD_REQUEST__FAILED = -1;
public static final int ASSET_DOWNLOAD_REQUEST__CHECKING = 1;
public static final int ASSET_DOWNLOAD_REQUEST__DO_DOWNLOAD_ASK = 2;
public static final int DOWNLOAD_STATUS__DOWNLOADING = 1;
public static final int DOWNLOAD_STATUS__UNZIPPING = 2;
public static final int DOWNLOAD_STATUS__FINISHED = 3;
Expand All @@ -83,6 +84,7 @@ public UpdateThread(Context context, boolean doDownload) {
@Override
public void run() {
if (PermissionChecker.hasExtStoragePerm(_context)) {
AppCast.ASSET_DOWNLOAD_REQUEST.send(_context, ASSET_DOWNLOAD_REQUEST__CHECKING);
String apiJsonS = NetworkUtils.performCall(URL_API, NetworkUtils.GET);
try {
JSONObject apiJson = new JSONObject(apiJsonS);
Expand All @@ -92,24 +94,22 @@ public void run() {
if (date.after(_appSettings.getLastAssetArchiveDate())) {
_appSettings.setLastArchiveCheckDate(new Date(System.currentTimeMillis()));
if (!_doDownload) {
AppCast.DOWNLOAD_REQUEST_RESULT.send(_context, DOWNLOAD_REQUEST_RESULT__DO_DOWNLOAD_ASK);
return;
AppCast.ASSET_DOWNLOAD_REQUEST.send(_context, ASSET_DOWNLOAD_REQUEST__DO_DOWNLOAD_ASK);
} else {
doDownload(date);
new LoadAssetsThread(_context).start();
return;
}
} else {
return;
}
return;
} catch (JSONException | ParseException e) {
e.printStackTrace();
}
}
AppCast.DOWNLOAD_REQUEST_RESULT.send(_context, DOWNLOAD_REQUEST_RESULT__FAILED);
AppCast.ASSET_DOWNLOAD_REQUEST.send(_context, ASSET_DOWNLOAD_REQUEST__FAILED);
}


@SuppressWarnings("ResultOfMethodCallIgnored")
private synchronized void doDownload(Date date) throws ParseException {
if (_isAlreadyDownloading || date.before(_appSettings.getLastAssetArchiveDate())) {
return;
Expand All @@ -121,23 +121,32 @@ private synchronized void doDownload(Date date) throws ParseException {
MemeData.getImages().clear();
MemeData.clearImagesWithTags();
FileUtils.deleteRecursive(file);
boolean ok;
if (file.mkdirs() && (templatesDir.exists() || templatesDir.mkdirs())) {
boolean ok = false;
if ((file.exists() || file.mkdirs()) && (templatesDir.exists() || templatesDir.mkdirs())) {
// Download
_lastPercent = -1;
AppCast.DOWNLOAD_STATUS.send(_context, DOWNLOAD_STATUS__DOWNLOADING, 0);
file = new File(file, FORMAT_MINUTE.format(date) + ".memetastic.zip");
ok = NetworkUtils.downloadFile(URL_ARCHIVE_ZIP, file, new Callback<Float>() {
public void onCallback(Float aFloat) {
if (_lastPercent != (int) (aFloat * 100)) {
int perc = (int) (aFloat * 100);
if (_lastPercent != perc) {
_lastPercent = (perc);
AppCast.DOWNLOAD_STATUS.send(_context, DOWNLOAD_STATUS__DOWNLOADING, _lastPercent * 3 / 4);
_lastPercent = (int) (aFloat * 100);
}
}
});

// Unpack
_lastPercent = -1;
AppCast.DOWNLOAD_STATUS.send(_context, DOWNLOAD_STATUS__UNZIPPING, 75);
if (ok) {
ok = ZipUtils.unzip(file, templatesDir, true, new Callback<Float>() {
public void onCallback(Float aFloat) {
if (_lastPercent != (int) (aFloat * 100)) {
AppCast.DOWNLOAD_STATUS.send(_context, DOWNLOAD_STATUS__UNZIPPING, 50 + _lastPercent / 4);
_lastPercent = (int) (aFloat * 100);
int perc = (int) (aFloat * 100);
if (_lastPercent != perc) {
_lastPercent = perc;
AppCast.DOWNLOAD_STATUS.send(_context, DOWNLOAD_STATUS__UNZIPPING, 75 + _lastPercent / 4);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;

import java.io.File;

import io.github.gsantner.memetastic.R;
import io.github.gsantner.memetastic.data.MemeLibConfig;
import io.github.gsantner.memetastic.util.AppSettings;
import io.github.gsantner.memetastic.util.ContextUtils;
Expand Down Expand Up @@ -36,15 +38,19 @@ private Bitmap loadStorageImage(File pathToImage) {
File cacheFile = new File(_context.getCacheDir(), pathToImage.getAbsolutePath().substring(1));
ContextUtils cu = ContextUtils.get();
Bitmap bitmap;
if (_loadThumbnail) {
if (cacheFile.exists()) {
bitmap = cu.loadImageFromFilesystem(cacheFile, _maxSize);
try {
if (_loadThumbnail) {
if (cacheFile.exists()) {
bitmap = cu.loadImageFromFilesystem(cacheFile, _maxSize);
} else {
bitmap = cu.loadImageFromFilesystem(pathToImage, _maxSize);
cu.writeImageToFileDetectFormat(cacheFile, bitmap, 65);
}
} else {
bitmap = cu.loadImageFromFilesystem(pathToImage, _maxSize);
cu.writeImageToFileDetectFormat(cacheFile, bitmap, 80);
}
} else {
bitmap = cu.loadImageFromFilesystem(pathToImage, _maxSize);
} catch (NullPointerException nul) {
bitmap = cu.drawableToBitmap(ContextCompat.getDrawable(_context, R.drawable.ic_mood_bad_black_256dp));
}

return bitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static void sendBroadcast(Context c, Intent i) {
public static IntentFilter getLocalBroadcastFilter() {
IntentFilter intentFilter = new IntentFilter();
String[] BROADCAST_ACTIONS = {
DOWNLOAD_REQUEST_RESULT.ACTION,
ASSET_DOWNLOAD_REQUEST.ACTION,
DOWNLOAD_STATUS.ACTION,
ASSETS_LOADED.ACTION
};
Expand All @@ -34,8 +34,8 @@ public static IntentFilter getLocalBroadcastFilter() {
//########################
//## Actions
//########################
public static class DOWNLOAD_REQUEST_RESULT {
public static final String ACTION = "DOWNLOAD_REQUEST_RESULT";
public static class ASSET_DOWNLOAD_REQUEST {
public static final String ACTION = "ASSET_DOWNLOAD_REQUEST";
public static final String EXTRA_RESULT = "EXTRA_RESULT";

public static void send(Context c, int result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int getThumbnailQualityReal() {
// Additional reduction of quality to ~2/3 is roughly 225
// 150 is very fast loaded, but blurry, 200 is still a little blurry, 225 seems to be
// a good tradeoff between quality (400-600) and speed (-125)
int val = getInt(R.string.pref_key__thumbnail_quality__percent, 24);
int val = getInt(R.string.pref_key__thumbnail_quality__percent, 19);
return (int) (100 + (939 * (val / 100.0)));
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
<string name="downloading_failed">Downloading failed</string>
<string name="unzipping">Unzipping</string>
<string name="downloading_success">Sucessfully downloaded</string>
<string name="download_latest_assets_message">This will download fonts and meme templates (about 20 MB data)</string>
<string name="download_latest_assets_message">This will download fonts and meme templates (about 20 MB data).</string>
<string name="download_latest_assets_message_more">In settings you can manually get the latest assets later too.</string>
<string name="download_latest_assets_title">Download latest assets</string>
<string name="checking_assets_for_update">Checking if there are more recent assets available</string>
</resources>

0 comments on commit 43a5ff7

Please sign in to comment.