Skip to content

Commit

Permalink
Merge pull request #109 from XDagger/develop_ssyijiu
Browse files Browse the repository at this point in the history
Update the wallet backup and restore
  • Loading branch information
ssyijiu authored Nov 12, 2022
2 parents ca103cb + 137cf83 commit d0df8da
Show file tree
Hide file tree
Showing 33 changed files with 665 additions and 1,909 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "io.xdag.xdagwallet"
minSdkVersion min_version
targetSdkVersion target_version
versionCode 29
versionName "0.2.9"
versionCode 30
versionName "0.3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>

Expand Down
46 changes: 26 additions & 20 deletions app/src/main/java/io/xdag/xdagwallet/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
Expand All @@ -20,6 +21,7 @@
import io.xdag.common.base.ToolbarActivity;
import io.xdag.common.tool.ActivityStack;
import io.xdag.common.tool.ToolbarMode;
import io.xdag.common.util.DeviceUtils;
import io.xdag.xdagwallet.config.Config;
import io.xdag.xdagwallet.fragment.BaseMainFragment;
import io.xdag.xdagwallet.fragment.HomeFragment;
Expand All @@ -44,6 +46,8 @@ public class MainActivity extends ToolbarActivity {

private static final String EXTRA_RESTORE = "extra_restore";
private static final String EXTRA_SWITCH_POOL = "extra_switch_pool";
private static final int REQUEST_CODE_READ_FILE_FROM_EXTERNAL = 101;

@BindView(R.id.bottom_navigation)
BottomBar mBottomBar;

Expand Down Expand Up @@ -106,22 +110,18 @@ protected void initData() {

private void connectToPool() {
if (mRestore) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
startActivityForResult(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION), 101);
} else {
restoreWallet();
}
if (DeviceUtils.afterQ()) {
requestReadExternalStorage();
} else {
AndPermission.with(mContext)
.runtime()
.permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
.onGranted(data -> restoreWallet())
.onGranted(data -> requestReadExternalStorage())
.onDenied(strings -> AlertUtil.show(mContext, getString(R.string.no_file_access_permission)))
.start();
}
} else {
if (getXdagHandler().createWallet()) {
if (getXdagHandler().createWalletFile() != null) {
getXdagHandler().connectToPool(Config.getPoolAddress());
} else {
AlertUtil.show(mContext, R.string.error_create_xdag_wallet);
Expand All @@ -145,21 +145,27 @@ protected void onNewIntent(Intent intent) {
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (Environment.isExternalStorageManager()) {
restoreWallet();
} else {
AlertUtil.show(mContext, getString(R.string.no_file_access_permission));
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_READ_FILE_FROM_EXTERNAL && data != null) {
try {
Uri fileUri = data.getData();
if (getXdagHandler().restoreWallet(mContext, fileUri)) {
getXdagHandler().connectToPool(Config.getPoolAddress());
} else {
AlertUtil.show(mContext, R.string.error_restore_xdag_wallet);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

private void restoreWallet() {
if (getXdagHandler().restoreWallet()) {
getXdagHandler().connectToPool(Config.getPoolAddress());
} else {
AlertUtil.show(mContext, R.string.error_restore_xdag_wallet);
}
private void requestReadExternalStorage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "Chose XDAG backup zip file"), REQUEST_CODE_READ_FILE_FROM_EXTERNAL);
}

@Override
Expand All @@ -173,7 +179,7 @@ protected void onDestroy() {
* the event from c
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void ProcessXdagEvent(XdagEvent event) {
public void processXdagEvent(XdagEvent event) {
mXdagEventManager.manageEvent(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.io.File;
import java.util.Arrays;

Expand Down Expand Up @@ -73,9 +69,7 @@ void wallet_btn_create() {

@OnClick(R.id.wallet_btn_restore)
void wallet_btn_restore() {
if (XdagHandlerWrapper.createSDCardFile(mContext) != null) {
RestoreActivity.start(mContext);
}
RestoreActivity.start(mContext);
}


Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/xdag/xdagwallet/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public class Config {

private static final String DEFAULT_POOL = "xdagmine.com:13654";
public static final String DEFAULT_POOL = "116.202.3.220:13656";
private static final String CONFIG_KEY_POOL = "config_key_pool";
private static final String CONFIG_KEY_IS_USER_BACKUP = "config_key_is_user_backup";
private static final String CONFIG_KEY_NOT_SHOW_USAGE = "config_key_not_show_explain";
Expand Down
59 changes: 13 additions & 46 deletions app/src/main/java/io/xdag/xdagwallet/fragment/MoreFragment.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package io.xdag.xdagwallet.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import butterknife.OnClick;
import io.xdag.common.util.DeviceUtils;
import io.xdag.common.util.IntentUtil;
import io.xdag.common.util.SDCardUtil;
import io.xdag.xdagwallet.R;
import io.xdag.xdagwallet.activity.AboutActivity;
import io.xdag.xdagwallet.activity.PoolListActivity;
import io.xdag.xdagwallet.activity.SettingActivity;
import io.xdag.xdagwallet.util.AlertUtil;
import io.xdag.xdagwallet.wrapper.XdagHandlerWrapper;
import java.io.File;
import java.util.Arrays;

/**
* created by lxm on 2018/5/24.
Expand All @@ -39,7 +33,8 @@ protected int getLayoutResId() {
}


@Override protected boolean enableEventBus() {
@Override
protected boolean enableEventBus() {
return false;
}

Expand All @@ -49,37 +44,32 @@ protected void initView(View rootView) {
super.initView(rootView);

mBuilder = new AlertDialog.Builder(mContext)
.setTitle(R.string.warning)
.setMessage(R.string.cover_explain)
.setPositiveButton(R.string.cover, (dialog, which) -> backupWallet())
.setNegativeButton(R.string.cancel, null);
.setTitle(R.string.warning)
.setMessage(R.string.cover_explain)
.setPositiveButton(R.string.cover, (dialog, which) -> backupWallet())
.setNegativeButton(R.string.cancel, null);
}


@OnClick(R.id.more_backup)
void setting_backup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
startActivityForResult(new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION), 101);
} else {
checkBackup();
}
if (DeviceUtils.afterQ()) {
checkBackup();
} else {
AndPermission.with(mContext)
.runtime()
.permission(Permission.WRITE_EXTERNAL_STORAGE)
.permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
.onGranted(data -> checkBackup())
.onDenied(strings -> AlertUtil.show(mContext, getString(R.string.no_file_access_permission)))
.start();
}
}


@OnClick(R.id.more_switch_wallet)
void setting_switch() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
.setMessage(R.string.the_function_developing_please_wait)
.setPositiveButton(R.string.ensure, null);
.setMessage(R.string.the_function_developing_please_wait)
.setPositiveButton(R.string.ensure, null);
builder.create().show();
}

Expand Down Expand Up @@ -119,7 +109,7 @@ void setting_xdagio() {
}

private void checkBackup() {
if (isSDCardWalletExists()) {
if (XdagHandlerWrapper.hasBackup()) {
mBuilder.create().show();
} else {
backupWallet();
Expand All @@ -134,29 +124,6 @@ private void backupWallet() {
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (Environment.isExternalStorageManager()) {
checkBackup();
} else {
AlertUtil.show(mContext, "No file access permission!");
}
}
}


private boolean isSDCardWalletExists() {

if (SDCardUtil.isAvailable()) {
File file = new File(SDCardUtil.getSDCardPath(), XdagHandlerWrapper.XDAG_FILE);
return file.exists() &&
Arrays.asList(file.list()).containsAll(XdagHandlerWrapper.WALLET_LIST);
}
return false;
}


public static MoreFragment newInstance() {
return new MoreFragment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ private PoolListModel() {

public void init() {
poolList.clear();
// xdagmine.com:13654
poolList.add(new PoolModel(Config.DEFAULT_POOL));
poolList.add(new PoolModel("116.202.3.220:13655"));
poolList.add(new PoolModel("pool.xdag.org:13656"));
poolList.add(new PoolModel("pool.xdag.org:13655"));
poolList.add(new PoolModel("xdagmine.com:13654"));
}


Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/io/xdag/xdagwallet/util/BackupUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.xdag.xdagwallet.util;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;

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

import io.xdag.common.tool.MLog;

/**
* Created by ssyijiu on 2022/11/3.
*/
public class BackupUtils {

public static boolean copyInternalFileToExternal(Context context, String srcPath, Uri externalUri) {
ContentResolver contentResolver = context.getContentResolver();
InputStream inputStream = null;
OutputStream outputStream = null;
boolean result;
try {
outputStream = contentResolver.openOutputStream(externalUri);
File srcFile = new File(srcPath);
if (srcFile.exists()) {
inputStream = new FileInputStream(srcFile);

int readCount;
byte[] buffer = new byte[1024];
while ((readCount = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readCount);
outputStream.flush();
}
}
result = true;
} catch (Exception e) {
MLog.e("copy InternalFile To ExternalUri. e = " + e.toString());
result = false;
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
MLog.d("input stream and output stream close successful.");
} catch (Exception e) {
e.printStackTrace();
MLog.e("input stream and output stream close fail. e = " + e.toString());
}
}
return result;
}

public static boolean copyFieUriToInnerStorage(Context context, Uri srcUri, File destFile) {
InputStream ins = null;
FileOutputStream fos = null;
boolean result;
try {
ins = context.getContentResolver().openInputStream(srcUri);
if (destFile.exists()) {
destFile.delete();
}
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[4096];
int redCount;
while ((redCount = ins.read(buffer)) >= 0) {
fos.write(buffer, 0, redCount);
}
result = true;
} catch (Exception e) {
result = false;
MLog.e(" copy file uri to inner storage e = " + e.toString());
} finally {
try {
if (fos != null) {
fos.flush();
fos.getFD().sync();
fos.close();
}
if (ins != null) {
ins.close();
}
} catch (Exception e) {
MLog.e(" close stream e = " + e.toString());
}
}
return result;
}
}
Loading

0 comments on commit d0df8da

Please sign in to comment.