2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Rewritten ShareSettingsActivity and de-duplicated SAF code in SharePlugin.

There was unused code and incorrect comments that were introduced in
commit e2e4086.
This commit is contained in:
Albert Vaca
2016-12-11 18:57:21 +01:00
parent 5c3b5a2737
commit b3449c87f7
4 changed files with 84 additions and 136 deletions

View File

@@ -190,9 +190,7 @@
<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">Select destination Folder</string>
<string name="share_destination_folder_preference_summary">Select destination folder at first use (on SD card...)</string>
<string name="share_destination_folder_preference">Destination directory</string>
<string name="open">Open</string>
<string name="close">Close</string>

View File

@@ -3,6 +3,11 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<Preference
android:id="@+id/share_destination_folder_preference"
android:key="share_destination_folder_preference"
android:title="@string/share_destination_folder_preference" />
<CheckBoxPreference
android:id="@+id/share_notification_preference"
android:key="share_notification_preference"
@@ -10,12 +15,4 @@
android:summary="@string/share_notification_preference_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:id="@+id/share_destination_folder_preference"
android:key="share_destination_folder_preference"
android:defaultValue="false"
android:title="@string/share_destination_folder_preference"
android:summary="@string/share_destination_folder_preference_summary" />
</PreferenceScreen>

View File

@@ -33,7 +33,6 @@ import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
@@ -53,7 +52,6 @@ import org.kde.kdeconnect.UserInterface.SettingsActivity;
import org.kde.kdeconnect_tp.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
@@ -102,8 +100,6 @@ public class SharePlugin extends Plugin {
return true;
}
private static final int READ_REQUEST_CODE = 42;
@Override
public boolean onPackageReceived(NetworkPackage np) {
@@ -116,53 +112,13 @@ public class SharePlugin extends Plugin {
final long fileLength = np.getPayloadSize();
final String filename = np.getString("filename", Long.toString(System.currentTimeMillis()));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String deviceDir = FilesHelper.toFileSystemSafeName(device.getName());
File destinationFullPath;
DocumentFile destinationDocument;
final OutputStream destinationOutput;
final Uri destinationUri;
final String mimeType;
if (prefs.contains("share_destination_folder_uri")) {
Uri folderUri = Uri.parse(prefs.getString("share_destination_folder_uri", null));
final DocumentFile destinationFolderDocument = DocumentFile.fromTreeUri(context, folderUri);
String name = filename.substring(0, filename.lastIndexOf("."));
mimeType = FilesHelper.getMimeTypeFromFile(filename);
if (destinationFolderDocument.findFile("kdeconnect") == null) {
destinationFolderDocument.createDirectory("kdeconnect");
}
if (destinationFolderDocument.findFile("kdeconnect").findFile(deviceDir) == null) {
destinationFolderDocument.findFile("kdeconnect").createDirectory(deviceDir);
}
destinationDocument = destinationFolderDocument.findFile("kdeconnect")
.findFile(deviceDir)
.createFile(mimeType, name);
destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri());
destinationUri = destinationDocument.getUri();
} else {
//Get the external storage and append "/kdeconnect/DEVICE_NAME/"
String destinationDir = Environment.getExternalStorageDirectory().getPath();
destinationDir = new File(destinationDir, "kdeconnect").getPath();
destinationDir = new File(destinationDir, deviceDir).getPath();
//Create directories if needed
new File(destinationDir).mkdirs();
//Append filename to the destination path
//Log.e("SharePlugin", "destinationFullPath:" + destinationFullPath);
destinationFullPath = new File(destinationDir, filename);
destinationOutput = new FileOutputStream(destinationFullPath.getPath());
destinationUri = Uri.parse(destinationFullPath.toURI().toString());
mimeType = FilesHelper.getMimeTypeFromFile(destinationFullPath.getPath());
}
String name = filename.substring(0, filename.lastIndexOf("."));
final String mimeType = FilesHelper.getMimeTypeFromFile(filename);
final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context);
final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, name);
final OutputStream destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri());
final Uri destinationUri = destinationDocument.getUri();
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -244,7 +200,7 @@ public class SharePlugin extends Plugin {
.setContentIntent(resultPendingIntent);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("share_notification_preference", true)) {
builder.setDefaults(Notification.DEFAULT_ALL);
}
@@ -325,7 +281,6 @@ public class SharePlugin extends Plugin {
parentActivity.startActivity(intent);
}
static void queuedSendUriList(Context context, final Device device, final ArrayList<Uri> uriList) {
//Read all the data early, as we only have permissions to do it while the activity is alive

View File

@@ -2,101 +2,99 @@ package org.kde.kdeconnect.Plugins.SharePlugin;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.provider.DocumentFile;
import android.view.View;
import android.widget.Toast;
import android.os.Environment;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.v4.provider.DocumentFile;
import android.util.Log;
import org.kde.kdeconnect.UserInterface.PluginSettingsActivity;
import org.kde.kdeconnect_tp.R;
import java.util.Locale;
import java.io.File;
import static android.R.attr.id;
public class ShareSettingsActivity extends PluginSettingsActivity {
private final static String PREFERENCE_DESTINATION = "share_destination_folder_uri";
public class ShareSettingsActivity extends PluginSettingsActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences prefs;
private static final int RESULT_PICKER = 42;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//addPreferencesFromResource(R.xml.fw_preferences); //deprecated
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
Preference filePicker = findPreference("share_destination_folder_preference");
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)) {
filePicker.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, RESULT_PICKER);
return true;
}
});
} else {
filePicker.setEnabled(false);
}
String path = PreferenceManager.getDefaultSharedPreferences(this).getString(PREFERENCE_DESTINATION, null);
if (path != null) {
filePicker.setSummary(Uri.parse(path).getPath());
} else {
filePicker.setSummary(getDefaultDestinationDirectory().getAbsolutePath());
}
}
private static File getDefaultDestinationDirectory() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}
private static final int READ_REQUEST_CODE = 42;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
// Sorry, only KitKat and up!
return;
}
if (key.equals("share_destination_folder_preference"))
if (sharedPreferences.getBoolean("share_destination_folder_preference", false)) {
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
//intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
//intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE);
public static DocumentFile getDestinationDirectory(Context context) {
String path = PreferenceManager.getDefaultSharedPreferences(context).getString(PREFERENCE_DESTINATION, null);
if (path != null) {
//There should be no way to enter here on api level < kitkat
DocumentFile treeDocumentFile = DocumentFile.fromTreeUri(context, Uri.parse(path));
if (treeDocumentFile.canWrite()) { //Checks for FLAG_DIR_SUPPORTS_CREATE on directories
return treeDocumentFile;
} else {
prefs.edit().remove("share_destination_folder_preference").apply();
}
// }
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all.
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri;
if (resultData != null) {
uri = resultData.getData();
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
DocumentFile df = DocumentFile.fromTreeUri(getApplicationContext(),uri);
df.listFiles();
prefs.edit().putString("share_destination_folder_uri", uri.toString()).apply();
prefs.contains("share_destination_folder_uri");
//owImage(uri);
//Maybe permission was revoked
Log.w("SharePlugin", "Share destination is not writable, falling back to default path.");
}
}
return DocumentFile.fromFile(getDefaultDestinationDirectory());
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == RESULT_PICKER
&& resultCode == Activity.RESULT_OK
&& resultData != null) {
Uri uri = resultData.getData();
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Preference filePicker = findPreference("share_destination_folder_preference");
filePicker.setSummary(uri.getPath());
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());
} */
}
}