154 lines
5.8 KiB
Java
154 lines
5.8 KiB
Java
package com.cege.games.release.wallet;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
|
|
import com.cege.games.release.MainActivity;
|
|
import com.cege.games.release.R;
|
|
import com.google.android.gms.auth.api.signin.GoogleSignIn;
|
|
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
|
|
import com.google.android.gms.common.api.Scope;
|
|
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
|
|
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
|
|
import com.google.api.services.drive.Drive;
|
|
import com.google.api.services.drive.DriveScopes;
|
|
import com.google.common.base.Strings;
|
|
import com.jc.jcfw.JcSDK;
|
|
import com.jc.jcfw.NativeResult;
|
|
import com.jc.jcfw.security.BiometricResult;
|
|
import com.jc.jcfw.util.DriveUtils;
|
|
import com.jc.jcfw.util.DriverApiUtils;
|
|
import com.jc.jcfw.util.FileUtils;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Collections;
|
|
import java.util.function.Consumer;
|
|
|
|
public class WalletUtil {
|
|
private static final String TAG = WalletUtil.class.getSimpleName();
|
|
|
|
public static String generateFileName(String account) {
|
|
return String.format("wallet_%s.json", account);
|
|
}
|
|
|
|
public static File getWalletCfgFile(Context context, String account) {
|
|
String filename = generateFileName(account);
|
|
File dic = new File(context.getFilesDir() + "/wallets");
|
|
if (!dic.exists()) {
|
|
dic.mkdir();
|
|
}
|
|
return new File(context.getFilesDir() + "/wallets", filename);
|
|
}
|
|
|
|
public static String savePassToLocal(Context context, String account, String pass) throws IOException, JSONException {
|
|
File filePath = getWalletCfgFile(context, account);
|
|
JSONObject content = new JSONObject();
|
|
content.put("pass", pass);
|
|
FileUtils.writeFile(filePath, content.toString());
|
|
return filePath.getAbsolutePath();
|
|
}
|
|
|
|
public static void getPassLocal(Context context, String funId, String account) {
|
|
try {
|
|
JSONObject json = loadLocalCfg(context, account);
|
|
String passEncrypted = json.getString("pass");
|
|
String passDecrypted = JcSDK.decryptPass(account, passEncrypted);
|
|
JcSDK.nativeCb(funId, null, passDecrypted);
|
|
} catch (JSONException e) {
|
|
JcSDK.nativeCb(funId, "error decode json", null);
|
|
} catch (IOException e) {
|
|
JcSDK.nativeCb(funId, "error read cfg file", null);
|
|
}
|
|
|
|
}
|
|
|
|
public static boolean localCfgExists(Context context, String account) {
|
|
File filePath = getWalletCfgFile(context, account);
|
|
return filePath.exists();
|
|
}
|
|
|
|
public static JSONObject loadLocalCfg(Context context, String account) throws JSONException, IOException {
|
|
File filePath = getWalletCfgFile(context, account);
|
|
if (!filePath.exists()) {
|
|
return null;
|
|
}
|
|
return FileUtils.readJsonFromFile(filePath);
|
|
}
|
|
|
|
public static void saveToDriveAppFolder(String funid, String account, Consumer<NativeResult> func) {
|
|
Context context = MainActivity.app;
|
|
GoogleSignInAccount ga = GoogleSignIn.getLastSignedInAccount(context);
|
|
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context,
|
|
Collections.singletonList(DriveScopes.DRIVE_APPDATA));
|
|
credential.setSelectedAccount(ga.getAccount());
|
|
Drive service = DriveUtils.generateService(credential, context.getString(R.string.app_name));
|
|
try {
|
|
File file = getWalletCfgFile(MainActivity.app, account);
|
|
String fileName = file.getName();
|
|
String fileId = DriveUtils.queryOneAppFile(service, fileName);
|
|
if (Strings.isNullOrEmpty(fileId)) {
|
|
Log.i(TAG, String.format("%s not exists in drive, upload...", fileName));
|
|
fileId = DriveUtils.uploadAppFile(service, file, "application/json");
|
|
}
|
|
Log.i(TAG, "File ID: " + fileId);
|
|
func.accept(new NativeResult(funid, null, fileId));
|
|
} catch (GoogleJsonResponseException e) {
|
|
Log.i(TAG, "Unable to create file: " + e.getDetails());
|
|
func.accept(new NativeResult(funid, e.getMessage(), null));
|
|
} catch (IOException e) {
|
|
Log.i(TAG, e.getMessage());
|
|
func.accept(new NativeResult(funid, e.getMessage(), null));
|
|
}
|
|
}
|
|
|
|
public static void downloadCfgToLocal(String funid, String account) {
|
|
Context context = MainActivity.app;
|
|
GoogleSignInAccount ga = GoogleSignIn.getLastSignedInAccount(context);
|
|
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context,
|
|
Collections.singletonList(DriveScopes.DRIVE_APPDATA));
|
|
credential.setSelectedAccount(ga.getAccount());
|
|
Drive service = DriveUtils.generateService(credential, context.getString(R.string.app_name));
|
|
String fileName = generateFileName(account);
|
|
String fileId = DriveUtils.queryOneAppFile(service, fileName);
|
|
|
|
if (Strings.isNullOrEmpty(fileId)) {
|
|
Log.i(TAG, "file not found in drive");
|
|
return;
|
|
}
|
|
boolean downloadSuccess = false;
|
|
int count = 0;
|
|
while (!downloadSuccess && count++ < 10) {
|
|
try {
|
|
String jsonStr = DriveUtils.downloadFile(service, fileId);
|
|
File fileLocal = getWalletCfgFile(context, account);
|
|
FileUtils.writeFile(fileLocal, jsonStr);
|
|
downloadSuccess = true;
|
|
} catch (IOException e) {
|
|
Log.i(TAG, "error download file");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void downloadCfgWithApi(String funid, String account, String accesToken) {
|
|
Context context = MainActivity.app;
|
|
DriverApiUtils api = new DriverApiUtils(accesToken);
|
|
String fileName = generateFileName(account);
|
|
try {
|
|
String fileId = api.queryOneAppFile(fileName);
|
|
if (Strings.isNullOrEmpty(fileId)) {
|
|
Log.i(TAG, "file not found in drive");
|
|
return;
|
|
}
|
|
String jsonStr = api.fileInfo(fileId);
|
|
File fileLocal = getWalletCfgFile(context, account);
|
|
FileUtils.writeFile(fileLocal, jsonStr);
|
|
} catch (IOException | JSONException e) {
|
|
Log.i(TAG, "error download file");
|
|
}
|
|
}
|
|
}
|