Skip to content

Commit 248e137

Browse files
Merge pull request #759 from nextcloud/chore/records
chore(records)!: Migrate data classes to records
2 parents 1a856ba + 35ae5ea commit 248e137

File tree

10 files changed

+137
-130
lines changed

10 files changed

+137
-130
lines changed

lib/src/main/java/com/nextcloud/android/sso/AccountImporter.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
*/
1010
package com.nextcloud.android.sso;
1111

12+
import static android.app.Activity.RESULT_CANCELED;
13+
import static android.app.Activity.RESULT_OK;
14+
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_FILES_ACCOUNT;
15+
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO;
16+
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO_EXCEPTION;
17+
import static com.nextcloud.android.sso.Constants.SSO_SHARED_PREFERENCE;
18+
1219
import android.Manifest;
1320
import android.accounts.Account;
1421
import android.accounts.AccountManager;
@@ -24,6 +31,10 @@
2431
import android.util.Log;
2532
import android.widget.Toast;
2633

34+
import androidx.core.app.ActivityCompat;
35+
import androidx.core.content.ContextCompat;
36+
import androidx.fragment.app.Fragment;
37+
2738
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
2839
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
2940
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
@@ -39,19 +50,9 @@
3950
import java.util.ArrayList;
4051
import java.util.List;
4152

42-
import androidx.core.app.ActivityCompat;
43-
import androidx.core.content.ContextCompat;
44-
import androidx.fragment.app.Fragment;
4553
import io.reactivex.annotations.NonNull;
4654
import io.reactivex.annotations.Nullable;
4755

48-
import static android.app.Activity.RESULT_CANCELED;
49-
import static android.app.Activity.RESULT_OK;
50-
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_FILES_ACCOUNT;
51-
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO;
52-
import static com.nextcloud.android.sso.Constants.NEXTCLOUD_SSO_EXCEPTION;
53-
import static com.nextcloud.android.sso.Constants.SSO_SHARED_PREFERENCE;
54-
5556
public class AccountImporter {
5657

5758
private static final String TAG = AccountImporter.class.getCanonicalName();
@@ -120,7 +121,7 @@ private static boolean appInstalledOrNot(Context context) {
120121
PackageManager pm = context.getPackageManager();
121122
for (final var appType : FilesAppTypeRegistry.getInstance().getTypes()) {
122123
try {
123-
pm.getPackageInfo(appType.packageId, PackageManager.GET_ACTIVITIES);
124+
pm.getPackageInfo(appType.packageId(), PackageManager.GET_ACTIVITIES);
124125
returnValue = true;
125126
break;
126127
} catch (PackageManager.NameNotFoundException e) {
@@ -365,7 +366,7 @@ private static Intent buildRequestAuthTokenIntent(Context context, Intent intent
365366
throw new NextcloudFilesAppAccountPermissionNotGrantedException(context);
366367
}
367368

368-
String componentName = FilesAppTypeRegistry.getInstance().findByAccountType(account.type).packageId;
369+
String componentName = FilesAppTypeRegistry.getInstance().findByAccountType(account.type).packageId();
369370

370371
Intent authIntent = new Intent();
371372
authIntent.setComponent(new ComponentName(componentName,

lib/src/main/java/com/nextcloud/android/sso/FilesAppTypeRegistry.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,77 @@
77
*/
88
package com.nextcloud.android.sso;
99

10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
1013
import com.nextcloud.android.sso.model.FilesAppType;
1114

15+
import java.util.Collection;
1216
import java.util.HashSet;
13-
import java.util.List;
14-
import java.util.Optional;
15-
import java.util.Set;
16-
17-
import androidx.annotation.NonNull;
18-
import androidx.annotation.Nullable;
1917

2018
public class FilesAppTypeRegistry {
2119
private static final FilesAppTypeRegistry FILES_APP_TYPE_REGISTRY = new FilesAppTypeRegistry();
22-
private final Set<FilesAppType> types = new HashSet<>();
20+
private final Collection<FilesAppType> types = new HashSet<>();
2321

2422
public FilesAppTypeRegistry() {
25-
types.add(new FilesAppType("com.nextcloud.client", "nextcloud", FilesAppType.Type.PROD));
26-
types.add(new FilesAppType("com.nextcloud.android.qa", "nextcloud.qa", FilesAppType.Type.QA));
27-
types.add(new FilesAppType("com.nextcloud.android.beta", "nextcloud.beta", FilesAppType.Type.DEV));
23+
types.add(new FilesAppType("com.nextcloud.client", "nextcloud", FilesAppType.Stage.PROD));
24+
types.add(new FilesAppType("com.nextcloud.android.qa", "nextcloud.qa", FilesAppType.Stage.QA));
25+
types.add(new FilesAppType("com.nextcloud.android.beta", "nextcloud.beta", FilesAppType.Stage.DEV));
2826
}
2927

3028
public static FilesAppTypeRegistry getInstance() {
3129
return FILES_APP_TYPE_REGISTRY;
3230
}
3331

34-
public synchronized void init(FilesAppType type) {
35-
types.clear();
36-
37-
if (type.type != FilesAppType.Type.PROD) {
38-
throw new IllegalArgumentException("If only one FilesAppType added, this must be PROD!");
32+
public synchronized void init(@NonNull FilesAppType type) {
33+
if (type.stage() != FilesAppType.Stage.PROD) {
34+
throw new IllegalArgumentException("If only one " + FilesAppType.class.getSimpleName() + " added, this must be " + FilesAppType.Stage.PROD.name() + "!");
3935
}
40-
36+
37+
types.clear();
4138
types.add(type);
4239
}
4340

44-
public synchronized void init(List<FilesAppType> types) {
45-
this.types.clear();
46-
47-
Optional<FilesAppType> prod = types.stream().filter(t -> t.type == FilesAppType.Type.PROD).findFirst();
48-
if (prod.isEmpty()) {
49-
throw new IllegalArgumentException("One provided FilesAppType must be PROD!");
41+
public synchronized void init(@NonNull Collection<FilesAppType> types) {
42+
if (!types.stream().anyMatch(t -> t.stage() == FilesAppType.Stage.PROD)) {
43+
throw new IllegalArgumentException("At least one provided " + FilesAppType.class.getSimpleName() + " must be " + FilesAppType.Stage.PROD.name() + "!");
5044
}
5145

46+
this.types.clear();
5247
this.types.addAll(types);
5348
}
5449

55-
public Set<FilesAppType> getTypes() {
50+
@NonNull
51+
public Collection<FilesAppType> getTypes() {
5652
return types;
5753
}
5854

55+
@NonNull
5956
public String[] getAccountTypes() {
60-
return types.stream().map(a -> a.accountType).toArray(String[]::new);
57+
return types
58+
.stream()
59+
.map(FilesAppType::accountType)
60+
.toArray(String[]::new);
6161
}
6262

6363

6464
/**
65-
* @return {@link FilesAppType.Type#PROD}, {@link FilesAppType.Type#QA}
66-
* or {@link FilesAppType.Type#DEV} depending on {@param accountType}.
67-
* Uses {@link FilesAppType.Type#PROD} as fallback.
65+
* @return {@link FilesAppType.Stage#PROD}, {@link FilesAppType.Stage#QA}
66+
* or {@link FilesAppType.Stage#DEV} depending on {@param accountType}.
67+
* Uses {@link FilesAppType.Stage#PROD} as fallback.
6868
*/
6969
@NonNull
7070
public FilesAppType findByAccountType(@Nullable String accountType) {
7171
for (final var type : types) {
72-
if (type.accountType.equalsIgnoreCase(accountType)) {
72+
if (type.accountType().equalsIgnoreCase(accountType)) {
7373
return type;
7474
}
7575
}
76-
return types.stream().filter(t -> t.type == FilesAppType.Type.PROD).findFirst().get();
76+
77+
return types
78+
.stream()
79+
.filter(t -> t.stage() == FilesAppType.Stage.PROD)
80+
.findFirst()
81+
.get();
7782
}
7883
}

lib/src/main/java/com/nextcloud/android/sso/api/AidlNetworkRequest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
package com.nextcloud.android.sso.api;
1111

12+
import static com.nextcloud.android.sso.aidl.ParcelFileDescriptorUtil.pipeFrom;
13+
import static com.nextcloud.android.sso.exceptions.SSOException.parseNextcloudCustomException;
14+
1215
import android.content.ComponentName;
1316
import android.content.Context;
1417
import android.content.Intent;
@@ -20,6 +23,9 @@
2023
import android.os.RemoteException;
2124
import android.util.Log;
2225

26+
import androidx.annotation.NonNull;
27+
import androidx.annotation.Nullable;
28+
2329
import com.google.gson.Gson;
2430
import com.google.gson.internal.LinkedTreeMap;
2531
import com.nextcloud.android.sso.FilesAppTypeRegistry;
@@ -38,12 +44,6 @@
3844
import java.util.ArrayList;
3945
import java.util.concurrent.atomic.AtomicBoolean;
4046

41-
import androidx.annotation.NonNull;
42-
import androidx.annotation.Nullable;
43-
44-
import static com.nextcloud.android.sso.aidl.ParcelFileDescriptorUtil.pipeFrom;
45-
import static com.nextcloud.android.sso.exceptions.SSOException.parseNextcloudCustomException;
46-
4747
public class AidlNetworkRequest extends NetworkRequest {
4848
private static final String TAG = AidlNetworkRequest.class.getCanonicalName();
4949

@@ -91,7 +91,7 @@ public void connect(String type) {
9191
Log.d(TAG, "[connect] Binding to AccountManagerService for type [" + type + "]");
9292
super.connect(type);
9393

94-
final String componentName = FilesAppTypeRegistry.getInstance().findByAccountType(type).packageId;
94+
final String componentName = FilesAppTypeRegistry.getInstance().findByAccountType(type).packageId();
9595

9696
Log.d(TAG, "[connect] Component name is: [" + componentName + "]");
9797

lib/src/main/java/com/nextcloud/android/sso/helper/VersionCheckHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import android.content.pm.PackageManager;
1313
import android.util.Log;
1414

15+
import androidx.annotation.NonNull;
16+
1517
import com.nextcloud.android.sso.FilesAppTypeRegistry;
1618
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
1719
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotSupportedException;
@@ -20,8 +22,6 @@
2022

2123
import java.util.Optional;
2224

23-
import androidx.annotation.NonNull;
24-
2525
public final class VersionCheckHelper {
2626

2727
private static final String TAG = VersionCheckHelper.class.getCanonicalName();
@@ -45,7 +45,7 @@ public static boolean verifyMinVersion(@NonNull Context context, int minVersion,
4545
.getInstance()
4646
.getTypes()
4747
.stream()
48-
.filter(t -> t.type == FilesAppType.Type.DEV)
48+
.filter(t -> t.stage() == FilesAppType.Stage.DEV)
4949
.findFirst();
5050
if (dev.isPresent()) {
5151
final int verCode = getNextcloudFilesVersionCode(context, dev.get());
@@ -66,7 +66,7 @@ public static boolean verifyMinVersion(@NonNull Context context, int minVersion,
6666
}
6767

6868
public static int getNextcloudFilesVersionCode(@NonNull Context context, @NonNull FilesAppType appType) throws PackageManager.NameNotFoundException {
69-
final var packageInfo = context.getPackageManager().getPackageInfo(appType.packageId, 0);
69+
final var packageInfo = context.getPackageManager().getPackageInfo(appType.packageId(), 0);
7070
final int verCode = packageInfo.versionCode;
7171
Log.d("VersionCheckHelper", "Version Code: " + verCode);
7272
return verCode;

lib/src/main/java/com/nextcloud/android/sso/model/FilesAppType.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@
99

1010
import androidx.annotation.NonNull;
1111

12-
public class FilesAppType {
13-
public final String packageId;
14-
public final String accountType;
15-
public final Type type;
12+
public record FilesAppType(@NonNull String packageId,
13+
@NonNull String accountType,
14+
@NonNull Stage stage) {
1615

17-
public FilesAppType(@NonNull String packageId, @NonNull String accountType, Type type) {
18-
this.packageId = packageId;
19-
this.accountType = accountType;
20-
this.type = type;
16+
public FilesAppType(@NonNull String accountType, @NonNull String packageId) {
17+
this(packageId, accountType, Stage.PROD);
2118
}
2219

23-
public enum Type {
20+
public enum Stage {
2421
PROD, QA, DEV
2522
}
2623
}

lib/src/main/java/com/nextcloud/android/sso/model/ocs/OcsCapabilitiesResponse.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,53 @@
1616
* <pre>
1717
* {@code
1818
* @GET("/ocs/v2.php/cloud/capabilities?format=json")
19-
* Call<OcsResponse<OcsCapabilities>> getCapabilities();
19+
* Call<OcsResponse < OcsCapabilities>> getCapabilities();
2020
* }
2121
* </pre>
2222
*
2323
* @see <a href="https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-api-overview.html#capabilities-api">Capabilities API</a>
2424
*/
2525
@SuppressWarnings("unused, SpellCheckingInspection")
26-
public class OcsCapabilitiesResponse {
27-
public OcsVersion version;
28-
public OcsCapabilities capabilities;
29-
30-
public static class OcsVersion {
31-
public int major;
32-
public int minor;
33-
public int macro;
34-
public String string;
35-
public String edition;
36-
public boolean extendedSupport;
26+
public record OcsCapabilitiesResponse(
27+
OcsVersion version,
28+
OcsCapabilities capabilities
29+
) {
30+
public record OcsVersion(
31+
int major,
32+
int minor,
33+
int macro,
34+
String string,
35+
String edition,
36+
boolean extendedSupport
37+
) {
3738
}
3839

39-
public static class OcsCapabilities {
40-
public OcsTheming theming;
41-
42-
public static class OcsTheming {
43-
public String name;
44-
public String url;
45-
public String slogan;
46-
public String color;
40+
public record OcsCapabilities(
41+
OcsTheming theming
42+
) {
43+
public record OcsTheming(
44+
String name,
45+
String url,
46+
String slogan,
47+
String color,
4748
@SerializedName("color-text")
48-
public String colorText;
49+
String colorText,
4950
@SerializedName("color-element")
50-
public String colorElement;
51+
String colorElement,
5152
@SerializedName("color-element-bright")
52-
public String colorElementBright;
53+
String colorElementBright,
5354
@SerializedName("color-element-dark")
54-
public String colorElementDark;
55-
public String logo;
56-
public String background;
55+
String colorElementDark,
56+
String logo,
57+
String background,
5758
@SerializedName("background-plain")
58-
public boolean backgroundPlain;
59+
boolean backgroundPlain,
5960
@SerializedName("background-default")
60-
public boolean backgroundDefault;
61+
boolean backgroundDefault,
6162
@SerializedName("logoheader")
62-
public String logoHeader;
63-
public String favicon;
63+
String logoHeader,
64+
String favicon
65+
) {
6466
}
6567
}
6668
}

lib/src/main/java/com/nextcloud/android/sso/model/ocs/OcsResponse.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,26 @@
1717
* <pre>
1818
* {@code
1919
* @GET("/ocs/v2.php/cloud/capabilities?format=json")
20-
* Call<OcsResponse<OcsCapabilitiesResponse>> getCapabilities();
20+
* Call<OcsResponse < OcsCapabilitiesResponse>> getCapabilities();
2121
* }
2222
* </pre>
2323
*
2424
* @param <T> defines the payload type of this {@link OcsResponse}.
2525
*/
2626
@SuppressWarnings("unused, SpellCheckingInspection")
27-
public class OcsResponse<T> {
28-
public OcsWrapper<T> ocs;
29-
30-
public static class OcsWrapper<T> {
31-
public OcsMeta meta;
32-
public T data;
33-
34-
public static class OcsMeta {
35-
public String status;
27+
public record OcsResponse<T>(
28+
OcsWrapper<T> ocs
29+
) {
30+
public record OcsWrapper<T>(
31+
OcsMeta meta,
32+
T data
33+
) {
34+
public record OcsMeta(
35+
String status,
3636
@SerializedName("statuscode")
37-
public int statusCode;
38-
public String message;
37+
int statusCode,
38+
String message
39+
) {
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)