Example #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLocation = Utility.getPreferredLocation(this);
    Uri contentUri = getIntent() != null ? getIntent().getData() : null;
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar ab = getSupportActionBar();
    if (ab != null) ab.setDisplayShowTitleEnabled(false);

    if (findViewById(R.id.weather_detail_container) != null) {
      // The detail container view will be present only in the large-screen layouts
      // (res/layout-sw600dp). If this view is present, then the activity should be
      // in two-pane mode
      mTwoPane = true;
      // In two-pane mode, show the detail view in this activity by
      // adding or replacing the detail fragment using a
      // fragment transaction.
      if (savedInstanceState == null) {
        DetailActivityFragment fragment = new DetailActivityFragment();
        if (contentUri != null) {
          Bundle args = new Bundle();
          args.putParcelable(DetailActivityFragment.DETAIL_URI, contentUri);
          fragment.setArguments(args);
        }
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.weather_detail_container, fragment, DETAILFRAGMENT_TAG)
            .commit();
      }
    } else {
      mTwoPane = false;
      getSupportActionBar().setElevation(0f);
    }

    ForecastFragment forecastFragment =
        ((ForecastFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_forecast));
    forecastFragment.setUseTodayLayout(!mTwoPane);
    if (contentUri != null) {
      forecastFragment.setInitialSelectedDate(
          WeatherContract.WeatherEntry.getDateFromUri(contentUri));
    }

    SunshineSyncAdapter.initializeSyncAdapter(this);

    // If Google Play Services is up to date, we'll want to register GCM. If it is not, we'll
    // skip the registration and this device will not receive any downstream messages from
    // our fake server. Because weather alerts are not a core feature of the app, this should
    // not affect the behavior of the app, from a user perspective.
    if (checkPlayServices()) {
      // Because this is the initial creation of the app, we'll want to be certain we have
      // a token. If we do not, then we will start the IntentService that will register this
      // application with GCM.
      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false);
      if (!sentToken) {
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
      }
    }
  }