Skip to content

Commit

Permalink
Use FileProvider to open folder externally (ref #838)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Oct 11, 2017
1 parent 9840076 commit 2626899
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.nutomic.syncthingandroid.activities.FolderActivity" />
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.nutomic.syncthingandroid"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />

</provider>

</application>

</manifest>
8 changes: 0 additions & 8 deletions src/main/java/com/nutomic/syncthingandroid/SyncthingApp.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nutomic.syncthingandroid;

import android.app.Application;
import android.os.StrictMode;

import com.nutomic.syncthingandroid.util.Languages;

Expand All @@ -21,13 +20,6 @@ public void onCreate() {
.inject(this);

new Languages(this).setLanguage(this);

// Set VM policy to avoid crash when sending folder URI to file manager.
StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setVmPolicy(policy);
}

public DaggerComponent component() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -52,7 +53,8 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
binding.directory.setText(folder.path);
binding.openFolder.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(folder.path)), "resource/folder");
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.nutomic.syncthingandroid", new File(folder.path));
intent.setData(contentUri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent.resolveActivity(getContext().getPackageManager()) != null) {
getContext().startActivity(intent);
Expand Down
6 changes: 6 additions & 0 deletions src/main/res/xml/filepaths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="/" />
<external-path name="external" path="/" />
<external-files-path name="external-files" path="/" />
</paths>

0 comments on commit 2626899

Please sign in to comment.