summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
blob: 66e4633fe5c64bf14f3f8fc71f77b2c754527999 (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
package org.libreoffice.storage.owncloud;

import java.io.File;
import java.net.URI;

import org.libreoffice.R;
import org.libreoffice.storage.DocumentProviderSettingsActivity;
import org.libreoffice.storage.IDocumentProvider;
import org.libreoffice.storage.IFile;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.preference.PreferenceManager;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;

/**
 * Implementation of IDocumentProvider for ownCloud servers.
 */
public class OwnCloudProvider implements IDocumentProvider,
        OnSharedPreferenceChangeListener {

    private Context context;
    private OwnCloudClient client;
    private File cacheDir;

    private String serverUrl;
    private String userName;
    private String password;

    public OwnCloudProvider(Context context) {
        this.context = context;

        // read preferences
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        serverUrl = preferences.getString(
                DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_SERVER, "");
        userName = preferences.getString(
                DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_USER_NAME, "");
        password = preferences.getString(
                DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_PASSWORD, "");

        setupClient();

        // make sure cache directory exists, and clear it
        // TODO: probably we should do smarter cache management
        cacheDir = new File(context.getCacheDir(), "ownCloud");
        if (cacheDir.exists()) {
            deleteRecursive(cacheDir);
        }
        cacheDir.mkdirs();
    }

    private void setupClient() {
        Uri serverUri = Uri.parse(serverUrl);
        client = OwnCloudClientFactory.createOwnCloudClient(serverUri, context,
                true);
        client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(
                userName, password));
    }

    @Override
    public IFile getRootDirectory() {
        return createFromUri(URI.create(FileUtils.PATH_SEPARATOR));
    }

    @Override
    public IFile createFromUri(URI uri) {
        ReadRemoteFileOperation refreshOperation = new ReadRemoteFileOperation(
                uri.getPath());
        RemoteOperationResult result = refreshOperation.execute(client);
        if (!result.isSuccess()) {
            throw buildRuntimeExceptionForResultCode(result.getCode());
        }
        if (result.getData().size() > 0) {
            return new OwnCloudFile(this, (RemoteFile) result.getData().get(0));
        }
        return null;
    }

    @Override
    public int getNameResource() {
        return R.string.owncloud;
    }

    /**
     * Used by OwnCloudFiles to get a configured client to run their own
     * operations.
     *
     * @return configured OwnCloudClient.
     */
    protected OwnCloudClient getClient() {
        return client;
    }

    /**
     * Used by OwnCloudFiles to get the cache directory they should download
     * files to.
     *
     * @return cache directory.
     */
    protected File getCacheDir() {
        return cacheDir;
    }

    /**
     * Build the proper RuntimeException for some error result.
     *
     * @param code Result code got from some RemoteOperationResult.
     * @return exception with the proper internationalized error message.
     */
    protected RuntimeException buildRuntimeExceptionForResultCode(ResultCode code) {
        int errorMessage;
        switch (code) {
            case WRONG_CONNECTION:  // SocketException
            case FILE_NOT_FOUND:    // HTTP 404
                errorMessage = R.string.owncloud_wrong_connection;
                break;
            case UNAUTHORIZED:      // wrong user/pass
                errorMessage = R.string.owncloud_unauthorized;
                break;
            default:
                errorMessage = R.string.owncloud_unspecified_error;
                break;
        }
        return new RuntimeException(context.getString(errorMessage));
    }

    /**
     * Deletes files and recursively deletes directories.
     *
     * @param file
     *            File or directory to be deleted.
     */
    private void deleteRecursive(File file) {
        if (file.isDirectory()) {
            for (File child : file.listFiles())
                deleteRecursive(child);
        }
        file.delete();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences preferences,
            String key) {
        boolean changed = false;
        if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_SERVER)) {
            serverUrl = preferences.getString(key, "");
            changed = true;
        }
        else if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_USER_NAME)) {
            userName = preferences.getString(key, "");
            changed = true;
        }
        else if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_PASSWORD)) {
            password = preferences.getString(key, "");
            changed = true;
        }

        if (changed)
            setupClient();
    }
}