@Override
 public void onActivityCreated(Bundle savedInstanceState) {
   alfSession = SessionUtils.getSession(getActivity());
   SessionUtils.checkSession(getActivity(), alfSession);
   super.onActivityCreated(savedInstanceState);
   setRetainInstance(true);
 }
  private void refresh() {
    if (accountCursor == null) {
      return;
    }
    if (accountCursor.isClosed()) {
      return;
    }

    Account currentAccount = SessionUtils.getAccount(getActivity());
    if (currentAccount == null) {
      currentAccount = AccountsPreferences.getDefaultAccount(getActivity());
    }

    if (currentAccount == null) {
      return;
    }

    for (int i = 0; i < accountCursor.getCount(); i++) {
      accountCursor.moveToPosition(i);
      if (accountCursor.getLong(AccountSchema.COLUMN_ID_ID) == currentAccount.getId()) {
        accountIndex = accountCursor.getPosition();
        break;
      }
    }

    spinnerAccount.setSelection(accountIndex);
  }
  public boolean onItemLongClick(GridView l, View v, int position, long id) {
    // We disable long click during import mode.
    if (mode == MODE_IMPORT || mode == MODE_PICK) {
      return false;
    }

    Node n = (Node) l.getItemAtPosition(position);
    boolean b = true;
    if (n instanceof NodePlaceHolder) {
      getActivity()
          .startActivity(
              new Intent(IntentIntegrator.ACTION_DISPLAY_OPERATIONS)
                  .putExtra(
                      IntentIntegrator.EXTRA_ACCOUNT_ID,
                      SessionUtils.getAccount(getActivity()).getId()));
      b = false;
    } else {
      l.setItemChecked(position, true);
      b = startSelection(n);
      if (DisplayUtils.hasCentralPane(getActivity())) {
        FragmentDisplayer.removeFragment(
            getActivity(), DisplayUtils.getCentralFragmentId(getActivity()));
        FragmentDisplayer.removeFragment(getActivity(), android.R.id.tabcontent);
      }
    }
    return b;
  };
  // ///////////////////////////////////////////////////////////////////////////
  // LIFECYCLE
  // ///////////////////////////////////////////////////////////////////////////
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.app_main_menu, container, false);

    spinnerAccount = (Spinner) rootView.findViewById(R.id.accounts_spinner);
    spinnerAccount.setOnItemSelectedListener(this);

    menuFavorites = (Button) rootView.findViewById(R.id.menu_favorites);

    if (SLIDING_TAG.equals(getTag())) {
      menuSlidingFavorites = (Button) rootView.findViewById(R.id.menu_favorites);
    }

    configurationManager = ApplicationManager.getInstance(getActivity()).getConfigurationManager();
    if (configurationManager != null
        && configurationManager.getConfigurationState()
            == ConfigurationManager.STATE_HAS_CONFIGURATION) {
      configure(configurationManager.getConfig(SessionUtils.getAccount(getActivity())));
    } else {
      display();
    }

    return rootView;
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {

    setRetainInstance(true);
    alfSession = SessionUtils.getSession(getActivity());
    if (RepositoryVersionHelper.isAlfrescoProduct(alfSession)) {
      setActivateThumbnail(true);
    }
    super.onActivityCreated(savedInstanceState);
  }
  public void refresh() {
    if (!ConnectivityUtils.hasNetwork((BaseActivity) getActivity())) {
      return;
    }

    if (parentFolder == null) {
      parentFolder = SessionUtils.getSession(getActivity()).getRootFolder();
    }
    super.refresh();
  }
 @Override
 public void onListItemClick(ListView l, View v, int position, long id) {
   super.onListItemClick(l, v, position, id);
   CloudNetwork network = (CloudNetwork) l.getItemAtPosition(position);
   Account currentAccount = SessionUtils.getAccount(getActivity());
   if (currentAccount != null
       && !currentAccount.getRepositoryId().equals(network.getIdentifier())) {
     ActionManager.reloadAccount(getActivity(), currentAccount, network.getIdentifier());
   }
 }
  // ///////////////////////////////////////////////////////////////////////////
  // LIFECYCLE
  // ///////////////////////////////////////////////////////////////////////////
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    acc = SessionUtils.getAccount(getActivity());

    int[] layouts = BaseCursorGridAdapterHelper.getGridLayoutId(getActivity(), this);
    adapter = new FavoriteCursorAdapter(this, null, layouts[0], selectedItems, getMode());
    gv.setAdapter(adapter);
    setListShown(false);
    getLoaderManager().initLoader(0, null, this);
  }
  // //////////////////////////////////////////////////////////////////////
  // LIFE CYCLE
  // //////////////////////////////////////////////////////////////////////
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    setRetainInstance(true);
    alfSession = SessionUtils.getSession(getActivity());
    SessionUtils.checkSession(getActivity(), alfSession);
    if (alfSession == null) {

    } else if (RepositoryVersionHelper.isAlfrescoProduct(alfSession)) {
      setActivateThumbnail(true);
    }

    // In case of Import mode, we disable thumbnails.
    if (getActivity() instanceof PublicDispatcherActivity) {
      mode = MODE_IMPORT;
      setActivateThumbnail(false);
    } else if (getActivity() instanceof PrivateDialogActivity) {
      mode = MODE_PICK;
      fragmentPick = ((PrivateDialogActivity) getActivity()).getOnPickDocumentFragment();
    }

    super.onActivityCreated(savedInstanceState);
  }
 public boolean hasParentFavorite() {
   Cursor parentCursorId = null;
   isSyncFolder = false;
   try {
     parentCursorId =
         SynchroManager.getCursorForId(
             context, SessionUtils.getAccount(getContext()), parentNode.getIdentifier());
     if (parentCursorId.getCount() == 1 && parentCursorId.moveToFirst()) {
       isSyncFolder = true;
     }
   } catch (Exception e) {
     // do nothing
   } finally {
     CursorUtils.closeCursor(parentCursorId);
   }
   return isSyncFolder;
 }
  private void display() {
    Account acc = SessionUtils.getAccount(getActivity());

    rootView.findViewById(R.id.menu_browse_activities).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_browse_root).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_browse_my_sites).setVisibility(View.VISIBLE);
    if (acc != null && acc.getTypeId() == Account.TYPE_ALFRESCO_CLOUD) {
      rootView.findViewById(R.id.menu_workflow).setVisibility(View.GONE);
    } else {
      rootView.findViewById(R.id.menu_workflow).setVisibility(View.VISIBLE);
    }
    rootView.findViewById(R.id.menu_favorites).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_search).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_downloads).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_notifications).setVisibility(View.VISIBLE);
    rootView.findViewById(R.id.menu_browse_shared).setVisibility(View.GONE);
    rootView.findViewById(R.id.menu_browse_userhome).setVisibility(View.GONE);
  }
  public void displayFavoriteStatut() {
    Cursor statutCursor = null;
    Drawable icon = getActivity().getResources().getDrawable(R.drawable.ic_favorite);
    Drawable statut = null;

    try {
      Account acc = SessionUtils.getAccount(getActivity());
      Boolean hasSynchroActive = GeneralPreferences.hasActivateSync(getActivity(), acc);

      if (hasSynchroActive && acc != null) {
        statutCursor =
            getActivity()
                .getContentResolver()
                .query(
                    SynchroProvider.CONTENT_URI,
                    SynchroSchema.COLUMN_ALL,
                    SynchroProvider.getAccountFilter(acc)
                        + " AND "
                        + SynchroSchema.COLUMN_STATUS
                        + " == "
                        + SyncOperation.STATUS_REQUEST_USER,
                    null,
                    null);
        if (statutCursor.getCount() > 0) {
          statut = getActivity().getResources().getDrawable(R.drawable.ic_warning_light);
        }
        statutCursor.close();

        if (menuSlidingFavorites != null) {
          menuSlidingFavorites.setCompoundDrawablesWithIntrinsicBounds(icon, null, statut, null);
        }
        menuFavorites.setCompoundDrawablesWithIntrinsicBounds(icon, null, statut, null);
      }
    } catch (Exception e) {

    } finally {
      if (statutCursor != null) {
        statutCursor.close();
      }
    }
  }
  public void createFiles(List<File> files) {
    if (files.size() == 1) {
      createFile(files.get(0));
      return;
    } else {
      OperationsRequestGroup group =
          new OperationsRequestGroup(getActivity(), SessionUtils.getAccount(getActivity()));
      for (File file : files) {
        group.enqueue(
            new CreateDocumentRequest(
                importFolder.getIdentifier(), file.getName(), new ContentFileProgressImpl(file)));
      }
      BatchOperationManager.getInstance(getActivity()).enqueue(group);

      if (getActivity() instanceof PublicDispatcherActivity) {
        getActivity().finish();
      }
    }
    tmpFile = null;
    createFile = null;
  }
  @Override
  public void onResume() {
    super.onResume();
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
    getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

    IntentFilter intentFilter = new IntentFilter(IntentIntegrator.ACTION_SYNCHRO_COMPLETED);
    intentFilter.addAction(IntentIntegrator.ACTION_CONFIGURATION_MENU);
    receiver = new MainMenuReceiver();
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);

    displayFavoriteStatut();

    if (configurationManager != null
        && configurationManager.getConfigurationState()
            == ConfigurationManager.STATE_HAS_CONFIGURATION) {
      configure(configurationManager.getConfig(SessionUtils.getAccount(getActivity())));
    } else {
      display();
    }
  }
  // ///////////////////////////////////////////////////////////////////////////
  // DROPDOWN EVENTS
  // ///////////////////////////////////////////////////////////////////////////
  @Override
  public void onItemSelected(
      AdapterView<?> parentView, View selectedItemView, int position, long id) {
    Cursor cursor = (Cursor) parentView.getItemAtPosition(position);
    int accountId = cursor.getInt(AccountSchema.COLUMN_ID_ID);

    switch (accountId) {
      case AccountCursorAdapter.NETWORK_ITEM:
        ((MainActivity) getActivity()).displayNetworks();
        hideSlidingMenu(false);
        break;
      case AccountCursorAdapter.MANAGE_ITEM:
        ((MainActivity) getActivity()).displayAccounts();
        hideSlidingMenu(false);
        break;

      default:
        Account currentAccount = SessionUtils.getAccount(getActivity());
        if (currentAccount != null
            && cursor.getCount() > 1
            && currentAccount.getId() != cursor.getLong(AccountSchema.COLUMN_ID_ID)) {
          hideSlidingMenu(true);

          // Request session loading for the selected account.
          LocalBroadcastManager.getInstance(getActivity())
              .sendBroadcast(
                  new Intent(IntentIntegrator.ACTION_LOAD_ACCOUNT)
                      .putExtra(
                          IntentIntegrator.EXTRA_ACCOUNT_ID,
                          cursor.getLong(AccountSchema.COLUMN_ID_ID)));

          // Update dropdown menu (eventual new items to display)
          cursorAdapter.swapCursor(
              AccountCursorAdapter.createMergeCursor(getActivity(), accountCursor));
        }
        break;
    }
  }
  public void getMenu(Menu menu, Node node) {
    MenuItem mi;

    Permissions permission =
        SessionUtils.getSession(context)
            .getServiceRegistry()
            .getDocumentFolderService()
            .getPermissions(node);

    mi =
        menu.add(
            Menu.NONE,
            MenuActionItem.MENU_DETAILS,
            Menu.FIRST + MenuActionItem.MENU_DETAILS,
            R.string.action_view_properties);
    mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

    if (permission.canEdit()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_EDIT,
              Menu.FIRST + MenuActionItem.MENU_EDIT,
              R.string.action_edit_properties);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }

    if (permission.canDelete()) {
      mi =
          menu.add(
              Menu.NONE,
              MenuActionItem.MENU_DELETE_FOLDER,
              Menu.FIRST + MenuActionItem.MENU_DELETE_FOLDER,
              R.string.delete);
      mi.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }
  }
  @Override
  protected void updateBottomText(ProgressViewHolder vh, Node item) {
    if (hasFavorite && favoriteInfos.containsKey(item.getIdentifier())) {
      FavoriteInfo favoriteInfo = favoriteInfos.get(item.getIdentifier());
      if (favoriteInfo.isFavorite) {
        vh.favoriteIcon.setVisibility(View.VISIBLE);
        vh.favoriteIcon.setImageResource(R.drawable.ic_favorite_dark);
      } else {
        vh.favoriteIcon.setVisibility(View.GONE);
      }

      if (SynchroManager.getInstance(getContext())
          .hasActivateSync(SessionUtils.getAccount(getContext()))) {
        switch (favoriteInfo.status) {
          case SyncOperation.STATUS_PENDING:
            displayStatut(vh, R.drawable.sync_status_pending);
            break;
          case SyncOperation.STATUS_RUNNING:
            displayStatut(vh, R.drawable.sync_status_loading);
            break;
          case SyncOperation.STATUS_PAUSED:
            displayStatut(vh, R.drawable.sync_status_pending);
            break;
          case SyncOperation.STATUS_MODIFIED:
            displayStatut(vh, R.drawable.sync_status_pending);
            break;
          case SyncOperation.STATUS_SUCCESSFUL:
            displayStatut(vh, R.drawable.sync_status_success);
            break;
          case SyncOperation.STATUS_FAILED:
            displayStatut(vh, R.drawable.sync_status_failed);
            break;
          case SyncOperation.STATUS_CANCEL:
            displayStatut(vh, R.drawable.sync_status_failed);
            break;
          case SyncOperation.STATUS_REQUEST_USER:
            displayStatut(vh, R.drawable.sync_status_failed);
            break;
          default:
            vh.favoriteIcon.setVisibility(View.GONE);
            vh.iconBottomRight.setVisibility(View.GONE);
            break;
        }
      } else {
        vh.iconBottomRight.setVisibility(View.GONE);
      }
    } else {
      vh.favoriteIcon.setVisibility(View.GONE);
      vh.iconBottomRight.setVisibility(View.GONE);
    }

    if (item instanceof NodePlaceHolder) {
      vh.bottomText.setEnabled(false);
      int status = (Integer) item.getPropertyValue(PublicAPIPropertyIds.REQUEST_STATUS);
      if (status == Operation.STATUS_PAUSED || status == Operation.STATUS_PENDING) {
        vh.bottomText.setVisibility(View.VISIBLE);
        int resId = R.string.download_await;
        switch ((Integer) item.getPropertyValue(PublicAPIPropertyIds.REQUEST_TYPE)) {
          case DownloadRequest.TYPE_ID:
            resId = R.string.download_await;
            break;
          case CreateDocumentRequest.TYPE_ID:
          case UpdateContentRequest.TYPE_ID:
            resId = R.string.upload_await;
            break;

          default:
            break;
        }
        vh.bottomText.setText(resId);
      } else {
        vh.bottomText.setVisibility(View.GONE);
      }
    } else {
      vh.bottomText.setVisibility(View.VISIBLE);
      super.updateBottomText(vh, item);
    }
  }
 @Override
 public Loader<LoaderResult<List<CloudNetwork>>> onCreateLoader(int id, Bundle args) {
   setListShown(false);
   return new CloudNetworksLoader(
       getActivity(), (CloudSession) SessionUtils.getSession(getActivity()));
 }
 @Override
 public void onStart() {
   acc = SessionUtils.getAccount(getActivity());
   getActivity().invalidateOptionsMenu();
   super.onStart();
 }
 public void refresh() {
   if (parentFolder == null) {
     parentFolder = SessionUtils.getSession(getActivity()).getRootFolder();
   }
   super.refresh();
 }