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: c02e80aa5c9d9fb853777920072ed7edac48e57c (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* 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.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

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

import de.greenrobot.dao.query.LazyList;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageThreaded;
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.ImageDownloadFinished;
import de.luhmer.owncloudnewsreader.helper.ImageHandler;

public class DownloadImagesService extends IntentService {

	public static final String LAST_ITEM_ID = "LAST_ITEM_ID";
    public static final String DOWNLOAD_FAVICONS_EXCLUSIVE = "DOWNLOAD_FAVICONS_EXCLUSIVE";
	private static Random random;

	private int NOTIFICATION_ID = 1;
	private NotificationManager notificationManager;
	private NotificationCompat.Builder mNotificationDownloadImages;

	private int maxCount;
	//private int total_size = 0;

    List<String> linksToImages = new LinkedList<>();

	public DownloadImagesService() {
		super(null);
	}

	public DownloadImagesService(String name) {
		super(name);
	}

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

    @Override
	public void onDestroy() {
		if(mNotificationDownloadImages != null)
		{
			if(maxCount == 0)
				notificationManager.cancel(NOTIFICATION_ID);
		}
		super.onDestroy();
	}

	@Override
	protected void onHandleIntent(Intent intent) {
        boolean downloadFavIconsExclusive = intent.getBooleanExtra(DOWNLOAD_FAVICONS_EXCLUSIVE, false);

        DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(this);
        Notification notify = BuildNotification();

        List<Feed> feedList = dbConn.getListOfFeeds();
        FavIconHandler favIconHandler = new FavIconHandler(this);
        for(Feed feed : feedList) {
            favIconHandler.PreCacheFavIcon(feed);
        }


        if(!downloadFavIconsExclusive) {
            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) {
                    notificationManager.notify(123, GetNotificationLimitImagesReached(10000));
                    break;
                }
            }
            ((LazyList)rssItemList).close();

            maxCount = links.size();

            if (maxCount > 0) {
                notificationManager.notify(NOTIFICATION_ID, notify);
            }

            linksToImages.addAll(links);

            StartNextDownloadInQueue();
        }
	}

    private synchronized void StartNextDownloadInQueue() {
        try {
            if(linksToImages.size() > 0) {
                String link = linksToImages.remove(0);
                new GetImageThreaded(link, imgDownloadFinished, 999).start();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Toast.makeText(this, "Error while downloading images.", Toast.LENGTH_LONG).show();
        }
    }

    private Notification GetNotificationLimitImagesReached(int limit) {
        Intent intentNewsReader = new Intent(this, NewsReaderListActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentNewsReader, 0);
        NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("ownCloud News Reader")
                .setContentText("Only " + limit + " images can be cached at once")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent);

        Notification notify = notifyBuilder.build();

        //Hide the notification after its selected
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        return notify;
    }

    private Notification BuildNotification() {
        Intent intentNewsReader = new Intent(this, NewsReaderListActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intentNewsReader, 0);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationDownloadImages = new NotificationCompat.Builder(this)
                .setContentTitle("ownCloud News Reader")
                .setContentText("Downloading Images for offline usage")
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent);

        Notification notify = mNotificationDownloadImages.build();

        //Hide the notification after its selected
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        //notify.flags |= Notification.FLAG_NO_CLEAR;

        return notify;
    }

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

	ImageDownloadFinished imgDownloadFinished = new ImageDownloadFinished() {

		@Override
		public void DownloadFinished(long ThreadId, Bitmap bitmap) {
            int count = maxCount - linksToImages.size();

            if(maxCount == count) {
            	notificationManager.cancel(NOTIFICATION_ID);
                //RemoveOldImages();
            } else {
                mNotificationDownloadImages
                        .setContentText("Downloading Images for offline usage - " + (count + 1) + "/" + maxCount)
                        .setProgress(maxCount, count + 1, false);

                notificationManager.notify(NOTIFICATION_ID, mNotificationDownloadImages.build());

                StartNextDownloadInQueue();
            }
		}
	};
}