Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Helpers/FilesHelper.java
Show All 14 Lines | |||||
15 | * GNU General Public License for more details. | 15 | * GNU General Public License for more details. | ||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | package org.kde.kdeconnect.Helpers; | 21 | package org.kde.kdeconnect.Helpers; | ||
22 | 22 | | |||
23 | import android.content.ContentResolver; | ||||
24 | import android.content.Context; | ||||
25 | import android.database.Cursor; | ||||
26 | import android.net.Uri; | ||||
27 | import android.provider.MediaStore; | ||||
23 | import android.util.Log; | 28 | import android.util.Log; | ||
24 | import android.webkit.MimeTypeMap; | 29 | import android.webkit.MimeTypeMap; | ||
25 | 30 | | |||
31 | import org.kde.kdeconnect.NetworkPacket; | ||||
32 | | ||||
26 | import java.io.File; | 33 | import java.io.File; | ||
34 | import java.io.InputStream; | ||||
27 | 35 | | |||
28 | public class FilesHelper { | 36 | public class FilesHelper { | ||
29 | 37 | | |||
30 | private static String getFileExt(String filename) { | 38 | private static String getFileExt(String filename) { | ||
31 | //return MimeTypeMap.getFileExtensionFromUrl(filename); | 39 | //return MimeTypeMap.getFileExtensionFromUrl(filename); | ||
32 | return filename.substring((filename.lastIndexOf(".") + 1)); | 40 | return filename.substring((filename.lastIndexOf(".") + 1)); | ||
33 | } | 41 | } | ||
34 | 42 | | |||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | |||||
95 | 103 | | |||
96 | private static int GetOpenFileCount() { | 104 | private static int GetOpenFileCount() { | ||
97 | return new File("/proc/self/fd").listFiles().length; | 105 | return new File("/proc/self/fd").listFiles().length; | ||
98 | } | 106 | } | ||
99 | 107 | | |||
100 | public static void LogOpenFileCount() { | 108 | public static void LogOpenFileCount() { | ||
101 | Log.e("KDE/FileCount", "" + GetOpenFileCount()); | 109 | Log.e("KDE/FileCount", "" + GetOpenFileCount()); | ||
102 | } | 110 | } | ||
111 | | ||||
112 | | ||||
113 | //Create the network package from the URI | ||||
114 | public static NetworkPacket uriToNetworkPacket(final Context context, final Uri uri, String type) { | ||||
115 | | ||||
116 | try { | ||||
117 | | ||||
118 | ContentResolver cr = context.getContentResolver(); | ||||
119 | InputStream inputStream = cr.openInputStream(uri); | ||||
120 | | ||||
121 | NetworkPacket np = new NetworkPacket(type); | ||||
122 | long size = -1; | ||||
123 | | ||||
124 | if (uri.getScheme().equals("file")) { | ||||
125 | // file:// is a non media uri, so we cannot query the ContentProvider | ||||
126 | | ||||
127 | np.set("filename", uri.getLastPathSegment()); | ||||
128 | | ||||
129 | try { | ||||
130 | size = new File(uri.getPath()).length(); | ||||
131 | } catch (Exception e) { | ||||
132 | Log.e("SendFileActivity", "Could not obtain file size"); | ||||
133 | e.printStackTrace(); | ||||
134 | } | ||||
135 | | ||||
136 | } else { | ||||
137 | // Probably a content:// uri, so we query the Media content provider | ||||
138 | | ||||
139 | Cursor cursor = null; | ||||
140 | try { | ||||
141 | String[] proj = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.DISPLAY_NAME}; | ||||
142 | cursor = cr.query(uri, proj, null, null, null); | ||||
143 | int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); | ||||
144 | cursor.moveToFirst(); | ||||
145 | String path = cursor.getString(column_index); | ||||
146 | np.set("filename", Uri.parse(path).getLastPathSegment()); | ||||
147 | size = new File(path).length(); | ||||
148 | } catch (Exception unused) { | ||||
149 | | ||||
150 | Log.w("SendFileActivity", "Could not resolve media to a file, trying to get info as media"); | ||||
151 | | ||||
152 | try { | ||||
153 | int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME); | ||||
154 | cursor.moveToFirst(); | ||||
155 | String name = cursor.getString(column_index); | ||||
156 | np.set("filename", name); | ||||
157 | } catch (Exception e) { | ||||
158 | e.printStackTrace(); | ||||
159 | Log.e("SendFileActivity", "Could not obtain file name"); | ||||
160 | } | ||||
161 | | ||||
162 | try { | ||||
163 | int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE); | ||||
164 | cursor.moveToFirst(); | ||||
165 | //For some reason this size can differ from the actual file size! | ||||
166 | size = cursor.getInt(column_index); | ||||
167 | } catch (Exception e) { | ||||
168 | Log.e("SendFileActivity", "Could not obtain file size"); | ||||
169 | e.printStackTrace(); | ||||
170 | } | ||||
171 | } finally { | ||||
172 | try { | ||||
173 | cursor.close(); | ||||
174 | } catch (Exception ignored) { | ||||
175 | } | ||||
176 | } | ||||
177 | | ||||
178 | } | ||||
179 | | ||||
180 | np.setPayload(new NetworkPacket.Payload(inputStream, size)); | ||||
181 | | ||||
182 | return np; | ||||
183 | } catch (Exception e) { | ||||
184 | Log.e("SendFileActivity", "Exception creating network packet", e); | ||||
185 | e.printStackTrace(); | ||||
186 | return null; | ||||
187 | } | ||||
188 | } | ||||
103 | } | 189 | } |