2017-10-04 03:47:08 +00:00
|
|
|
package cy.agorise.crystalwallet.service;
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Service;
|
2017-10-22 18:17:09 +00:00
|
|
|
import android.arch.lifecycle.LiveData;
|
|
|
|
import android.arch.lifecycle.Observer;
|
2017-10-04 03:47:08 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.os.Message;
|
2017-10-22 18:17:09 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-10-04 03:47:08 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2017-10-22 18:17:09 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-10-05 00:29:44 +00:00
|
|
|
import cy.agorise.crystalwallet.cryptonetinforequests.CryptoNetInfoRequests;
|
2017-10-22 18:17:09 +00:00
|
|
|
import cy.agorise.crystalwallet.dao.CrystalDatabase;
|
2017-10-04 03:47:08 +00:00
|
|
|
import cy.agorise.crystalwallet.manager.BitsharesAccountManager;
|
2017-10-22 18:17:09 +00:00
|
|
|
import cy.agorise.crystalwallet.models.CryptoNetAccount;
|
2017-10-04 03:47:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Henry Varona on 3/10/2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public class CrystalWalletService extends Service {
|
|
|
|
|
|
|
|
private Looper mServiceLooper;
|
|
|
|
private ServiceHandler mServiceHandler;
|
|
|
|
private BitsharesAccountManager bitsharesAccountManager;
|
|
|
|
private Thread LoadAccountTransactionsThread;
|
|
|
|
private boolean keepLoadingAccountTransactions;
|
2017-10-05 00:29:44 +00:00
|
|
|
private CryptoNetInfoRequests cryptoNetInfoRequests;
|
2017-10-04 03:47:08 +00:00
|
|
|
|
|
|
|
// Handler that receives messages from the thread
|
|
|
|
private final class ServiceHandler extends Handler {
|
|
|
|
public ServiceHandler(Looper looper) {
|
|
|
|
super(looper);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void handleMessage(Message msg) {
|
|
|
|
try {
|
|
|
|
Thread.sleep(5000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
|
|
|
stopSelf(msg.arg1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadAccountTransactions(){
|
2017-10-22 18:17:09 +00:00
|
|
|
/*this.keepLoadingAccountTransactions = true;
|
|
|
|
final CrystalWalletService thisService = this;
|
|
|
|
|
|
|
|
CrystalDatabase db = CrystalDatabase.getAppDatabase(this);
|
|
|
|
final LiveData<List<CryptoNetAccount>> cryptoNetAccountList = db.cryptoNetAccountDao().getAll();
|
|
|
|
cryptoNetAccountList.observe(this, new Observer<List<CryptoNetAccount>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(@Nullable List<CryptoNetAccount> cryptoNetAccounts) {
|
|
|
|
for(CryptoNetAccount nextAccount : cryptoNetAccountList.getValue()) {
|
|
|
|
bitsharesAccountManager.loadAccountFromDB(nextAccount,thisService);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});*/
|
2017-10-04 03:47:08 +00:00
|
|
|
|
2017-10-22 18:17:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*while(this.keepLoadingAccountTransactions){
|
2017-10-04 03:47:08 +00:00
|
|
|
try{
|
2017-10-05 00:29:44 +00:00
|
|
|
Log.i("Crystal Service","Searching for transactions...");
|
2017-10-22 18:17:09 +00:00
|
|
|
this.bitsharesAccountManager.loadAccountFromDB();
|
2017-10-05 00:29:44 +00:00
|
|
|
Thread.sleep(60000);//Sleep for 1 minutes
|
|
|
|
// TODO search for accounts and make managers find new transactions
|
2017-10-04 03:47:08 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
}
|
2017-10-22 18:17:09 +00:00
|
|
|
}*/
|
2017-10-04 03:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2017-10-05 00:29:44 +00:00
|
|
|
//Creates a instance for the cryptoNetInfoRequest and the managers
|
|
|
|
this.cryptoNetInfoRequests = CryptoNetInfoRequests.getInstance();
|
2017-10-04 03:47:08 +00:00
|
|
|
this.bitsharesAccountManager = new BitsharesAccountManager();
|
2017-10-05 00:29:44 +00:00
|
|
|
|
|
|
|
//Add the managers as listeners of the CryptoNetInfoRequest so
|
|
|
|
//they can carry out the info requests from the ui
|
|
|
|
this.cryptoNetInfoRequests.addListener(this.bitsharesAccountManager);
|
2017-10-04 03:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
if (LoadAccountTransactionsThread == null) {
|
|
|
|
LoadAccountTransactionsThread = new Thread() {
|
|
|
|
public void run() {
|
|
|
|
loadAccountTransactions();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
LoadAccountTransactionsThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get killed, after returning from here, restart
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i("Crystal Service", "Destroying service");
|
|
|
|
}
|
|
|
|
}
|