Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DownloadChangelogTask.java « async_tasks « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d43ffab6f2f87aea048932f0f9b45c679a9e66d4 (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
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
160
161
162
163
164
package de.luhmer.owncloudnewsreader.async_tasks;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.AsyncTask;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import de.luhmer.owncloudnewsreader.view.ChangeLogFileListView;

/**
 * Downloads the owncloud news reader changelog from github, transforms it into xml
 * and saves it as tempfile. This xml tempfile can be used for changeloglib library.
 */
public class DownloadChangelogTask extends AsyncTask<Void, Void, String> {

    @SuppressWarnings("unused")
    private static final String TAG = "DownloadChangelogTask";

    private static final String README_URL = "https://raw.githubusercontent.com/nextcloud/news-android/master/CHANGELOG.md";
    private static final String FILE_NAME = "changelog.xml";

    private Context mContext;
    private ChangeLogFileListView mChangelogView;
    private Listener mListener;
    private IOException exception;

    /**
     * @param context
     * @param changelogView  this list view will be automatically filled when
     *                       downloading and saving has finished
     * @param listener       called when task has finished or errors have been raised
     */
    public DownloadChangelogTask(Context context,
                                 ChangeLogFileListView changelogView,
                                 Listener listener) {
        mContext = context;
        mChangelogView = changelogView;
        mListener = listener;
    }


    @Override
    protected String doInBackground(Void... params) {
        String path = null;

        try {
            ArrayList<String> changelogArr = downloadReadme();
            String xml = convertToXML(changelogArr);
            path = saveToTempFile(xml, FILE_NAME);
        } catch (IOException e) {
            exception = e;
        }

        return path;
    }

    @Override
    protected void onPostExecute(String filePath) {
        if (exception != null) {
            mListener.onError(exception);
            return;
        }

        mChangelogView.loadFile(filePath);
        mChangelogView.getAdapter().registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                mListener.onSuccess();
            }
        });
    }

    private ArrayList<String> downloadReadme() throws IOException {
        ArrayList<String> changelogArr = new ArrayList<>();

        URL url = new URL(README_URL);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream isTemp = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(isTemp));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                changelogArr.add(inputLine.replace("<", "[").replace(">", "]"));
            }
            in.close();
        } finally {
            urlConnection.disconnect();
        }

        return changelogArr;
    }

    private String convertToXML(ArrayList<String> changelogArr) {
        changelogArr.add("");

        // create xml nodes
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        builder.append("<changelog bulletedList=\"true\">");

        boolean versionStarted = false;

        for (String line : changelogArr) {
            if (line.startsWith("- ")) {
                // change entry
                builder.append("<changelogtext>");
                builder.append(line.substring(2).trim());
                builder.append("</changelogtext>");
            } else if (line.equals("")) {
                // version end
                if (versionStarted) {
                    versionStarted = false;
                    builder.append("</changelogversion>");
                }
            } else if (!line.contains("---------------------")) {
                // version start
                versionStarted = true;
                builder.append("<changelogversion versionName=\"" + line + "\">");
            }
        }

        builder.append("</changelog>");

        return builder.toString();
    }

    private String saveToTempFile(String content, String fileName) throws IOException {
        File file = File.createTempFile(fileName, null, mContext.getCacheDir());
        BufferedWriter out = new BufferedWriter(new FileWriter(file));

        try {
            out.write(content);
        } finally {
            out.close();
        }

        return "file://" + file.getAbsolutePath();
    }


    public interface Listener {

        /**
         * Called when ChangeLogFileListView instance has successfully been updated.
         */
        void onSuccess();

        /**
         * Called when some error has been thrown during download, parsing or saving.
         */
        void onError(IOException e);
    }
}