Esempio n. 1
0
  @Override
  protected void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    setContentView(R.layout.activity_main);
    mLogsUI = (TextView) findViewById(R.id.logs);
    // Broadcast receiver to handle common action such as refresh the UI or open/hide the match log
    mLoggerCallback =
        new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
              case Constants.ACTION_REFRESH_UI:
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                for (Fragment fragment : fragments) {
                  if (fragment instanceof RefreshableFragment && fragment.isVisible()) {
                    ((RefreshableFragment) fragment).refresh();
                  }
                }
                break;
              case Constants.ACTION_SHOW_LOG:
                String aux = intent.getStringExtra(SportEventHandler.EXTRA_COMMENT);
                if (aux == null) {
                  aux = "";
                }
                mLogsUI.setText(aux);
                break;
            }
          }
        };

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerView = (FrameLayout) findViewById(R.id.navigation_drawer);
    mDrawerMenu = (ListView) findViewById(R.id.navigation_drawer_menu);
    mDrawerScrim = findViewById(R.id.navigation_drawer_scrim);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    TypedArray colorPrimaryDark =
        getTheme().obtainStyledAttributes(new int[] {R.attr.colorPrimaryDark});
    mDrawerLayout.setStatusBarBackgroundColor(colorPrimaryDark.getColor(0, 0xFF000000));
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    colorPrimaryDark.recycle();

    ImageView drawerHeader = new ImageView(this);
    drawerHeader.setImageResource(R.drawable.drawer_gcm_logo);
    mDrawerMenu.addHeaderView(drawerHeader);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      // Set the drawer width accordingly with the guidelines: window_width - toolbar_height.
      toolbar.addOnLayoutChangeListener(
          new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(
                View view,
                int left,
                int top,
                int right,
                int bottom,
                int oldLeft,
                int oldTop,
                int oldRight,
                int oldBottom) {
              if (left == 0 && top == 0 && right == 0 && bottom == 0) {
                return;
              }
              DisplayMetrics metrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(metrics);
              float logicalDensity = metrics.density;
              int maxWidth = (int) Math.ceil(320 * logicalDensity);
              DrawerLayout.LayoutParams params =
                  (DrawerLayout.LayoutParams) mDrawerView.getLayoutParams();
              int newWidth = view.getWidth() - view.getHeight();
              params.width = (newWidth > maxWidth ? maxWidth : newWidth);
              mDrawerView.setLayoutParams(params);
            }
          });
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
      mDrawerView.setOnApplyWindowInsetsListener(
          new View.OnApplyWindowInsetsListener() {
            @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
            @Override
            public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
              // Set scrim height to match status bar height.
              mDrawerScrim.setLayoutParams(
                  new FrameLayout.LayoutParams(
                      FrameLayout.LayoutParams.MATCH_PARENT, insets.getSystemWindowInsetTop()));
              return insets;
            }
          });
    }

    int activeItemIndicator =
        (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            ? android.R.layout.simple_list_item_activated_1
            : android.R.layout.simple_list_item_checked;

    mMainMenu = new MainMenu(this);
    mDrawerMenu.setOnItemClickListener(this);
    mDrawerMenu.setAdapter(
        new ArrayAdapter<>(
            getSupportActionBar().getThemedContext(),
            activeItemIndicator,
            android.R.id.text1,
            mMainMenu.getEntries()));

    mDrawerToggle =
        new ActionBarDrawerToggle(
            this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
          @Override
          public void onDrawerOpened(View drawerView) {
            // The user learned how to open the drawer. Do not open it for him anymore.
            getAppPreferences().edit().putBoolean(PREF_OPEN_DRAWER_AT_STARTUP, false).apply();
            super.onDrawerOpened(drawerView);
          }
        };

    boolean activityResumed = (savedState != null);
    boolean openDrawer = getAppPreferences().getBoolean(PREF_OPEN_DRAWER_AT_STARTUP, true);
    int lastScreenId = getAppPreferences().getInt(PREF_LAST_SCREEN_ID, 0);
    selectItem(lastScreenId);
    if (!activityResumed && openDrawer) {
      mDrawerLayout.openDrawer(mDrawerView);
    }
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    /*
     * Here we check if the Activity was created by the user clicking on one of our GCM
     * notifications:
     * 1. Check if the action of the intent used to launch the Activity.
     * 2. Print out any additional data sent with the notification. This is included as extras
     *  on the intent.
     */
    Intent launchIntent = getIntent();
    if (Constants.NOTIFICATION_OPEN_ACTION.equals(launchIntent.getAction())) {
      Bundle data = launchIntent.getExtras();
      // data.isEmpty(); // Force the bundle to unparcel so that toString() works
      String Local = data.getString(SportEventHandler.EXTRA_LOCAL);
      String Away = data.getString(SportEventHandler.EXTRA_AWAY);
      String LocalScore = data.getString(SportEventHandler.EXTRA_LOCAL_SCORE);
      String AwayScore = data.getString(SportEventHandler.EXTRA_AWAY_SCORE);
      String Status = data.getString(SportEventHandler.EXTRA_STATUS);
      String comment = data.getString(SportEventHandler.EXTRA_COMMENT);
      String matchId = data.getString(SportEventHandler.EXTRA_MATCHID);
      SportEventHandler.startActionScore(
          this, matchId, Local, Away, LocalScore, AwayScore, Status, comment);
      // String format = getResources().getString(R.string.notification_intent_received);
      // mLogger.log(Log.INFO, String.format(format, data));
    }

    // Check if Google Play Services APK
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
    checkPlayServices();
  }