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

ImageHandler.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: 90c2029fd96e3c39fee787d949e13619c6c27d30 (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
211
212
213
214
215
216
217
218
219
220
/**
* 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.content.Context;
import android.graphics.drawable.Drawable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImageHandler {
    //private static final String TAG = "DownloadImagesFromWeb";



    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }




    public static List<File> getFilesFromDir(File dir) {
    	List<File> files = new ArrayList<File>();
		if(dir.isDirectory())
			for (File file : dir.listFiles())
			    if (file.isFile())
			        files.add(file);
		return files;
	}

    public static long getFolderSize(File dir) {
    	long size = 0;
		if(dir.isDirectory())
		{
			for (File file : dir.listFiles()) {
			    if (file.isFile()) {
			        size += file.length();
			    }
			    else
			        size += getFolderSize(file);
			}
		}
		return size;
	}

    public static File getFullPathOfCacheFileSafe(String WEB_URL_TO_FILE, String rootPath) {
        try {
            return getFullPathOfCacheFile(WEB_URL_TO_FILE, rootPath);
        } catch (Exception ex) {
            return null;
        }
    }

    public static File getFullPathOfCacheFile(String WEB_URL_TO_FILE, String rootPath) throws Exception
	{
		URL url = new URL(WEB_URL_TO_FILE.trim());

		MessageDigest m = MessageDigest.getInstance("MD5");
		m.reset();
		m.update(url.toString().getBytes());
		byte[] digest = m.digest();
		BigInteger bigInt = new BigInteger(1,digest);
		String hashtext = bigInt.toString(16);

		String fileEnding = "";
		try
		{
			fileEnding = url.getFile().substring(url.getFile().lastIndexOf("."));
			fileEnding = fileEnding.replaceAll("\\?(.*)", "");
		}
		catch(Exception ex)
		{
			fileEnding = ".png";
			//ex.printStackTrace();
		}

		return new File(rootPath + "/" + hashtext  + fileEnding);
	}




	public static List<String> getImageLinksFromText(String text)
	{
		List<String> links = new ArrayList<String>();
		Pattern pattern = Pattern.compile("<img[^>]*>");
		Pattern patternSrcLink = Pattern.compile("src=\"(.*?)\"");
		Matcher matcher = pattern.matcher(text);
	    // Check all occurance
	    while (matcher.find()) {
	    	//System.out.print("Start index: " + matcher.start());
	    	//System.out.print(" End index: " + matcher.end() + " ");
	    	//System.out.println(matcher.group());

	    	Matcher matcherSrcLink = patternSrcLink.matcher(matcher.group());
	    	if(matcherSrcLink.find()) {
	    		links.add(matcherSrcLink.group(1));
	    	}
	    }
	    return links;
	}


	public static boolean clearCache(Context context)
	{
        String path = FileUtils.getPath(context);
		boolean result = deleteDir(new File(path));
        createNoMediaFile(context);
        return result;
	}

    public static void createNoMediaFile(Context context) {
        String path = FileUtils.getPath(context);
        createEmptyFile(path + "/.nomedia");
    }

    public static void createEmptyFile(String path) {
        try {
            File file = new File(path);
            if(!file.exists()) {
                new File(file.getParent()).mkdirs();
                FileOutputStream fOut = new FileOutputStream(file, false);
                OutputStreamWriter osw = new OutputStreamWriter(fOut);
                osw.flush();
                osw.close();
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

	// Deletes all files and subdirectories under dir.
	// Returns true if all deletions were successful.
	// If a deletion fails, the method stops attempting to delete and returns
	// false.
	private static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}






    /*
    public static Bitmap loadBitmap(String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            copy(in, out);
            out.flush();

            final byte[] data = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inSampleSize = 1;

            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
        } catch (IOException e) {
            Log.d(TAG, "Could not load Bitmap from: " + url);
        } finally {
            closeStream(in);
            closeStream(out);
        }

        return bitmap;
    }*/
}