Skip to content

Commit bbaf355

Browse files
committed
Create UserAccountManager and move getAccounts()
Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
1 parent 98a134f commit bbaf355

34 files changed

+640
-241
lines changed

findbugs-filter.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
</Match>
2323
<Bug pattern="PATH_TRAVERSAL_IN" />
2424
<Bug pattern="ANDROID_EXTERNAL_FILE_ACCESS" />
25+
26+
<!-- This is unmanageable for now dur to large amount of interconnected static state -->
27+
<Bug pattern="FCCD_FIND_CLASS_CIRCULAR_DEPENDENCY"/>
2528
</FindBugsFilter>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Chris Narkiewicz
5+
* Copyright (C) 2919 Chris Narkiewicz <hello@ezaquarii.com>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
9+
* License as published by the Free Software Foundation; either
10+
* version 3 of the License, or any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public
18+
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package com.nextcloud.client.di;
21+
22+
import dagger.Module;
23+
24+
@Module
25+
abstract class VariantComponentsModule {
26+
}

src/generic/java/com/owncloud/android/utils/PushUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Nextcloud Android client application
33
*
44
* @author Mario Danic
5+
* @author Chris Narkiewicz
56
* Copyright (C) 2017 Mario Danic
7+
* Copyright (C) 2019 Chris Narkiewicz
68
*
79
* This program is free software: you can redistribute it and/or modify
810
* it under the terms of the GNU Affero General Public License as published by
@@ -22,6 +24,7 @@
2224

2325
import android.content.Context;
2426

27+
import com.nextcloud.client.account.UserAccountManager;
2528
import com.owncloud.android.MainApp;
2629
import com.owncloud.android.datamodel.SignatureVerification;
2730
import com.nextcloud.client.preferences.AppPreferencesImpl;
@@ -34,11 +37,11 @@ public final class PushUtils {
3437
private PushUtils() {
3538
}
3639

37-
public static void pushRegistrationToServer(final String pushToken) {
40+
public static void pushRegistrationToServer(final UserAccountManager accountManager, final String pushToken) {
3841
// do nothing
3942
}
4043

41-
public static void reinitKeys() {
44+
public static void reinitKeys(UserAccountManager accountManager) {
4245
Context context = MainApp.getAppContext();
4346
AppPreferencesImpl.fromContext(context).setKeysReInitEnabled();
4447
}
@@ -47,7 +50,12 @@ public static Key readKeyFromFile(boolean readPublicKey) {
4750
return null;
4851
}
4952

50-
public static SignatureVerification verifySignature(Context context, byte[] signatureBytes, byte[] subjectBytes) {
53+
public static SignatureVerification verifySignature(
54+
final Context context,
55+
final UserAccountManager accountManager,
56+
final byte[] signatureBytes,
57+
final byte[] subjectBytes
58+
) {
5159
return null;
5260
}
5361

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Chris Narkiewicz
5+
* Copyright (C) 2919 Chris Narkiewicz <hello@ezaquarii.com>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
9+
* License as published by the Free Software Foundation; either
10+
* version 3 of the License, or any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public
18+
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package com.nextcloud.client.di;
21+
22+
import com.owncloud.android.services.firebase.NCFirebaseInstanceIDService;
23+
24+
import dagger.Module;
25+
import dagger.android.ContributesAndroidInjector;
26+
27+
@Module
28+
abstract class VariantComponentsModule {
29+
@ContributesAndroidInjector abstract NCFirebaseInstanceIDService ncFirebaseInstanceIDService();
30+
}

src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseInstanceIDService.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,33 @@
2323

2424
import com.google.firebase.iid.FirebaseInstanceId;
2525
import com.google.firebase.iid.FirebaseInstanceIdService;
26+
import com.nextcloud.client.account.UserAccountManager;
2627
import com.nextcloud.client.preferences.AppPreferences;
27-
import com.nextcloud.client.preferences.AppPreferencesImpl;
2828
import com.owncloud.android.R;
2929
import com.owncloud.android.utils.PushUtils;
3030

31+
import javax.inject.Inject;
32+
33+
import dagger.android.AndroidInjection;
34+
3135
public class NCFirebaseInstanceIDService extends FirebaseInstanceIdService {
3236

33-
private AppPreferences preferences;
37+
38+
@Inject AppPreferences preferences;
39+
@Inject UserAccountManager accountManager;
3440

3541
@Override
3642
public void onCreate() {
3743
super.onCreate();
38-
preferences = AppPreferencesImpl.fromContext(this);
44+
AndroidInjection.inject(this);
3945
}
4046

4147
@Override
4248
public void onTokenRefresh() {
4349
//You can implement this method to store the token on your server
4450
if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) {
4551
preferences.setPushToken(FirebaseInstanceId.getInstance().getToken());
46-
PushUtils.pushRegistrationToServer(preferences.getPushToken());
52+
PushUtils.pushRegistrationToServer(accountManager, preferences.getPushToken());
4753
}
4854
}
4955
}

src/gplay/java/com/owncloud/android/utils/PushUtils.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Nextcloud Android client application
33
*
44
* @author Mario Danic
5+
* @author Chris Narkiewicz
56
* Copyright (C) 2017-2018 Mario Danic
7+
* Copyright (C) 2019 Chris Narkiewicz
68
*
79
* This program is free software: you can redistribute it and/or modify
810
* it under the terms of the GNU Affero General Public License as published by
@@ -29,6 +31,7 @@
2931
import android.util.Log;
3032

3133
import com.google.gson.Gson;
34+
import com.nextcloud.client.account.UserAccountManager;
3235
import com.nextcloud.client.preferences.AppPreferences;
3336
import com.nextcloud.client.preferences.AppPreferencesImpl;
3437
import com.owncloud.android.MainApp;
@@ -194,7 +197,7 @@ private static void deleteRegistrationForAccount(Account account) {
194197
}
195198
}
196199

197-
public static void pushRegistrationToServer(final String token) {
200+
public static void pushRegistrationToServer(final UserAccountManager accountManager, final String token) {
198201
arbitraryDataProvider = new ArbitraryDataProvider(MainApp.getAppContext().getContentResolver());
199202

200203
if (!TextUtils.isEmpty(MainApp.getAppContext().getResources().getString(R.string.push_server_url)) &&
@@ -213,7 +216,7 @@ public static void pushRegistrationToServer(final String token) {
213216
String providerValue;
214217
PushConfigurationState accountPushData = null;
215218
Gson gson = new Gson();
216-
for (Account account : AccountUtils.getAccounts(context)) {
219+
for (Account account : accountManager.getAccounts()) {
217220
providerValue = arbitraryDataProvider.getValue(account, KEY_PUSH);
218221
if (!TextUtils.isEmpty(providerValue)) {
219222
accountPushData = gson.fromJson(providerValue,
@@ -359,9 +362,9 @@ private static int saveKeyToFile(Key key, String path) {
359362
return -1;
360363
}
361364

362-
public static void reinitKeys() {
365+
public static void reinitKeys(final UserAccountManager accountManager) {
363366
Context context = MainApp.getAppContext();
364-
Account[] accounts = AccountUtils.getAccounts(context);
367+
Account[] accounts = accountManager.getAccounts();
365368
for (Account account : accounts) {
366369
deleteRegistrationForAccount(account);
367370
}
@@ -375,7 +378,7 @@ public static void reinitKeys() {
375378

376379
AppPreferences preferences = AppPreferencesImpl.fromContext(context);
377380
String pushToken = preferences.getPushToken();
378-
pushRegistrationToServer(pushToken);
381+
pushRegistrationToServer(accountManager, pushToken);
379382
preferences.setKeysReInitEnabled();
380383
}
381384

@@ -411,13 +414,18 @@ private static void migratePushKeys() {
411414
}
412415
}
413416

414-
public static SignatureVerification verifySignature(Context context, byte[] signatureBytes, byte[] subjectBytes) {
417+
public static SignatureVerification verifySignature(
418+
final Context context,
419+
final UserAccountManager accountManager,
420+
final byte[] signatureBytes,
421+
final byte[] subjectBytes
422+
) {
415423
Signature signature = null;
416424
PublicKey publicKey;
417425
SignatureVerification signatureVerification = new SignatureVerification();
418426
signatureVerification.setSignatureValid(false);
419427

420-
Account[] accounts = AccountUtils.getAccounts(context);
428+
Account[] accounts = accountManager.getAccounts();
421429

422430
ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
423431
String arbitraryValue;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Chris Narkiewicz
5+
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
9+
* License as published by the Free Software Foundation; either
10+
* version 3 of the License, or any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public
18+
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package com.nextcloud.client.account;
21+
22+
import android.accounts.Account;
23+
24+
import androidx.annotation.NonNull;
25+
26+
public interface UserAccountManager {
27+
28+
int ACCOUNT_VERSION = 1;
29+
int ACCOUNT_VERSION_WITH_PROPER_ID = 2;
30+
String ACCOUNT_USES_STANDARD_PASSWORD = "ACCOUNT_USES_STANDARD_PASSWORD";
31+
32+
/**
33+
* Get configured NextCloud's user accounts.
34+
*
35+
* @return Array of accounts or empty array, if accounts are not configured.
36+
*/
37+
@NonNull
38+
Account[] getAccounts();
39+
40+
/**
41+
* Update the accounts in AccountManager to meet the current
42+
* version of accounts expected by the app, if needed.
43+
* <p>
44+
* Introduced to handle a change in the structure of stored account names
45+
* needed to allow different Nextcloud servers in the same domain, but not in
46+
* the same path.
47+
*/
48+
void updateAccountVersion();
49+
50+
/**
51+
* Extract username from account.
52+
*
53+
* Full account name is in form of "username@nextcloud.domain".
54+
*
55+
* @param account Account instance
56+
* @return User name (without domain) or null, if name cannot be extracted.
57+
*/
58+
static String getUsername(Account account) {
59+
if (account != null && account.name != null) {
60+
return account.name.substring(0, account.name.lastIndexOf('@'));
61+
} else {
62+
return null;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)