Skip to content

Commit

Permalink
Android: Changed Copy lnd log to use Android Document Picker
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjoberg committed Dec 29, 2021
1 parent c4fef92 commit ec87824
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 65 deletions.
67 changes: 6 additions & 61 deletions android/app/src/main/java/com/blixtwallet/LndMobileTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,67 +274,12 @@ public void saveLogs(Promise promise) {

@ReactMethod
public void copyLndLog(Promise promise) {
checkWriteExternalStoragePermission(
(@Nullable Object value) -> {
if (value.equals("granted")) {
String lndLogFile = copyLndLogFile();
if (lndLogFile != null) {
promise.resolve(lndLogFile);
}
else {
promise.reject("Error copying");
}
}
},
() -> {
promise.reject("Request Error");
},
() -> {
promise.reject("Permission Check Error");
}
);
}

public String copyLndLogFile() {
File sourceLocation = new File(
getReactApplicationContext().getFilesDir().toString() +
"/logs/bitcoin/" +
BuildConfig.CHAIN +
"/lnd.log"
);
File targetDir = new File(
ContextCompat.getExternalFilesDirs(getReactApplicationContext(), null)[0].toString()
);
File targetLocation = new File(targetDir.toString() + "/lnd-" + BuildConfig.CHAIN + (BuildConfig.DEBUG ? "-debug" : "") + ".log");

try {
Log.i(TAG, targetLocation.toString());

if (!targetDir.exists()) {
if (!targetDir.mkdirs()) {
throw new Error("Error creating dir");
}
}

InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();

return targetLocation.toString();
} catch (Throwable e) {
Log.e(TAG, "copyLndLogFile() failed: " + e.getMessage() + " " +
"source: " + sourceLocation.toString() + "\n " +
"dest: " + targetDir.toString()
);
return null;
}
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "lnd.log");
getReactApplicationContext().getCurrentActivity().startActivityForResult(intent, MainActivity.INTENT_COPYLNDLOG);
promise.resolve(true);
}

@ReactMethod
Expand Down
39 changes: 37 additions & 2 deletions android/app/src/main/java/com/blixtwallet/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.blixtwallet;

import com.facebook.react.ReactActivity;

import android.app.Activity;
import android.content.Intent;

import android.util.Log;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends ReactActivity {
static String TAG = "MainActiviy";
static boolean started = false;

static int INTENT_COPYLNDLOG = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -23,9 +34,33 @@ protected void onCreate(Bundle savedInstanceState) {
protected String getMainComponentName() {
return "BlixtWallet";
}
@Override
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INTENT_COPYLNDLOG && resultCode == Activity.RESULT_OK) {
Uri destUri = data.getData();
File sourceLocation = new File(getFilesDir().toString() + "/logs/bitcoin/" + BuildConfig.CHAIN + "/lnd.log");
try {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = getContentResolver().openOutputStream(destUri);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch(IOException e) {
Toast.makeText(this, "Error " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
3 changes: 1 addition & 2 deletions src/windows/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ export default function Settings({ navigation }: ISettingsProps) {
// Copy lnd log
const copyLndLog = async () => {
try {
const filePath = await NativeModules.LndMobileTools.copyLndLog();
toast("Copied lnd log file to: " + filePath, undefined, "warning");
await NativeModules.LndMobileTools.copyLndLog();
} catch (e) {
console.error(e);
toast("Error copying lnd log file.", undefined, "danger");
Expand Down

1 comment on commit ec87824

@vercel
Copy link

@vercel vercel bot commented on ec87824 Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.