wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*  Copyright (C) 2021-2024 Gordon Williams, Petr Vaněk
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
 
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
 
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
 
public class GpxReceiverActivity extends AbstractGBActivity {
    private static final Logger LOG = LoggerFactory.getLogger(ActivitySummaryDetail.class);
    boolean toOverwrite = false;
    ArrayList<FileToProcess> fileList = new ArrayList<>();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpx_receiver);
        Button gpx_receiver_ok = findViewById(R.id.gpx_receiver_ok);
        Button gpx_receiver_cancel = findViewById(R.id.gpx_receiver_cancel);
        View gpx_receiver_overwrite_label = findViewById(R.id.gpx_receiver_overwrite_label);
        TextView gpx_receiver_files_listing = findViewById(R.id.gpx_receiver_files_listing);
        TextView gpx_receiver_received_label = findViewById(R.id.gpx_receiver_received_label);
 
        gpx_receiver_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
 
        gpx_receiver_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (FileToProcess fileToProcess : fileList) {
                    save_file(fileToProcess.source, fileToProcess.destination);
                }
                finish();
            }
        });
 
        final Intent intent = getIntent();
        final ClipData intentClipData = intent.getClipData();
        StringBuilder fileListingText = new StringBuilder();
        ArrayList<Uri> documentUris = new ArrayList<>();
 
        if (savedInstanceState == null) {
            if (intent.getData() != null) {
                documentUris.add(intent.getData());
            } else {
                if (intentClipData != null && intentClipData.getItemCount() > 0) {
                    for (int i = 0; i < intentClipData.getItemCount(); i++) {
                        documentUris.add(intentClipData.getItemAt(i).getUri());
                    }
                }
            }
        }
 
        if (documentUris != null) {
            for (Uri uri : documentUris) {
                if (uri!=null && uri.getPath().toLowerCase().endsWith(".gpx")) {
                    FileToProcess file = new FileToProcess(uri);
                    fileList.add(file);
                    fileListingText.append(String.format("%s %s\n\n", file.name, file.exists ? getString(R.string.dbmanagementactivity_overwrite) : ""));
                }
            }
        }
 
        if (toOverwrite) {
            gpx_receiver_overwrite_label.setVisibility(View.VISIBLE);
        } else {
            gpx_receiver_overwrite_label.setVisibility(View.GONE);
        }
 
        gpx_receiver_received_label.setText(String.format("%s %s", getString(R.string.gpx_receiver_files_received), fileList.toArray().length));
        gpx_receiver_files_listing.setText(fileListingText.toString());
    }
 
    private String get_file_name(Uri source) {
        int cut = source.getPath().lastIndexOf("/");
        String fileName = null;
        if (cut != -1) {
            fileName = source.getPath().substring(cut + 1);
        }
        return fileName;
    }
 
    private File create_file_from_uri(Uri source) {
        File destination = null;
        try {
            File external = FileUtils.getExternalFilesDir();
            String fileName = get_file_name(source);
 
            if (fileName != null) {
                destination = new File(external + "/" + fileName);
            }
        } catch (IOException exception) {
            LOG.error("Error creating file", exception);
        }
        return destination;
    }
 
    private void save_file(Uri source, File destination) {
        try {
            String fileName = get_file_name(source);
            if (fileName != null) {
                FileUtils.copyURItoFile(GpxReceiverActivity.this, source, destination);
            }
 
        } catch (IOException exception) {
            LOG.error("Error copying file", exception);
        }
    }
 
    private class FileToProcess {
        String name;
        Uri source;
        File destination;
        boolean exists;
 
        FileToProcess(Uri file) {
            this.name = get_file_name(file);
            this.source = file;
            this.destination = create_file_from_uri(file);
            this.exists = this.destination.exists();
 
            if (this.exists) {
                toOverwrite = true;
            }
        }
    }
}