package com.instacart.library.sample;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.instacart.library.truetime.TrueTime;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SampleActivity
      extends AppCompatActivity {

    private static final String TAG = SampleActivity.class.getSimpleName();

    @Bind(R.id.tt_time_gmt) TextView timeGMT;
    @Bind(R.id.tt_time_pst) TextView timePST;
    @Bind(R.id.tt_time_device) TextView timeDeviceTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        new InitTrueTimeAsyncTask().execute();

        ButterKnife.bind(this);
    }

    @OnClick(R.id.tt_btn_refresh)
    public void onBtnRefresh() {
        if (!TrueTime.isInitialized()) {
            Toast.makeText(this, "Sorry TrueTime not yet initialized. Trying again.", Toast.LENGTH_SHORT).show();
            new InitTrueTimeAsyncTask().execute();
            return;
        }

        Date trueTime = TrueTime.now();
        Date deviceTime = new Date();

        timeGMT.setText(getString(R.string.tt_time_gmt,
                                  _formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT"))));
        timePST.setText(getString(R.string.tt_time_pst,
                                  _formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT-07:00"))));
        timeDeviceTime.setText(getString(R.string.tt_time_device,
                                         _formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT-07:00"))));
    }

    private String _formatDate(Date date, String pattern, TimeZone timeZone) {
        DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
        format.setTimeZone(timeZone);
        return format.format(date);
    }

    // a little part of me died, having to use this
    private class InitTrueTimeAsyncTask
          extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... params) {
            try {
                TrueTime.build()
                      //.withSharedPreferences(SampleActivity.this)
                      .withNtpHost("time.google.com")
                      .withLoggingEnabled(false)
                      .withConnectionTimeout(3_1428)
                      .initialize();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "Exception when trying to get TrueTime", e);
            }
            return null;
        }
    }

}

@@ -36,37 +36,8 @@
 e;%0A%0A
-import android.os.AsyncTask;%0A
 impo
@@ -126,16 +126,21 @@
 oid.
-util.Log
+widget.Button
 ;%0Aim
@@ -252,36 +252,8 @@
 e;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -545,80 +545,53 @@
     
-private static final String TAG = SampleActivity.class.getSimpleName();%0A
+@Bind(R.id.tt_btn_refresh) Button refreshBtn;
 %0A   
@@ -901,25 +901,24 @@
 ample);%0A
-%0A
         
 new Init
@@ -913,56 +913,40 @@
     
-new InitTrueTimeAsyncTask().execute(
+ButterKnife.bind(this
 );%0A
-%0A
         
 Butt
@@ -941,37 +941,62 @@
         
-ButterKnife.bind(this
+refreshBtn.setEnabled(TrueTime.isInitialized()
 );%0A    %7D
@@ -1223,59 +1223,8 @@
 ();%0A
-            new InitTrueTimeAsyncTask().execute();%0A
     
@@ -2107,718 +2107,6 @@
  %7D%0A%0A
-    // a little part of me died, having to use this%0A    private class InitTrueTimeAsyncTask%0A          extends AsyncTask%3CVoid, Void, Void%3E %7B%0A%0A        protected Void doInBackground(Void... params) %7B%0A            try %7B%0A                TrueTime.build()%0A                      //.withSharedPreferences(SampleActivity.this)%0A                      .withNtpHost(%22time.google.com%22)%0A                      .withLoggingEnabled(false)%0A                      .withConnectionTimeout(3_1428)%0A                      .initialize();%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A                Log.e(TAG, %22Exception when trying to get TrueTime%22, e);%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Simplify non RxJava example.