Skip to content

Commit

Permalink
Migrate simple cases of getCurrentAccount() to getUser()
Browse files Browse the repository at this point in the history
Migrate trivially convertible uses of getCurrentAccount()
to new user model - getUser().

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
  • Loading branch information
ezaquarii committed Nov 25, 2019
1 parent c8eba1d commit a7eb714
Show file tree
Hide file tree
Showing 43 changed files with 445 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal class AnonymousUser(private val accountType: String) : User {

override val accountName: String = "anonymous"
override val server = Server(URI.create(""), MainApp.MINIMUM_SUPPORTED_SERVER_VERSION)
override val isAnonymous = true

override fun toPlatformAccount(): Account {
return Account(accountName, accountType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal class RegisteredUser(
private val ownCloudAccount: OwnCloudAccount,
override val server: Server
) : User {
override val isAnonymous = false

override val accountName: String get() {
return account.name
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/nextcloud/client/account/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.owncloud.android.lib.common.OwnCloudAccount
interface User {
val accountName: String
val server: Server
val isAnonymous: Boolean

/**
* This is temporary helper method created to facilitate incremental refactoring.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/nextcloud/client/account/UserAccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,24 @@ public interface UserAccountManager extends CurrentAccountProvider {
* @return Version of the OC server corresponding to account, according to the data saved
* in the system AccountManager
*/
@Deprecated
@NonNull
OwnCloudVersion getServerVersion(Account account);

@Deprecated
boolean isSearchSupported(@Nullable Account account);

/**
* Check if user's account supports media streaming. This is a property of server where user has his account.
*
* @deprecated Please use {@link OwnCloudVersion#isMediaStreamingSupported()} directly,
* obtainable from {@link User#getServer()} and {@link Server#getVersion()}
*
* @param account Account used to perform {@link android.accounts.AccountManager} lookup.
*
* @return true is server supports media streaming, false otherwise
*/
@Deprecated
boolean isMediaStreamingSupported(@Nullable Account account);

void resetOwnCloudAccount();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/nextcloud/client/di/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.nextcloud.client.logger.Logger;
import com.nextcloud.client.logger.LoggerImpl;
import com.nextcloud.client.logger.LogsRepository;
import com.nextcloud.client.network.ClientFactory;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.ui.activities.data.activities.ActivitiesRepository;
Expand Down Expand Up @@ -106,8 +107,8 @@ ActivitiesRepository activitiesRepository(ActivitiesServiceApi api) {
}

@Provides
FilesRepository filesRepository(UserAccountManager accountManager) {
return new RemoteFilesRepository(new FilesServiceApiImpl(accountManager));
FilesRepository filesRepository(UserAccountManager accountManager, ClientFactory clientFactory) {
return new RemoteFilesRepository(new FilesServiceApiImpl(accountManager, clientFactory));
}

@Provides
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/nextcloud/client/network/ClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.app.Activity;
import android.net.Uri;

import com.nextcloud.client.account.User;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.accounts.AccountUtils;

Expand All @@ -35,10 +36,31 @@

public interface ClientFactory {

/**
* This exception wraps all possible errors thrown by trigger-happy
* OwnCloudClient constructor, making try-catch blocks manageable.
*
* This is a temporary refactoring measure, until a better
* error handling method can be procured.
*/
@Deprecated
class CreationException extends Exception {

private static final long serialVersionUID = 0L;

CreationException(Throwable t) {
super(t);
}
}

OwnCloudClient create(User user) throws CreationException;

@Deprecated
OwnCloudClient create(Account account)
throws OperationCanceledException, AuthenticatorException, IOException,
AccountUtils.AccountNotFoundException;

@Deprecated
OwnCloudClient create(Account account, Activity currentActivity)
throws OperationCanceledException, AuthenticatorException, IOException,
AccountUtils.AccountNotFoundException;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/nextcloud/client/network/ClientFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Context;
import android.net.Uri;

import com.nextcloud.client.account.User;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils;
Expand All @@ -41,6 +42,18 @@ class ClientFactoryImpl implements ClientFactory {
this.context = context;
}

@Override
public OwnCloudClient create(User user) throws CreationException {
try {
return OwnCloudClientFactory.createOwnCloudClient(user.toPlatformAccount(), context);
} catch (OperationCanceledException|
AuthenticatorException|
IOException|
AccountUtils.AccountNotFoundException e) {
throw new CreationException(e);
}
}

@Override
public OwnCloudClient create(Account account)
throws OperationCanceledException, AuthenticatorException, IOException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.SharedPreferences;

import com.nextcloud.client.account.CurrentAccountProvider;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.FileDataStorageManager;
Expand Down Expand Up @@ -282,7 +283,7 @@ public boolean isFingerprintUnlockEnabled() {
@Override
public String getFolderLayout(OCFile folder) {
return getFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
currentAccountProvider.getUser(),
PREF__FOLDER_LAYOUT,
folder,
FOLDER_LAYOUT_LIST);
Expand All @@ -291,7 +292,7 @@ public String getFolderLayout(OCFile folder) {
@Override
public void setFolderLayout(OCFile folder, String layout_name) {
setFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
currentAccountProvider.getUser(),
PREF__FOLDER_LAYOUT,
folder,
layout_name);
Expand All @@ -300,7 +301,7 @@ public void setFolderLayout(OCFile folder, String layout_name) {
@Override
public FileSortOrder getSortOrderByFolder(OCFile folder) {
return FileSortOrder.sortOrders.get(getFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
currentAccountProvider.getUser(),
PREF__FOLDER_SORT_ORDER,
folder,
FileSortOrder.sort_a_to_z.name));
Expand All @@ -309,7 +310,7 @@ public FileSortOrder getSortOrderByFolder(OCFile folder) {
@Override
public void setSortOrder(OCFile folder, FileSortOrder sortOrder) {
setFolderPreference(context,
currentAccountProvider.getCurrentAccount(),
currentAccountProvider.getUser(),
PREF__FOLDER_SORT_ORDER,
folder,
sortOrder.name);
Expand All @@ -322,28 +323,23 @@ public FileSortOrder getSortOrderByType(FileSortOrder.Type type) {

@Override
public FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder) {
Account account = currentAccountProvider.getCurrentAccount();
if (account == null) {
User user = currentAccountProvider.getUser();
if (user.isAnonymous()) {
return defaultOrder;
}

ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());

String value = dataProvider.getValue(account.name, PREF__FOLDER_SORT_ORDER + "_" + type);
String value = dataProvider.getValue(user.getAccountName(), PREF__FOLDER_SORT_ORDER + "_" + type);

return value.isEmpty() ? defaultOrder : FileSortOrder.sortOrders.get(value);
}

@Override
public void setSortOrder(FileSortOrder.Type type, FileSortOrder sortOrder) {
Account account = currentAccountProvider.getCurrentAccount();

if (account == null) {
throw new IllegalArgumentException("Account may not be null!");
}

User user = currentAccountProvider.getUser();
ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
dataProvider.storeOrUpdateKeyValue(account.name, PREF__FOLDER_SORT_ORDER + "_" + type, sortOrder.name);
dataProvider.storeOrUpdateKeyValue(user.getAccountName(), PREF__FOLDER_SORT_ORDER + "_" + type, sortOrder.name);
}

@Override
Expand Down Expand Up @@ -576,22 +572,22 @@ public long getPhotoSearchTimestamp() {
* @return Preference value
*/
private static String getFolderPreference(final Context context,
final Account account,
final User user,
final String preferenceName,
final OCFile folder,
final String defaultValue) {
if (account == null) {
if (user.isAnonymous()) {
return defaultValue;
}

ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(), context.getContentResolver());

String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
String value = dataProvider.getValue(user.getAccountName(), getKeyFromFolder(preferenceName, folder));
OCFile prefFolder = folder;
while (prefFolder != null && value.isEmpty()) {
prefFolder = storageManager.getFileById(prefFolder.getParentId());
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, prefFolder));
value = dataProvider.getValue(user.getAccountName(), getKeyFromFolder(preferenceName, prefFolder));
}
return value.isEmpty() ? defaultValue : value;
}
Expand All @@ -605,16 +601,12 @@ private static String getFolderPreference(final Context context,
* @param value Preference value to set.
*/
private static void setFolderPreference(final Context context,
final Account account,
final User user,
final String preferenceName,
final OCFile folder,
final String value) {
if (account == null) {
throw new IllegalArgumentException("Account may not be null!");
}

ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
dataProvider.storeOrUpdateKeyValue(user.getAccountName(), getKeyFromFolder(preferenceName, folder), value);
}

private static String getKeyFromFolder(String preferenceName, OCFile folder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

import com.blikoon.qrcodescanner.QrCodeActivity;
import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.device.DeviceInfo;
import com.nextcloud.client.di.Injectable;
Expand Down Expand Up @@ -1735,8 +1736,8 @@ protected boolean createAccount(RemoteOperationResult authResult) {
}

/// add the new account as default in preferences, if there is none already
Account defaultAccount = accountManager.getCurrentAccount();
if (defaultAccount == null) {
User defaultAccount = accountManager.getUser();
if (defaultAccount.isAnonymous()) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putString("select_oc_account", accountName);
editor.apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,8 @@ private OCShare createShareInstance(Cursor c) {

private void resetShareFlagsInAllFiles() {
ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
String[] whereArgs = new String[]{account.name};
Expand All @@ -1279,8 +1279,8 @@ private void resetShareFlagsInAllFiles() {

private void resetShareFlagsInFolder(OCFile folder) {
ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?";
String[] whereArgs = new String[]{account.name, String.valueOf(folder.getFileId())};
Expand All @@ -1299,8 +1299,8 @@ private void resetShareFlagsInFolder(OCFile folder) {

private void resetShareFlagInAFile(String filePath) {
ContentValues cv = new ContentValues();
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE);
cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
String[] whereArgs = new String[]{account.name, filePath};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;

import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.FileDataStorageManager;
Expand Down Expand Up @@ -61,8 +62,8 @@ public boolean onCreate() {
}

private OCFile getFile(Uri uri) {
Account account = accountManager.getCurrentAccount();
FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account,
User user = accountManager.getUser();
FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user.toPlatformAccount(),
MainApp.getAppContext().getContentResolver());

return fileDataStorageManager.getFileByPath(uri.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.provider.BaseColumns;
import android.widget.Toast;

import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager;
Expand Down Expand Up @@ -180,18 +181,14 @@ private Cursor searchForUsersOrGroups(Uri uri) {

// need to trust on the AccountUtils to get the current account since the query in the client side is not
// directly started by our code, but from SearchView implementation
Account account = accountManager.getCurrentAccount();

if (account == null) {
throw new IllegalArgumentException("Account may not be null!");
}
User user = accountManager.getUser();

String userQuery = lastPathSegment.toLowerCase(Locale.ROOT);

// request to the OC server about users and groups matching userQuery
GetShareesRemoteOperation searchRequest = new GetShareesRemoteOperation(userQuery, REQUESTED_PAGE,
RESULTS_PER_PAGE);
RemoteOperationResult result = searchRequest.execute(account, getContext());
RemoteOperationResult result = searchRequest.execute(user.toPlatformAccount(), getContext());
List<JSONObject> names = new ArrayList<>();

if (result.isSuccess()) {
Expand All @@ -217,8 +214,10 @@ private Cursor searchForUsersOrGroups(Uri uri) {
Uri remoteBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_REMOTE).build();
Uri emailBaseUri = new Uri.Builder().scheme(CONTENT).authority(DATA_EMAIL).build();

FileDataStorageManager manager = new FileDataStorageManager(account, getContext().getContentResolver());
boolean federatedShareAllowed = manager.getCapability(account.name).getFilesSharingFederationOutgoing()
FileDataStorageManager manager = new FileDataStorageManager(user.toPlatformAccount(),
getContext().getContentResolver());
boolean federatedShareAllowed = manager.getCapability(user.getAccountName())
.getFilesSharingFederationOutgoing()
.isTrue();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ private void setupContent() {
PorterDuff.Mode.SRC_IN);

FileDataStorageManager storageManager = new FileDataStorageManager(getAccount(), getContentResolver());
adapter = new ActivityListAdapter(this, getUserAccountManager(), this, storageManager, getCapabilities(), false);
adapter = new ActivityListAdapter(this, getUserAccountManager(), this, storageManager,
getCapabilities(), false);
recyclerView.setAdapter(adapter);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
Expand Down
Loading

0 comments on commit a7eb714

Please sign in to comment.