fixed sending cached images
[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.database.MatrixCursor;
31 import android.graphics.Bitmap;
32 import android.net.Uri;
33 import android.os.ParcelFileDescriptor;
34 import android.provider.OpenableColumns;
35
36 import com.owncloud.android.MainApp;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
41 import com.owncloud.android.lib.common.utils.Log_OC;
42
43 import java.io.ByteArrayOutputStream;
44 import java.io.File;
45 import java.io.FileNotFoundException;
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48
49 public class DiskLruImageCacheFileProvider extends ContentProvider {
50 private static String TAG = FileDataStorageManager.class.getSimpleName();
51 private FileDataStorageManager mFileDataStorageManager;
52
53 public static final String AUTHORITY = "org.owncloud.beta.imageCache.provider";
54
55 @Override
56 public boolean onCreate() {
57 return true;
58 }
59
60 private OCFile getFile(Uri uri){
61 Account account = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
62 mFileDataStorageManager = new FileDataStorageManager(account,
63 MainApp.getAppContext().getContentResolver());
64
65 OCFile ocFile = mFileDataStorageManager.getFileByPath(uri.getPath());
66 return ocFile;
67 }
68
69 @Override
70 public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
71 Log_OC.d(TAG, "try to send: " + uri);
72
73 OCFile ocFile = getFile(uri);
74
75 Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
76 String.valueOf("r" + ocFile.getRemoteId()));
77
78 // create a file to write bitmap data
79 File f = new File(MainApp.getAppContext().getCacheDir(), ocFile.getFileName());
80 try {
81 f.createNewFile();
82
83 //Convert bitmap to byte array
84 ByteArrayOutputStream bos = new ByteArrayOutputStream();
85 thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bos);
86 byte[] bitmapdata = bos.toByteArray();
87
88 //write the bytes in file
89 FileOutputStream fos = null;
90 try {
91 fos = new FileOutputStream(f);
92 } catch (FileNotFoundException e) {
93 e.printStackTrace();
94 }
95 fos.write(bitmapdata);
96 fos.flush();
97 fos.close();
98
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102
103 return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
104 }
105
106 @Override
107 public String getType(Uri uri) {
108 OCFile ocFile = getFile(uri);
109 return ocFile.getMimetype();
110 }
111
112 @Override
113 public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3, String arg4) {
114 MatrixCursor cursor = null;
115
116 OCFile ocFile = getFile(uri);
117 File file = new File(MainApp.getAppContext().getCacheDir(), ocFile.getFileName());
118 if (file.exists()) {
119 cursor = new MatrixCursor(new String[] {
120 OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE });
121 cursor.addRow(new Object[] { uri.getLastPathSegment(),
122 file.length() });
123 }
124
125 return cursor;
126 }
127
128 @Override
129 public Uri insert(Uri uri, ContentValues values) {
130 return null;
131 }
132
133 @Override
134 public int delete(Uri uri, String selection, String[] selectionArgs) {
135 return 0;
136 }
137
138 @Override
139 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
140 return 0;
141 }
142 }