diff --git a/demoapp/build.gradle b/demoapp/build.gradle index d22ca9c..c5f420e 100644 --- a/demoapp/build.gradle +++ b/demoapp/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 30 + compileSdkVersion 33 buildToolsVersion '28.0.3' defaultConfig { applicationId "co.faria.turbolinks.demo" minSdkVersion 24 - targetSdkVersion 30 + targetSdkVersion 33 versionCode 1 versionName "1.0" } diff --git a/demoapp/src/main/AndroidManifest.xml b/demoapp/src/main/AndroidManifest.xml index 668376f..54979e3 100644 --- a/demoapp/src/main/AndroidManifest.xml +++ b/demoapp/src/main/AndroidManifest.xml @@ -3,6 +3,9 @@ xmlns:android="http://schemas.android.com/apk/res/android"> + + + + @@ -21,6 +26,16 @@ + + + + diff --git a/demoapp/src/main/java/co/faria/turbolinks/demo/MainActivity.java b/demoapp/src/main/java/co/faria/turbolinks/demo/MainActivity.java index c6668a6..a07fc34 100644 --- a/demoapp/src/main/java/co/faria/turbolinks/demo/MainActivity.java +++ b/demoapp/src/main/java/co/faria/turbolinks/demo/MainActivity.java @@ -3,17 +3,24 @@ import android.content.Intent; import android.os.Bundle; +import co.faria.turbolinks.ActivityResultListener; +import co.faria.turbolinks.ActivityResultListenerRegistry; import co.faria.turbolinks.TurbolinksAdapter; import co.faria.turbolinks.TurbolinksSession; import co.faria.turbolinks.TurbolinksView; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -public class MainActivity extends AppCompatActivity { +import java.util.ArrayList; + +public class MainActivity extends AppCompatActivity implements ActivityResultListenerRegistry { // ----------------------------------------------------------------------- // Activity overrides // ----------------------------------------------------------------------- + private ArrayList activityResultListener = new ArrayList<>(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -22,4 +29,22 @@ protected void onCreate(Bundle savedInstanceState) { TurbolinksSession.getDefault(this) // create session .activity(this); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + for (ActivityResultListener item: activityResultListener) { + item.onActivityResult(requestCode, resultCode, data); + } + } + + @Override + public void registerActivityResultListener(ActivityResultListener listener) { + activityResultListener.add(listener); + } + + @Override + public void unregisterActivityResultListener(ActivityResultListener listener) { + activityResultListener.remove(listener); + } } diff --git a/demoapp/src/main/java/co/faria/turbolinks/demo/TurbolinksFragment.java b/demoapp/src/main/java/co/faria/turbolinks/demo/TurbolinksFragment.java index a880aec..da776a5 100644 --- a/demoapp/src/main/java/co/faria/turbolinks/demo/TurbolinksFragment.java +++ b/demoapp/src/main/java/co/faria/turbolinks/demo/TurbolinksFragment.java @@ -23,6 +23,13 @@ public class TurbolinksFragment extends Fragment implements TurbolinksAdapter { private String location; private TurbolinksView turbolinksView; + @Override + public View onCreateView(LayoutInflater inflater, + ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.turbolinks_fragment, container, false); + } + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -37,20 +44,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat // For this example we set a default location, unless one is passed in through an intent location = requireActivity().getIntent().getStringExtra(INTENT_URL) != null ? requireActivity().getIntent().getStringExtra(INTENT_URL) : BASE_URL; - } - - @Override - public View onCreateView(LayoutInflater inflater, - ViewGroup container, - Bundle savedInstanceState) { - return inflater.inflate(R.layout.turbolinks_fragment, container, false); - } - - @Override - public void onResume() { - super.onResume(); - - // Execute the visit TurbolinksSession.getDefault(requireActivity()) .adapter(this) .fragment(this) diff --git a/demoapp/src/main/res/xml/provider_paths.xml b/demoapp/src/main/res/xml/provider_paths.xml new file mode 100644 index 0000000..a2c573f --- /dev/null +++ b/demoapp/src/main/res/xml/provider_paths.xml @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/local.properties b/local.properties deleted file mode 100644 index e0985d2..0000000 --- a/local.properties +++ /dev/null @@ -1,8 +0,0 @@ -## This file must *NOT* be checked into Version Control Systems, -# as it contains information specific to your local configuration. -# -# Location of the SDK. This is only used by Gradle. -# For customization when using a Version Control System, please read the -# header note. -#Sun Dec 05 22:05:18 CET 2021 -sdk.dir=/Users/dave/Library/Android/sdk diff --git a/turbolinks/src/main/java/co/faria/turbolinks/TLChromeClientWithFileChooser.java b/turbolinks/src/main/java/co/faria/turbolinks/TLChromeClientWithFileChooser.java index 0095f75..bcd8bfe 100644 --- a/turbolinks/src/main/java/co/faria/turbolinks/TLChromeClientWithFileChooser.java +++ b/turbolinks/src/main/java/co/faria/turbolinks/TLChromeClientWithFileChooser.java @@ -12,6 +12,7 @@ import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.net.Uri; +import android.os.Environment; import android.os.Handler; import android.os.Message; import android.os.Parcelable; @@ -222,7 +223,12 @@ private void proceedOnType(String type, final boolean isCaptureEnabled, int mode this.activity.requestPermissions(new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, VIDEO_REQUEST_CODE); - makeVideo(); // need to make sure you have "android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO" permission + // need to make sure you have "android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO" permission + try { + makeVideo(); + } catch (IOException e) { + e.printStackTrace(); + } } else { this.activity.requestPermissions(new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE, @@ -296,7 +302,7 @@ private void pickImage(boolean captureOnly) throws IOException { } } - private void makeVideo() { + private void makeVideo() throws IOException { boolean hasPermission = (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.CAMERA) @@ -307,8 +313,11 @@ private void makeVideo() { } Intent iImageCapture = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); iImageCapture.setFlags(FLAG_ACTIVITY_SINGLE_TOP); - mediaUri = Uri.fromFile(new File(getVideoCaptureCachePath())); - iImageCapture.putExtra("output", mediaUri); + + File videoFile = getVideoCaptureCachePath(); + mediaUri = Uri.fromFile(videoFile); + + iImageCapture.putExtra(MediaStore.EXTRA_OUTPUT, getProvidedImageUri(videoFile)); activity.startActivityForResult(Intent.createChooser(iImageCapture, "Image Chooser"), FILE_CHOOSER_REQUEST_CODE); } @@ -320,14 +329,17 @@ private Uri getProvidedImageUri(File imageFile) { private File getImageCaptureCacheFile() throws IOException { // Create an image file name - String imageFileName = "cached_photo" + System.currentTimeMillis(); + String imageFileName = "temp_photo" + System.currentTimeMillis(); File storageDir = activity.getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES); return File.createTempFile(imageFileName, ".jpg", storageDir); } - private String getVideoCaptureCachePath() { - return activity.getApplicationContext().getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + File.separator + "cached_video" + System.currentTimeMillis() + ".mp4"; + private File getVideoCaptureCachePath() throws IOException { + String videoFileName = "temp_video" + System.currentTimeMillis(); + File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_MOVIES); + + return File.createTempFile(videoFileName, ".mp4", storageDir); } private void cleanCacheUri() { @@ -354,13 +366,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { Uri[] resValue = null; // if mediaUri exists, we have generated content => data is null - if (mediaUri != null) { - File file = new File(mediaUri.getPath()); - if (file.exists()) { - resValue = new Uri[]{mediaUri}; - } - } - if (resValue == null && (data != null)) { + if (data != null) { ClipData clipData = data.getClipData(); // multiple files selected if (clipData != null) { @@ -372,6 +378,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { resValue = new Uri[]{data.getData()}; } } + if (resValue == null && mediaUri != null) { + File file = new File(mediaUri.getPath()); + if (file.exists()) { + resValue = new Uri[]{mediaUri}; + } + } execFileCallback(resValue); }