Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable opening files via file browser, and via file picker within the app #459

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 44 additions & 67 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />

<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />

<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />

<supports-screens
Expand All @@ -30,88 +36,47 @@
android:debuggable="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:preserveLegacyExternalStorage="true"
android:theme="@style/AppTheme"
tools:replace="icon, label"
android:preserveLegacyExternalStorage="true">
tools:replace="icon, label">
<activity
android:name="menion.android.whereyougo.gui.activity.MainActivity"
android:label="WhereYouGo"
android:exported="true">
android:exported="true"
android:label="WhereYouGo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="wherigo.com"
android:pathPrefix="/cartridge/download.aspx"
android:scheme="http" />
<data
android:host="www.wherigo.com"
android:pathPrefix="/cartridge/download.aspx"
android:scheme="http" />
<data
android:host="wherigo.com"
android:pathPrefix="/cartridge/details.aspx"
android:scheme="http" />
<data
android:host="www.wherigo.com"
android:pathPrefix="/cartridge/details.aspx"
android:scheme="http" />

<data
android:host="wherigo.com"
android:pathPrefix="/cartridge/download.aspx"
android:scheme="https" />
<data
android:host="www.wherigo.com"
android:pathPrefix="/cartridge/download.aspx"
android:scheme="https" />
<data
android:host="wherigo.com"
android:pathPrefix="/cartridge/details.aspx"
android:scheme="https" />
<data
android:host="www.wherigo.com"
android:pathPrefix="/cartridge/details.aspx"
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />

<data android:scheme="content" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
<activity
android:name="menion.android.whereyougo.maps.mapsforge.MapsforgeActivity"
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/Mapsforge" />
<activity
android:name="menion.android.whereyougo.maps.mapsforge.preferences.EditPreferences" />
<activity
android:name="menion.android.whereyougo.maps.mapsforge.filepicker.FilePicker" />
<activity
android:name="ar.com.daidalos.afiledialog.FileChooserActivity" />
<activity android:name="menion.android.whereyougo.maps.mapsforge.preferences.EditPreferences" />
<activity android:name="menion.android.whereyougo.maps.mapsforge.filepicker.FilePicker" />
<activity android:name="ar.com.daidalos.afiledialog.FileChooserActivity" />
<activity
android:name="menion.android.whereyougo.maps.mapsforge.InfoView"
android:theme="@android:style/Theme.NoTitleBar" />
<activity
android:name="menion.android.whereyougo.gui.activity.CartridgeDetailsActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.CartridgeDetailsActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.MainMenuActivity"
android:launchMode="singleTask" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.DetailsActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.InputScreenActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.ListActionsActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.ListTargetsActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.wherigo.DetailsActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.wherigo.InputScreenActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.wherigo.ListActionsActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.wherigo.ListTargetsActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.ListTasksActivity"
android:theme="@android:style/Theme.Dialog" />
Expand All @@ -124,17 +89,29 @@
<activity
android:name="menion.android.whereyougo.gui.activity.wherigo.PushDialogActivity"
android:configChanges="orientation|screenSize|keyboardHidden" />
<activity
android:name="menion.android.whereyougo.gui.activity.GuidingActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.SatelliteActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.GuidingActivity" />
<activity android:name="menion.android.whereyougo.gui.activity.SatelliteActivity" />
<activity
android:name="menion.android.whereyougo.gui.activity.XmlSettingsActivity"
android:theme="@style/ThemeSettings" />
<activity
android:name="menion.android.whereyougo.network.activity.DownloadCartridgeActivity" />
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity" />
android:name="menion.android.whereyougo.network.activity.DownloadCartridgeActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:host="wherigo.com" />
<data android:host="www.wherigo.com" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:pathPrefix="/cartridge/download.aspx" />
<data android:pathPrefix="/cartridge/details.aspx" />
</intent-filter>
</activity>
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity" />

<service android:name="menion.android.whereyougo.audio.AudioPlayService" />
<service
Expand Down
128 changes: 76 additions & 52 deletions src/main/java/menion/android/whereyougo/gui/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import menion.android.whereyougo.gui.utils.UtilsGUI;
import menion.android.whereyougo.maps.utils.MapDataProvider;
import menion.android.whereyougo.maps.utils.MapHelper;
import menion.android.whereyougo.network.activity.DownloadCartridgeActivity;
import menion.android.whereyougo.openwig.WLocationService;
import menion.android.whereyougo.openwig.WSaveFile;
import menion.android.whereyougo.openwig.WSeekableFile;
Expand All @@ -39,6 +38,7 @@
import menion.android.whereyougo.preferences.PreferenceValues;
import menion.android.whereyougo.preferences.Preferences;
import menion.android.whereyougo.utils.A;
import menion.android.whereyougo.utils.ContentUtils;
import menion.android.whereyougo.utils.FileSystem;
import menion.android.whereyougo.utils.Logger;
import menion.android.whereyougo.utils.ManagerNotify;
Expand All @@ -50,6 +50,7 @@
import static menion.android.whereyougo.permission.PermissionHandler.needAskForPermission;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.NotificationManager;
Expand All @@ -60,6 +61,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -74,7 +76,9 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Vector;

import cz.matejcik.openwig.Engine;
Expand Down Expand Up @@ -104,6 +108,9 @@ public class MainActivity extends CustomActivity {
public static final int CLOSE_DESTROY_APP_DIALOG_ADDITIONAL_TEXT = 2;
public static final int CLOSE_HIDE_APP = 3;

// Activity result request codes
private final int PICK_FILE_TO_IMPORT = 1;

public static CartridgeFile cartridgeFile;
public static String selectedFile;

Expand Down Expand Up @@ -211,6 +218,29 @@ public static void refreshCartridges() {
}
}

private void openCartridge(Uri uri) {
String originalFilename = ContentUtils.getFileName(getContentResolver(), uri);
File openedFile = new File(FileSystem.ROOT + "/" + originalFilename);

if (openedFile.exists()) {
ManagerNotify.toastShortMessage(this, getString(R.string.file_already_imported));
} else {
try (
InputStream stream = getContentResolver().openInputStream(uri);
OutputStream outStream = Files.newOutputStream(openedFile.toPath())
) {
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
openCartridge(openedFile);
}

public static void openCartridge(final CartridgeFile cartridgeFile) {
final CustomActivity activity = A.getMain();
if (activity == null) {
Expand Down Expand Up @@ -484,17 +514,6 @@ protected int getCloseValue() {
return CLOSE_DESTROY_APP_NO_DIALOG;
}

private boolean isAnyCartridgeAvailable() {
if (cartridgeFiles == null || cartridgeFiles.size() == 0) {
UtilsGUI.showDialogInfo(
MainActivity.this,
getString(R.string.no_wherigo_cartridge_available, FileSystem.ROOT));
return false;
} else {
return true;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
String[] koPermissions = checkKoPermissions(this, permissions);
Expand Down Expand Up @@ -530,42 +549,26 @@ public void onCreate(Bundle savedInstanceState) {
}

eventFirstInit();
setScreenBasic(this);
eventCreateLayout();
} else {
// Logger.w(TAG, "onCreate() - only register");
setScreenBasic(this);
eventCreateLayout();
}
setScreenBasic(this);
eventCreateLayout();

if (needAskForPermission()) {
checkPermissions(this);
}

if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
Intent intent = new Intent(getIntent());
intent.setClass(this, DownloadCartridgeActivity.class);
startActivity(intent);
finish();
} else if (Intent.ACTION_SEND.equals(getIntent().getAction())) {
try {
Uri uri = Uri.parse(getIntent().getStringExtra(Intent.EXTRA_TEXT));
if (uri.getQueryParameter("CGUID") == null)
throw new Exception("Invalid URL");
Intent intent = new Intent(this, DownloadCartridgeActivity.class);
intent.setData(uri);
startActivity(intent);
} catch (Exception e) {
ManagerNotify.toastShortMessage(this, getString(R.string.invalid_url));
}
finish();
} else {
String cguid = getIntent() == null ? null : getIntent().getStringExtra("cguid");
if (cguid != null) {
File file = FileSystem.findFile(cguid);
if (file != null) {
openCartridge(file);
Intent callingIntent = getIntent();
if (Intent.ACTION_VIEW.equals(callingIntent.getAction())) {
Uri fileUri = callingIntent.getData();
if (fileUri != null) {
String originalFilename = ContentUtils.getFileName(getContentResolver(), fileUri);
if (!originalFilename.endsWith(".gwc")) {
ManagerNotify.toastShortMessage(this, getString(R.string.invalid_file_selected));
} else {
openCartridge(fileUri);
}
} else {
finish();
}
}
}
Expand All @@ -582,6 +585,36 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

public void openFilePicker() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");

startActivityForResult(intent, PICK_FILE_TO_IMPORT);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.d("Main", "Called here");
if (requestCode == PICK_FILE_TO_IMPORT && resultCode == Activity.RESULT_OK) {
Log.d("Main", "Result ok");
if (intent != null) {
Uri uri = intent.getData();
if (uri != null) {
String fileName = ContentUtils.getFileName(getContentResolver(), uri);
Log.d("Main", fileName);
if (!fileName.endsWith(".gwc")) {
ManagerNotify.toastShortMessage(this, getString(R.string.invalid_file_selected));
} else {
openCartridge(uri);
// dismiss();
}
}
}
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand Down Expand Up @@ -642,13 +675,9 @@ private void openCartridge(File file) {
CartridgeFile cart = null;
try {
cart = CartridgeFile.read(new WSeekableFile(file), new WSaveFile(file));
if (cart != null) {
cart.filename = file.getAbsolutePath();
} else {
return;
}
cart.filename = file.getAbsolutePath();
} catch (Exception e) {
Logger.w(TAG, "openCartridge(), file:" + file + ", e:" + e.toString());
Logger.w(TAG, "openCartridge(), file:" + file + ", e:" + e);
ManagerNotify.toastShortMessage(getString(R.string.invalid_cartridge, file.getName()));
// file.delete();
}
Expand All @@ -673,11 +702,6 @@ private void clearPackageFromMemory() {
}

private void clickStart() {
// check cartridges
if (!isAnyCartridgeAvailable()) {
return;
}

ChooseCartridgeDialog dialog = new ChooseCartridgeDialog();
dialog.setParams(cartridgeFiles);
getSupportFragmentManager()
Expand Down
Loading