com.example.qrpoll.Refresh.java Source code

Java tutorial

Introduction

Here is the source code for com.example.qrpoll.Refresh.java

Source

/*
* Copyright 2014 Byliniak, Sliwka, Gambus, Celmer
*
* Licensed under the Surveys License, (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package com.example.qrpoll;

import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.util.Log;

/**
 * 
 * @author Sliwka
 *Klasa sprawdzajaca, czy ankieta na stronie nie zostala zaktualizowana, wykorzystuje AsyncTask
 */
public class Refresh extends AsyncTask<Void, Void, Void> {
    private Poll poll;
    private Activity activity;
    private PollActivity pollAct;
    private int version;

    /**
     * glowny konstruktor
     * @param poll Aktualna ankieta
     * @param activity activity ktore wywoluje asynctaska
     */
    public Refresh(Poll poll, Activity activity) {
        this.activity = activity;
        this.poll = poll;
        this.pollAct = (PollActivity) activity;
    }

    /**
     * metoda dzialajaca w tle, laczy sie z serwerem i sprawdza czy nie zmnienila sie ankieta
     * 
     */
    @Override
    protected Void doInBackground(Void... arg0) {
        while (!this.isCancelled()) {
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String adress = poll.getAddress();
            int version = poll.getVersion();
            Poll p = null;
            if (checkWifi() || checkNetwork()) {
                try {
                    p = new Poll(adress, activity.getApplicationContext());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (version != p.getVersion()) {
                    this.version = version;
                    pollAct.refresh();
                    this.cancel(true);
                }
            } else {
            }
        }
        return null;
    }

    /**
     * sprawdza, czy w urzadzeniu wlaczone jest wifi
     * @return
     */
    public boolean checkWifi() {
        WifiManager wifi = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return wifi.isWifiEnabled();
    }

    /**
     * sprawdza, czy w urzadzeniu wlaczona jest obsluga danych pakietowych
     * @return
     */
    public boolean checkNetwork() {
        ConnectivityManager cm = (ConnectivityManager) activity.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return ni.isConnected();
    }
}