summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
blob: 9aa19735bbd420fcf3c9c1472afebcbfaff36b03 (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
/* -*- 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;

import org.libreoffice.storage.local.LocalDocumentsDirectoryProvider;
import org.libreoffice.storage.local.LocalDocumentsProvider;

import android.content.Context;

/**
 * Keeps the instances of the available IDocumentProviders in the system.
 * Instances are maintained in a sorted list and providers have to be
 * accessed from their position.
 *
 * The factory follows the Singleton pattern, there is only one instance of it
 * in the application and it must be retrieved with
 * DocumentProviderFactory.getInstance().
 */
public final class DocumentProviderFactory {

    /**
     * Private factory instance for the Singleton pattern.
     */
    private static DocumentProviderFactory instance = null;

    private IDocumentProvider[] providers = {
            new LocalDocumentsDirectoryProvider(), new LocalDocumentsProvider() };

    private String[] providerNames;

    private DocumentProviderFactory() {
        // private to prevent external instances of the factory
    }

    /**
     * Initializes the factory with some context. If this method is called for
     * twice or more times those calls will have no effect.
     *
     * @param context
     *            Application context for the factory.
     */
    public static void initialize(Context context) {
        if (instance == null) {
            // initialize instance
            instance = new DocumentProviderFactory();

            // initialize document providers list
            instance.providerNames = new String[instance.providers.length];
            for (int i = 0; i < instance.providers.length; i++) {
                instance.providerNames[i] = context.getString(instance
                        .getProvider(i).getNameResource());
            }
        }
    }

    /**
     * Retrieve the unique instance of the factory.
     *
     * @return the unique factory object or null if it is not yet initialized.
     */
    public static DocumentProviderFactory getInstance() {
        return instance;
    }

    /**
     * Retrieve the provider associated to a certain position.
     *
     * @param position
     * @return document provider in that position.
     */
    public IDocumentProvider getProvider(int position) {
        return providers[position];
    }

    /**
     * Returns a sorted list of the names of the providers. Order is meaningful
     * to retrieve the actual provider object with getProvider().
     *
     * @return Array with the names of the available providers.
     */
    public String[] getNames() {
        return providerNames;
    }

    /**
     * Returns the default provider.
     *
     * @return default provider.
     */
    public IDocumentProvider getDefaultProvider() {
        return providers[0];
    }
}