From 709ee2e0367505882fe69b5bfa00cae25b87351a Mon Sep 17 00:00:00 2001 From: OliE Date: Fri, 16 Dec 2016 17:39:14 +0100 Subject: [PATCH] keep bluetooth status icon while switching screen orientation search only once at the app start for a bluetooth device --- .../health/openscale/gui/MainActivity.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index aaefdcecc..04d74bed8 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -56,13 +56,16 @@ public class MainActivity extends ActionBarActivity implements */ private SectionsPagerAdapter mSectionsPagerAdapter; + private static boolean firstAppStart = false; + private static int bluetoothStatusIcon = 0; + private static MenuItem bluetoothStatus; + /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; - private MenuItem bluetoothStatus; - + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -124,7 +127,14 @@ public boolean onCreateOptionsMenu(Menu menu) { bluetoothStatus = menu.findItem(R.id.action_bluetooth_status); - invokeSearchBluetoothDevice(); + // Just search for a bluetooth device just once at the start of the app + if (!firstAppStart) { + invokeSearchBluetoothDevice(); + firstAppStart = true; + } else { + // Set current bluetooth status icon while e.g. orientation changes + setBluetoothStatusIcon(bluetoothStatusIcon); + } return true; } @@ -150,7 +160,7 @@ public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_reconnection) + " " + deviceName, Toast.LENGTH_SHORT).show(); invokeSearchBluetoothDevice(); } else { - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_disabled)); + setBluetoothStatusIcon(R.drawable.bluetooth_disabled); Toast.makeText(getApplicationContext(), "Bluetooth " + getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show(); } return true; @@ -180,18 +190,18 @@ private void invokeSearchBluetoothDevice() { // Check if Bluetooth 4.x is available if (Integer.parseInt(deviceType) == BluetoothCommunication.BT_MI_SCALE) { if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_disabled)); + setBluetoothStatusIcon(R.drawable.bluetooth_disabled); Toast.makeText(getApplicationContext(), "Bluetooth 4.x " + getResources().getString(R.string.info_is_not_available), Toast.LENGTH_SHORT).show(); return; } } - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_searching)); + setBluetoothStatusIcon(R.drawable.bluetooth_searching); OpenScale.getInstance(getApplicationContext()).stopSearchingForBluetooth(); OpenScale.getInstance(getApplicationContext()).startSearchingForBluetooth(Integer.parseInt(deviceType), deviceName, callbackBtHandler); } else { - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_disabled)); + setBluetoothStatusIcon(R.drawable.bluetooth_disabled); } } @@ -201,7 +211,7 @@ public void handleMessage(Message msg) { switch (msg.what) { case BluetoothCommunication.BT_RETRIEVE_SCALE_DATA: - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_connection_success)); + setBluetoothStatusIcon(R.drawable.bluetooth_connection_success); ScaleData scaleBtData = (ScaleData) msg.obj; // if no user id is set, use the current user id @@ -213,22 +223,22 @@ public void handleMessage(Message msg) { OpenScale.getInstance(getApplicationContext()).updateScaleData(); break; case BluetoothCommunication.BT_CONNECTION_LOST: - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_connection_lost)); + setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_lost), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "Bluetooth connection lost"); break; case BluetoothCommunication.BT_NO_DEVICE_FOUND: - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_connection_lost)); + setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_no_device), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "No Bluetooth device found"); break; case BluetoothCommunication.BT_CONNECTION_ESTABLISHED: - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_connection_success)); + setBluetoothStatusIcon(R.drawable.bluetooth_connection_success); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_successful), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "Bluetooth connection successful established"); break; case BluetoothCommunication.BT_UNEXPECTED_ERROR: - bluetoothStatus.setIcon(getResources().getDrawable(R.drawable.bluetooth_connection_lost)); + setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_error) + ": " + msg.obj, Toast.LENGTH_SHORT).show(); Log.e("OpenScale", "Bluetooth unexpected error: " + msg.obj); break; @@ -236,6 +246,11 @@ public void handleMessage(Message msg) { } }; + private void setBluetoothStatusIcon(int iconRessource) { + bluetoothStatusIcon = iconRessource; + bluetoothStatus.setIcon(getResources().getDrawable(bluetoothStatusIcon)); + } + @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {