aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/export/ExportFactory.java
blob: e743eb245d65e89e32f96be674c79209166732c2 (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
package cgeo.geocaching.export;

import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;
import cgeo.geocaching.utils.Log;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.ArrayAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Factory to create a dialog with all available exporters.
 */
public abstract class ExportFactory {

    /**
     * Contains instances of all available exporter classes.
     */
    private static final List<Class<? extends Export>> exporterClasses;

    static {
        final ArrayList<Class<? extends Export>> temp = new ArrayList<Class<? extends Export>>();
        temp.add(FieldnoteExport.class);
        temp.add(GpxExport.class);
        exporterClasses = Collections.unmodifiableList(temp);
    }

    /**
     * Creates a dialog so that the user can select an exporter.
     *
     * @param caches
     *            The {@link List} of {@link cgeo.geocaching.Geocache} to be exported
     * @param activity
     *            The {@link Activity} in whose context the dialog should be shown
     */
    public static void showExportMenu(final List<Geocache> caches, final Activity activity) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.export).setIcon(R.drawable.ic_menu_share);

        final ArrayList<Export> export = new ArrayList<Export>();
        for (Class<? extends Export> exporterClass : exporterClasses) {
            try {
                export.add(exporterClass.newInstance());
            } catch (Exception ex) {
                Log.e("showExportMenu", ex);
            }
        }

        final ArrayAdapter<Export> adapter = new ArrayAdapter<Export>(activity, android.R.layout.select_dialog_item, export);

        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                dialog.dismiss();
                final Export selectedExport = adapter.getItem(item);
                selectedExport.export(caches, activity);
            }
        });

        builder.create().show();
    }
}