Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e554583a authored by Bartosz Przybylski's avatar Bartosz Przybylski
Browse files

Use gson to parse user info

parent 42423b08
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
 */
package com.owncloud.android.lib.common;

import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

/**
@@ -27,10 +29,15 @@ import org.parceler.Parcel;

@Parcel
public class Quota {
    @SerializedName("free")
    public long free;
    @SerializedName("used")
    public long used;
    @SerializedName("total")
    public long total;
    public long quota;
    @SerializedName("quota")
    public transient long quota;
    @SerializedName("relative")
    public double relative;

    public Quota() {
+11 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
 */
package com.owncloud.android.lib.common;

import com.google.gson.annotations.SerializedName;

import org.parceler.Parcel;

/**
@@ -27,14 +29,23 @@ import org.parceler.Parcel;

@Parcel
public class UserInfo {
    @SerializedName("id")
    public String id;
    @SerializedName("enabled")
    public Boolean enabled;
    @SerializedName(value = "display-name", alternate = {"displayname"})
    public String displayName;
    @SerializedName("email")
    public String email;
    @SerializedName("phone")
    public String phone;
    @SerializedName("address")
    public String address;
    @SerializedName(value = "website", alternate = {"webpage"})
    public String website;
    @SerializedName("twitter")
    public String twitter;
    @SerializedName("quota")
    public Quota quota;

    public UserInfo() {
+16 −84
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ package com.owncloud.android.lib.resources.users;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.Quota;
@@ -55,30 +58,13 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {

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

    private static final String NODE_OCS = "ocs";
    private static final String NODE_DATA = "data";

    // OCS Route
    private static final String OCS_ROUTE_SELF = "/ocs/v1.php/cloud/user";
    private static final String OCS_ROUTE_SEARCH = "/ocs/v1.php/cloud/users/";

    // JSON Node names
    private static final String NODE_OCS = "ocs";
    private static final String NODE_DATA = "data";
    private static final String NODE_ID = "id";
    private static final String NODE_DISPLAY_NAME = "display-name";
    private static final String NODE_DISPLAY_NAME_ALT = "displayname";
    private static final String NODE_EMAIL = "email";
    private static final String NODE_ENABLED = "enabled";
    private static final String NODE_PHONE = "phone";
    private static final String NODE_ADDRESS = "address";
    private static final String NODE_WEBSITE = "website";
    private static final String NODE_WEBPAGE = "webpage";
    private static final String NODE_TWITTER = "twitter";

    private static final String NODE_QUOTA = "quota";
    private static final String NODE_QUOTA_FREE = "free";
    private static final String NODE_QUOTA_USED = "used";
    private static final String NODE_QUOTA_TOTAL = "total";
    private static final String NODE_QUOTA_RELATIVE = "relative";

    /**
     * Quota return value for a not computed space value.
     */
@@ -140,73 +126,24 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
                String response = get.getResponseBodyAsString();
                Log_OC.d(TAG, "Successful response: " + response);

                // Parse the response
                JSONObject respJSON = new JSONObject(response);
                JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
                JSONObject respData = respOCS.getJSONObject(NODE_DATA);
                Gson gson = new Gson();
                JsonParser parser = new JsonParser();
                JsonObject respJson = (JsonObject)parser.parse(response);

                UserInfo userInfo = new UserInfo();
                JsonObject respData = respJson.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA);
                UserInfo userInfo = gson.fromJson(respData, UserInfo.class);

                // we don't really always have the ID
                if (respData.has(NODE_ID)) {
                    userInfo.setId(respData.getString(NODE_ID));
                } else {
                    if (TextUtils.isEmpty(userID)) {
                if (userInfo.getId() == null) {
                    if (TextUtils.isEmpty(userID))
                        userInfo.setId(credentials.getUsername());
                    } else {
                    else
                        userInfo.setId(userID);
                }
                }

                // Two endpoints, two different responses
                if (respData.has(NODE_DISPLAY_NAME)) {
                    userInfo.setDisplayName(respData.getString(NODE_DISPLAY_NAME));
                } else {
                    userInfo.setDisplayName(respData.getString(NODE_DISPLAY_NAME_ALT));
                }

                if (hasData(respData, NODE_EMAIL)) {
                    userInfo.setEmail(respData.getString(NODE_EMAIL));
                }

                if (respData.has(NODE_QUOTA) && !respData.isNull(NODE_QUOTA)) {
                    JSONObject quota = respData.getJSONObject(NODE_QUOTA);
                    final Long quotaFree = quota.getLong(NODE_QUOTA_FREE);
                    final Long quotaUsed = quota.getLong(NODE_QUOTA_USED);
                    final Long quotaTotal = quota.getLong(NODE_QUOTA_TOTAL);
                    final Double quotaRelative = quota.getDouble(NODE_QUOTA_RELATIVE);

                    Long quotaValue;
                    try {
                        quotaValue = quota.getLong(NODE_QUOTA);
                    } catch (JSONException e) {
                        Log_OC.i(TAG, "Legacy server in use < Nextcloud 9.0.54");
                        quotaValue = QUOTA_LIMIT_INFO_NOT_AVAILABLE;
                if (userInfo.getQuota().getQuota() == 0) {
                    userInfo.getQuota().setQuota(QUOTA_LIMIT_INFO_NOT_AVAILABLE);
                }

                    userInfo.setQuota(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative, quotaValue));
                }

                if (hasData(respData, NODE_PHONE))
                    userInfo.setPhone(respData.getString(NODE_PHONE));

                if (hasData(respData, NODE_ADDRESS))
                    userInfo.setAddress(respData.getString(NODE_ADDRESS));

                // webpage is a field name pre NC12
                if (hasData(respData, NODE_WEBSITE))
                    userInfo.setWebsite(respData.getString(NODE_WEBSITE));
                else if (hasData(respData, NODE_WEBPAGE))
                    userInfo.setWebsite(respData.getString(NODE_WEBPAGE));

                if (hasData(respData, NODE_TWITTER))
                    userInfo.setTwitter(respData.getString(NODE_TWITTER));

                if (respData.has(NODE_ENABLED)) {
                    userInfo.setEnabled(respData.getBoolean(NODE_ENABLED));
                }

                // Result
                result = new RemoteOperationResult(true, get);
                // Username in result.data
                ArrayList<Object> data = new ArrayList<>();
@@ -233,11 +170,6 @@ public class GetRemoteUserInfoOperation extends RemoteOperation {
        return result;
    }

    private boolean hasData(JSONObject respData, String objectName) throws JSONException {
        return respData.has(objectName) && !respData.isNull(objectName) &&
                !TextUtils.isEmpty(respData.getString(objectName));
    }

    private boolean isSuccess(int status) {
        return (status == HttpStatus.SC_OK);
    }