throttle avatar lookup

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
This commit is contained in:
tobiasKaminsky 2019-08-22 10:18:36 +02:00
parent 903b38c18d
commit fa0bd04139
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
5 changed files with 40 additions and 18 deletions

View file

@ -67,6 +67,9 @@ public class ArbitraryDataProvider {
); );
} }
public void storeOrUpdateKeyValue(String accountName, String key, long newValue) {
storeOrUpdateKeyValue(accountName, key, String.valueOf(newValue));
}
public void storeOrUpdateKeyValue(String accountName, String key, String newValue) { public void storeOrUpdateKeyValue(String accountName, String key, String newValue) {
ArbitraryDataSet data = getArbitraryDataSet(accountName, key); ArbitraryDataSet data = getArbitraryDataSet(accountName, key);
@ -106,9 +109,8 @@ public class ArbitraryDataProvider {
} }
} }
public Long getLongValue(String accountName, String key) {
public Long getLongValue(Account account, String key) { String value = getValue(accountName, key);
String value = getValue(account, key);
if (value.isEmpty()) { if (value.isEmpty()) {
return -1l; return -1l;
@ -117,6 +119,11 @@ public class ArbitraryDataProvider {
} }
} }
public Long getLongValue(Account account, String key) {
return getLongValue(account.name, key);
}
public boolean getBooleanValue(String accountName, String key) { public boolean getBooleanValue(String accountName, String key) {
return TRUE.equalsIgnoreCase(getValue(accountName, key)); return TRUE.equalsIgnoreCase(getValue(accountName, key));
} }

View file

@ -89,6 +89,7 @@ public final class ThumbnailsCacheManager {
private static final String PNG_MIMETYPE = "image/png"; private static final String PNG_MIMETYPE = "image/png";
private static final String CACHE_FOLDER = "thumbnailCache"; private static final String CACHE_FOLDER = "thumbnailCache";
public static final String AVATAR = "avatar"; public static final String AVATAR = "avatar";
private static final String AVATAR_TIMESTAMP = "avatarTimestamp";
private static final String ETAG = "ETag"; private static final String ETAG = "ETag";
private static final Object mThumbnailsDiskCacheLock = new Object(); private static final Object mThumbnailsDiskCacheLock = new Object();
@ -868,28 +869,29 @@ public final class ThumbnailsCacheManager {
private @NotNull private @NotNull
Drawable doAvatarInBackground() { Drawable doAvatarInBackground() {
Bitmap avatar = null; Bitmap avatar;
String accountName = mUserId + "@" + mServerName; String accountName = mUserId + "@" + mServerName;
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(mContext.getContentResolver()); ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(mContext.getContentResolver());
String eTag = arbitraryDataProvider.getValue(accountName, ThumbnailsCacheManager.AVATAR); String eTag = arbitraryDataProvider.getValue(accountName, ThumbnailsCacheManager.AVATAR);
long timestamp = arbitraryDataProvider.getLongValue(accountName, ThumbnailsCacheManager.AVATAR_TIMESTAMP);
String avatarKey = "a_" + mUserId + "_" + mServerName + "_" + eTag; String avatarKey = "a_" + mUserId + "_" + mServerName + "_" + eTag;
avatar = getBitmapFromDiskCache(avatarKey);
int px = getAvatarDimension(); // Download avatar from server, only if older than 60 min or avatar does not exist
if ((System.currentTimeMillis() - timestamp >= 60 * 60 * 1000 || avatar == null) && mClient != null) {
// Download avatar from server
if (mClient != null) {
GetMethod get = null; GetMethod get = null;
try { try {
int px = getAvatarDimension();
String uri = mClient.getBaseUri() + "/index.php/avatar/" + Uri.encode(mUserId) + "/" + px; String uri = mClient.getBaseUri() + "/index.php/avatar/" + Uri.encode(mUserId) + "/" + px;
Log_OC.d("Avatar", "URI: " + uri); Log_OC.d("Avatar", "URI: " + uri);
get = new GetMethod(uri); get = new GetMethod(uri);
// only use eTag if available and corresponding avatar is still there // only use eTag if available and corresponding avatar is still there
// (might be deleted from cache) // (might be deleted from cache)
if (!eTag.isEmpty() && getBitmapFromDiskCache(avatarKey) != null) { if (!eTag.isEmpty() && avatar != null) {
get.setRequestHeader("If-None-Match", eTag); get.setRequestHeader("If-None-Match", eTag);
} }
@ -916,6 +918,9 @@ public final class ThumbnailsCacheManager {
avatar = handlePNG(avatar, px, px); avatar = handlePNG(avatar, px, px);
String newImageKey = "a_" + mUserId + "_" + mServerName + "_" + newETag; String newImageKey = "a_" + mUserId + "_" + mServerName + "_" + newETag;
addBitmapToCache(newImageKey, avatar); addBitmapToCache(newImageKey, avatar);
arbitraryDataProvider.storeOrUpdateKeyValue(accountName,
ThumbnailsCacheManager.AVATAR_TIMESTAMP,
System.currentTimeMillis());
} else { } else {
return TextDrawable.createAvatar(mAccount, mAvatarRadius); return TextDrawable.createAvatar(mAccount, mAvatarRadius);
} }
@ -923,10 +928,11 @@ public final class ThumbnailsCacheManager {
case HttpStatus.SC_NOT_MODIFIED: case HttpStatus.SC_NOT_MODIFIED:
// old avatar // old avatar
avatar = getBitmapFromDiskCache(avatarKey);
mClient.exhaustResponse(get.getResponseBodyAsStream()); mClient.exhaustResponse(get.getResponseBodyAsStream());
arbitraryDataProvider.storeOrUpdateKeyValue(accountName,
ThumbnailsCacheManager.AVATAR_TIMESTAMP,
System.currentTimeMillis());
break; break;
default: default:
// everything else // everything else
mClient.exhaustResponse(get.getResponseBodyAsStream()); mClient.exhaustResponse(get.getResponseBodyAsStream());

View file

@ -42,7 +42,6 @@ import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.UploadFileOperation; import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.services.OperationsService; import com.owncloud.android.services.OperationsService;
import com.owncloud.android.ui.activity.ContactsPreferenceActivity;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
@ -55,6 +54,8 @@ import java.util.List;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import static com.owncloud.android.ui.activity.ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP;
/** /**
* Job that backup contacts to /Contacts-Backup and deletes files older than x days * Job that backup contacts to /Contacts-Backup and deletes files older than x days
*/ */
@ -81,9 +82,13 @@ public class ContactsBackupJob extends Job {
final Account account = accountManager.getAccountByName(bundle.getString(ACCOUNT, "")); final Account account = accountManager.getAccountByName(bundle.getString(ACCOUNT, ""));
if (account == null) {
return Result.FAILURE;
}
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver()); ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContext().getContentResolver());
Long lastExecution = arbitraryDataProvider.getLongValue(account, Long lastExecution = arbitraryDataProvider.getLongValue(account,
ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP); PREFERENCE_CONTACTS_LAST_BACKUP);
if (force || (lastExecution + 24 * 60 * 60 * 1000) < Calendar.getInstance().getTimeInMillis()) { if (force || (lastExecution + 24 * 60 * 60 * 1000) < Calendar.getInstance().getTimeInMillis()) {
Log_OC.d(TAG, "start contacts backup job"); Log_OC.d(TAG, "start contacts backup job");
@ -102,8 +107,8 @@ public class ContactsBackupJob extends Job {
// store execution date // store execution date
arbitraryDataProvider.storeOrUpdateKeyValue(account.name, arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
ContactsPreferenceActivity.PREFERENCE_CONTACTS_LAST_BACKUP, PREFERENCE_CONTACTS_LAST_BACKUP,
String.valueOf(Calendar.getInstance().getTimeInMillis())); Calendar.getInstance().getTimeInMillis());
} else { } else {
Log_OC.d(TAG, "last execution less than 24h ago"); Log_OC.d(TAG, "last execution less than 24h ago");
} }

View file

@ -411,7 +411,7 @@ public class PreviewImageActivity extends FileActivity implements
} }
// Call to reset image zoom to initial state // Call to reset image zoom to initial state
((PreviewImagePagerAdapter) mViewPager.getAdapter()).resetZoom(); // ((PreviewImagePagerAdapter) mViewPager.getAdapter()).resetZoom();
} }
} }

View file

@ -97,6 +97,7 @@ import pl.droidsonroids.gif.GifDrawable;
public class PreviewImageFragment extends FileFragment implements Injectable { public class PreviewImageFragment extends FileFragment implements Injectable {
private static final String EXTRA_FILE = "FILE"; private static final String EXTRA_FILE = "FILE";
private static final String EXTRA_ZOOM = "ZOOM";
private static final String ARG_FILE = "FILE"; private static final String ARG_FILE = "FILE";
private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST"; private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
@ -219,8 +220,9 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) { if (savedInstanceState != null) {
if (!mIgnoreFirstSavedState) { if (!mIgnoreFirstSavedState) {
OCFile file = savedInstanceState.getParcelable(PreviewImageFragment.EXTRA_FILE); OCFile file = savedInstanceState.getParcelable(EXTRA_FILE);
setFile(file); setFile(file);
mImageView.setScale(savedInstanceState.getFloat(EXTRA_ZOOM));
} else { } else {
mIgnoreFirstSavedState = false; mIgnoreFirstSavedState = false;
} }
@ -230,7 +232,9 @@ public class PreviewImageFragment extends FileFragment implements Injectable {
@Override @Override
public void onSaveInstanceState(@NonNull Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelable(PreviewImageFragment.EXTRA_FILE, getFile());
outState.putFloat(EXTRA_ZOOM, mImageView.getScale());
outState.putParcelable(EXTRA_FILE, getFile());
} }
@Override @Override