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

import butterknife.ButterKnife;
import butterknife.InjectView;

import cgeo.geocaching.files.AbstractFileListActivity;
import cgeo.geocaching.files.IFileSelectionView;
import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.files.SimpleDirChooser;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.FileSelectionListAdapter;

import org.openintents.intents.FileManagerIntents;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SelectMapfileActivity extends AbstractFileListActivity<FileSelectionListAdapter> implements IFileSelectionView {

    public SelectMapfileActivity() {
        super("map");
    }

    @InjectView(R.id.select_dir) protected Button selectDirectory;

    private String mapFile;

    private final static int REQUEST_DIRECTORY = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.inject(this);

        mapFile = Settings.getMapFile();

        selectDirectory.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    final Intent dirChooser = new Intent(FileManagerIntents.ACTION_PICK_DIRECTORY);
                    dirChooser.putExtra(FileManagerIntents.EXTRA_TITLE,
                            getString(R.string.simple_dir_chooser_title));
                    dirChooser.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT,
                            getString(android.R.string.ok));
                    startActivityForResult(dirChooser, REQUEST_DIRECTORY);
                } catch (android.content.ActivityNotFoundException ignored) {
                    // OI file manager not available
                    final Intent dirChooser = new Intent(SelectMapfileActivity.this, SimpleDirChooser.class);
                    dirChooser.putExtra(Intents.EXTRA_START_DIR, LocalStorage.getStorage().getAbsolutePath());
                    startActivityForResult(dirChooser, REQUEST_DIRECTORY);
                }
            }
        });
        selectDirectory.setText(getResources().getString(R.string.simple_dir_chooser_title));
        selectDirectory.setVisibility(View.VISIBLE);
    }

    @Override
    public void close() {

        Intent intent = new Intent();
        intent.putExtra(Intents.EXTRA_MAP_FILE, mapFile);

        setResult(RESULT_OK, intent);

        finish();
    }

    @Override
    protected FileSelectionListAdapter getAdapter(List<File> files) {
        return new FileSelectionListAdapter(this, files);
    }

    @Override
    protected List<File> getBaseFolders() {
        List<File> folders = new ArrayList<>();
        for (File dir : LocalStorage.getStorages()) {
            folders.add(new File(dir, "mfmaps"));
            folders.add(new File(new File(dir, "Locus"), "mapsVector"));
            folders.add(new File(dir, LocalStorage.CACHE_DIRNAME));
        }
        return folders;
    }

    @Override
    public String getCurrentFile() {
        return mapFile;
    }

    @Override
    public void setCurrentFile(String name) {
        mapFile = name;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        if (requestCode == REQUEST_DIRECTORY) {
            mapFile = new File(data.getData().getPath()).getAbsolutePath();
            close();
        }
    }

    @Override
    protected boolean requireFiles() {
        return false;
    }
}