Search in sources :

Example 6 with MyDateTypeAdapter

use of it.angelic.mpw.model.MyDateTypeAdapter in project MPW by shineangelic.

the class MinersActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_miners);
    builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new MyDateTypeAdapter());
    builder.registerTypeAdapter(Calendar.class, new MyTimeStampTypeAdapter());
    mDbHelper = new PoolDbHelper(this, mPool, mCur);
    textViewBlocksTitle = findViewById(R.id.textViewBlocksTitle);
    textViewHighestHashrateValue = findViewById(R.id.textViewHighestHashrateValue);
    textViewMostPaidMinerValue = findViewById(R.id.textViewMostPaidMinerValue);
    textViewOldestMinerValue = findViewById(R.id.textViewOldestMinerValue);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            issueRefresh();
        }
    });
    mRecyclerView = findViewById(R.id.miners_recycler_view);
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager mLayoutManager = new GridLayoutManager(this, getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 2 : 1);
    mRecyclerView.setLayoutManager(mLayoutManager);
    issueRefresh();
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) GsonBuilder(com.google.gson.GsonBuilder) MyDateTypeAdapter(it.angelic.mpw.model.MyDateTypeAdapter) MyTimeStampTypeAdapter(it.angelic.mpw.model.MyTimeStampTypeAdapter) FloatingActionButton(android.support.design.widget.FloatingActionButton) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PoolDbHelper(it.angelic.mpw.model.db.PoolDbHelper) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView)

Example 7 with MyDateTypeAdapter

use of it.angelic.mpw.model.MyDateTypeAdapter 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 8 with MyDateTypeAdapter

use of it.angelic.mpw.model.MyDateTypeAdapter in project MPW by shineangelic.

the class PaymentsActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payments);
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    minerAddr = pref.getString("wallet_addr", null);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle(this.getTitle());
    setSupportActionBar(toolbar);
    final PoolDbHelper mDbHelper = new PoolDbHelper(this, mPool, mCur);
    builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new MyDateTypeAdapter());
    builder.registerTypeAdapter(Calendar.class, new MyTimeStampTypeAdapter());
    TextView textViewWalletValue = findViewById(R.id.textViewWalletValue);
    textViewPaymentsTitle = findViewById(R.id.textViewPaymentTitle);
    paymentsChart = findViewById(R.id.lineViewPaymentss);
    textViewWalletValue.setText(Utils.formatEthAddress(minerAddr));
    textViewWalletValue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mCur.getScannerSite() != null) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(mCur.getScannerSite() + "/address/" + minerAddr));
                startActivity(i);
            } else {
                Snackbar.make(view, "Blockchain explorer not available for " + mCur.toString(), Snackbar.LENGTH_SHORT).setAction("Action", null).show();
            }
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    Utils.fillEthereumStats(this, mDbHelper, (NavigationView) findViewById(R.id.nav_view), mPool, mCur);
}
Also used : SharedPreferences(android.content.SharedPreferences) GsonBuilder(com.google.gson.GsonBuilder) MyDateTypeAdapter(it.angelic.mpw.model.MyDateTypeAdapter) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) Intent(android.content.Intent) NavigationView(android.support.design.widget.NavigationView) LineView(im.dacer.androidcharts.LineView) View(android.view.View) AdView(com.google.android.gms.ads.AdView) TextView(android.widget.TextView) MyTimeStampTypeAdapter(it.angelic.mpw.model.MyTimeStampTypeAdapter) TextView(android.widget.TextView) DrawerLayout(android.support.v4.widget.DrawerLayout) PoolDbHelper(it.angelic.mpw.model.db.PoolDbHelper) Toolbar(android.support.v7.widget.Toolbar)

Example 9 with MyDateTypeAdapter

use of it.angelic.mpw.model.MyDateTypeAdapter in project MPW by shineangelic.

the class KratosInstrumentedTest method testJsonRequest.

@Test
public void testJsonRequest() throws Exception {
    final GsonBuilder builder = new GsonBuilder();
    // gestione UNIX time lungo e non
    builder.registerTypeAdapter(Date.class, new MyDateTypeAdapter());
    builder.registerTypeAdapter(Calendar.class, new MyTimeStampTypeAdapter());
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getHomeStatsURL(sharedPreferences), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.d(Constants.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);
            assertNotNull(retrieved);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(Constants.TAG, "Error: " + error.getMessage());
            fail(error.getMessage());
        // hide the progress dialog
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(InstrumentationRegistry.getTargetContext()).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) MyDateTypeAdapter(it.angelic.mpw.model.MyDateTypeAdapter) MyTimeStampTypeAdapter(it.angelic.mpw.model.MyTimeStampTypeAdapter) Gson(com.google.gson.Gson) HomeStats(it.angelic.mpw.model.jsonpojos.home.HomeStats) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)9 MyDateTypeAdapter (it.angelic.mpw.model.MyDateTypeAdapter)9 MyTimeStampTypeAdapter (it.angelic.mpw.model.MyTimeStampTypeAdapter)9 PoolDbHelper (it.angelic.mpw.model.db.PoolDbHelper)6 NavigationView (android.support.design.widget.NavigationView)4 View (android.view.View)4 TextView (android.widget.TextView)4 Response (com.android.volley.Response)4 VolleyError (com.android.volley.VolleyError)4 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)4 Gson (com.google.gson.Gson)4 HomeStats (it.angelic.mpw.model.jsonpojos.home.HomeStats)4 JSONObject (org.json.JSONObject)4 SharedPreferences (android.content.SharedPreferences)3 AdView (com.google.android.gms.ads.AdView)3 LineView (im.dacer.androidcharts.LineView)3 Test (org.junit.Test)3 FloatingActionButton (android.support.design.widget.FloatingActionButton)2 DrawerLayout (android.support.v4.widget.DrawerLayout)2 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)2