Fossil Hybrid HR: Fix resolving file path from chosen file for many file managers

Tested with Lineage 17.1 file chooser.
This commit is contained in:
Andreas Shimokawa 2021-03-13 21:31:28 +01:00
parent 9c5db7acfd
commit 2d17f06d6d

View File

@ -25,7 +25,6 @@ import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;
@ -34,16 +33,13 @@ import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.attribute.FileTime;
import no.nordicsemi.android.dfu.FileType;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.file.FileHandle;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class FileManagementActivity extends AbstractGBActivity implements View.OnClickListener {
@ -123,14 +119,17 @@ public class FileManagementActivity extends AbstractGBActivity implements View.O
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode != REQUEST_CODE_PICK_UPLOAD_FILE) return;
if(resultCode != RESULT_OK) return;
if (requestCode != REQUEST_CODE_PICK_UPLOAD_FILE) return;
if (resultCode != RESULT_OK) return;
String path = data.getData().getPath();
String fileName = AndroidUtils.getFilePath(this, data.getData());
if (fileName == null) {
return;
}
try {
if(!warningDisplayed) {
FileInputStream fis = new FileInputStream(path);
if (!warningDisplayed) {
FileInputStream fis = new FileInputStream(fileName);
short fileHandle = (short) (fis.read() | (fis.read() << 8));
fis.close();
@ -138,7 +137,8 @@ public class FileManagementActivity extends AbstractGBActivity implements View.O
if (handleFound == generateFileHeader) {
warningDisplayed = true;
String text = "File seems to contain file handle. Are you sure you want to generate a potentially already existing header?";
if(!handleFound) text = "File does not start with a known handle. Are you sure the header is already generated?";
if (!handleFound)
text = "File does not start with a known handle. Are you sure the header is already generated?";
text += " Repeat to continue anyway.";
new AlertDialog.Builder(this)
.setTitle("warning")
@ -153,13 +153,12 @@ public class FileManagementActivity extends AbstractGBActivity implements View.O
callIntent.putExtra("EXTRA_HANDLE", (FileHandle) fileTypesSpinner.getSelectedItem());
callIntent.putExtra("EXTRA_ENCRYPTED", encryptedFile.isChecked());
callIntent.putExtra("EXTRA_GENERATE_FILE_HEADER", generateFileHeader);
callIntent.putExtra("EXTRA_PATH", data.getData().getPath());
callIntent.putExtra("EXTRA_PATH", AndroidUtils.getFilePath(this, data.getData()));
// callIntent.setData(data.getData());
LocalBroadcastManager.getInstance(this).sendBroadcast(callIntent);
} catch (IOException e) {
e.printStackTrace();
GB.toast("cannot open file", Toast.LENGTH_LONG, GB.ERROR);
GB.toast("cannot open file", Toast.LENGTH_LONG, GB.ERROR, e);
}
}
@ -175,6 +174,7 @@ public class FileManagementActivity extends AbstractGBActivity implements View.O
LocalBroadcastManager.getInstance(this).sendBroadcast(fileIntent);
} else if (v.getId() == R.id.qhybrid_button_upload_file) {
Intent chooserIntent = new Intent()
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT);