summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
blob: 07387f1e551128907f619f7b96d311ef54abb1fe (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
/* -*- 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 java.util.HashSet;
import java.util.Set;

import org.libreoffice.storage.external.ExtsdDocumentsProvider;
import org.libreoffice.storage.external.OTGDocumentsProvider;
import org.libreoffice.storage.local.LocalDocumentsDirectoryProvider;
import org.libreoffice.storage.local.LocalDocumentsProvider;

import android.content.Context;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

/**
 * 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 {
    public static int EXTSD_PROVIDER_INDEX = 2;
    public static int OTG_PROVIDER_INDEX = 3;

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

    private IDocumentProvider[] providers;

    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.providers = new IDocumentProvider[4];
            instance.providers[0] = new LocalDocumentsDirectoryProvider(0);
            instance.providers[1] = new LocalDocumentsProvider(1);
            instance.providers[OTG_PROVIDER_INDEX] = new OTGDocumentsProvider(OTG_PROVIDER_INDEX, context);
            instance.providers[EXTSD_PROVIDER_INDEX] = new ExtsdDocumentsProvider(EXTSD_PROVIDER_INDEX, context);

            // initialize document provider names 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 id.
     *
     * @param id
     * @return document provider with that id.
     */
    public IDocumentProvider getProvider(int id) {
        // as for now, id == position in providers array
        return providers[id];
    }

    /**
     * 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];
    }

    public Set<OnSharedPreferenceChangeListener> getChangeListeners() {
        Set<OnSharedPreferenceChangeListener> listeners =
                new HashSet<OnSharedPreferenceChangeListener>();
        for (IDocumentProvider provider : providers) {
            if (provider instanceof OnSharedPreferenceChangeListener)
                listeners.add((OnSharedPreferenceChangeListener) provider);
        }
        return listeners;
    }
}