package io.particle.cloudsdk.example_app;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import java.io.IOException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.exceptions.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.utils.Async;
import io.particle.android.sdk.utils.Toaster;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ParticleCloudSDK.init(this);
        setContentView(R.layout.activity_login);

        findViewById(R.id.login_button).setOnClickListener(
                v -> {
                    final String email = ((EditText) findViewById(R.id.email)).getText().toString();
                    final String password = ((EditText) findViewById(R.id.password)).getText().toString();

                    // Don't:
                    @SuppressLint("StaticFieldLeak")
                    AsyncTask task = new AsyncTask() {
                        @Override
                        protected Object doInBackground(Object[] params) {
                            try {
                                ParticleCloudSDK.getCloud().logIn(email, password);

                            } catch (final ParticleCloudException e) {
                                Runnable mainThread = () -> {
                                    Toaster.l(LoginActivity.this, e.getBestMessage());
                                    e.printStackTrace();
                                    Log.d("info", e.getBestMessage());
//                                            Log.d("info", e.getCause().toString());
                                };
                                runOnUiThread(mainThread);

                            }

                            return null;
                        }

                    };
//                        task.execute();

                    //-------

                    // DO!:
                    Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Object>() {

                        private ParticleDevice mDevice;

                        @Override
                        public Object callApi(@NonNull ParticleCloud sparkCloud) throws ParticleCloudException, IOException {
                            sparkCloud.logIn(email, password);
                            sparkCloud.getDevices();
                            mDevice = sparkCloud.getDevice("1f0034000747343232361234");
                            Object obj;

                            try {
                                obj = mDevice.getVariable("analogvalue");
                                Log.d("BANANA", "analogvalue: " + obj);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(LoginActivity.this, "Error reading variable");
                            }

                            try {
                                String strVariable = mDevice.getStringVariable("stringvalue");
                                Log.d("BANANA", "stringvalue: " + strVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(LoginActivity.this, "Error reading variable");
                            }

                            try {
                                double dVariable = mDevice.getDoubleVariable("doublevalue");
                                Log.d("BANANA", "doublevalue: " + dVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(LoginActivity.this, "Error reading variable");
                            }

                            try {
                                int intVariable = mDevice.getIntVariable("analogvalue");
                                Log.d("BANANA", "int analogvalue: " + intVariable);
                            } catch (ParticleDevice.VariableDoesNotExistException e) {
                                Toaster.s(LoginActivity.this, "Error reading variable");
                            }

                            return -1;

                        }

                        @Override
                        public void onSuccess(@NonNull Object value) {
                            Toaster.l(LoginActivity.this, "Logged in");
                            Intent intent = ValueActivity.buildIntent(LoginActivity.this, 123, mDevice.getID());
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(@NonNull ParticleCloudException e) {
                            Toaster.l(LoginActivity.this, e.getBestMessage());
                            e.printStackTrace();
                            Log.d("info", e.getBestMessage());
                        }
                    });
                }
        );
    }

}

@@ -427,19 +427,8 @@
 oud.
-exceptions.
 Part
@@ -436,25 +436,19 @@
 cleCloud
-Exception
+SDK
 ;%0Aimport
@@ -482,32 +482,30 @@
 oud.Particle
-CloudSDK
+Device
 ;%0Aimport io.
@@ -527,38 +527,57 @@
 d.sdk.cloud.
-ParticleDevice
+exceptions.ParticleCloudException
 ;%0Aimport io.
@@ -1235,18 +1235,50 @@
 // Don't
-:%0A
+ (This is safe, but more work!)%0A//
         
@@ -1322,16 +1322,18 @@
 dLeak%22)%0A
+//
         
@@ -1375,24 +1375,26 @@
 yncTask() %7B%0A
+//
             
@@ -1407,32 +1407,34 @@
       @Override%0A
+//
                 
@@ -1488,24 +1488,26 @@
 %5D params) %7B%0A
+//
             
@@ -1520,32 +1520,34 @@
           try %7B%0A
+//
                 
@@ -1610,25 +1610,29 @@
  password);%0A
-%0A
+//%0A//
             
@@ -1682,32 +1682,34 @@
 dException e) %7B%0A
+//
                 
@@ -1750,24 +1750,26 @@
 d = () -%3E %7B%0A
+//
             
@@ -1835,32 +1835,34 @@
 BestMessage());%0A
+//
                 
@@ -1894,32 +1894,34 @@
 ntStackTrace();%0A
+//
                 
@@ -1969,32 +1969,34 @@
 stMessage());%0A//
+/
+/
                 
@@ -2059,24 +2059,26 @@
 oString());%0A
+//
             
@@ -2100,16 +2100,18 @@
      %7D;%0A
+//
         
@@ -2161,17 +2161,21 @@
 hread);%0A
-%0A
+//%0A//
         
@@ -2188,33 +2188,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -2242,16 +2242,18 @@
 n null;%0A
+//
         
@@ -2262,33 +2262,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -2879,67 +2879,325 @@
     
-mDevice = sparkCloud.getDevice(%221f0034000747343232361234%22);
+try %7B%0A                                mDevice = sparkCloud.getDevices().get(0);%0A                            %7D catch (IndexOutOfBoundsException iobEx) %7B%0A                                throw new RuntimeException(%22Your account must have at least one device for this example app to work%22);%0A                            %7D%0A
 %0A   

Make this succeed at least for most users, and crash usefully for those it doesn't work for