Merge remote-tracking branch 'remotes/upstream/streaming' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / adapter / DiskLruImageCacheFileProvider.java
1 /**
2 * ownCloud Android client application
3 *
4 * Copyright (C) 2015 Tobias Kaminsky
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * adapted from: http://stephendnicholas.com/archives/974
20 *
21 */
22
23 package com.owncloud.android.ui.adapter;
24
25 import android.accounts.Account;
26 import android.content.ContentProvider;
27 import android.content.ContentValues;
28 import android.content.UriMatcher;
29 import android.database.Cursor;
30 import android.graphics.Bitmap;
31 import android.net.Uri;
32 import android.os.ParcelFileDescriptor;
33
34 import com.owncloud.android.MainApp;
35 import com.owncloud.android.authentication.AccountUtils;
36 import com.owncloud.android.datamodel.FileDataStorageManager;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
39 import com.owncloud.android.lib.common.utils.Log_OC;
40
41 import java.io.ByteArrayOutputStream;
42 import java.io.File;
43 import java.io.FileNotFoundException;
44 import java.io.FileOutputStream;
45 import java.io.IOException;
46
47 public class DiskLruImageCacheFileProvider extends ContentProvider {
48 private static String TAG = FileDataStorageManager.class.getSimpleName();
49
50 public static final String AUTHORITY = "com.owncloud.imageCache.provider";
51
52 @Override
53 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
54 Account account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
55 FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account,
56 MainApp.getAppContext().getContentResolver());
57
58 OCFile ocFile = fileDataStorageManager.getFileByPath(uri.getPath());
59
60 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
61 String.valueOf("r" + ocFile.getRemoteId()));
62
63 // create a file to write bitmap data
64 File f = new File(MainApp.getAppContext().getCacheDir(), ocFile.getFileName());
65 try {
66 f.createNewFile();
67
68 //Convert bitmap to byte array
69 ByteArrayOutputStream bos = new ByteArrayOutputStream();
70 thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bos);
71 byte[] bitmapdata = bos.toByteArray();
72
73 //write the bytes in file
74 FileOutputStream fos = null;
75 try {
76 fos = new FileOutputStream(f);
77 } catch (FileNotFoundException e) {
78 e.printStackTrace();
79 }
80 fos.write(bitmapdata);
81 fos.flush();
82 fos.close();
83
84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87
88 return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
89 }
90
91
92 @Override
93 public boolean onCreate() {
94 return true;
95 }
96
97 @Override
98 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
99 return null;
100 }
101
102 @Override
103 public String getType(Uri uri) {
104 return null;
105 }
106
107 @Override
108 public Uri insert(Uri uri, ContentValues values) {
109 return null;
110 }
111
112 @Override
113 public int delete(Uri uri, String selection, String[] selectionArgs) {
114 return 0;
115 }
116
117 @Override
118 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
119 return 0;
120 }
121 }