summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/storage/local/LocalDocumentsDirectoryProvider.java
blob: c2e03dfc7bdd0ec78ed6d41c56691f284343548d (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
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package org.libreoffice.storage.local;

import java.io.File;

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

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;

/**
 * A convenience IDocumentProvider to browse the /sdcard/Documents directory.
 *
 * Extends LocalDocumentsProvider to overwrite getRootDirectory and set it to
 * /sdcard/Documents. Most documents will probably be stored there so there is
 * no need for the user to browse the filesystem from the root every time.
 */
public class LocalDocumentsDirectoryProvider extends LocalDocumentsProvider {

    public LocalDocumentsDirectoryProvider(int id) {
        super(id);
    }

    private static File getDocumentsDir() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // DIRECTORY_DOCUMENTS is 19 or later only
            return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        } else {
            return new File(Environment.getExternalStorageDirectory() + "/Documents");
        }
    }

    @Override
    public IFile getRootDirectory(Context context) {
        File documentsDirectory = getDocumentsDir();
        if (!documentsDirectory.exists()) {
            // might be a little counter-intuitive: if we were granted READ permission already, we're also granted the write-permission
            // when we ask for it, since they are both in the same storage group (for 5.1 and lower it is granted at install-time already)
            // see https://developer.android.com/guide/topics/permissions/requesting.html#perm-groups
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                if(!documentsDirectory.mkdirs()) {
                    // fallback to the toplevel dir - might be due to the dir not mounted/used as USB-Mass-Storage or similar
                    // TODO: handle unavailability of the storage/failure of the mkdir properly
                    Log.e("LocalDocumentsProvider", "not sure how we ended up here - if we have read permissions to use it in the first place, we also should have the write-permissions..");
                    documentsDirectory = Environment.getExternalStorageDirectory();
                }
            }
        }
        return new LocalFile(documentsDirectory);
    }

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

    @Override
    public boolean checkProviderAvailability(Context context) {
        File documentsDirectory = getDocumentsDir();
        return documentsDirectory.exists() && ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }
}