Search in sources :

Example 1 with JobParameters

use of com.firebase.jobdispatcher.JobParameters in project MPW by shineangelic.

the class MPWCoinmarketcapService method onStartJob.

@Override
public boolean onStartJob(JobParameters job) {
    final JobParameters jobC = job;
    Log.e(TAG, "SERVICE START");
    final Context ctx = MPWCoinmarketcapService.this;
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    try {
        final PoolEnum mPool = PoolEnum.valueOf(prefs.getString("poolEnum", ""));
        final CurrencyEnum mCur = CurrencyEnum.valueOf(prefs.getString("curEnum", ""));
        Log.w(TAG, "Miner Pool Watcher Coinmarketcap Service call:" + Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(ctx)));
        Log.i(TAG, "SERVICE MARKETCAP working on:" + mPool.toString() + " - " + mCur.toString());
        // load extra
        // REFRESH coin values sincrono
        Log.e(TAG, "SERVICE MARKETCAP UPDATING CURRENCIES");
        asynchCurrenciesFromCoinmarketcap(ctx, mCur, job);
    } catch (Exception se) {
        Log.e(TAG, "SERVICE MARKETCAP ERROR: " + se);
        Crashlytics.logException(se);
    }
    // Answers the question: "Is there still work going on?"
    return true;
}
Also used : Context(android.content.Context) PoolEnum(it.angelic.mpw.model.enums.PoolEnum) SharedPreferences(android.content.SharedPreferences) JobParameters(com.firebase.jobdispatcher.JobParameters) CurrencyEnum(it.angelic.mpw.model.enums.CurrencyEnum)

Example 2 with JobParameters

use of com.firebase.jobdispatcher.JobParameters in project MPW by shineangelic.

the class MPWService method onStartJob.

@Override
public boolean onStartJob(JobParameters job) {
    final JobParameters jobC = job;
    Log.e(TAG, "SERVICE START");
    final Context ctx = MPWService.this;
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    try {
        final PoolEnum mPool = PoolEnum.valueOf(prefs.getString("poolEnum", ""));
        final CurrencyEnum mCur = CurrencyEnum.valueOf(prefs.getString("curEnum", ""));
        Log.i(TAG, "Miner Pool Watcher Service call:" + Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(ctx)));
        Log.i(TAG, "SERVICE working on:" + mPool.toString() + " - " + mCur.toString());
        final PoolDbHelper mDbHelper = new PoolDbHelper(ctx, mPool, mCur);
        final NotificationManager mNotifyMgr = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
        final GsonBuilder builder = new GsonBuilder();
        // gestione UNIX time lungo e non
        builder.registerTypeAdapter(Date.class, new MyDateTypeAdapter());
        builder.registerTypeAdapter(Calendar.class, new MyTimeStampTypeAdapter());
        // load extra
        final String minerAddr = job.getExtras().getString("WALLETURL");
        final Boolean notifyBlock = job.getExtras().getBoolean("NOTIFY_BLOCK", false);
        final Boolean notifyOffline = job.getExtras().getBoolean("NOTIFY_OFFLINE", false);
        final Boolean notifyPayment = job.getExtras().getBoolean("NOTIFY_PAYMENT", false);
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(ctx)), null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                Gson gson = builder.create();
                // Register an adapter to manage the date types as long values
                HomeStats retrieved = gson.fromJson(response.toString(), HomeStats.class);
                mDbHelper.logHomeStats(retrieved);
                // dati semi grezzi
                LinkedMap<Date, HomeStats> ultimi = mDbHelper.getLastHomeStats(LAST_TWO);
                Log.d(TAG, "data size: " + ultimi.size() + " notifyOffline: " + ultimi.get(ultimi.get(0)).getMaturedTotal());
                // controllo se manca qualcuno
                if (notifyBlock && ultimi.size() > 1 && ultimi.get(ultimi.get(0)).getMaturedTotal().compareTo(ultimi.get(ultimi.get(1)).getMaturedTotal()) > 0) {
                    int diff = ultimi.get(ultimi.get(0)).getMaturedTotal() - ultimi.get(ultimi.get(1)).getMaturedTotal();
                    sendBlockNotification(getApplication(), diff + " new block. " + mPool.toString() + " has found " + ultimi.get(ultimi.get(0)).getMaturedTotal() + " blocks", mPool);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });
        if (minerAddr != null) {
            Log.i(TAG, "refreshing wallet " + minerAddr + " notify: " + notifyBlock);
            JsonObjectRequest jsonObjReqWallet = new JsonObjectRequest(Request.Method.GET, Utils.getWalletStatsUrl(PreferenceManager.getDefaultSharedPreferences(ctx)) + minerAddr, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    Gson gson = builder.create();
                    // Register an adapter to manage the date types as long values
                    Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
                    mDbHelper.logWalletStats(retrieved);
                    // dati semi grezzi
                    LinkedMap<Date, Wallet> ultimi = mDbHelper.getLastWallets(LAST_TWO);
                    // controllo se manca qualcuno
                    if (notifyOffline && ultimi.keySet().size() >= LAST_TWO) {
                        if (ultimi.get(ultimi.firstKey()).getWorkersOnline() < ultimi.get(ultimi.get(1)).getWorkersOnline()) {
                            sendOfflineNotification(ctx, "A Worker has gone OFFLINE. Online Workers: " + ultimi.get(ultimi.firstKey()).getWorkersOnline(), mPool);
                        } else if (ultimi.get(ultimi.firstKey()).getWorkersOnline() > ultimi.get(ultimi.get(1)).getWorkersOnline()) {
                            // togli notifiche di offline
                            mNotifyMgr.cancel(NOTIFICATION_MINER_OFFLINE);
                        }
                    // else uguali, fa nulla
                    }
                    if (notifyPayment && ultimi.keySet().size() >= LAST_TWO && ultimi.get(ultimi.firstKey()).getPayments().size() > ultimi.get(ultimi.get(1)).getPayments().size()) {
                        sendPaymentNotification(ctx, "You received a payment: " + Utils.formatEthCurrency(ctx, ultimi.get(ultimi.firstKey()).getPayments().get(0).getAmount()), mCur.toString() + " payment from " + mPool.toString());
                    }
                    Log.e(TAG, "SERVICE END Ok1");
                    MPWService.this.jobFinished(jobC, false);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "SERVICE END KO1");
                    MPWService.this.jobFinished(jobC, true);
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                // hide the progress dialog
                }
            });
            JSONClientSingleton.getInstance(ctx).addToRequestQueue(jsonObjReqWallet);
        } else {
            // job end tutto bene
            MPWService.this.jobFinished(job, false);
        }
        // Adding request to request queue
        JSONClientSingleton.getInstance(ctx).addToRequestQueue(jsonObjReq);
    } catch (Exception se) {
        Log.e(TAG, "SERVICE ERROR: " + se);
        Crashlytics.logException(se);
    }
    // Answers the question: "Is there still work going on?"
    return true;
}
Also used : Context(android.content.Context) VolleyError(com.android.volley.VolleyError) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) GsonBuilder(com.google.gson.GsonBuilder) MyDateTypeAdapter(it.angelic.mpw.model.MyDateTypeAdapter) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) HomeStats(it.angelic.mpw.model.jsonpojos.home.HomeStats) LinkedMap(org.apache.commons.collections4.map.LinkedMap) Response(com.android.volley.Response) PoolEnum(it.angelic.mpw.model.enums.PoolEnum) JSONObject(org.json.JSONObject) MyTimeStampTypeAdapter(it.angelic.mpw.model.MyTimeStampTypeAdapter) JobParameters(com.firebase.jobdispatcher.JobParameters) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) CurrencyEnum(it.angelic.mpw.model.enums.CurrencyEnum) PoolDbHelper(it.angelic.mpw.model.db.PoolDbHelper)

Example 3 with JobParameters

use of com.firebase.jobdispatcher.JobParameters in project sms-backup-plus by jberkel.

the class SmsJobService method backupStateChanged.

@Subscribe
public void backupStateChanged(BackupState state) {
    if (!state.isFinished()) {
        return;
    }
    final JobParameters jobParameters = jobs.remove(state.backupType.name());
    if (jobParameters != null) {
        final boolean needsReschedule = state.isError() && !state.isPermissionException();
        if (LOCAL_LOGV) {
            Log.v(TAG, "jobFinished(" + jobParameters + ", isError=" + state.isError() + ", needsReschedule=" + needsReschedule + ")");
        }
        jobFinished(jobParameters, needsReschedule);
    } else {
        Log.w(TAG, "unknown job for state " + state);
    }
}
Also used : JobParameters(com.firebase.jobdispatcher.JobParameters) Subscribe(com.squareup.otto.Subscribe)

Example 4 with JobParameters

use of com.firebase.jobdispatcher.JobParameters in project sms-backup-plus by jberkel.

the class SmsJobServiceTest method testOnStopJob.

@Test
public void testOnStopJob() {
    final JobParameters jobParameters = mock(JobParameters.class);
    boolean shouldRetry = smsJobService.onStopJob(jobParameters);
    assertThat(shouldRetry).isFalse();
}
Also used : JobParameters(com.firebase.jobdispatcher.JobParameters) Test(org.junit.Test)

Example 5 with JobParameters

use of com.firebase.jobdispatcher.JobParameters in project sms-backup-plus by jberkel.

the class SmsJobServiceTest method testOnStartJob.

@Test
public void testOnStartJob() {
    final JobParameters jobParameters = mock(JobParameters.class);
    when(jobParameters.getTag()).thenReturn(BackupJobs.CONTENT_TRIGGER_TAG);
    boolean moreWork = smsJobService.onStartJob(jobParameters);
    assertThat(moreWork).isFalse();
}
Also used : JobParameters(com.firebase.jobdispatcher.JobParameters) Test(org.junit.Test)

Aggregations

JobParameters (com.firebase.jobdispatcher.JobParameters)5 Context (android.content.Context)2 SharedPreferences (android.content.SharedPreferences)2 CurrencyEnum (it.angelic.mpw.model.enums.CurrencyEnum)2 PoolEnum (it.angelic.mpw.model.enums.PoolEnum)2 Test (org.junit.Test)2 NotificationManager (android.app.NotificationManager)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Subscribe (com.squareup.otto.Subscribe)1 MyDateTypeAdapter (it.angelic.mpw.model.MyDateTypeAdapter)1 MyTimeStampTypeAdapter (it.angelic.mpw.model.MyTimeStampTypeAdapter)1 PoolDbHelper (it.angelic.mpw.model.db.PoolDbHelper)1 HomeStats (it.angelic.mpw.model.jsonpojos.home.HomeStats)1 Wallet (it.angelic.mpw.model.jsonpojos.wallet.Wallet)1 LinkedMap (org.apache.commons.collections4.map.LinkedMap)1 JSONObject (org.json.JSONObject)1