@Override
  public void onClick(View view) {
    // Close the navigation drawer, currently all click listeners are drawer items
    mDrawerLayout.closeDrawer(GravityCompat.START);

    if (view == mNavAccountItemLayout) {
      EvercamPlayApplication.sendEventAnalytics(
          this, R.string.category_menu, R.string.action_manage_account, R.string.label_account);

      startActivityForResult(
          new Intent(CamerasActivity.this, ManageAccountsActivity.class),
          Constants.REQUEST_CODE_MANAGE_ACCOUNT);
    } else if (view == mNavSettingsItemLayout) {
      EvercamPlayApplication.sendEventAnalytics(
          this, R.string.category_menu, R.string.action_settings, R.string.label_settings);

      startActivity(new Intent(CamerasActivity.this, CameraPrefsActivity.class));
    } else if (view == mNavFeedbackItemLayout) {
      startActivity(new Intent(CamerasActivity.this, FeedbackActivity.class));
    } else if (view == mNavAboutItemLayout) {
      Intent aboutIntent = new Intent(CamerasActivity.this, SimpleWebActivity.class);
      aboutIntent.putExtra(Constants.BUNDLE_KEY_URL, getString(R.string.evercam_url));
      startActivity(aboutIntent);
    }
  }
  boolean resizeCameras() {
    try {
      int screen_width = readScreenWidth(this);
      camerasPerRow = recalculateCameraPerRow();

      io.evercam.androidapp.custom.FlowLayout camsLineView =
          (io.evercam.androidapp.custom.FlowLayout) this.findViewById(R.id.cameras_flow_layout);
      for (int i = 0; i < camsLineView.getChildCount(); i++) {
        LinearLayout pview = (LinearLayout) camsLineView.getChildAt(i);
        CameraLayout cameraLayout = (CameraLayout) pview.getChildAt(0);

        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        params.width =
            ((i + 1 % camerasPerRow == 0)
                ? (screen_width - (i % camerasPerRow) * (screen_width / camerasPerRow))
                : screen_width / camerasPerRow);
        params.width = params.width - 1; // 1 pixels spacing between cameras
        params.height = (int) (params.width / (1.25));
        params.setMargins(1, 1, 0, 0); // 1 pixels spacing between cameras
        cameraLayout.setLayoutParams(params);
      }
      return true;
    } catch (Exception e) {
      Log.e(TAG, e.toString() + "::" + Log.getStackTraceString(e));

      sendToMint(e);

      EvercamPlayApplication.sendCaughtException(this, e);
      CustomedDialog.showUnexpectedErrorDialog(CamerasActivity.this);
    }
    return false;
  }
  private void updateCameraNames() {
    try {
      io.evercam.androidapp.custom.FlowLayout camsLineView =
          (io.evercam.androidapp.custom.FlowLayout) this.findViewById(R.id.cameras_flow_layout);
      for (int i = 0; i < camsLineView.getChildCount(); i++) {
        LinearLayout pview = (LinearLayout) camsLineView.getChildAt(i);
        CameraLayout cameraLayout = (CameraLayout) pview.getChildAt(0);

        cameraLayout.updateTitleIfDifferent();
      }
    } catch (Exception e) {
      Log.e(TAG, e.toString());
      EvercamPlayApplication.sendCaughtException(this, e);
    }
  }
  // Remove all the cameras so that all activities being performed can be
  // stopped
  public boolean removeAllCameraViews() {
    try {
      stopAllCameraViews();

      io.evercam.androidapp.custom.FlowLayout camsLineView =
          (io.evercam.androidapp.custom.FlowLayout) this.findViewById(R.id.cameras_flow_layout);
      camsLineView.removeAllViews();

      return true;
    } catch (Exception e) {
      Log.e(TAG, e.toString() + "::" + Log.getStackTraceString(e));

      sendToMint(e);

      EvercamPlayApplication.sendCaughtException(this, e);
      CustomedDialog.showUnexpectedErrorDialog(CamerasActivity.this);
    }
    return false;
  }
  // Tells that the item has been selected from the menu. Now check and get
  // the selected item and perform the relevant action
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == R.id.menurefresh) {
      EvercamPlayApplication.sendEventAnalytics(
          this, R.string.category_menu, R.string.action_refresh, R.string.label_list_refresh);

      if (refresh != null) refresh.setActionView(R.layout.actionbar_indeterminate_progress);

      startCameraLoadingTask();

    } else if (itemId == R.id.menu_logout) {
      showSignOutDialog();
    } else {
      return super.onOptionsItemSelected(item);
    }

    return true;
  }
  @Override
  public void onRestart() {
    super.onRestart();

    if (MainActivity.isUserLogged(this)) {
      // Reload camera list if default user has been changed, or offline settings has been changed
      if (isUserChanged() || isOfflineSettingChanged()) {
        new CamerasCheckInternetTask(CamerasActivity.this, InternetCheckType.START)
            .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
      } else {
        try {
          new CamerasCheckInternetTask(CamerasActivity.this, InternetCheckType.RESTART)
              .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

        } catch (RejectedExecutionException e) {
          EvercamPlayApplication.sendCaughtExceptionNotImportant(activity, e);
        }
      }
      usernameOnStop = "";
    } else {
      startActivity(new Intent(this, SlideActivity.class));
      finish();
    }
  }
  /**
   * Add all camera views to the main grid page
   *
   * @param reloadImages reload camera images or not
   * @param showThumbnails show thumbnails that returned by Evercam or not, if true and if thumbnail
   *     not available, it will request latest snapshot instead. If false, it will request neither
   *     thumbnail nor latest snapshot.
   */
  public boolean addAllCameraViews(final boolean reloadImages, final boolean showThumbnails) {
    try {
      // Recalculate camera per row
      camerasPerRow = recalculateCameraPerRow();

      io.evercam.androidapp.custom.FlowLayout camsLineView =
          (io.evercam.androidapp.custom.FlowLayout) this.findViewById(R.id.cameras_flow_layout);

      int screen_width = readScreenWidth(this);

      int index = 0;

      for (final EvercamCamera evercamCamera : AppData.evercamCameraList) {
        // Don't show offline camera
        if (!PrefsManager.showOfflineCameras(this) && !evercamCamera.isActive()) {
          continue;
        }

        final LinearLayout cameraListLayout = new LinearLayout(this);

        int indexPlus = index + 1;

        if (reloadImages) evercamCamera.loadingStatus = ImageLoadingStatus.not_started;

        final CameraLayout cameraLayout = new CameraLayout(this, evercamCamera, showThumbnails);

        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        params.width =
            ((indexPlus % camerasPerRow == 0)
                ? (screen_width - (index % camerasPerRow) * (screen_width / camerasPerRow))
                : screen_width / camerasPerRow);
        params.width = params.width - 1; // 1 pixels spacing between cameras
        params.height = (int) (params.width / (1.25));
        params.setMargins(0, 0, 0, 0); // No spacing between cameras
        cameraLayout.setLayoutParams(params);

        cameraListLayout.addView(cameraLayout);

        camsLineView.addView(
            cameraListLayout, new io.evercam.androidapp.custom.FlowLayout.LayoutParams(0, 0));

        index++;

        new Handler()
            .postDelayed(
                new Runnable() {
                  @Override
                  public void run() {

                    Rect cameraBounds = new Rect();
                    cameraListLayout.getHitRect(cameraBounds);

                    Rect offlineIconBounds = cameraLayout.getOfflineIconBounds();
                    int layoutWidth = cameraBounds.right - cameraBounds.left;
                    int offlineStartsAt = offlineIconBounds.left;
                    int offlineIconWidth = offlineIconBounds.right - offlineIconBounds.left;

                    if (layoutWidth > offlineStartsAt + offlineIconWidth * 2) {
                      cameraLayout.showOfflineIconAsFloat = false;
                    } else {
                      cameraLayout.showOfflineIconAsFloat = true;
                    }
                  }
                },
                200);
      }

      if (refresh != null) refresh.setActionView(null);

      return true;
    } catch (Exception e) {
      Log.e(TAG, e.toString(), e);

      sendToMint(e);

      EvercamPlayApplication.sendCaughtException(this, e);
      CustomedDialog.showUnexpectedErrorDialog(CamerasActivity.this);
    }
    return false;
  }