Skip to content

Commit

Permalink
[Mobile] Moved View initialisation code from onCreateView() to onResu…
Browse files Browse the repository at this point in the history
…me() to avoid NullPointerExceptions. Activity in LineFragment is now initialised in onResume() method as a variable. Added disabling of Firebase Performance collection when running in Firebase Test Lab.
  • Loading branch information
thecosmicfrog committed Dec 4, 2019
1 parent 34e0997 commit f4e8cb1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -39,7 +39,7 @@ plugins {
}

allprojects {
ext.versionCode = 154
ext.versionCode = 155

repositories {
google()
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -92,6 +93,7 @@ public class LineFragment extends Fragment {
private static StopNameIdMap mapStopNameId;
private static String localeDefault;

private FragmentActivity activity;
private Context context;
private View rootView = null;
private Menu menu;
Expand Down Expand Up @@ -206,28 +208,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
/* Instantiate a new StopNameIdMap. */
mapStopNameId = new StopNameIdMap(localeDefault);

if (isAdded()) {
isInitialised = initFragment();
}

/*
* If an Intent did not bring us to this Activity and there is a stop name saved in shared
* preferences, load that stop.
* This provides persistence to the app across shutdowns.
*/
if (!getActivity().getIntent().hasExtra(Constant.STOP_NAME)) {
if (Preferences.selectedStopName(context, Constant.NO_LINE) != null) {
String stopName = Preferences.selectedStopName(context, Constant.NO_LINE);

setTabAndSpinner(stopName);
}
}

imageViewBottomNavAlerts =
getActivity().findViewById(R.id.imageview_bottomnav_alerts);
textViewBottomNavAlerts =
getActivity().findViewById(R.id.textview_bottomnav_alerts);

return rootView;
}

Expand All @@ -245,6 +225,8 @@ public void onResume() {

super.onResume();

activity = getActivity();

/* Remove Favourites tutorial if it has been completed once already. */
if (line.equals(Constant.RED_LINE)
&& Preferences.hasRunOnce(context, Constant.TUTORIAL_FAVOURITES)) {
Expand All @@ -257,15 +239,35 @@ public void onResume() {
}

if (isAdded()) {
isInitialised = initFragment();

/*
* If an Intent did not bring us to this Activity and there is a stop name saved in
* shared preferences, load that stop.
* This provides persistence to the app across shutdowns.
*/
if (!activity.getIntent().hasExtra(Constant.STOP_NAME)) {
if (Preferences.selectedStopName(context, Constant.NO_LINE) != null) {
String stopName = Preferences.selectedStopName(context, Constant.NO_LINE);

setTabAndSpinner(stopName);
}
}

imageViewBottomNavAlerts =
activity.findViewById(R.id.imageview_bottomnav_alerts);
textViewBottomNavAlerts =
activity.findViewById(R.id.textview_bottomnav_alerts);

/*
* If a Favourite stop brought us to this Activity, load that stop's forecast.
* If a tapped notification brought us to this Activity, load the forecast for the stop
* sent with that Intent.
* If the previous cases are not matched, and the user has selected a default stop, load
* the forecast for that.
*/
if (getActivity().getIntent().hasExtra(Constant.STOP_NAME)) {
String stopName = getActivity().getIntent().getStringExtra(Constant.STOP_NAME);
if (activity.getIntent().hasExtra(Constant.STOP_NAME)) {
String stopName = activity.getIntent().getStringExtra(Constant.STOP_NAME);

/*
* Track whether or not the tab and spinner has been set. If it has, clear the Extra
Expand All @@ -274,28 +276,28 @@ public void onResume() {
boolean hasSetTabAndSpinner = setTabAndSpinner(stopName);

if (hasSetTabAndSpinner) {
getActivity().getIntent().removeExtra(Constant.STOP_NAME);
activity.getIntent().removeExtra(Constant.STOP_NAME);
}
} else if (getActivity().getIntent().hasExtra(Constant.NOTIFY_STOP_NAME)) {
} else if (activity.getIntent().hasExtra(Constant.NOTIFY_STOP_NAME)) {
/*
* Track whether or not the tab and spinner has been set. If it has, clear the Extra
* so it doesn't break the Default Stop setting.
*/
boolean hasSetTabAndSpinner =
setTabAndSpinner(
getActivity().getIntent().getStringExtra(Constant.NOTIFY_STOP_NAME)
activity.getIntent().getStringExtra(Constant.NOTIFY_STOP_NAME)
);

if (hasSetTabAndSpinner) {
getActivity().getIntent().removeExtra(Constant.NOTIFY_STOP_NAME);
activity.getIntent().removeExtra(Constant.NOTIFY_STOP_NAME);
}
} else if (getActivity().getIntent().hasExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)) {
} else if (activity.getIntent().hasExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)) {
activityRouter(
getActivity().getIntent().getStringExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)
activity.getIntent().getStringExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN)
);

/* Clear the Extra to avoid opening the same Activity on every start. */
getActivity().getIntent().removeExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN);
activity.getIntent().removeExtra(INTENT_EXTRA_ACTIVITY_TO_OPEN);
} else if (!Preferences.defaultStopName(context).equals(getString(R.string.none))
&& Preferences.defaultStopName(context) != null) {
setTabAndSpinner(Preferences.defaultStopName(context));
Expand Down Expand Up @@ -393,7 +395,7 @@ private void initFragmentVars() {
* Initialise Fragment and its views.
*/
private boolean initFragment() {
tabLayout = getActivity().findViewById(R.id.tablayout);
tabLayout = activity.findViewById(R.id.tablayout);

progressBar = rootView.findViewById(resProgressBar);

Expand Down Expand Up @@ -693,7 +695,7 @@ private void setIsLoading(final boolean loading) {
* Only run if Fragment is attached to Activity. Without this check, the app is liable
* to crash when the screen is rotated many times in a given period of time.
*/
getActivity().runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (loading) {
Expand Down Expand Up @@ -791,13 +793,13 @@ public void autoReloadStopForecast(int delayTimeMillis) {
public void run() {
/* Check Fragment is attached to Activity to avoid NullPointerExceptions. */
if (isAdded()) {
getActivity().runOnUiThread(new Runnable() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (shouldAutoReload) {
loadStopForecast(
Preferences.selectedStopName(
getActivity().getApplicationContext(),
activity.getApplicationContext(),
line
),
false
Expand Down Expand Up @@ -859,7 +861,7 @@ public void success(ApiTimes apiTimes, Response response) {

if (apiCreatedTime != null) {
StopForecastUtil.showSnackbar(
getActivity(),
activity,
"Times updated at " + apiCreatedTime
);
}
Expand Down
Expand Up @@ -39,6 +39,7 @@
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.firebase.perf.FirebasePerformance;

import org.thecosmicfrog.luasataglance.R;
import org.thecosmicfrog.luasataglance.util.Analytics;
Expand All @@ -54,6 +55,8 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

configureFirebasePerformanceCollection();

setContentView(R.layout.activity_main);

getScreenHeight();
Expand Down Expand Up @@ -328,15 +331,29 @@ private void setTabIndicatorColor(TabLayout tabLayout) {
}
}

/**
* Check whether or not we are running in Firebase Test Lab.
* @return Whether or not we are running in Firebase Test Lab.
*/
private boolean isRunningInFirebaseTestLab() {
String settingFirebaseTestLab =
Settings.System.getString(getContentResolver(), "firebase.test.lab");

Log.i(LOG_TAG, "Running in Firebase Test Lab.");

return settingFirebaseTestLab != null && settingFirebaseTestLab.equals("true");
}

/**
* Show What's New dialog to user if they have recently updated the app.
*/
private void showWhatsNewDialog() {
/* Don't show the What's New dialog if we're running in Firebase Test Lab. */
String settingFirebaseTestLab =
Settings.System.getString(getContentResolver(), "firebase.test.lab");
if (settingFirebaseTestLab != null && settingFirebaseTestLab.equals("true")) {
Log.i(LOG_TAG, "Running in Firebase Test Lab.");
if (isRunningInFirebaseTestLab()) {
Log.i(
LOG_TAG,
"Running in Firebase Test Lab. Not showing What's New dialog."
);

return;
}
Expand Down Expand Up @@ -392,4 +409,20 @@ private void getScreenHeight() {

Preferences.saveScreenHeight(getApplicationContext(), dpHeight);
}

/**
* Enable or disable Firebase Performance collection.
*/
private void configureFirebasePerformanceCollection() {
/* Disable Firebase Performance collection if we're running in Firebase Test Lab. */
if (isRunningInFirebaseTestLab()) {
Log.i(
LOG_TAG,
"Running in Firebase Test Lab. Disabling Firebase Performance collection."
);

FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false);
}
}
}

0 comments on commit f4e8cb1

Please sign in to comment.