Skip to content

Commit f5255ff

Browse files
authored
Merge pull request #80 from marcosdiez/new_android_api
android api 34
2 parents 5515650 + f37c74e commit f5255ff

File tree

9 files changed

+168
-145
lines changed

9 files changed

+168
-145
lines changed

ShareViaHttp/app/build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdk 32
4+
compileSdk 34
55

66
defaultConfig {
77
applicationId "com.MarcosDiez.shareviahttp"
88
minSdkVersion 14
9-
targetSdk 32
10-
versionCode 34
11-
versionName "2.0.14"
9+
targetSdk 34
10+
versionCode 35
11+
versionName "2.0.15"
1212
setProperty("archivesBaseName", "$applicationId-$versionName")
1313
}
1414

ShareViaHttp/app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<uses-permission android:name="android.permission.INTERNET" />
55
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66
<uses-permission android:name="android.permission.READ_CONTACTS" />
7+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
8+
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
9+
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
10+
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
711

812
<uses-feature
913
android:name="android.hardware.touchscreen"

ShareViaHttp/app/src/main/java/com/MarcosDiez/shareviahttp/UriInterpretation.java

+153-136
Original file line numberDiff line numberDiff line change
@@ -8,161 +8,178 @@
88
import android.content.ContentResolver;
99
import android.database.Cursor;
1010
import android.net.Uri;
11+
import android.os.ParcelFileDescriptor;
1112
import android.provider.OpenableColumns;
1213
import android.util.Log;
1314

1415
import java.io.File;
1516
import java.io.FileNotFoundException;
17+
import java.io.IOException;
1618
import java.io.InputStream;
1719
import java.io.UnsupportedEncodingException;
1820
import java.net.URLDecoder;
1921

2022
public class UriInterpretation {
2123

22-
private long size = -1;
23-
private String name = null;
24+
private long size = -1;
25+
private String name = null;
2426
private String path = null;
25-
private String mime;
26-
private boolean isDirectory = false;
27-
private Uri uri;
27+
private String mime;
28+
private boolean isDirectory = false;
29+
private Uri uri;
2830
private ContentResolver contentResolver;
2931

30-
public InputStream getInputStream() throws FileNotFoundException {
31-
return contentResolver.openInputStream(uri);
32-
}
32+
public InputStream getInputStream() throws FileNotFoundException {
33+
return contentResolver.openInputStream(uri);
34+
}
3335

34-
public UriInterpretation(Uri uri, ContentResolver contentResolver) {
35-
this.uri = uri;
36+
public UriInterpretation(Uri uri, ContentResolver contentResolver) {
37+
this.uri = uri;
38+
39+
this.contentResolver = contentResolver;
40+
41+
Cursor metadataCursor = contentResolver.query(uri, new String[]{
42+
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null,
43+
null, null);
44+
45+
if (metadataCursor != null) {
46+
try {
47+
if (metadataCursor.moveToFirst()) {
48+
path = name = metadataCursor.getString(0);
49+
size = metadataCursor.getInt(1);
50+
51+
// sometimes this ContentResolver gives me the wrong file size ...
52+
// here is the fix
53+
// https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size
54+
try {
55+
ParcelFileDescriptor pfd = contentResolver.openFileDescriptor(uri, "r");
56+
size = pfd.getStatSize();
57+
pfd.close();
58+
} catch (FileNotFoundException e) {
59+
// throw new RuntimeException(e);
60+
// since this is plan B, I don't care about the exception
61+
} catch (IOException e) {
62+
// throw new RuntimeException(e);
63+
// since this is plan B, I don't care about the exception
64+
}
65+
}
66+
} finally {
67+
metadataCursor.close();
68+
}
69+
}
70+
71+
if (name == null) {
72+
name = uri.getLastPathSegment();
73+
path = uri.toString();
74+
}
3675

37-
this.contentResolver = contentResolver;
76+
getMime(uri, contentResolver);
3877

39-
Cursor metadataCursor = contentResolver.query(uri, new String[] {
40-
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null,
41-
null, null);
78+
getFileSize(uri);
4279

43-
if (metadataCursor != null) {
44-
try {
45-
if (metadataCursor.moveToFirst()) {
46-
path = name = metadataCursor.getString(0);
47-
size = metadataCursor.getInt(1);
48-
}
49-
} finally {
50-
metadataCursor.close();
51-
}
52-
}
80+
}
5381

54-
if (name == null) {
55-
name = uri.getLastPathSegment();
56-
path = uri.toString();
57-
}
58-
59-
getMime(uri, contentResolver);
60-
61-
getFileSize(uri);
62-
63-
}
64-
65-
private void getFileSize(Uri uri) {
66-
if (size <= 0) {
67-
String uriString = uri.toString();
68-
if (uriString.startsWith("file://")) {
69-
File f = new File(uriString.substring("file://".length()));
70-
isDirectory = f.isDirectory();
71-
if (isDirectory) {
72-
// Log.v(Util.myLogName, "We are dealing with a directory.");
73-
size = 0;
74-
return;
75-
}
76-
size = f.length();
77-
if (size == 0) {
78-
try {
79-
uriString = URLDecoder.decode(uriString, "UTF-8").substring(
80-
"file://".length());
81-
f = new File(uriString);
82-
size = f.length();
83-
}catch (UnsupportedEncodingException e) {
84-
//
85-
}
86-
}
87-
///Log.v(Util.myLogName, "zzz" + size);
88-
89-
} else {
90-
try {
91-
File f = new File(uriString);
92-
isDirectory = f.isDirectory();
93-
return;
94-
} catch (Exception e) {
95-
Log.v(Util.myLogName, "Not a file... " + uriString);
96-
e.printStackTrace();
97-
}
98-
Log.v(Util.myLogName, "Not a file: " + uriString);
99-
100-
}
101-
}
102-
}
103-
104-
private void getMime(Uri uri, ContentResolver contentResolver) {
105-
mime = contentResolver.getType(uri);
106-
if (mime == null || name == null) {
107-
mime = "application/octet-stream";
108-
if(name == null){
109-
return;
110-
}
111-
}
112-
if (mime.equals("application/octet-stream")) {
113-
// we can do better than that
114-
int pos = name.lastIndexOf('.');
115-
if (pos < 0)
116-
return;
117-
String extension = name.substring(pos).toLowerCase();
118-
if (extension.equals(".jpg")) {
119-
mime = "image/jpeg";
120-
return;
121-
}
122-
if (extension.equals(".png")) {
123-
mime = "image/png";
124-
return;
125-
}
126-
if (extension.equals(".gif")) {
127-
mime = "image/gif";
128-
return;
129-
}
130-
if (extension.equals(".mp4")) {
131-
mime = "video/mp4";
132-
return;
133-
}
134-
if (extension.equals(".avi")) {
135-
mime = "video/avi";
136-
return;
137-
}
138-
if (extension.equals(".mov")) {
139-
mime = "video/mov";
140-
return;
141-
}
142-
if (extension.equals(".vcf")) {
143-
mime = "text/x-vcard";
144-
return;
145-
}
146-
if (extension.equals(".txt")) {
147-
mime = "text/plain";
148-
return;
149-
}
150-
if (extension.equals(".html")) {
151-
mime = "text/html";
152-
return;
153-
}
154-
if (extension.equals(".json")) {
155-
mime = "application/json";
156-
return;
157-
}
158-
if (extension.equals(".epub")) {
159-
mime = "application/epub+zip";
160-
return;
161-
}
162-
163-
}
164-
165-
}
82+
private void getFileSize(Uri uri) {
83+
if (size <= 0) {
84+
String uriString = uri.toString();
85+
if (uriString.startsWith("file://")) {
86+
File f = new File(uriString.substring("file://".length()));
87+
isDirectory = f.isDirectory();
88+
if (isDirectory) {
89+
// Log.v(Util.myLogName, "We are dealing with a directory.");
90+
size = 0;
91+
return;
92+
}
93+
size = f.length();
94+
if (size == 0) {
95+
try {
96+
uriString = URLDecoder.decode(uriString, "UTF-8").substring(
97+
"file://".length());
98+
f = new File(uriString);
99+
size = f.length();
100+
} catch (UnsupportedEncodingException e) {
101+
//
102+
}
103+
}
104+
///Log.v(Util.myLogName, "zzz" + size);
105+
106+
} else {
107+
try {
108+
File f = new File(uriString);
109+
isDirectory = f.isDirectory();
110+
return;
111+
} catch (Exception e) {
112+
Log.v(Util.myLogName, "Not a file... " + uriString);
113+
e.printStackTrace();
114+
}
115+
Log.v(Util.myLogName, "Not a file: " + uriString);
116+
117+
}
118+
}
119+
}
120+
121+
private void getMime(Uri uri, ContentResolver contentResolver) {
122+
mime = contentResolver.getType(uri);
123+
if (mime == null || name == null) {
124+
mime = "application/octet-stream";
125+
if (name == null) {
126+
return;
127+
}
128+
}
129+
if (mime.equals("application/octet-stream")) {
130+
// we can do better than that
131+
int pos = name.lastIndexOf('.');
132+
if (pos < 0)
133+
return;
134+
String extension = name.substring(pos).toLowerCase();
135+
if (extension.equals(".jpg")) {
136+
mime = "image/jpeg";
137+
return;
138+
}
139+
if (extension.equals(".png")) {
140+
mime = "image/png";
141+
return;
142+
}
143+
if (extension.equals(".gif")) {
144+
mime = "image/gif";
145+
return;
146+
}
147+
if (extension.equals(".mp4")) {
148+
mime = "video/mp4";
149+
return;
150+
}
151+
if (extension.equals(".avi")) {
152+
mime = "video/avi";
153+
return;
154+
}
155+
if (extension.equals(".mov")) {
156+
mime = "video/mov";
157+
return;
158+
}
159+
if (extension.equals(".vcf")) {
160+
mime = "text/x-vcard";
161+
return;
162+
}
163+
if (extension.equals(".txt")) {
164+
mime = "text/plain";
165+
return;
166+
}
167+
if (extension.equals(".html")) {
168+
mime = "text/html";
169+
return;
170+
}
171+
if (extension.equals(".json")) {
172+
mime = "application/json";
173+
return;
174+
}
175+
if (extension.equals(".epub")) {
176+
mime = "application/epub+zip";
177+
return;
178+
}
179+
180+
}
181+
182+
}
166183

167184
public long getSize() {
168185
return size;

ShareViaHttp/app/src/main/res/values-cs/strings.xml

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<resources>
33
<string name="app_name">Sdílejte přes HTTP</string>
44

5-
<string name="standard_url" translatable="false">http://127.0.0.1/</string>
6-
75
<string name="technical_info">Technická omezení: V rámci WiFi to bude fungovat vždy. Jinde to bude záviset na firewallech, NAT atd. Neblokovaná IPv6 adresa by měla fungovat všude.</string>
86

97
<string name="rate_app">Ohodnoťte aplikaci</string>

ShareViaHttp/app/src/main/res/values-it/strings.xml

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<resources>
33
<!-- Italian Translation provided by Sylvie Ventura <sylvieventura AT tiscali DOT it> -->
44
<string name="app_name">Share via HTTP</string>
5-
<string name="standard_url" translatable="false">http://127.0.0.1/</string>
65
<string name="technical_info">Restrizioni tecniche: All\'interno di una rete wifi funziona sempre. Altrimenti dipende dalla configurazione del firewall, della NAT, e così via. Gl indirizzi IPv6 dovrebbero poter funzionare in ogni caso.</string>
76
<string name="rate_app">Valuta l\'app</string>
87
<string name="button_qrcode">Crea QR</string>

ShareViaHttp/app/src/main/res/values-pt/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
<string name="alert_dialog_ok">OK</string>
1717
<string name="alert_dialog_cancel">Cancelar</string>
18+
<string name="url_clipboard">A URL foi copiada para a área de transferência</string>
1819
<string name="privacy_policy">Política de Privacidade</string>
1920
<string name="connected_ip">Conectado: %s</string>
2021
<string name="disconnected_ip">Desconectado: %s</string>
22+
<string name="server_running">Servidor HTTP rodando</string>
2123

2224
</resources>

ShareViaHttp/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
google()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:7.3.1'
8+
classpath 'com.android.tools.build:gradle:8.3.0'
99
}
1010
}
1111

ShareViaHttp/gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
android.defaults.buildfeatures.buildconfig=true
2+
android.nonFinalResIds=false
3+
android.nonTransitiveRClass=false
14
android.useAndroidX=true
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Mon Oct 11 07:31:24 BRT 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)