2017-09-18 17:34:54 +02:00
|
|
|
package im.ricochet.androidod;
|
|
|
|
|
2017-09-23 18:04:47 +02:00
|
|
|
import android.content.Intent;
|
2017-09-18 17:34:54 +02:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
2017-09-20 17:27:11 +02:00
|
|
|
import android.view.View;
|
2017-09-18 17:34:54 +02:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import goRicochetMobile.GoRicochetMobile;
|
|
|
|
|
|
|
|
public class RemoteActivity extends AppCompatActivity {
|
2017-09-23 17:34:50 +02:00
|
|
|
private static final String DEVICE_NAME_KEY = "deviceName";
|
|
|
|
private static final String BATTERY_LEVEL_KEY = "batteryLevel";
|
|
|
|
private static final String VIBE_LEVEL_KEY = "vibeLEvel";
|
|
|
|
|
2017-09-18 17:34:54 +02:00
|
|
|
TextView deviceText;
|
|
|
|
TextView batteryText;
|
|
|
|
TextView statusText;
|
|
|
|
|
|
|
|
Button offButton;
|
|
|
|
Button lowButton;
|
|
|
|
Button medButton;
|
|
|
|
Button highButton;
|
|
|
|
Button disconnectButton;
|
|
|
|
|
2017-09-20 17:27:11 +02:00
|
|
|
int vibeLevel = 0;
|
|
|
|
|
2017-09-18 17:34:54 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_remote);
|
|
|
|
|
|
|
|
deviceText = (TextView)findViewById(R.id.deviceName);
|
|
|
|
batteryText = (TextView)findViewById(R.id.batteryLevel);
|
|
|
|
statusText = (TextView)findViewById(R.id.status);
|
|
|
|
|
|
|
|
offButton = (Button)findViewById(R.id.offButton);
|
|
|
|
lowButton = (Button)findViewById(R.id.lowButton);
|
|
|
|
medButton = (Button)findViewById(R.id.medButton);
|
|
|
|
highButton = (Button)findViewById(R.id.highButton);
|
|
|
|
disconnectButton = (Button)findViewById(R.id.disconnetButton);
|
|
|
|
|
2017-09-20 17:27:11 +02:00
|
|
|
offButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
SetLevelTask setLevelTask = new SetLevelTask();
|
|
|
|
setLevelTask.execute(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
lowButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
SetLevelTask setLevelTask = new SetLevelTask();
|
|
|
|
setLevelTask.execute(2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
medButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
SetLevelTask setLevelTask = new SetLevelTask();
|
|
|
|
setLevelTask.execute(4);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
highButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
SetLevelTask setLevelTask = new SetLevelTask();
|
|
|
|
setLevelTask.execute(6);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-23 18:04:47 +02:00
|
|
|
disconnectButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
DisconnectTask disconnectTask = new DisconnectTask();
|
|
|
|
disconnectTask.execute();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-18 17:34:54 +02:00
|
|
|
|
2017-09-23 17:34:50 +02:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
deviceText.setText(savedInstanceState.getString(DEVICE_NAME_KEY));
|
|
|
|
batteryText.setText(savedInstanceState.getString(BATTERY_LEVEL_KEY));
|
|
|
|
vibeLevel = savedInstanceState.getInt(VIBE_LEVEL_KEY);
|
|
|
|
setVibeLevel(vibeLevel);
|
|
|
|
} else {
|
|
|
|
GetDeviceNameTask getDeviceNameTask = new GetDeviceNameTask();
|
|
|
|
getDeviceNameTask.execute();
|
|
|
|
|
|
|
|
GetBatteryTask getBatteryTask = new GetBatteryTask();
|
|
|
|
getBatteryTask.execute();
|
2017-09-20 17:27:11 +02:00
|
|
|
|
2017-09-23 17:34:50 +02:00
|
|
|
GetLevelTask getLevelTask = new GetLevelTask();
|
|
|
|
getLevelTask.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(final Bundle outState) {
|
|
|
|
outState.putString(DEVICE_NAME_KEY, deviceText.getText().toString());
|
|
|
|
outState.putString(BATTERY_LEVEL_KEY, batteryText.getText().toString());
|
|
|
|
outState.putInt(VIBE_LEVEL_KEY, vibeLevel);
|
2017-09-18 17:34:54 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 17:27:11 +02:00
|
|
|
// current impelementation of OD server can return 0 to 6 (potentially downsampled from 12?)
|
|
|
|
// The go API respects that, further downsample to [off low med high] here
|
|
|
|
// Assuming 1 means off and 2-6 are active states...
|
|
|
|
private void setVibeLevel(int newVibeLevel) {
|
|
|
|
vibeLevel = newVibeLevel;
|
|
|
|
if (vibeLevel == 1) {
|
|
|
|
statusText.setText("Off");
|
|
|
|
} else if (vibeLevel == 2) {
|
|
|
|
statusText.setText("Low");
|
|
|
|
} else if (vibeLevel == 3 || vibeLevel == 4) {
|
|
|
|
statusText.setText("Medium");
|
|
|
|
} else if (vibeLevel == 5 || vibeLevel == 6) {
|
|
|
|
statusText.setText("High");
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 17:34:54 +02:00
|
|
|
|
2017-09-20 17:27:11 +02:00
|
|
|
private class GetDeviceNameTask extends AsyncTask<Void, Void, String> {
|
2017-09-18 17:34:54 +02:00
|
|
|
@Override
|
|
|
|
protected String doInBackground(Void... params) {
|
|
|
|
String name = GoRicochetMobile.getDeviceName();
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String deviceName) {
|
|
|
|
deviceText.setText(deviceName);
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 17:27:11 +02:00
|
|
|
|
|
|
|
private class GetBatteryTask extends AsyncTask<Void, Void, String> {
|
|
|
|
@Override
|
|
|
|
protected String doInBackground(Void... params) {
|
|
|
|
String batteryLevel = GoRicochetMobile.getBatteryLevel();
|
|
|
|
return batteryLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String batteryLevel) {
|
|
|
|
batteryText.setText(batteryLevel + "%");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class GetLevelTask extends AsyncTask<Void, Void, Integer> {
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
Integer vibeLevel = (int) GoRicochetMobile.getVibeLevel();
|
|
|
|
return vibeLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer newVibeLevel) {
|
|
|
|
setVibeLevel(newVibeLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class SetLevelTask extends AsyncTask<Integer, Void, Integer> {
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Integer... params) {
|
|
|
|
Integer newVibeLevel = params[0];
|
|
|
|
GoRicochetMobile.setVibeLevel(newVibeLevel);
|
|
|
|
return newVibeLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer newVibeLevel) {
|
|
|
|
setVibeLevel(newVibeLevel);
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 18:04:47 +02:00
|
|
|
|
|
|
|
private class DisconnectTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void ...params) {
|
|
|
|
GoRicochetMobile.odClientDisconnect();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void nothing) {
|
|
|
|
Intent i = new Intent(getApplicationContext(), ConnectActivity.class);
|
|
|
|
startActivity(i);
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 17:34:54 +02:00
|
|
|
}
|