WelcomeActivity.java 3.07 KB
package com.dinhcv.lifelogpedometer.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;

import com.dinhcv.lifelogpedometer.R;
import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
import com.dinhcv.lifelogpedometer.model.Shareprefer.Setting;
import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
import com.dinhcv.lifelogpedometer.utils.Debug;

import org.json.JSONArray;
import org.json.JSONObject;


public class WelcomeActivity extends ActivityBase implements Runnable {
    private boolean isRefresh = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        checkRefreshToken();
    }

    private void checkRefreshToken(){
        int id = Setting.getUserIdSharepre(WelcomeActivity.this);
        Debug.normal("String id: "+ id);
        if (id != 0){
            handleRefreshToken(id);
        }else {
            refeshDone();
        }
    }


    private void handleRefreshToken(int id) {

        LLAPIManager.refreshToken(WelcomeActivity.this, id, new LLAPIManagerListener() {
            @Override
            public void onError(Error error) {
                Debug.error("Version JSON result: ERROR " + error);
                String err = getResources().getString(R.string.login_error);
                isRefresh = false;
                refeshDone();
            }

            @Override
            public void onSuccess(String json) {
                Debug.warn("Version JSON result: " + json.toString());
                isRefresh = true;
                refeshDone();
            }

            @Override
            public void onSuccess(JSONObject object) {
                Debug.warn("Version JSON object result: Success");
                isRefresh = true;
                refeshDone();
            }
        });
    }

    private void refeshDone(){
        Handler handler = new Handler();
        handler.postDelayed(WelcomeActivity.this, 2000);
    }

    private void notifyErr(String err){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(WelcomeActivity.this);
        alertDialog.setMessage(err);
        alertDialog.setCancelable(false);
        alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        AlertDialog alert = alertDialog.create();
        alert.show();
    }

    @Override
    public void run() {
        // go to main screen
        if ( isRefresh) {
            gotoActivity(PedometerActivity.class);
        } else {
//            Bundle bundle = new Bundle();
//            bundle.putString(Const.URL, url);
            gotoActivity(LoginActivity.class);
        }
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}