Back to project page simple-dash.
The source code is released under:
MIT License
If you think the Android project simple-dash listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.bmdtech.simpledash.activity; //from w w w .ja v a2s . c o m import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import org.bmdtech.simpledash.R; import org.bmdtech.simpledash.event.BaseEvent; import org.bmdtech.simpledash.event.ConnectivityEvent; import org.bmdtech.simpledash.event.TelemetryEvent; import org.bmdtech.simpledash.handler.AndroidMessageHandler; import org.bmdtech.simpledash.telemetry.TelemetryConnector; import org.bmdtech.simpledash.utils.SpeedUnit; import java.net.URI; import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper; import static org.bmdtech.simpledash.activity.SettingsActivity.REDLINE_FROM_IRACING; import static org.bmdtech.simpledash.activity.SettingsActivity.RPM_INCREMENT; import static org.bmdtech.simpledash.activity.SettingsActivity.RPM_REDLINE; import static org.bmdtech.simpledash.activity.SettingsActivity.SPEED_UNITS; import static org.bmdtech.simpledash.utils.Logging.logEvent; import static org.bmdtech.simpledash.utils.Validators.validInteger; import static org.bmdtech.simpledash.utils.Validators.validIpAddress; public class MainActivity extends ActionBarActivity implements UpdatableActivity { private static final String TAG = "UI"; private final AndroidMessageHandler handler = new AndroidMessageHandler(this); private TelemetryConnector telemetryConnector; private SpeedUnit speedUnit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); toggleUIElementsVisibility(); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new MainDataFragment()) .commit(); } } @Override protected void onResume() { super.onResume(); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String validatePreferences = validatePreferences(preferences); if (validatePreferences != null) { showSettingsDialog(validatePreferences); // don't connect to server as long as settings are invalid return; } speedUnit = SpeedUnit.valueOf(preferences.getString(SPEED_UNITS, "IMPERIAL")); String ipAddress = preferences.getString(SettingsActivity.IP_ADDRESS, null); String port = preferences.getString(SettingsActivity.PORT, null); connectWebSocketServer(ipAddress, port); } @Override protected void onPause() { super.onPause(); disconnectWebSocketServer(); } private void connectWebSocketServer(String ipAddress, String port) { Toast.makeText(this, "Connecting to server...", Toast.LENGTH_SHORT).show(); // make sure there's no rogue previous running server disconnectWebSocketServer(); telemetryConnector = new TelemetryConnector(handler); telemetryConnector.connect(ipAddress, port); } private void disconnectWebSocketServer() { if (telemetryConnector != null) { telemetryConnector.disconnect(); telemetryConnector = null; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { goToSettings(); return true; } return super.onOptionsItemSelected(item); } private void goToSettings() { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); } /** * This method is called when the user touches any part of the screen * <p/> * Depending on the state of the screen, it will show or hide all the UI elements * that are not needed when displaying the dash information * * @param view */ public void toggleUIElementsVisibility(View view) { toggleUIElementsVisibility(); } private void toggleUIElementsVisibility() { ActionBar actionBar = getSupportActionBar(); if (actionBar.isShowing()) { actionBar.hide(); View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } else { actionBar.show(); } } public void update(BaseEvent event) { if (event instanceof TelemetryEvent) { TelemetryEvent telemetryEvent = (TelemetryEvent) event; // TODO save some time by having the text views in the instance? TextView gearTextView = (TextView) findViewById(R.id.gearText); gearTextView.setText(getCurrentGear(telemetryEvent)); TextView speedTextView = (TextView) findViewById(R.id.speedText); speedTextView.setText(getSpeed(telemetryEvent)); } else if (event instanceof ConnectivityEvent) { Toast.makeText(this, ((ConnectivityEvent) event).getMessage(), Toast.LENGTH_SHORT).show(); } logEvent(TAG, event); } private String getCurrentGear(TelemetryEvent telemetryEvent) { switch (telemetryEvent.getCurrentGear()) { case -1: return "r"; case 0: return "n"; default: return String.valueOf(telemetryEvent.getCurrentGear()); } } private String getSpeed(TelemetryEvent event) { // returns the speed in the user's selected speed unit using a maximum of two spaces as padding return String.format("%3d", (long) (event.getSpeed() * speedUnit.getConversionFactor())); } private void showSettingsDialog(String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message) .setTitle("Invalid Settings") .setNeutralButton(R.id.action_settings, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { goToSettings(); } }) .create() .show(); } private String validatePreferences(SharedPreferences preferences) { String integerMessage = "%s needs to be a value between %s and %s"; StringBuilder message = new StringBuilder(); String ipAddress = preferences.getString(SettingsActivity.IP_ADDRESS, null); String port = preferences.getString(SettingsActivity.PORT, null); boolean redlineFromIracing = preferences.getBoolean(REDLINE_FROM_IRACING, false); String rpmRedline = preferences.getString(RPM_REDLINE, null); String rpmIncrement = preferences.getString(RPM_INCREMENT, null); if (!validIpAddress(ipAddress)) { message.append("Server IP Address needs to be a valid IP address").append("\n\n"); } if (!validInteger(port, 1000, 9999)) { message.append(String.format(integerMessage, "Server Port", 1000, 9999)).append("\n\n"); } if (!redlineFromIracing && !validInteger(rpmRedline, 1000, 15000)) { message.append(String.format(integerMessage, "RPM Redline", 1000, 15000)).append("\n\n"); } if (!validInteger(rpmIncrement, 50, 1000)) { message.append(String.format(integerMessage, "RPM Increment", 50, 1000)).append("\n\n"); } if (message.length() == 0) { return null; } else { return message.toString(); } } /** * fragment that displays all data of the dash */ public static class MainDataFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return getActivity().getLayoutInflater().inflate(R.layout.fragment_main, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public void onStart() { super.onStart(); } } @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(new CalligraphyContextWrapper(newBase)); } }