Process intent streams

pull/190/head
M66B 4 years ago
parent 385a829cba
commit f1c5a57133

@ -26,6 +26,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -42,12 +43,16 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.documentfile.provider.DocumentFile;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -366,6 +371,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this)) { if (!this.getClass().equals(ActivityMain.class) && Helper.shouldAuthenticate(this)) {
Intent intent = getIntent(); Intent intent = getIntent();
finishAndRemoveTask(); finishAndRemoveTask();
processStreams(intent);
Intent main = new Intent(this, ActivityMain.class) Intent main = new Intent(this, ActivityMain.class)
.putExtra("intent", intent); .putExtra("intent", intent);
main.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); main.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
@ -373,6 +379,63 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
} }
} }
private void processStreams(Intent intent) {
intent.setClipData(null);
if (intent.hasExtra(Intent.EXTRA_STREAM)) {
if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (uris == null)
intent.removeExtra(Intent.EXTRA_STREAM);
else {
ArrayList<Uri> processed = new ArrayList<>();
for (Uri uri : uris)
processed.add(processUri(uri));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, processed);
}
} else {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri == null)
intent.removeExtra(Intent.EXTRA_STREAM);
else
intent.putExtra(Intent.EXTRA_STREAM, processUri(uri));
}
}
}
private Uri processUri(Uri uri) {
try {
String fname = null;
try {
DocumentFile dfile = DocumentFile.fromSingleUri(this, uri);
if (dfile != null)
fname = dfile.getName();
} catch (SecurityException ex) {
Log.e(ex);
}
if (TextUtils.isEmpty(fname))
fname = uri.getLastPathSegment();
if (TextUtils.isEmpty(fname))
return uri;
File dir = new File(getCacheDir(), "shared");
if (!dir.exists())
dir.mkdir();
File file = new File(dir, fname);
Log.i("Copying shared file to " + file);
Helper.copy(getContentResolver().openInputStream(uri), new FileOutputStream(file));
return FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);
} catch (Throwable ex) {
Log.w(ex);
return uri;
}
}
@Override @Override
public void startActivity(Intent intent) { public void startActivity(Intent intent) {
try { try {

@ -190,6 +190,7 @@ public class WorkerCleanup extends Worker {
File[] references = new File(context.getFilesDir(), "references").listFiles(); File[] references = new File(context.getFilesDir(), "references").listFiles();
File[] photos = new File(context.getCacheDir(), "photo").listFiles(); File[] photos = new File(context.getCacheDir(), "photo").listFiles();
File[] calendars = new File(context.getCacheDir(), "calendar").listFiles(); File[] calendars = new File(context.getCacheDir(), "calendar").listFiles();
File[] shared = new File(context.getCacheDir(), "shared").listFiles();
if (messages != null) if (messages != null)
files.addAll(Arrays.asList(messages)); files.addAll(Arrays.asList(messages));
@ -201,6 +202,8 @@ public class WorkerCleanup extends Worker {
files.addAll(Arrays.asList(photos)); files.addAll(Arrays.asList(photos));
if (calendars != null) if (calendars != null)
files.addAll(Arrays.asList(calendars)); files.addAll(Arrays.asList(calendars));
if (shared != null)
files.addAll(Arrays.asList(shared));
// Cleanup message files // Cleanup message files
Log.i("Cleanup message files"); Log.i("Cleanup message files");

@ -15,4 +15,7 @@
<cache-path <cache-path
name="images" name="images"
path="images" /> path="images" />
<cache-path
name="shared"
path="shared" />
</paths> </paths>

Loading…
Cancel
Save