gt:         TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
context: /**
   * Copyright 2016 Adam Feinstein
   * <p/>
   * This file is part of Mini Scoreboard.
   * <p/>
   * Mini Scoreboard is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * <p/>
   * Mini Scoreboard is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * <p/>
   * You should have received a copy of the GNU General Public License
   * along with Mini Scoreboard.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.gelakinetic.miniscoreboard.activity;
  
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.support.annotation.MainThread;
  import android.support.annotation.StringRes;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.support.design.widget.TabLayout;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.preference.PreferenceManager;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.ProgressBar;
  
  import com.gelakinetic.miniscoreboard.R;
  import com.gelakinetic.miniscoreboard.database.DatabaseScoreEntry;
  import com.gelakinetic.miniscoreboard.fragment.DailyFragment;
  import com.gelakinetic.miniscoreboard.fragment.HistoryFragment;
  import com.gelakinetic.miniscoreboard.fragment.MiniScoreboardFragment;
  import com.gelakinetic.miniscoreboard.fragment.MiniScoreboardPreferenceFragment;
  import com.gelakinetic.miniscoreboard.fragment.StatsFragment;
  import com.gelakinetic.miniscoreboard.fragment.dialog.AboutDialogFragment;
  import com.gelakinetic.miniscoreboard.fragment.dialog.AllUsersDialogFragment;
  import com.gelakinetic.miniscoreboard.fragment.dialog.ScoreInputDialogFragment;
  import com.gelakinetic.miniscoreboard.fragment.dialog.UserNameInputDialogFragment;
  import com.gelakinetic.miniscoreboard.notification.MiniScoreboardAlarm;
  import com.gelakinetic.miniscoreboard.ui.ViewPagerAdapter;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseUser;
  import com.google.firebase.database.ChildEventListener;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  
  import static com.gelakinetic.miniscoreboard.database.DatabaseKeys.KEY_DAILY_SCORES;
  import static com.gelakinetic.miniscoreboard.database.DatabaseKeys.KEY_DAILY_WINNERS;
  import static com.gelakinetic.miniscoreboard.database.DatabaseKeys.KEY_PERSONAL_SCORES;
  import static com.gelakinetic.miniscoreboard.database.DatabaseKeys.KEY_USERS;
  
  public class MainActivity extends AppCompatActivity {
  
      /* Request code for intents */
      private static final int REQ_CODE_SETTINGS = 52394;
  
      /* Tags for fragments */
      public static final String DATE_PICKER_TAG = "DATE_PICKER_TAG";
      public static final String DIALOG_TAG = "DIALOG_TAG";
  
      /* The user data */
      private FirebaseUser mCurrentUser;
  
      /* UI Elements */
      private View mRootView;
      private FloatingActionButton mFab;
      private ViewPagerAdapter mViewPagerAdapter;
      private ViewPager mViewPager;
      private ProgressBar mProgressBar;
      private MenuItem mChangeUserMenuItem;
  
      private boolean mWasLaunchedFromNotification = false;
  
      /* Shared Preferences */
      private SharedPreferences mSharedPreferences;
  
      private final SharedPreferences.OnSharedPreferenceChangeListener mListener =
              new SharedPreferences.OnSharedPreferenceChangeListener() {
                  /**
                   * Called when a preference changes
                   * @param sharedPreferences A reference to the sharedPreferences
                   * @param prefKey The key for the preference which changed
                   */
                  @Override
                  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String prefKey) {
                      if (prefKey.equals(getString(R.string.pref_key_daily_notification))) {
                          if (mSharedPreferences.getBoolean(prefKey, false)) {
                              MiniScoreboardAlarm.setAlarm(MainActivity.this, false);
                          } else {
                              MiniScoreboardAlarm.cancelAlarm(MainActivity.this);
                          }
                      }
                  }
              };
  
      /* A hash map of user names, because there is no join */
      public final HashMap<String, String> mUsernameHashMap = new HashMap<>();
      private DatabaseReference mUsernameDatabaseReference;
      private final ChildEventListener mUsernameEventListener = new ChildEventListener() {
          /**
           * Called when a child is added to the database. The key for this value is the uid
           * @param dataSnapshot The child added, a String
           * @param s Unused
           */
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              /* This is called multiple times on init */
              mUsernameHashMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(String.class));
          }
  
          /**
           * Called when a child is changed in the database. The key for this value is the uid
           * @param dataSnapshot The child added, a String
           * @param s Unused
           */
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
              onChildRemoved(dataSnapshot);
              onChildAdded(dataSnapshot, s);
          }
  
          /**
           * Called when a child is removed from the database. The key for this value is the uid
           * @param dataSnapshot The child added, a String
           */
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
              mUsernameHashMap.remove(dataSnapshot.getKey());
          }
  
          /**
           * Called when a child is moved in the database. The data isn't ordered, so it doesn't matter
           * @param dataSnapshot The child added, a String
           * @param s Unused
           */
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
              /* The app doesn't care about username order */
          }
  
          /**
           * Called when the database operation is cancelled
           *
           * @param databaseError The database error that cancelled the operation
           */
          @Override
          public void onCancelled(DatabaseError databaseError) {
              /* TODO some error handling */
          }
      };
  
      /**
       * Create an Intento to launch a MainActivity
       *
       * @param context The Context to make the Intent with
       * @return An Intent to launch a MainActivity
       */
      @MainThread
      public static Intent createIntent(Context context) {
          Intent in = new Intent();
          in.setClass(context, MainActivity.class);
          return in;
      }
  
      /**
       * Called when this Activity is created. Connect to the database to get the usernames
       * and set up the UI
       *
       * @param savedInstanceState passed to super.onCreate()
       */
      @MainThread
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          /* Make sure the user is authenticated */
          mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
          if (mCurrentUser == null) {
              startActivity(AuthUiActivity.createIntent(this));
              finish();
          }
  
          mUsernameDatabaseReference = FirebaseDatabase.getInstance().getReference().child(KEY_USERS);
          /* Load the initial username hashmap */
          mUsernameDatabaseReference.addListenerForSingleValueEvent(
                  new ValueEventListener() {
                      /**
                       * Called when the requested data from the database is fetched.
                       * After the initial read, a listener will be set up for changes
                       *
                       * @param dataSnapshot The data from the database, a bunch of usernames with uid keys
                       */
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                              mUsernameHashMap.put(snapshot.getKey(), snapshot.getValue(String.class));
                          }
  
                          /* Check if a username exists for this user */
                          if (null == mUsernameHashMap.get(mCurrentUser.getUid())) {
                              /* If it doesn't, prompt them for a username */
                              UserNameInputDialogFragment newFragment = new UserNameInputDialogFragment();
                              newFragment.show(getSupportFragmentManager(), DIALOG_TAG);
                          }
  
                          /* Now that the usernames are loaded, add the listener for any changes */
                          mUsernameDatabaseReference.addChildEventListener(mUsernameEventListener);
  
                          /* And set up the view pager */
                          setupViewPager(mViewPager);
  
                          /* Hide the progress bar */
                          showIndeterminateProgressBar(false);
  
                          /* If the app was launched from the notification, now is the time to
                           * click the fab
                           */
                          if (mWasLaunchedFromNotification) {
                              mFab.callOnClick();
                              mWasLaunchedFromNotification = false;
                          }
                      }
  
                      /**
                       * Called when the database operation is cancelled
                       *
                       * @param databaseError The database error that cancelled the operation
                       */
                      @Override
                      public void onCancelled(DatabaseError databaseError) {
                          /* TODO some error handling */
                      }
                  });
          
          /* Set up the root view */
          setContentView(R.layout.activity_main);
          mRootView = findViewById(android.R.id.content);
  
          /* Set up the Toolbar */
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          if (null != getSupportActionBar()) {
              getSupportActionBar().setDisplayHomeAsUpEnabled(false);
          }
  
          /* Set up the FloatingActionButton */
          mFab = (FloatingActionButton) findViewById(R.id.fab);
          mFab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  ScoreInputDialogFragment newFragment = new ScoreInputDialogFragment();
                  newFragment.show(getSupportFragmentManager(), DIALOG_TAG);
              }
          });
          mFab.hide();
  
          /* Set up the ViewPager, don't load fragments until the username hashmap is received */
          mViewPager = (ViewPager) findViewById(R.id.viewpager);
          /* With three fragments, all will stay loaded */
          mViewPager.setOffscreenPageLimit(2);
  
          /* Set up the TabLayout */
  