Compare commits
11 Commits
foreground
...
master
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7ce4b9830b | ||
![]() |
6a460b2952 | ||
![]() |
6a1ed20e07 | ||
![]() |
96540f4a50 | ||
![]() |
211fd26dca | ||
![]() |
92e9c35584 | ||
![]() |
3df644e6a8 | ||
![]() |
5444d765e7 | ||
![]() |
38eff704cd | ||
![]() |
057038cca7 | ||
![]() |
cba7fc2555 |
@ -227,7 +227,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
public static final int REQUEST_ID_READ_EXTERNAL_STORAGE = 2001;
|
public static final int REQUEST_ID_READ_EXTERNAL_STORAGE = 2001;
|
||||||
|
|
||||||
private boolean checkAndRequestPermissions() {
|
private boolean checkAndRequestPermissions() {
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(
|
if (ContextCompat.checkSelfPermission(
|
||||||
this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
|
this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.localtransfer;
|
package com.localtransfer;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -10,6 +12,8 @@ import android.view.MenuItem;
|
|||||||
|
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
import androidx.preference.PreferenceFragmentCompat;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
@ -49,14 +53,22 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
SwitchPreference shared;
|
SwitchPreference shared;
|
||||||
shared = findPreference("use_shared_storage");
|
shared = findPreference("use_shared_storage");
|
||||||
if(shared.isChecked())
|
if(shared.isChecked()) {
|
||||||
directory.setVisible(true);
|
if(checkAndRequestPermissions())
|
||||||
|
directory.setVisible(true);
|
||||||
|
else
|
||||||
|
shared.setChecked(false);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
directory.setVisible(false);
|
directory.setVisible(false);
|
||||||
|
|
||||||
shared.setOnPreferenceClickListener(preference -> {
|
shared.setOnPreferenceClickListener(preference -> {
|
||||||
if(shared.isChecked())
|
if(shared.isChecked()) {
|
||||||
directory.setVisible(true);
|
if(checkAndRequestPermissions())
|
||||||
|
directory.setVisible(true);
|
||||||
|
else
|
||||||
|
shared.setChecked(false);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
directory.setVisible(false);
|
directory.setVisible(false);
|
||||||
return false;
|
return false;
|
||||||
@ -71,6 +83,35 @@ public class SettingsActivity extends AppCompatActivity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkAndRequestPermissions() {
|
||||||
|
|
||||||
|
if (ContextCompat.checkSelfPermission(
|
||||||
|
getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ==
|
||||||
|
PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Log.d("READ_EXTERNAL_STORAGE", "already granted");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ActivityCompat.requestPermissions(getActivity(), new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, MainActivity.REQUEST_ID_READ_EXTERNAL_STORAGE);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode,
|
||||||
|
String permissions[],
|
||||||
|
int[] grantResults) {
|
||||||
|
switch (requestCode) {
|
||||||
|
case MainActivity.REQUEST_ID_READ_EXTERNAL_STORAGE:
|
||||||
|
if (grantResults.length > 0 &&
|
||||||
|
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Log.d("READ_EXTERNAL_STORAGE", "granted");
|
||||||
|
} else {
|
||||||
|
Log.d("READ_EXTERNAL_STORAGE", "No granted");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
@ -28,6 +28,8 @@ public class UploadFile extends Transfer {
|
|||||||
|
|
||||||
public Uri uri;
|
public Uri uri;
|
||||||
|
|
||||||
|
public String type;
|
||||||
|
|
||||||
public UploadFile() {
|
public UploadFile() {
|
||||||
this.id = View.generateViewId();
|
this.id = View.generateViewId();
|
||||||
instances.add(this);
|
instances.add(this);
|
||||||
@ -44,9 +46,19 @@ public class UploadFile extends Transfer {
|
|||||||
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
||||||
cursor.moveToFirst();
|
cursor.moveToFirst();
|
||||||
|
|
||||||
file.name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
file.type = activity.getContentResolver().getType(uri);
|
||||||
file.size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
|
|
||||||
file.sizeSI = Transfer.humanReadableByteCountBin(file.size);
|
int col_name = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||||
|
if (col_name != -1)
|
||||||
|
file.name = cursor.getString(col_name);
|
||||||
|
if(file.name == null)
|
||||||
|
file.name = file.type.split("/")[0] + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "." + file.type.split("/")[1];
|
||||||
|
|
||||||
|
int col_size = cursor.getColumnIndex(OpenableColumns.SIZE);
|
||||||
|
if (col_name != -1) {
|
||||||
|
file.size = cursor.getLong(col_size);
|
||||||
|
file.sizeSI = Transfer.humanReadableByteCountBin(file.size);
|
||||||
|
}
|
||||||
|
|
||||||
file.AddTransfer();
|
file.AddTransfer();
|
||||||
}
|
}
|
||||||
@ -116,15 +128,7 @@ public class UploadFile extends Transfer {
|
|||||||
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
Cursor cursor = activity.getContentResolver().query(uri, null, null, null, null);
|
||||||
cursor.moveToFirst();
|
cursor.moveToFirst();
|
||||||
|
|
||||||
String type = activity.getContentResolver().getType(uri);
|
String fileNameUTF8 = new String(name.getBytes(), StandardCharsets.ISO_8859_1);
|
||||||
|
|
||||||
String fileName = null;
|
|
||||||
int col_name = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
||||||
if (col_name != -1)
|
|
||||||
fileName = cursor.getString(col_name);
|
|
||||||
if(fileName == null)
|
|
||||||
fileName = type.split("/")[0] + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "." + type.split("/")[1];
|
|
||||||
String fileNameUTF8 = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
|
|
||||||
|
|
||||||
url = new URL(protocol, host, port, root + "/upload.php");
|
url = new URL(protocol, host, port, root + "/upload.php");
|
||||||
|
|
||||||
@ -152,6 +156,7 @@ public class UploadFile extends Transfer {
|
|||||||
InputStream in = activity.getContentResolver().openInputStream(uri);
|
InputStream in = activity.getContentResolver().openInputStream(uri);
|
||||||
|
|
||||||
size = in.available();
|
size = in.available();
|
||||||
|
sizeSI = Transfer.humanReadableByteCountBin(size);
|
||||||
|
|
||||||
while ((bufferLength = in.read(buffer)) > 0) {
|
while ((bufferLength = in.read(buffer)) > 0) {
|
||||||
request.write(buffer, 0, bufferLength);
|
request.write(buffer, 0, bufferLength);
|
||||||
@ -168,7 +173,7 @@ public class UploadFile extends Transfer {
|
|||||||
int responseCode = conn.getResponseCode();
|
int responseCode = conn.getResponseCode();
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
message = "File " + fileName + " successful upload";
|
message = "File " + name + " successful upload";
|
||||||
state = Transfer.STATE_SUCCESS;
|
state = Transfer.STATE_SUCCESS;
|
||||||
progressEnd();
|
progressEnd();
|
||||||
}
|
}
|
||||||
|
@ -300,7 +300,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
|
|
||||||
DownloadFile dl = (DownloadFile) DownloadFile.getFileById(id);
|
DownloadFile dl = (DownloadFile) DownloadFile.getFileById(id);
|
||||||
|
|
||||||
if(dl.type.equals("text/plain")) {
|
if(dl.mime.equals("text/plain")) {
|
||||||
String text = null;
|
String text = null;
|
||||||
try {
|
try {
|
||||||
InputStream is = getActivity().getContentResolver().openInputStream(dl.uri);
|
InputStream is = getActivity().getContentResolver().openInputStream(dl.uri);
|
||||||
@ -321,7 +321,7 @@ public class DownloadFragment extends Fragment {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||||
intent.setType(dl.type);
|
intent.setType(dl.mime);
|
||||||
intent.putExtra(Intent.EXTRA_TEXT, text);
|
intent.putExtra(Intent.EXTRA_TEXT, text);
|
||||||
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
|
||||||
} catch (ActivityNotFoundException e) {
|
} catch (ActivityNotFoundException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user