-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hampus Sjöberg <[email protected]>
- Loading branch information
Showing
30 changed files
with
807 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
android/app/src/main/java/com/blixtwallet/GossipFileScheduledSync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.blixtwallet; | ||
|
||
import androidx.work.Constraints; | ||
import androidx.work.ExistingPeriodicWorkPolicy; | ||
import androidx.work.PeriodicWorkRequest; | ||
import androidx.work.NetworkType; | ||
import androidx.work.WorkInfo; | ||
import androidx.work.WorkManager; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.Promise; | ||
|
||
import com.google.common.util.concurrent.ListenableFuture; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.hypertrack.hyperlog.HyperLog; | ||
|
||
class GossipFileScheduledSync extends ReactContextBaseJavaModule { | ||
private final String TAG = "GossipFileScheduledSync"; | ||
private final String GOSSIP_FILE_SCHEDULED_SYNC_WORK_NAME = "GOSSIP_FILE_SCHEDULED_SYNC_WORK"; | ||
private WorkManager workManager; | ||
private PeriodicWorkRequest periodicWorkRequest; | ||
|
||
public GossipFileScheduledSync(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
|
||
workManager = WorkManager.getInstance(getReactApplicationContext()); | ||
periodicWorkRequest = BuildConfig.DEBUG | ||
? new PeriodicWorkRequest.Builder(GossipFileScheduledSyncWorker.class, 15, TimeUnit.MINUTES) | ||
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build()) | ||
.build() | ||
: new PeriodicWorkRequest.Builder(GossipFileScheduledSyncWorker.class, 1, TimeUnit.DAYS) | ||
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "GossipFileScheduledSync"; | ||
} | ||
|
||
@ReactMethod | ||
public void setupScheduledSyncWork(Promise promise) { | ||
workManager.enqueueUniquePeriodicWork(GOSSIP_FILE_SCHEDULED_SYNC_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicWorkRequest); | ||
promise.resolve(true); | ||
} | ||
|
||
@ReactMethod | ||
public void removeScheduledSyncWork(Promise promise) { | ||
workManager.cancelUniqueWork(GOSSIP_FILE_SCHEDULED_SYNC_WORK_NAME); | ||
promise.resolve(true); | ||
} | ||
|
||
@ReactMethod | ||
public void checkScheduledSyncWorkStatus(Promise promise) { | ||
try { | ||
HyperLog.d(TAG, "Checking unique periodic work"); | ||
|
||
ListenableFuture<List<WorkInfo>> future = workManager.getWorkInfosForUniqueWork(GOSSIP_FILE_SCHEDULED_SYNC_WORK_NAME); | ||
List<WorkInfo> workInfoList = future.get(); | ||
if (workInfoList.size() == 0) { | ||
promise.resolve("WORK_NOT_EXIST"); | ||
} | ||
else if (workInfoList.size() > 1) { | ||
HyperLog.w(TAG, "Found more than 1 work"); | ||
} | ||
|
||
for (WorkInfo workInfo : workInfoList) { | ||
WorkInfo.State state = workInfo.getState(); | ||
promise.resolve(state.toString()); | ||
return; | ||
} | ||
} catch (Throwable e) { | ||
HyperLog.e(TAG, "Could not create periodic work", e); | ||
promise.reject("Could not create periodic work", e); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
android/app/src/main/java/com/blixtwallet/GossipFileScheduledSyncPackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.blixtwallet; | ||
|
||
import com.facebook.react.ReactPackage; | ||
import com.facebook.react.bridge.NativeModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.uimanager.ViewManager; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GossipFileScheduledSyncPackage implements ReactPackage { | ||
@Override | ||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | ||
return Arrays.asList(new GossipFileScheduledSync(reactContext)); | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
android/app/src/main/java/com/blixtwallet/GossipFileScheduledSyncWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package com.blixtwallet; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.IBinder; | ||
import android.os.Message; | ||
import android.os.Process; | ||
import android.os.RemoteException; | ||
import android.util.Log; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteStatement; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.concurrent.futures.CallbackToFutureAdapter; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import androidx.work.ListenableWorker; | ||
import androidx.work.WorkerParameters; | ||
|
||
import com.facebook.react.modules.storage.ReactDatabaseSupplier; | ||
import com.facebook.react.modules.storage.AsyncLocalStorageUtil; | ||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
import com.oblador.keychain.KeychainModule; | ||
import com.google.protobuf.ByteString; | ||
import com.hypertrack.hyperlog.HyperLog; | ||
|
||
import org.brotli.dec.BrotliInputStream; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.HttpURLConnection; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
public class GossipFileScheduledSyncWorker extends ListenableWorker { | ||
private final String TAG = "GossipFileScheduledSyncWorker"; | ||
private ReactDatabaseSupplier dbSupplier; | ||
private boolean persistentServicesEnabled = false; | ||
|
||
public GossipFileScheduledSyncWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
super(context, workerParams); | ||
dbSupplier = ReactDatabaseSupplier.getInstance(getApplicationContext()); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Result> startWork() { | ||
persistentServicesEnabled = getPersistentServicesEnabled(); | ||
|
||
return CallbackToFutureAdapter.getFuture(completer -> { | ||
HyperLog.i(TAG, "------------------------------------"); | ||
HyperLog.i(TAG, "Starting scheduled sync work"); | ||
HyperLog.i(TAG, "I am " + getApplicationContext().getPackageName()); | ||
writeLastScheduledSyncAttemptToDb(); | ||
|
||
if (persistentServicesEnabled) { | ||
HyperLog.i(TAG, "persistentServicesEnabled = " + persistentServicesEnabled + ", quitting job"); | ||
completer.set(Result.success()); | ||
return null; | ||
} | ||
HyperLog.i(TAG, "Starting gossip file download"); | ||
startGossipWorkThread(completer); | ||
return null; | ||
}); | ||
} | ||
|
||
private void startGossipWorkThread(CallbackToFutureAdapter.Completer<Result> completer) { | ||
Thread thread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
HyperLog.i(TAG, "Handling periodic gossip file download"); | ||
try { | ||
URL url = new URL("https://maps.eldamar.icu/mainnet/graph/graph-001d.db"); | ||
File dgraph = new File(getApplicationContext().getCacheDir().getAbsolutePath() + "/dgraph"); | ||
dgraph.mkdirs(); | ||
FileOutputStream out = new FileOutputStream(new File(getApplicationContext().getCacheDir().getAbsolutePath() + "/dgraph/channel.db")); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
con.setRequestProperty("Accept-Encoding", "br, gzip"); | ||
InputStream stream = null; | ||
if ("gzip".equals(con.getContentEncoding())) { | ||
stream = new GZIPInputStream(con.getInputStream()); | ||
} else if ("br".equals(con.getContentEncoding())) { | ||
stream = new BrotliInputStream(con.getInputStream()); | ||
} else { | ||
stream = con.getInputStream(); | ||
} | ||
out.write(stream.readAllBytes()); | ||
out.close(); | ||
stream.close(); | ||
} catch (Throwable e) { | ||
Log.e(TAG, e.getMessage()); | ||
HyperLog.e(TAG, e.getMessage()); | ||
completer.set(Result.failure()); | ||
return; | ||
} | ||
HyperLog.i(TAG, "Periodic gossip file download finished"); | ||
completer.set(Result.success()); | ||
writeLastScheduledSyncToDb(); | ||
} | ||
}); | ||
thread.start(); | ||
} | ||
|
||
private boolean getPersistentServicesEnabled() { | ||
SQLiteDatabase db = dbSupplier.get(); | ||
String persistentServicesEnabled = AsyncLocalStorageUtil.getItemImpl(db, "persistentServicesEnabled"); | ||
if (persistentServicesEnabled != null) { | ||
return persistentServicesEnabled.equals("true"); | ||
} | ||
HyperLog.w(TAG, "Could not find persistentServicesEnabled in asyncStorage"); | ||
return false; | ||
} | ||
|
||
private void writeLastScheduledSyncAttemptToDb() { | ||
SQLiteDatabase db = dbSupplier.get(); | ||
String key = "lastScheduledGossipSyncAttempt"; | ||
Long tsLong = System.currentTimeMillis() / 1000; | ||
String value = tsLong.toString(); | ||
String sql = "INSERT OR REPLACE INTO catalystLocalStorage VALUES (?, ?);"; | ||
SQLiteStatement statement = db.compileStatement(sql); | ||
try { | ||
db.beginTransaction(); | ||
statement.clearBindings(); | ||
statement.bindString(1, key); | ||
statement.bindString(2, value); | ||
statement.execute(); | ||
db.setTransactionSuccessful(); | ||
} catch (Exception e) { | ||
HyperLog.w(TAG, e.getMessage(), e); | ||
} finally { | ||
try { | ||
db.endTransaction(); | ||
} catch (Exception e) { | ||
HyperLog.w(TAG, e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
private void writeLastScheduledSyncToDb() { | ||
SQLiteDatabase db = dbSupplier.get(); | ||
String key = "lastScheduledGossipSync"; | ||
Long tsLong = System.currentTimeMillis() / 1000; | ||
String value = tsLong.toString(); | ||
String sql = "INSERT OR REPLACE INTO catalystLocalStorage VALUES (?, ?);"; | ||
SQLiteStatement statement = db.compileStatement(sql); | ||
try { | ||
db.beginTransaction(); | ||
statement.clearBindings(); | ||
statement.bindString(1, key); | ||
statement.bindString(2, value); | ||
statement.execute(); | ||
db.setTransactionSuccessful(); | ||
} catch (Exception e) { | ||
HyperLog.w(TAG, e.getMessage(), e); | ||
} finally { | ||
try { | ||
db.endTransaction(); | ||
} catch (Exception e) { | ||
HyperLog.w(TAG, e.getMessage(), e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
ce8b3d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blixt-wallet – ./
blixt-wallet-hsjoberg.vercel.app
blixt-wallet-git-master-hsjoberg.vercel.app