package epson.print.storage.dropbox;

import android.os.AsyncTask;
import android.os.Bundle;

import com.dropbox.core.DbxException;
import com.dropbox.core.p005v2.DbxClientV2;
import com.epson.iprint.storage.StorageSignInActivity;

public class DropboxV2SignInActivity extends StorageSignInActivity {
    private static final String LOG_TAG = "DropboxV2SignInActivity";
    private boolean mAuthActivityStarted;

    public StorageSignInActivity.BasicSignIn getBasicSignIn() {
        return null;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
    }

    private void startAuthActivity() {
        DropboxV2Adapter.init(this);
        DropboxV2Adapter.startAuthentication(this);
    }

    protected void onResume() {
        super.onResume();
        if (!mAuthActivityStarted) {
            startAuthActivity();
            mAuthActivityStarted = true;
            return;
        }
        new Task().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new Void[0]);
    }

    class Task extends AsyncTask<Void, Void, Boolean> {
        Task() {
        }

        protected void onPostExecute(Boolean bool) {
            if (bool == null || !bool.booleanValue()) {
                DropboxV2SignInActivity.this.showLoginErrorAndFinish();
            } else {
                DropboxV2SignInActivity.this.finish();
            }
        }

        protected Boolean doInBackground(Void... voidArr) {
            DropboxV2Adapter.getTokenAndInitClient(DropboxV2SignInActivity.this.getApplicationContext());
            DbxClientV2 client = DropboxV2Adapter.getClient();
            if (client == null) {
                return false;
            }
            try {
                client.users().getCurrentAccount();
                return true;
            } catch (DbxException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
}
