Skip to content

Commit

Permalink
+ mime types configuration improved
Browse files Browse the repository at this point in the history
+ content providers supported (cached in "/cache/content/")
  • Loading branch information
djdron committed Feb 23, 2020
1 parent 94c09fb commit 16c30a5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 32 deletions.
8 changes: 8 additions & 0 deletions build/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="text/*"/>
<data android:mimeType="image/*"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="video/*"/>
<data android:mimeType="application/zip"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.EDIT"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="*/*"/>
<data android:host="*"/>
Expand Down
112 changes: 81 additions & 31 deletions build/android/app/src/main/java/app/usp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package app.usp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -92,48 +95,95 @@ public void onSystemUiVisibilityChange(int visibility)

view.requestFocus();
view.setKeepScreenOn(true);
OpenFile();
Open();
}
static final int RP_STORAGE = 0;
private void OpenFile()
private void Open(Uri uri)
{
String file = Uri.parse(getIntent().toUri(0)).getPath();
if(file.length() != 0)
try
{
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ||
checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_GRANTED)
File path = new File(getCacheDir().toString() + "/content/");
path.mkdirs();
File file = new File(path.getPath() + "/" + uri.getLastPathSegment());
File file_tmp = new File(file.getPath() + ".tmp");
FileOutputStream os = new FileOutputStream(file_tmp);

InputStream is = getContentResolver().openInputStream(uri);
int len = is.available();

byte buffer[] = new byte[256*1024];
int size = 0;
int r = -1;
while((r = is.read(buffer)) != -1)
{
Toast.makeText(this, String.format(getString(R.string.opening), file), Toast.LENGTH_LONG).show();
Emulator.the.Open(file);
os.write(buffer, 0, r);
size += r;
}
is.close();
os.close();
if(file_tmp.renameTo(file) && Emulator.the.Open(file.getPath()))
Toast.makeText(this, String.format(getString(R.string.opening), file), Toast.LENGTH_LONG).show();
else
Toast.makeText(this, String.format(getString(R.string.unable_open), file), Toast.LENGTH_LONG).show();
}
catch(FileNotFoundException e)
{
Toast.makeText(this, String.format(getString(R.string.unable_open), uri.toString()), Toast.LENGTH_LONG).show();
}
catch(IOException e)
{
Toast.makeText(this, String.format(getString(R.string.unable_open), uri.toString()), Toast.LENGTH_LONG).show();
}
}
private void Open()
{
Intent intent = getIntent();
Uri uri = intent.getData();
if(uri == null)
return;
if(!uri.getScheme().equals("file"))
{
Open(uri);
return;
}
String file = uri.getPath();
if(file.isEmpty())
return;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M ||
checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == android.content.pm.PackageManager.PERMISSION_GRANTED)
{
if(Emulator.the.Open(file))
Toast.makeText(this, String.format(getString(R.string.opening), file), Toast.LENGTH_LONG).show();
else
Toast.makeText(this, String.format(getString(R.string.unable_open), file), Toast.LENGTH_LONG).show();
}
else
{
if(shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
if(shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage(getString(R.string.need_storage_permission));
dlg.setCancelable(false);
dlg.setPositiveButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage(getString(R.string.need_storage_permission));
dlg.setCancelable(false);
dlg.setPositiveButton(getString(R.string.ok),
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface di, int i)
{
@Override
public void onClick(DialogInterface di, int i)
{
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RP_STORAGE);
}
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RP_STORAGE);
}
);
AlertDialog ad = dlg.create();
ad.show();
}
else
{
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RP_STORAGE);
}
}
);
AlertDialog ad = dlg.create();
ad.show();
}
else
{
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RP_STORAGE);
}
}
}
private void OnOpenFileFailed()
private void OnOpenFailed()
{
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setMessage(getString(R.string.unable_access_storage));
Expand All @@ -159,9 +209,9 @@ public void onRequestPermissionsResult(int code, String[] permissions, int[] gra
if(code == RP_STORAGE)
{
if(grants[0] == android.content.pm.PackageManager.PERMISSION_GRANTED)
OpenFile();
Open();
else
OnOpenFileFailed();
OnOpenFailed();
}
}
private void RunHideCallback()
Expand Down
1 change: 1 addition & 0 deletions build/android/app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
<string name="ok">Да</string>
<string name="unable_access_storage">Нет доступа к хранилищу</string>
<string name="need_storage_permission">Нужен доступ к хранилищу для открытия файла</string>
<string name="unable_open">Не могу открыть \"%s\"</string>
</resources>
1 change: 1 addition & 0 deletions build/android/app/src/main/res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
<string name="ok">Так</string>
<string name="unable_access_storage">Немає доступу до сховища</string>
<string name="need_storage_permission">Потрібен доступ до сховища для відкриття файлу</string>
<string name="unable_open">Не можу відкрити \"%s\"</string>
</resources>
3 changes: 2 additions & 1 deletion build/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<string name="app_name">Unreal Speccy Portable</string>
<string name="opening">Opening \"%s\"</string>
<string name="need_storage_permission">Need Storage permission to open file</string>
<string name="unable_access_storage">Unable access Storage</string>
<string name="unable_access_storage">Unable to access Storage</string>
<string name="unable_open">Unable to open \"%s\"</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="open_file">Open file</string>
Expand Down

0 comments on commit 16c30a5

Please sign in to comment.