Skip to content

Commit

Permalink
Merge pull request #4 from eduvo/bugfix/MBMOB-5878-android-teacher-qu…
Browse files Browse the repository at this point in the history
…ick-add-add-photos-photos-are-cashed-and-cannot-be-uploaded

<MBMOB-5878: Android > Teacher > Quick Add > Add Photos > Photos are сashed and cannot be uploaded.
  • Loading branch information
vladbakalo authored Feb 9, 2023
2 parents 06909a3 + a9886b9 commit 1a6d1d2
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 39 deletions.
4 changes: 2 additions & 2 deletions demoapp/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
15 changes: 15 additions & 0 deletions demoapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
Expand All @@ -11,7 +14,9 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config">

<activity
android:exported="true"
android:name="co.faria.turbolinks.demo.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
Expand All @@ -21,6 +26,16 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<provider
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:name="androidx.core.content.FileProvider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>

</manifest>
27 changes: 26 additions & 1 deletion demoapp/src/main/java/co/faria/turbolinks/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> activityResultListener = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions demoapp/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
<external-files-path
name="Download"
path="Download/" />
</paths>
8 changes: 0 additions & 8 deletions local.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 1a6d1d2

Please sign in to comment.