Skip to content

Commit

Permalink
Change the approach for passing the subscription id to the cellular f…
Browse files Browse the repository at this point in the history
…ragment
  • Loading branch information
christianrowlands committed Dec 14, 2023
1 parent bbb7f29 commit 6b99c62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,22 @@ public static class CellularCollectionAdapter extends FragmentStateAdapter
@Override
public Fragment createFragment(int position)
{
NetworkDetailsFragment networkDetailsFragment = new NetworkDetailsFragment();
Bundle args = new Bundle();

// If there are no subscriptions, we might still be able to get survey results because
// of emergency call support. It could also be that READ_PHONE_STATE permissions were not granted.
if (subscriptions.isEmpty())
{
return new NetworkDetailsFragment(CellularController.DEFAULT_SUBSCRIPTION_ID);
args.putInt(NetworkDetailsFragment.SUBSCRIPTION_ID_KEY, CellularController.DEFAULT_SUBSCRIPTION_ID);
} else
{
int subscriptionId = subscriptions.get(position).getSubscriptionId();
args.putInt(NetworkDetailsFragment.SUBSCRIPTION_ID_KEY, subscriptionId);
}

int subscriptionId = subscriptions.get(position).getSubscriptionId();
return new NetworkDetailsFragment(subscriptionId);
networkDetailsFragment.setArguments(args);
return networkDetailsFragment;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
*/
public class NetworkDetailsFragment extends AServiceDataFragment implements ICellularSurveyRecordListener, LocationListener
{
static final String TITLE = "Details";
public static final String SUBSCRIPTION_ID_KEY = "subscription_id";

// The next two values have been added because certain devices don't follow the Interger#MAX_VALUE approach defined
// in the Android API. The phone is supposed to report Interger#MAX_VALUE to indicate "Unknown/Unset" values, but
Expand All @@ -80,7 +80,6 @@ public class NetworkDetailsFragment extends AServiceDataFragment implements ICel
// of right now to prevent invalid values from being reported.
private static final int RSCP_UNSET_VALUE_120 = -120;
private static final int RSCP_UNSET_VALUE_24 = -24;
public static final String SUBSCRIPTION_ID_KEY = "subscription_id";

private final DecimalFormat locationFormat = new DecimalFormat("###.#####");

Expand All @@ -89,37 +88,15 @@ public class NetworkDetailsFragment extends AServiceDataFragment implements ICel
private FragmentNetworkDetailsBinding binding;
private CellularViewModel viewModel;

/**
* Default constructor for the NetworkDetailsFragment. This constructor is used when the
* fragment is restored from a previous state. Use the other constructor for creating a new
* instance of this fragment.
*/
public NetworkDetailsFragment()
{
// -1 indicates that this fragment was restored, and therefore we need to read the
// subscriptionId from the view model. Otherwise, the subscriptionId is set in the constructor.
subscriptionId = -1;
}

public NetworkDetailsFragment(int subscriptionId)
{
this.subscriptionId = subscriptionId;
Timber.i("Creating a new NetworkDetailsFragment subscriptionId=%d", subscriptionId);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (subscriptionId == -1 && savedInstanceState != null)
{
int restoredId = savedInstanceState.getInt(SUBSCRIPTION_ID_KEY, -1);
if (restoredId != -1)
{
Timber.d("Restoring the subscriptionId from the savedInstanceState. subscriptionId=%d", restoredId);
subscriptionId = restoredId;
}
}

Bundle args = getArguments();
//noinspection DataFlowIssue
subscriptionId = args.getInt(SUBSCRIPTION_ID_KEY, -1);
Timber.d("Retrieving the subscriptionId from the arguments. subscriptionId=%d", subscriptionId);
}

@Nullable
Expand Down Expand Up @@ -152,13 +129,6 @@ public void onResume()
startAndBindToService();
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt(SUBSCRIPTION_ID_KEY, subscriptionId);
}

@Override
public void onDestroyView()
{
Expand Down

0 comments on commit 6b99c62

Please sign in to comment.