2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 09:58:08 +00:00

Added a Share setting to add received files to the "Downloads" app

Only works for old-school (not-Storage Access Framework) paths, so we have
to keep track of the fact that we are using one or the other.

Also this requires the permission DOWNLOAD_WITHOUT_NOTIFICATION, but
hopefully the play store won't make users confirm this one.

This behaviour is enabled by default.
This commit is contained in:
Albert Vaca 2016-12-11 21:03:39 +01:00
parent f8dd9bf923
commit 1334dae342
6 changed files with 96 additions and 35 deletions

View File

@ -27,6 +27,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:allowBackup="true"

View File

@ -160,6 +160,10 @@
<string name="custom_device_list">Add devices by IP</string>
<string name="share_notification_preference">Noisy notifications</string>
<string name="share_notification_preference_summary">Vibrate and play a sound when receiving a file</string>
<string name="share_destination_customize">Customize destination directory</string>
<string name="share_destination_customize_summary_disabled">Received files will appear in Downloads</string>
<string name="share_destination_customize_summary_enabled">Files will be stored in the directory below</string>
<string name="share_destination_folder_preference">Destination directory</string>
<string name="title_activity_notification_filter">Notification filter</string>
<string name="filter_apps_info">Notifications will be synchronized for the selected apps.</string>
<string name="sftp_internal_storage">Internal storage</string>
@ -190,8 +194,8 @@
<string name="findmyphone_title_tablet">Find my tablet</string>
<string name="findmyphone_description">Rings this device so you can find it</string>
<string name="findmyphone_found">Found</string>
<string name="share_destination_folder_preference">Destination directory</string>
<string name="open">Open</string>
<string name="close">Close</string>
</resources>

View File

@ -3,6 +3,14 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBoxPreference
android:id="@+id/share_destination_customize"
android:key="share_destination_custom"
android:title="@string/share_destination_customize"
android:summaryOff="@string/share_destination_customize_summary_disabled"
android:summaryOn="@string/share_destination_customize_summary_enabled"
android:defaultValue="false" />
<Preference
android:id="@+id/share_destination_folder_preference"
android:key="share_destination_folder_preference"

View File

@ -27,17 +27,35 @@ import java.io.File;
public class FilesHelper {
public static String getFileExt(String fileName) {
//return MimeTypeMap.getFileExtensionFromUrl(fileName);
return fileName.substring((fileName.lastIndexOf(".") + 1), fileName.length());
public static String getFileExt(String filename) {
//return MimeTypeMap.getFileExtensionFromUrl(filename);
return filename.substring((filename.lastIndexOf(".") + 1));
}
public static String getFileNameWithoutExt(String filename) {
int dot = filename.lastIndexOf(".");
return (dot < 0)? filename : filename.substring(0, dot);
}
public static String getMimeTypeFromFile(String file) {
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(getFileExt(file));
if (mime == null) mime = "*/*";
return mime;
}
public static String findNonExistingNameForNewFile(String path, String filename) {
int dot = filename.lastIndexOf(".");
String name = (dot < 0)? filename : filename.substring(0, dot);
String ext = (dot < 0)? "" : filename.substring(filename.lastIndexOf("."));
int num = 1;
while (new File(path+"/"+filename).exists()) {
filename = name+" ("+num+")"+ext;
num++;
}
return filename;
}
//Following code from http://activemq.apache.org/maven/5.7.0/kahadb/apidocs/src-html/org/apache/kahadb/util/IOHelper.html
/**
* Converts any string into a string that is safe to use as a file name.

View File

@ -21,6 +21,7 @@
package org.kde.kdeconnect.Plugins.SharePlugin;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@ -110,20 +111,22 @@ public class SharePlugin extends Plugin {
final InputStream input = np.getPayload();
final long fileLength = np.getPayloadSize();
final String filename = np.getString("filename", Long.toString(System.currentTimeMillis()));
final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis()));
int dot = filename.lastIndexOf(".");
String name = (dot < 0)? filename : filename.substring(0, dot);
String ext = (dot < 0)? "" : filename.substring(filename.lastIndexOf("."));
//We need to check for already existing files only when storing in the default path.
//User-defined paths use the new Storage Access Framework that already handles this.
final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context);
final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath();
final String filename = customDestination? originalFilename : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename);
final String nameWithoutExtension = FilesHelper.getFileNameWithoutExt(filename);
final String mimeType = FilesHelper.getMimeTypeFromFile(filename);
final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context);
final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, name);
final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, nameWithoutExtension);
final OutputStream destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri());
final Uri destinationUri = destinationDocument.getUri();
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final int notificationId = (int)System.currentTimeMillis();
Resources res = context.getResources();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
@ -135,6 +138,7 @@ public class SharePlugin extends Plugin {
.setOngoing(true)
.setProgress(100,0,true);
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager,notificationId, builder.build());
new Thread(new Runnable() {
@ -172,7 +176,7 @@ public class SharePlugin extends Plugin {
}
try {
Log.i("SharePlugin", "Transfer finished");
Log.i("SharePlugin", "Transfer finished: "+destinationUri.getPath());
//Update the notification and allow to open the file from it
Resources res = context.getResources();
@ -189,7 +193,7 @@ public class SharePlugin extends Plugin {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentText(res.getString(R.string.received_file_text, filename))
builder.setContentText(res.getString(R.string.received_file_text, destinationDocument.getName()))
.setContentIntent(resultPendingIntent);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -199,9 +203,15 @@ public class SharePlugin extends Plugin {
NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());
if (successful) {
//Make sure it is added to the Android Gallery
if (!customDestination) {
Log.i("SharePlugin","Adding to downloads");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false);
} else {
//Make sure it is added to the Android Gallery anyway
MediaStoreHelper.indexFile(context, destinationUri);
}
}
} catch (Exception e) {
Log.e("SharePlugin", "Receiver thread exception");

View File

@ -9,6 +9,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.v4.provider.DocumentFile;
@ -20,16 +21,28 @@ import java.io.File;
public class ShareSettingsActivity extends PluginSettingsActivity {
private final static String PREFERENCE_CUSTOMIZE_DESTINATION = "share_destination_custom";
private final static String PREFERENCE_DESTINATION = "share_destination_folder_uri";
private static final int RESULT_PICKER = 42;
private static final int RESULT_PICKER = Activity.RESULT_FIRST_USER;
private Preference filePicker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Preference filePicker = findPreference("share_destination_folder_preference");
final CheckBoxPreference customDownloads = (CheckBoxPreference) findPreference("share_destination_custom");
filePicker = findPreference("share_destination_folder_preference");
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)) {
customDownloads.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateFilePickerStatus((Boolean) newValue);
return true;
}
});
filePicker.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
@ -39,22 +52,35 @@ public class ShareSettingsActivity extends PluginSettingsActivity {
}
});
} else {
customDownloads.setEnabled(false);
filePicker.setEnabled(false);
}
boolean customized = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(PREFERENCE_CUSTOMIZE_DESTINATION, false);
updateFilePickerStatus(customized);
}
void updateFilePickerStatus(boolean enabled) {
filePicker.setEnabled(enabled);
String path = PreferenceManager.getDefaultSharedPreferences(this).getString(PREFERENCE_DESTINATION, null);
if (path != null) {
if (enabled && path != null) {
filePicker.setSummary(Uri.parse(path).getPath());
} else {
filePicker.setSummary(getDefaultDestinationDirectory().getAbsolutePath());
}
}
private static File getDefaultDestinationDirectory() {
public static File getDefaultDestinationDirectory() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
public static boolean isCustomDestinationEnabled(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREFERENCE_CUSTOMIZE_DESTINATION, false);
}
//Will return the appropriate directory, whether it is customized or not
public static DocumentFile getDestinationDirectory(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREFERENCE_CUSTOMIZE_DESTINATION, false)) {
String path = PreferenceManager.getDefaultSharedPreferences(context).getString(PREFERENCE_DESTINATION, null);
if (path != null) {
//There should be no way to enter here on api level < kitkat
@ -66,6 +92,7 @@ public class ShareSettingsActivity extends PluginSettingsActivity {
Log.w("SharePlugin", "Share destination is not writable, falling back to default path.");
}
}
}
return DocumentFile.fromFile(getDefaultDestinationDirectory());
}
@ -88,13 +115,6 @@ public class ShareSettingsActivity extends PluginSettingsActivity {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString(PREFERENCE_DESTINATION, uri.toString()).apply();
}
/* else { // Here to ease debugging. Removes the setting so we use the default url.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().remove(PREFERENCE_DESTINATION).apply();
Preference filePicker = findPreference("share_destination_folder_preference");
filePicker.setSummary(getDefaultDestinationDirectory().getAbsolutePath());
} */
}
}