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

Capabilities.java « model « shared « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5514a91bdfdc8fc5d49385c6e0cf54148ac053e0 (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
package it.niedermann.owncloud.notes.shared.model;

import android.graphics.Color;
import android.util.Log;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import com.bumptech.glide.load.HttpException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;

import org.json.JSONException;
import org.json.JSONObject;

import it.niedermann.android.util.ColorUtil;

import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;

/**
 * This entity class is used to return relevant data of the HTTP response.
 */
public class Capabilities {

    private static final String TAG = Capabilities.class.getSimpleName();

    private static final String JSON_OCS = "ocs";
    private static final String JSON_OCS_META = "meta";
    private static final String JSON_OCS_META_STATUSCODE = "statuscode";
    private static final String JSON_OCS_DATA = "data";
    private static final String JSON_OCS_DATA_CAPABILITIES = "capabilities";
    private static final String JSON_OCS_DATA_CAPABILITIES_NOTES = "notes";
    private static final String JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION = "api_version";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING = "theming";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR = "color";
    private static final String JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT = "color-text";

    private String apiVersion = null;

    @ColorInt
    private int color = -16743735;
    @ColorInt
    private int textColor = -16777216;
    @Nullable
    private String eTag;

    public Capabilities() {

    }

    @VisibleForTesting
    public Capabilities(@NonNull String response, @Nullable String eTag) throws NextcloudHttpRequestFailedException {
        this.eTag = eTag;
        final JSONObject ocs;
        try {
            ocs = new JSONObject(response).getJSONObject(JSON_OCS);
            if (ocs.has(JSON_OCS_META)) {
                final JSONObject meta = ocs.getJSONObject(JSON_OCS_META);
                if (meta.has(JSON_OCS_META_STATUSCODE)) {
                    if (meta.getInt(JSON_OCS_META_STATUSCODE) == HTTP_UNAVAILABLE) {
                        Log.i(TAG, "Capabilities Endpoint: This instance is currently in maintenance mode.");
                        throw new NextcloudHttpRequestFailedException(HTTP_UNAVAILABLE, new HttpException(HTTP_UNAVAILABLE));
                    }
                }
            }
            if (ocs.has(JSON_OCS_DATA)) {
                final JSONObject data = ocs.getJSONObject(JSON_OCS_DATA);
                if (data.has(JSON_OCS_DATA_CAPABILITIES)) {
                    final JSONObject capabilities = data.getJSONObject(JSON_OCS_DATA_CAPABILITIES);
                    if (capabilities.has(JSON_OCS_DATA_CAPABILITIES_NOTES)) {
                        final JSONObject notes = capabilities.getJSONObject(JSON_OCS_DATA_CAPABILITIES_NOTES);
                        if (notes.has(JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION)) {
                            this.apiVersion = notes.getString(JSON_OCS_DATA_CAPABILITIES_NOTES_API_VERSION);
                        }
                    }
                    if (capabilities.has(JSON_OCS_DATA_CAPABILITIES_THEMING)) {
                        final JSONObject theming = capabilities.getJSONObject(JSON_OCS_DATA_CAPABILITIES_THEMING);
                        if (theming.has(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR)) {
                            try {
                                this.color = Color.parseColor(ColorUtil.INSTANCE.formatColorToParsableHexString(theming.getString(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        if (theming.has(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT)) {
                            try {
                                this.textColor = Color.parseColor(ColorUtil.INSTANCE.formatColorToParsableHexString(theming.getString(JSON_OCS_DATA_CAPABILITIES_THEMING_COLOR_TEXT)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void setApiVersion(String apiVersion) {
        this.apiVersion = apiVersion;
    }

    public String getApiVersion() {
        return apiVersion;
    }

    @Nullable
    public String getETag() {
        return eTag;
    }

    public void setETag(@Nullable String eTag) {
        this.eTag = eTag;
    }

    public int getColor() {
        return color;
    }

    public void setColor(@ColorInt int color) {
        this.color = color;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(@ColorInt int textColor) {
        this.textColor = textColor;
    }

    @NonNull
    @Override
    public String toString() {
        return "Capabilities{" +
                "apiVersion='" + apiVersion + '\'' +
                ", color=" + color +
                ", textColor=" + textColor +
                ", eTag='" + eTag + '\'' +
                '}';
    }
}