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

FavIconHandler.java « helper « 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: 9f2ac621ab4f23194827712953497454f59994bf (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
/**
* 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.helper;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.util.SparseArray;
import android.widget.ImageView;

import java.io.File;
import java.lang.ref.WeakReference;

import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.async_tasks.GetImageAsyncTask;
import de.luhmer.owncloudnewsreader.database.DatabaseConnection;

public class FavIconHandler {
    private static final String TAG = "FavIconHandler";

    public static Drawable GetFavIconFromCache(String URL_TO_PAGE, Context context, String feedID)
	{
		try
		{
			File favIconFile = ImageHandler.getFullPathOfCacheFile(URL_TO_PAGE, ImageHandler.getPathFavIcons(context));
			if(favIconFile.isFile() && favIconFile.length() > 0)
			{
				if(feedID != null) {
                	DatabaseConnection dbConn = new DatabaseConnection(context);
                	try {
                		if(dbConn.getAvgColourOfFeedByDbId(feedID) == null) {
	                		Bitmap bitmap = BitmapFactory.decodeFile(favIconFile.getAbsolutePath());
	                		String avg = ColourCalculator.ColourHexFromBitmap(bitmap);
	                		dbConn.setAvgColourOfFeedByDbId(feedID, avg);
                		}
                	} finally {
                		dbConn.closeDatabase();
                	}
                }

				return Drawable.createFromPath(favIconFile.getPath());
			}
		}
		catch(Exception ex)
		{
            //Log.d(TAG, ex.getMessage());
			//ex.printStackTrace();
		}
		return null;
	}

    public static int getResourceIdForRightDefaultFeedIcon(Context context)
	{
		if(ThemeChooser.isDarkTheme(context))
			return R.drawable.default_feed_icon_light;
		else
			return R.drawable.default_feed_icon_dark;

	}

	static SparseArray<FavIconCache> imageViewReferences = new SparseArray<FavIconCache>();
	String feedID;

    static SparseArray<FavIconCache> favIconToFeedId = new SparseArray<FavIconCache>();
    public static void PreCacheFavIcon(String WEB_URL_TO_FILE, Context context, String feedID) {

        FavIconCache favIconCache = new FavIconCache();
        favIconCache.context = context;
        favIconCache.WEB_URL_TO_FILE = WEB_URL_TO_FILE;

        int key = Integer.parseInt(feedID);
        favIconToFeedId.put(key, favIconCache);
        GetImageAsyncTask giAsync = new GetImageAsyncTask(WEB_URL_TO_FILE, favIconDownloadFinished, key, ImageHandler.getPathFavIcons(context), context, null);
        giAsync.scaleImage = true;
        giAsync.dstHeight = 2*32;
        giAsync.dstWidth = 2*32;
        giAsync.feedID = feedID;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            // Execute in parallel
            giAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ((Void)null));
        else
            giAsync.execute((Void)null);
    }

    static ImageDownloadFinished favIconDownloadFinished = new ImageDownloadFinished() {

        @Override
        public void DownloadFinished(int AsynkTaskId, String fileCachePath, BitmapDrawableLruCache lruCache) {
            FavIconCache favIconCache = favIconToFeedId.get(AsynkTaskId);
            FavIconHandler.GetFavIconFromCache(favIconCache.WEB_URL_TO_FILE, favIconCache.context, String.valueOf(AsynkTaskId));
            imageViewReferences.remove(AsynkTaskId);
        }
    };


	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void GetImageAsync(ImageView imageView, String WEB_URL_TO_FILE, Context context, String feedID, BitmapDrawableLruCache lruCache)
	{
		this.feedID = feedID;

		boolean setImageAlready = false;
		if(lruCache != null) {
			if(lruCache.get(feedID) != null) {
				if (imageView != null) {
	                imageView.setImageDrawable(lruCache.get(feedID));
	                setImageAlready = true;
	            }
			}
		}
		if(!setImageAlready) {
			WeakReference<ImageView> imageViewReference = new WeakReference<ImageView>(imageView);
			FavIconCache favIconCache = new FavIconCache();
			favIconCache.context = context;
			favIconCache.WEB_URL_TO_FILE = WEB_URL_TO_FILE;
			favIconCache.imageViewReference = imageViewReference;

			int key = 0;
			if(imageViewReferences.size() > 0)
				key = imageViewReferences.keyAt(imageViewReferences.size() -1) + 1;
			imageViewReferences.append(key, favIconCache);


			imageView.setImageDrawable(null);
			GetImageAsyncTask giAsync = new GetImageAsyncTask(WEB_URL_TO_FILE, imgDownloadFinished, key, ImageHandler.getPathFavIcons(context), context/*, imageView*/, lruCache);
			giAsync.scaleImage = true;
			giAsync.dstHeight = 2*32;
			giAsync.dstWidth = 2*32;
			giAsync.feedID = feedID;
			//giAsync.execute((Void)null);

			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
				// Execute in parallel
				giAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ((Void)null));
			else
				giAsync.execute((Void)null);
		}
	}

	ImageDownloadFinished imgDownloadFinished = new ImageDownloadFinished() {

		@Override
		public void DownloadFinished(int AsynkTaskId, String fileCachePath, BitmapDrawableLruCache lruCache) {
			//WeakReference<ImageView> imageViewRef = imageViewReferences.get(AsynkTaskId);
			FavIconCache favIconCache = imageViewReferences.get(AsynkTaskId);
			WeakReference<ImageView> imageViewRef = favIconCache.imageViewReference;

			if(imageViewRef != null)
			{
	            ImageView imageView = imageViewRef.get();
	            if (imageView != null) {
	            	BitmapDrawable bd = (BitmapDrawable) FavIconHandler.GetFavIconFromCache(favIconCache.WEB_URL_TO_FILE, favIconCache.context, feedID);
	            	if(lruCache != null && feedID != null && bd != null)
	            		lruCache.put(feedID, bd);
	                imageView.setImageDrawable(bd);
	            }
			}

			imageViewReferences.remove(AsynkTaskId);
		}
	};

	static class FavIconCache
	{
		public WeakReference<ImageView> imageViewReference;
		public String WEB_URL_TO_FILE;
		public Context context;
	}
}