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

DownloadImagesService.java « services « 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: c6aec03cd411f893f3f6076338a6b24bda60c264 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package de.luhmer.owncloudnewsreader.services;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationCompat;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import de.greenrobot.dao.query.LazyList;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.async_tasks.DownloadImageHandler;
import de.luhmer.owncloudnewsreader.database.DatabaseConnectionOrm;
import de.luhmer.owncloudnewsreader.database.model.Feed;
import de.luhmer.owncloudnewsreader.database.model.RssItem;
import de.luhmer.owncloudnewsreader.helper.FavIconHandler;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;
import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;

public class DownloadImagesService extends JobIntentService {

	public static final String LAST_ITEM_ID = "LAST_ITEM_ID";
    private static final String TAG = DownloadImagesService.class.getCanonicalName();

    public enum DownloadMode { FAVICONS_ONLY, PICTURES_ONLY, FAVICONS_AND_PICTURES }
    public static final String DOWNLOAD_MODE_STRING = "DOWNLOAD_MODE";
	private static Random random;

	private int NOTIFICATION_ID = 1923;
	private NotificationCompat.Builder mNotificationDownloadImages;

    private int maxCount;
    private NotificationManager mNotificationManager;


    /**
     * Unique job/channel ID for this service.
     */
    private static final int JOB_ID = 1000;
    private static final String CHANNEL_ID = "Download Images Service";


    /**
     * Convenience method for enqueuing work in to this service.
     */
    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, DownloadImagesService.class, JOB_ID, work);
    }



    @Override
    public void onCreate() {
        super.onCreate();
        try {
            maxCount = 0;
            if (random == null)
                random = new Random();
            NOTIFICATION_ID = random.nextInt();

            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
	public void onDestroy() {
        Log.d(TAG, "onDestroy");
		if(mNotificationDownloadImages != null)
		{
			if(maxCount == 0) {
                mNotificationManager.cancel(NOTIFICATION_ID);
            }
		}
		super.onDestroy();
	}

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        DownloadMode downloadMode = (DownloadMode) intent.getSerializableExtra(DOWNLOAD_MODE_STRING);

        DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(this);
        mNotificationDownloadImages = NextcloudNotificationManager.buildNotificationDownloadImageService(this, CHANNEL_ID);

        if(downloadMode.equals(DownloadMode.FAVICONS_ONLY)) {
            List<Feed> feedList = dbConn.getListOfFeeds();
            FavIconHandler favIconHandler = new FavIconHandler(getApplicationContext());
            for(Feed feed : feedList) {
                try {
                    favIconHandler.preCacheFavIcon(feed, getApplicationContext());
                } catch(IllegalStateException ex) {
                    Log.e(TAG, ex.getMessage());
                }
            }
        } else if(downloadMode.equals(DownloadMode.FAVICONS_AND_PICTURES) || downloadMode.equals(DownloadMode.PICTURES_ONLY)) {
            long lastId = intent.getLongExtra(LAST_ITEM_ID, 0);
            List<RssItem> rssItemList = dbConn.getAllItemsWithIdHigher(lastId);
            List<String> links = new ArrayList<>();
            for(RssItem rssItem : rssItemList) {
                String body = rssItem.getBody();
                links.addAll(ImageHandler.getImageLinksFromText(body));

                if(links.size() > 10000) {
                    NextcloudNotificationManager.showNotificationImageDownloadLimitReached(this, CHANNEL_ID, 10000);
                    break;
                }
            }
            ((LazyList)rssItemList).close();

            maxCount = links.size();

            if (maxCount > 0) {
                mNotificationManager.notify(NOTIFICATION_ID, mNotificationDownloadImages.build());
            }

            downloadImages(links);
        }
	}

    private void downloadImages(List<String> linksToImages) {
        try {
            while(linksToImages.size() > 0) {
                String link = linksToImages.remove(0);
                new DownloadImageHandler(link).downloadSync();

                updateNotificationProgress(linksToImages.size());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.e(TAG, "Error while downloading images.");
            mNotificationDownloadImages
                    .setContentText("Error while downloading images - " + ex.toString())
                    .setProgress(0, 0, false);
            mNotificationManager.notify(NOTIFICATION_ID, mNotificationDownloadImages.build());
        }
    }

    private void updateNotificationProgress(int remainingImagesCount) {
        int count = maxCount - remainingImagesCount;
        if(maxCount == count) {
            mNotificationManager.cancel(NOTIFICATION_ID);
            //RemoveOldImages();
        } else {
            mNotificationDownloadImages
                    .setContentText((count + 1) + "/" + maxCount + " - " + getString(R.string.notification_download_images_offline))
                    .setProgress(maxCount, count + 1, false);

            mNotificationManager.notify(NOTIFICATION_ID, mNotificationDownloadImages.build());
        }
    }

    private void RemoveOldImages() {
        ImageLoader.getInstance().clearDiskCache();
    }

}