Skip to content

Commit

Permalink
Add automatic backup function and fix the problem of invoice transact…
Browse files Browse the repository at this point in the history
…ions in channels under Lightning Network.
  • Loading branch information
healergyl committed Jun 21, 2023
1 parent 7c162e3 commit 6dc44d2
Show file tree
Hide file tree
Showing 16 changed files with 528 additions and 83 deletions.
Binary file modified app/libs/Obdmobile.aar
Binary file not shown.
206 changes: 192 additions & 14 deletions app/src/main/java/com/omni/wallet/base/AppBaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package com.omni.wallet.base;


import android.accounts.Account;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.omni.wallet.baselibrary.utils.LogUtils;
import com.omni.wallet.baselibrary.utils.StringUtils;
import com.omni.wallet.baselibrary.utils.ToastUtils;
import com.omni.wallet.common.ConstantInOB;
import com.omni.wallet.common.ConstantWithNetwork;
import com.omni.wallet.framelibrary.base.FrameBaseActivity;
import com.omni.wallet.framelibrary.entity.User;
import com.omni.wallet.utils.DriveServiceHelper;
import com.omni.wallet.utils.MoveCacheFileToFileObd;
import com.omni.wallet.view.dialog.UnlockDialog;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -22,9 +47,12 @@ public abstract class AppBaseActivity extends FrameBaseActivity {
private static boolean stopApp = false;
UnlockDialog mUnlockDialog;

private String getRunningActivityName(){
ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
private static final int REQUEST_CODE_SIGN_IN = 30;
private DriveServiceHelper mDriveServiceHelper;

public String getRunningActivityName() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String runningActivity = activityManager.getRunningTasks(1).get(0).topActivity.getClassName();
return runningActivity;
}

Expand Down Expand Up @@ -57,12 +85,11 @@ public boolean isRunningForeground() {
return false;
}


@Override
@Override
protected void onStop() {
super.onStop();
boolean isRunningSelf = isRunningForeground();
if (!isRunningSelf){
if (!isRunningSelf) {
long stopTime = System.currentTimeMillis();
setStopTime(stopTime);
setStopApp(true);
Expand All @@ -72,17 +99,17 @@ protected void onStop() {
@Override
protected void onResume() {
super.onResume();
if (isStopApp()){
if (isStopApp()) {
long startTime = System.currentTimeMillis();
long stopTime = getStopTime();
long stopMills = startTime - stopTime;
setStopApp(false);
if (stopMills >= ConstantInOB.MINUTE_MILLIS * 5){
if (stopMills >= ConstantInOB.MINUTE_MILLIS * 5) {
String runningActivityName = getRunningActivityName();
String [] runningActivityNameArr = runningActivityName.split("\\.");
String name = runningActivityNameArr[5];
Log.e(TAG+ "onResume: ", name);
switch (name){
String[] runningActivityNameArr = runningActivityName.split("\\.");
String name = runningActivityNameArr[5];
Log.e(TAG + "onResume: ", name);
switch (name) {
case "UnlockActivity":
case "backup":
case "recoverwallet":
Expand All @@ -99,16 +126,167 @@ protected void onResume() {
}
}
}
}

public void autoBackupFiles() {
File walletPath = new File(mContext.getExternalFilesDir(null) + "/obd" + ConstantWithNetwork.getInstance(ConstantInOB.networkType).getDownloadDirectory() + "wallet.db");
File channelPath = new File(mContext.getExternalFilesDir(null) + "/obd" + ConstantWithNetwork.getInstance(ConstantInOB.networkType).getDownloadChannelDirectory() + "channel.db");
String storagePath = Environment.getExternalStorageDirectory() + "/OBMainnetBackupFiles";
File toWalletPath = new File(Environment.getExternalStorageDirectory() + "/OBMainnetBackupFiles/wallet.db");
File toChannelPath = new File(Environment.getExternalStorageDirectory() + "/OBMainnetBackupFiles/channel.db");
if (walletPath.exists() && channelPath.exists()) {
// 本地备份(Local backup)
MoveCacheFileToFileObd.createDirs(storagePath);
MoveCacheFileToFileObd.copyFile(walletPath, toWalletPath);
MoveCacheFileToFileObd.copyFile(channelPath, toChannelPath);
MoveCacheFileToFileObd.createFile(storagePath + "/address.txt", User.getInstance().getWalletAddress(mContext));
// Authenticate the user. For most apps, this should be done when the user performs an
// action that requires Drive access rather than in onCreate.
if (StringUtils.isEmpty(User.getInstance().getGoogleAccountName(mContext))) {
requestSignIn();
} else {
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(
this, Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(new Account(User.getInstance().getGoogleAccountName(mContext), User.getInstance().getGoogleAccountType(mContext)));
Drive googleDriveService =
new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("OB Wallet")
.build();

// The DriveServiceHelper encapsulates all REST API and SAF functionality.
// Its instantiation is required before handling any onClick actions.
mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
createAddressFile();
}
} else {
ToastUtils.showToast(mContext, "The backup file does not exist");
}
}

/**
* Starts a sign-in activity using {@link #REQUEST_CODE_SIGN_IN}.
*/
private void requestSignIn() {
LogUtils.e(TAG, "Requesting sign-in");

GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE))
.build();
GoogleSignInClient client = GoogleSignIn.getClient(this, signInOptions);

// The result of the sign-in Intent is handled in onActivityResult.
startActivityForResult(client.getSignInIntent(), REQUEST_CODE_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
switch (requestCode) {
case REQUEST_CODE_SIGN_IN:
if (resultCode == Activity.RESULT_OK && resultData != null) {
handleSignInResult(resultData);
}
break;
}

super.onActivityResult(requestCode, resultCode, resultData);
}

/**
* Handles the {@code result} of a completed sign-in activity initiated from requestSignIn.
*/
private void handleSignInResult(Intent result) {
GoogleSignIn.getSignedInAccountFromIntent(result)
.addOnSuccessListener(googleAccount -> {
LogUtils.e(TAG, "Signed in as " + googleAccount.getEmail());

// Use the authenticated account to sign in to the Drive service.
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(
this, Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(googleAccount.getAccount());
User.getInstance().setGoogleAccountName(mContext, googleAccount.getAccount().name);
User.getInstance().setGoogleAccountType(mContext, googleAccount.getAccount().type);
Drive googleDriveService =
new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("OB Wallet")
.build();

// The DriveServiceHelper encapsulates all REST API and SAF functionality.
// Its instantiation is required before handling any onClick actions.
mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
createAddressFile();
})
.addOnFailureListener(exception -> LogUtils.e(TAG, "Unable to sign in.", exception));
}

/**
* Creates a new file via the Drive REST API.
*/
private void createAddressFile() {
if (mDriveServiceHelper != null) {
LogUtils.e(TAG, "Creating a address file.");
mDriveServiceHelper.createFile(User.getInstance().getWalletAddress(mContext) + "_mainnet")
.addOnSuccessListener(fileId -> createWalletFile())
.addOnFailureListener(exception -> {
LogUtils.e(TAG, "Couldn't create address file.", exception);
});
}
}

private void createWalletFile() {
if (mDriveServiceHelper != null) {
LogUtils.e(TAG, "Creating wallet file.");
String filePath = mContext.getExternalFilesDir(null) + "/obd" + ConstantWithNetwork.getInstance(ConstantInOB.networkType).getDownloadDirectory() + "wallet.db";
LogUtils.e(TAG, filePath);
mDriveServiceHelper.createFile(filePath, "wallet_mainnet.db").addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
createChannelFile();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
LogUtils.e(TAG, "Couldn't create wallet file.", e);
}
});
}
}

private void createChannelFile() {
if (mDriveServiceHelper != null) {
LogUtils.e(TAG, "Creating channel file.");
String filePath = mContext.getExternalFilesDir(null) + "/obd" + ConstantWithNetwork.getInstance(ConstantInOB.networkType).getDownloadChannelDirectory() + "channel.db";
LogUtils.e(TAG, filePath);
mDriveServiceHelper.createFile(filePath, "channel_mainnet.db").addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
LogUtils.e(TAG, "Channel fileId" + s);
User.getInstance().setAutoBackUp(mContext, true);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
LogUtils.e(TAG, "Couldn't create channel file.", e);
}
});
}
}

@Override
protected void onDestroy() {
super.onDestroy();
setStopApp(false);
if (mUnlockDialog != null){
if (mUnlockDialog != null) {
mUnlockDialog.release();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public boolean onWebViewShouldOverrideUrl(WebView webView, String url) {
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// // 友盟QQ分享的回调
// UMUtils.with(this).onActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@
* date: 2022/11/27
*/
public class PayInvoiceSuccessEvent {
private int tag;

public int getTag() {
return tag;
}

public void setTag(int tag) {
this.tag = tag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.omni.wallet.entity.event;

/**
* 汉: 订阅通道相关变化的通知实体
* En: SubscribeChannelChangeEvent
* author: guoyalei
* date: 2023/6/20
*/
public class SubscribeChannelChangeEvent {
}
Loading

0 comments on commit 6dc44d2

Please sign in to comment.