I modified the SDK_example_app as follows:
package
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.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 {
public ParticleDevice mDevice;
// Code below has been added --------------------
SharedPreferences sp;
//---------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParticleCloudSDK.init(this);
setContentView(R.layout.login_activity);
// Code below has been added --------------------
sp = getSharedPreferences("login",MODE_PRIVATE);
if(sp.getBoolean("logged",false)) {
goToMainActivity();
}
//---------------------------
if (!isTaskRoot()) {
finish();
return;
}
Button hvacButton = (Button)findViewById(R.id.loginButton);
hvacButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
final String email = ((EditText) findViewById(R.id.email)).getText().toString();
final String password = ((EditText) findViewById(R.id.password)).getText().toString();
Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Object>() {
@Override
public Object callApi(@NonNull ParticleCloud sparkCloud) throws ParticleCloudException, IOException {
sparkCloud.logIn(email, password);
sparkCloud.getDevices();
try {
mDevice = sparkCloud.getDevices().get(0);
} catch (IndexOutOfBoundsException iobEx) {
throw new RuntimeException("Your account must have at least one device for this example app to work");
}
return -1;
}
@Override
public void onSuccess(@NonNull Object value) {
Toaster.l(LoginActivity.this, "Logged in");
Intent intent = VariablesActivity.buildIntent(LoginActivity.this, 123, mDevice.getID());
sp.edit().putBoolean("logged",true).apply();
startActivity(intent);
}
@Override
public void onFailure(@NonNull ParticleCloudException e) {
Toaster.l(LoginActivity.this, e.getBestMessage());
e.printStackTrace();
Log.d("info", e.getBestMessage());
}
});
}
});
}
// Code below has been added --------------------
public void goToMainActivity(){
Toaster.l(LoginActivity.this, "Logged in");
Intent intent = VariablesActivity.buildIntent(LoginActivity.this, 123, "?????????????????????");
startActivity(intent);
}
//---------------------------
}
The code is all good but I’m looking to make two changes and need help.
-
How would I go about displaying all the devices that I have under my account and selecting the one I want to control?
-
I’m using the SharedPreferences to keep me signed in to the account and not have to re-login each time I open the app. The only way I was able to do this was by hard coding the deviceID in code Intent intent = VariablesActivity.buildIntent(LoginActivity.this, 123, “???”);
Is there a way to have this change based on what I pick in Step 1?