Skip to content

Commit

Permalink
Fixed crashes on at least some Android devices while picking files
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Jan 15, 2022
1 parent 2cd01a2 commit 62de2dc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,59 @@
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

// Timer usage reference: https://stackoverflow.com/a/45604466/2373034
// Handler usage reference: https://stackoverflow.com/a/6242292/2373034
public class NativeGalleryMediaPickerResultFragment extends DialogFragment
{
public static int uiUpdateInterval = 100;
public static String progressBarLabel = "Please wait...";

private final NativeGalleryMediaPickerResultOperation resultOperation;

private final Timer uiUpdateTimer = new Timer();
private ProgressBar progressBar;

private final Handler uiUpdateHandler = new Handler( Looper.getMainLooper() );
private final Runnable progressBarUpdateTask = new Runnable()
{
@Override
public void run()
{
if( resultOperation.finished )
{
resultOperation.sendResultToUnity();
dismiss();
}
else
{
try
{
if( progressBar != null )
{
if( resultOperation.progress >= 0 )
{
if( progressBar.isIndeterminate() )
progressBar.setIndeterminate( false );

progressBar.setProgress( resultOperation.progress );
}
else if( !progressBar.isIndeterminate() )
progressBar.setIndeterminate( true );
}
}
finally
{
uiUpdateHandler.postDelayed( progressBarUpdateTask, uiUpdateInterval );
}
}
}
};

public NativeGalleryMediaPickerResultFragment()
{
resultOperation = null;
Expand Down Expand Up @@ -114,38 +148,14 @@ public void onClick( DialogInterface dialog, int which )
public void onActivityCreated( Bundle savedInstanceState )
{
super.onActivityCreated( savedInstanceState );

uiUpdateTimer.scheduleAtFixedRate( new TimerTask()
{
@Override
public void run()
{
if( resultOperation.finished )
{
resultOperation.sendResultToUnity();
dismiss();
}
else if( progressBar != null )
{
if( resultOperation.progress >= 0 )
{
if( progressBar.isIndeterminate() )
progressBar.setIndeterminate( false );

progressBar.setProgress( resultOperation.progress );
}
else if( !progressBar.isIndeterminate() )
progressBar.setIndeterminate( true );
}
}
}, 0, uiUpdateInterval );
progressBarUpdateTask.run();
}

@Override
public void onDetach()
{
progressBar = null;
uiUpdateTimer.cancel();
uiUpdateHandler.removeCallbacks( progressBarUpdateTask );

super.onDetach();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,22 @@ private String copyToTempFile( Uri uri )
filename = "temp";

String extension = null;
String mime = resolver.getType( uri );
if( mime != null )
int filenameExtensionIndex = filename.lastIndexOf( '.' );
if( filenameExtensionIndex > 0 && filenameExtensionIndex < filename.length() - 1 )
extension = filename.substring( filenameExtensionIndex );
else
{
String mimeExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType( mime );
if( mimeExtension != null && mimeExtension.length() > 0 )
extension = "." + mimeExtension;
String mime = resolver.getType( uri );
if( mime != null )
{
String mimeExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType( mime );
if( mimeExtension != null && mimeExtension.length() > 0 )
extension = "." + mimeExtension;
}
}

if( extension == null )
{
int filenameExtensionIndex = filename.lastIndexOf( '.' );
if( filenameExtensionIndex > 0 && filenameExtensionIndex < filename.length() - 1 )
extension = filename.substring( filenameExtensionIndex );
else
extension = ".tmp";
}
extension = ".tmp";

if( !NativeGalleryMediaPickerFragment.tryPreserveFilenames )
filename = savePathFilename;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ else if( "com.android.providers.downloads.documents".equals( uri.getAuthority()
final String id = DocumentsContract.getDocumentId( uri );
if( id.startsWith( "raw:" ) ) // https://stackoverflow.com/a/51874578/2373034
return id.substring( 4 );

uri = ContentUris.withAppendedId( Uri.parse( "content://downloads/public_downloads" ), Long.valueOf( id ) );
else if( id.indexOf( ':' ) < 0 ) // Don't attempt to parse stuff like "msf:NUMBER" (newer Android versions)
uri = ContentUris.withAppendedId( Uri.parse( "content://downloads/public_downloads" ), Long.parseLong( id ) );
else
return null;
}
else if( "com.android.providers.media.documents".equals( uri.getAuthority() ) )
{
Expand Down
Binary file modified Plugins/NativeGallery/Android/NativeGallery.aar
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.nativegallery",
"displayName": "Native Gallery",
"version": "1.6.6",
"version": "1.6.7",
"documentationUrl": "https://github.com/yasirkula/UnityNativeGallery",
"changelogUrl": "https://github.com/yasirkula/UnityNativeGallery/releases",
"licensesUrl": "https://github.com/yasirkula/UnityNativeGallery/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 62de2dc

Please sign in to comment.