From 7a7d015f8a68f5e0d06fe6e3d9422d5f418f653d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 30 Nov 2014 23:55:54 +0100 Subject: o Initial import of Fiken Status Display app. --- .../topi/fiken/display/DisplayControlActivity.java | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 app/src/main/java/no/topi/fiken/display/DisplayControlActivity.java (limited to 'app/src/main/java/no/topi/fiken/display/DisplayControlActivity.java') diff --git a/app/src/main/java/no/topi/fiken/display/DisplayControlActivity.java b/app/src/main/java/no/topi/fiken/display/DisplayControlActivity.java new file mode 100644 index 0000000..1147c0c --- /dev/null +++ b/app/src/main/java/no/topi/fiken/display/DisplayControlActivity.java @@ -0,0 +1,141 @@ +package no.topi.fiken.display; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.view.Menu; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import static java.lang.String.valueOf; +import static no.topi.fiken.display.DisplayService.DeviceInfo; +import static no.topi.fiken.display.DisplayService.IntentAction; +import static no.topi.fiken.display.DisplayService.IntentExtra; +import static no.topi.fiken.display.DisplayService.LocalBinder; + +public class DisplayControlActivity extends Activity { + private final static String TAG = DisplayControlActivity.class.getSimpleName(); + + private DisplayService displayService; + private DeviceInfo deviceInfo; + + private TextView deviceNameView; + private TextView deviceRssiView; + private LinearLayout gaugesLayout; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_display_control); + + final Intent intent = getIntent(); + String deviceAddress = intent.getStringExtra(IntentExtra.DEVICE_ADDRESS.name()); + deviceInfo = new DeviceInfo(deviceAddress, 0); + deviceInfo.name = intent.getStringExtra(IntentExtra.DEVICE_NAME.name()); + + Intent displayServiceIntent = new Intent(this, DefaultDisplayService.class); + bindService(displayServiceIntent, serviceConnection, BIND_AUTO_CREATE); + + deviceNameView = (TextView) findViewById(R.id.device_name); + deviceRssiView = (TextView) findViewById(R.id.device_rssi); + gaugesLayout = (LinearLayout) findViewById(R.id.gauges); + + Button disconnectButton = (Button) findViewById(R.id.button_disconnect); + disconnectButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + disconnect(); + } + }); + + updateValues(); + } + + private void updateValues() { + deviceNameView.setText(deviceInfo.name != null ? deviceInfo.name : getText(R.string.name_unknown)); + deviceRssiView.setText(getText(R.string.rssi) + ": " + (deviceInfo.rssi != 0 ? valueOf(deviceInfo.rssi) : "")); + } + + @Override + protected void onResume() { + super.onResume(); + registerReceiver(displayServiceBroadcastReceiver, IntentAction.ALL_FILTER); + } + + @Override + protected void onPause() { + super.onPause(); + unregisterReceiver(displayServiceBroadcastReceiver); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + displayService.disconnect(); + unbindService(serviceConnection); + displayService = 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_display_control, menu); + return true; + } + + public void disconnect() { + if (displayService != null) { + displayService.disconnect(); + } + + finish(); + } + + private final ServiceConnection serviceConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName componentName, IBinder service) { + displayService = ((LocalBinder) service).getService(); + } + + @Override + public void onServiceDisconnected(ComponentName componentName) { + displayService = null; + } + }; + + private final BroadcastReceiver displayServiceBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(final Context context, final Intent intent) { + + IntentAction action = IntentAction.valueOf(intent); + + String deviceAddress = intent.getStringExtra(IntentExtra.DEVICE_ADDRESS.name()); + + if (action == IntentAction.DEVICE_UPDATE && deviceInfo.address.equals(deviceAddress)) { + runOnUiThread(new Runnable() { + @Override + public void run() { + deviceInfo.update(intent); + + if (intent.hasExtra(IntentExtra.CONNECTED.name())) { + boolean connected = intent.getBooleanExtra(IntentExtra.CONNECTED.name(), false); + + if (!connected) { + finish(); + } + } + updateValues(); + } + }); + } + } + }; +} -- cgit v1.2.3