Skip to content

Commit

Permalink
Avatars: update when changed in server, full load in background, sele…
Browse files Browse the repository at this point in the history
…ctive server fetches, refactoring and clean-up
  • Loading branch information
davivel committed Oct 18, 2016
1 parent 328f4de commit 7748321
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 237 deletions.
3 changes: 2 additions & 1 deletion res/layout/account_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
>
Expand All @@ -31,7 +32,7 @@
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/standard_margin"
android:layout_marginStart="@dimen/standard_margin"
android:src="@drawable/ic_menu_archive"
android:src="@drawable/ic_account_plus"
/>

<ImageView
Expand Down
227 changes: 161 additions & 66 deletions src/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions src/com/owncloud/android/datamodel/UserProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.owncloud.android.datamodel;

import android.support.annotation.Nullable;

/**
* ownCloud Android client application
*
* @author David A. Velasco
* Copyright (C) 2016 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
public class UserProfile {

private long mId;
private String mAccountName;

private String mUserId;
private String mDisplayName = "";
private String mEmail = "";

private UserAvatar mAvatar;

public UserProfile(String accountName, String userId, String displayName, String email) {
mAccountName = accountName;
mUserId = userId;
mDisplayName = displayName;
mEmail = email;

mAvatar = null;
}

public String getAccountName() {
return mAccountName;
}

public String getUserId() {
return mUserId;
}

public String getDisplayName() {
return mDisplayName;
}

public String getEmail() {
return mEmail;
}

@Nullable
public UserAvatar getAvatar() {
return mAvatar;
}

public void setAvatar(UserAvatar avatar) {
mAvatar = avatar;
}

public static class UserAvatar {

private String mCacheKey;
private String mMimeType;
private String mEtag;

public UserAvatar(String cacheKey, String mimeType, String etag) {
mCacheKey = cacheKey;
mMimeType = mimeType;
mEtag = etag;
}

public String getCacheKey() {
return mCacheKey;
}

public String getMimeType() {
return mMimeType;
}

public String getEtag() {
return mEtag;
}
}
}
194 changes: 194 additions & 0 deletions src/com/owncloud/android/datamodel/UserProfilesRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package com.owncloud.android.datamodel;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.owncloud.android.MainApp;
import com.owncloud.android.db.ProviderMeta;
import com.owncloud.android.lib.common.utils.Log_OC;

import java.io.File;

/**
* ownCloud Android client application
*
* Copyright (C) 2016 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/

/**
* Minimum to get things working.
*
* Working around FileContentProvider, we have no interest in exporting user profiles to other apps.
*/
public class UserProfilesRepository {

private static final String TAG = UserProfilesRepository.class.getName();

private SQLiteDatabase mDb;

public UserProfilesRepository() {
File dbFile = MainApp.getAppContext().getDatabasePath(ProviderMeta.DB_NAME);
mDb = SQLiteDatabase.openDatabase(
dbFile.getAbsolutePath(),
null,
SQLiteDatabase.OPEN_READWRITE
);
}

/**
* Persist a user profile.
*
* Minimum to get things working: only storing info about avatar.
*
* Working around ContentProvider
*
* @param userProfile User profile.
*/
public void update(UserProfile userProfile) {

if (userProfile == null) {
throw new IllegalArgumentException("Received userProfile with NULL value");
}

if (userProfile.getAvatar() != null) {
// map avatar properties to columns
ContentValues avatarValues = new ContentValues();
avatarValues.put(
ProviderMeta.ProviderTableMeta.USER_AVATARS__ACCOUNT_NAME,
userProfile.getAccountName()
);
avatarValues.put(
ProviderMeta.ProviderTableMeta.USER_AVATARS__CACHE_KEY,
userProfile.getAvatar().getCacheKey()
);
avatarValues.put(
ProviderMeta.ProviderTableMeta.USER_AVATARS__ETAG,
userProfile.getAvatar().getEtag()
);
avatarValues.put(
ProviderMeta.ProviderTableMeta.USER_AVATARS__MIME_TYPE,
userProfile.getAvatar().getMimeType()
);

mDb.beginTransaction();
try {
if (avatarExists(userProfile)) {
// not new, UPDATE
int count = mDb.update(
ProviderMeta.ProviderTableMeta.USER_AVATARS__TABLE_NAME,
avatarValues,
ProviderMeta.ProviderTableMeta.USER_AVATARS__ACCOUNT_NAME + "=?",
new String[]{String.valueOf(userProfile.getAccountName())}
);
Log_OC.d(TAG, "Avatar updated");

} else {
// new, CREATE
mDb.insert(
ProviderMeta.ProviderTableMeta.USER_AVATARS__TABLE_NAME,
null,
avatarValues
);
Log_OC.d(TAG, "Avatar inserted");
}
mDb.setTransactionSuccessful();

} finally {
mDb.endTransaction();
}
}
}

/**
* Gets the information about a user avatar bound to an OC account.
*
* Shortcut method prevent retrieving a full {@link UserProfile},
* specially now that {@link UserProfile}s are not really stored. Naughty trick.
*
* @param accountName Name of an OC account.
* @return Information about a user avatar bound to an OC account, or NULL if
* there is no avatar for the given account.
*/
public UserProfile.UserAvatar getAvatar(String accountName) {
UserProfile.UserAvatar avatar = null;
Cursor c = null;
try {
c = mDb.query(
ProviderMeta.ProviderTableMeta.USER_AVATARS__TABLE_NAME,
null,
ProviderMeta.ProviderTableMeta.USER_AVATARS__ACCOUNT_NAME + "=?",
new String[]{accountName},
null, null, null
);
if (c != null && c.moveToFirst()) {
avatar = new UserProfile.UserAvatar(
c.getString(c.getColumnIndex(
ProviderMeta.ProviderTableMeta.USER_AVATARS__CACHE_KEY
)),
c.getString(c.getColumnIndex(
ProviderMeta.ProviderTableMeta.USER_AVATARS__MIME_TYPE
)),
c.getString(
c.getColumnIndex(ProviderMeta.ProviderTableMeta.USER_AVATARS__ETAG
))
);
} // else, no avatar to return
} catch (Exception e) {
Log_OC.e(TAG, "Exception while querying avatar", e);
} finally {
if (c != null) {
c.close();
}
}
return avatar;
}

public void deleteAvatar(String accountName) {
try {
mDb.delete(
ProviderMeta.ProviderTableMeta.USER_AVATARS__TABLE_NAME,
ProviderMeta.ProviderTableMeta.USER_AVATARS__ACCOUNT_NAME + "=?",
new String[]{String.valueOf(accountName)}
);
Log_OC.d(TAG, "Avatar deleted");

} catch (Exception e) {
Log_OC.e(TAG, "Exception while deleting avatar", e);
}
}

private boolean avatarExists(UserProfile userProfile) {
boolean exists = false;
Cursor c = null;
try {
c = mDb.query(
ProviderMeta.ProviderTableMeta.USER_AVATARS__TABLE_NAME,
null,
ProviderMeta.ProviderTableMeta.USER_AVATARS__ACCOUNT_NAME + "=?",
new String[]{userProfile.getAccountName()},
null, null, null
);
exists = (c != null && c.moveToFirst());
} finally {
if (c != null) {
c.close();
}
}
return exists;
}

}
10 changes: 9 additions & 1 deletion src/com/owncloud/android/db/ProviderMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class ProviderMeta {

public static final String DB_NAME = "filelist";
public static final int DB_VERSION = 14;
public static final int DB_VERSION = 15;

private ProviderMeta() {
}
Expand All @@ -43,6 +43,8 @@ static public class ProviderTableMeta implements BaseColumns {
public static final String OCSHARES_TABLE_NAME = "ocshares";
public static final String CAPABILITIES_TABLE_NAME = "capabilities";
public static final String UPLOADS_TABLE_NAME = "list_of_uploads";
public static final String USER_AVATARS__TABLE_NAME = "user_avatars";

public static final Uri CONTENT_URI = Uri.parse("content://"
+ MainApp.getAuthority() + "/");
public static final Uri CONTENT_URI_FILE = Uri.parse("content://"
Expand Down Expand Up @@ -151,5 +153,11 @@ static public class ProviderTableMeta implements BaseColumns {

public static final String UPLOADS_DEFAULT_SORT_ORDER = ProviderTableMeta._ID + " collate nocase desc";


// Columns of user_avatars table
public static final String USER_AVATARS__ACCOUNT_NAME = "account_name";
public static final String USER_AVATARS__CACHE_KEY = "cache_key";
public static final String USER_AVATARS__ETAG = "etag";
public static final String USER_AVATARS__MIME_TYPE = "mime_type";
}
}
Loading

0 comments on commit 7748321

Please sign in to comment.