Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Account.java « model « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba8fb72b7eddbff334d5f396e6eb8b94564d4212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package it.niedermann.nextcloud.deck.model;

import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.io.Serializable;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.model.ocs.Capabilities;
import it.niedermann.nextcloud.deck.model.ocs.Version;
import it.niedermann.nextcloud.deck.ui.accountswitcher.AccountSwitcherDialog;
import it.niedermann.nextcloud.deck.util.ColorUtil;

@Entity(indices = {@Index(value = "name", unique = true)})
public class Account implements Serializable {
    @Ignore
    private static final long serialVersionUID = 0;

    @PrimaryKey(autoGenerate = true)
    protected Long id;

    @NonNull
    private String name;

    @NonNull
    private String userName;

    @NonNull
    private String url;

    @NonNull
    @ColumnInfo(defaultValue = "#0082c9")
    private String color = "#0082c9";

    @NonNull
    @ColumnInfo(defaultValue = "#ffffff")
    private String textColor = "#ffffff";

    @NonNull
    @ColumnInfo(defaultValue = "0.6.4")
    private String serverDeckVersion = "0.6.4";

    @NonNull
    @ColumnInfo(defaultValue = "0")
    private boolean maintenanceEnabled = false;

    @Ignore
    public Account(Long id, @NonNull String name, @NonNull String userName, @NonNull String url) {
        this(name, userName, url);
        this.id = id;
    }

    @Ignore
    public Account(String name, String userName, String url) {
        this.name = name;
        this.userName = userName;
        this.url = url;
    }

    @Ignore
    public Account(Long id) {
        this.id = id;
    }

    public Account() {
    }

    public void applyCapabilities(Capabilities capabilities) {
        maintenanceEnabled = capabilities.isMaintenanceEnabled();
        if (!isMaintenanceEnabled()) {
            try {
                // Nextcloud might return color format #000 which cannot be parsed by Color.parseColor()
                // https://github.com/stefan-niedermann/nextcloud-deck/issues/466
                color = ColorUtil.formatColorToParsableHexString(capabilities.getColor());
                textColor = ColorUtil.formatColorToParsableHexString(capabilities.getTextColor());
            } catch (Exception e) {
                DeckLog.logError(e);
                color = "#0082c9";
                color = "#ffffff";
            }
            if (capabilities.getDeckVersion() != null) {
                serverDeckVersion = capabilities.getDeckVersion().getOriginalVersion();
            }
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NonNull
    public String getName() {
        return name;
    }

    public void setName(@NonNull String name) {
        this.name = name;
    }

    @NonNull
    public String getUserName() {
        return userName;
    }

    public void setUserName(@NonNull String userName) {
        this.userName = userName;
    }

    @NonNull
    public String getUrl() {
        return url;
    }

    public void setUrl(@NonNull String url) {
        this.url = url;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    @NonNull
    public String getColor() {
        return color;
    }

    public void setColor(@NonNull String color) {
        this.color = color;
    }

    @NonNull
    public String getTextColor() {
        return textColor;
    }

    public void setTextColor(@NonNull String textColor) {
        this.textColor = textColor;
    }

    public Version getServerDeckVersionAsObject() {
        return Version.of(serverDeckVersion);
    }

    @NonNull
    public String getServerDeckVersion() {
        return serverDeckVersion;
    }

    public void setServerDeckVersion(@NonNull String serverDeckVersion) {
        this.serverDeckVersion = serverDeckVersion;
    }

    public boolean isMaintenanceEnabled() {
        return maintenanceEnabled;
    }

    public void setMaintenanceEnabled(boolean maintenanceEnabled) {
        this.maintenanceEnabled = maintenanceEnabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Account account = (Account) o;

        if (maintenanceEnabled != account.maintenanceEnabled) return false;
        if (id != null ? !id.equals(account.id) : account.id != null) return false;
        if (!name.equals(account.name)) return false;
        if (!userName.equals(account.userName)) return false;
        if (!url.equals(account.url)) return false;
        if (!color.equals(account.color)) return false;
        if (!textColor.equals(account.textColor)) return false;
        return serverDeckVersion.equals(account.serverDeckVersion);
    }

    /**
     * A cache buster parameter is added for duplicate account names on different hosts which shall be fetched from the same {@link SingleSignOnAccount} (e. g. {@link AccountSwitcherDialog})
     *
     * @return an {@link String} to fetch the avatar for this account.
     */
    public String getAvatarUrl(@Px int size) {
        return getUrl() + "/index.php/avatar/" + Uri.encode(getUserName()) + "/" + size;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + name.hashCode();
        result = 31 * result + userName.hashCode();
        result = 31 * result + url.hashCode();
        result = 31 * result + color.hashCode();
        result = 31 * result + textColor.hashCode();
        result = 31 * result + serverDeckVersion.hashCode();
        result = 31 * result + (maintenanceEnabled ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", userName='" + userName + '\'' +
                ", url='" + url + '\'' +
                ", color='" + color + '\'' +
                ", textColor='" + textColor + '\'' +
                ", serverDeckVersion='" + serverDeckVersion + '\'' +
                ", maintenanceEnabled=" + maintenanceEnabled +
                '}';
    }
}