Skip to content

Commit

Permalink
Merge pull request #172 from NordicSemiconductor/bugfix
Browse files Browse the repository at this point in the history
Activity restoration bugs fixed
  • Loading branch information
philips77 authored Aug 7, 2024
2 parents 44fb0cc + ca73a24 commit e1a1a1f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
11 changes: 10 additions & 1 deletion sample/src/main/java/io/runtime/mcumgr/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;
import io.runtime.mcumgr.sample.application.Dagger2Application;
import io.runtime.mcumgr.sample.di.Injectable;
import io.runtime.mcumgr.sample.fragment.DeviceFragment;
import io.runtime.mcumgr.sample.fragment.FilesFragment;
Expand All @@ -51,6 +52,8 @@ public class MainActivity extends AppCompatActivity
@Inject
McuMgrViewModelFactory viewModelFactory;
@Inject
BluetoothDevice device;
@Inject
Uri logSessionUri;

private Fragment deviceFragment;
Expand All @@ -66,6 +69,13 @@ public AndroidInjector<Object> androidInjector() {

@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
final BluetoothDevice device = getIntent().getParcelableExtra(EXTRA_DEVICE);
// The target must be set before calling super.onCreate(Bundle).
// Otherwise, Dagger2 will fail to inflate this Activity.
// The target has to be set here, otherwise restoring the state will fail had the
// Activity been destroyed and recreated.
((Dagger2Application) getApplication()).setTarget(device);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Expand All @@ -76,7 +86,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
);
}

final BluetoothDevice device = getIntent().getParcelableExtra(EXTRA_DEVICE);
String deviceName = getString(R.string.unknown_device);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
final String name = device.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import javax.inject.Inject;

import io.runtime.mcumgr.sample.adapter.DevicesAdapter;
import io.runtime.mcumgr.sample.application.Dagger2Application;
import io.runtime.mcumgr.sample.databinding.ActivityScannerBinding;
import io.runtime.mcumgr.sample.di.Injectable;
import io.runtime.mcumgr.sample.utils.Utils;
Expand Down Expand Up @@ -225,10 +224,6 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {

@Override
public void onItemClick(@NonNull final BluetoothDevice device) {
// The target must be set before calling super.onCreate(Bundle).
// Otherwise, Dagger2 will fail to inflate this Activity.
((Dagger2Application) getApplication()).setTarget(device);

final Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_DEVICE, device);
startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ private String getPhyAsString() {
}

private static String getPhyAsString(@PhyValue final int phy) {
switch (phy) {
case PhyCallback.PHY_LE_CODED: return "LE Coded";
case PhyCallback.PHY_LE_2M: return "LE 2M";
default:
case PhyCallback.PHY_LE_1M: return "LE 1M";
}
return switch (phy) {
case PhyCallback.PHY_LE_CODED -> "LE Coded";
case PhyCallback.PHY_LE_2M -> "LE 2M";
case PhyCallback.PHY_LE_1M -> "LE 1M";
default -> "Unknown (" + phy + ")";
};
}

@Override
Expand Down Expand Up @@ -319,7 +319,6 @@ private void recalculateMetadata() {

static class SavedState extends BaseSavedState {
private float maxThroughput;
private float[] instantaneousThroughputData;
private float[] averageThroughputData;
private float[] connectionIntervalData;
private float currentConnectionInterval;
Expand All @@ -338,7 +337,6 @@ static class SavedState extends BaseSavedState {
SavedState(Parcel in) {
super(in);
maxThroughput = in.readFloat();
instantaneousThroughputData = in.createFloatArray();
averageThroughputData = in.createFloatArray();
connectionIntervalData = in.createFloatArray();
currentConnectionInterval = in.readFloat();
Expand All @@ -353,8 +351,8 @@ static class SavedState extends BaseSavedState {

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeFloat(maxThroughput);
dest.writeFloatArray(instantaneousThroughputData);
dest.writeFloatArray(averageThroughputData);
dest.writeFloatArray(connectionIntervalData);
dest.writeFloat(currentConnectionInterval);
Expand Down

0 comments on commit e1a1a1f

Please sign in to comment.