/**
  * Use this method to signal this Activity that it shall update its view.
  *
  * @param file : An {@link OCFile}
  */
 public void updateFileDetails(OCFile file, Account ocAccount) {
   mFile = file;
   if (ocAccount != null
       && (mStorageManager == null || (mAccount != null && !mAccount.equals(ocAccount)))) {
     mStorageManager =
         new FileDataStorageManager(
             ocAccount, getActivity().getApplicationContext().getContentResolver());
   }
   mAccount = ocAccount;
   updateFileDetails();
 }
Exemple #2
0
  /**
   * Sets and validates the ownCloud {@link Account} associated to the Activity.
   *
   * <p>If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
   *
   * <p>POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
   *
   * @param account New {@link Account} to set.
   * @param savedAccount When 'true', account was retrieved from a saved instance state.
   */
  protected void setAccount(Account account, boolean savedAccount) {
    Account oldAccount = mAccount;
    boolean validAccount =
        (account != null
            && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
    if (validAccount) {
      mAccount = account;
      mAccountWasSet = true;
      mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));

    } else {
      swapToDefaultAccount();
    }
  }
Exemple #3
0
  /**
   * Tries to swap the current ownCloud {@link Account} for other valid and existing.
   *
   * <p>If no valid ownCloud {@link Account} exists, the the user is requested to create a new
   * ownCloud {@link Account}.
   *
   * <p>POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
   */
  private void swapToDefaultAccount() {
    // default to the most recently used account
    Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
    if (newAccount == null) {
      /// no account available: force account creation
      createFirstAccount();
      mRedirectingToSetupAccount = true;
      mAccountWasSet = false;
      mAccountWasRestored = false;

    } else {
      mAccountWasSet = true;
      mAccountWasRestored = (newAccount.equals(mAccount));
      mAccount = newAccount;
    }
  }
  @SmallTest
  public void testConversionToExtras() {
    Account account1 = new Account("account1", "type1");
    Bundle b1 = new Bundle();
    b1.putParcelable("acc", account1);
    b1.putString("str", "String");

    SyncOperation op1 =
        new SyncOperation(
            account1, 0, 1, "foo", 0, SyncOperation.REASON_PERIODIC, "authority1", b1, false);

    PersistableBundle pb = op1.toJobInfoExtras();
    SyncOperation op2 = SyncOperation.maybeCreateFromJobExtras(pb);

    assertTrue("Account fields in extras not persisted.", account1.equals(op2.extras.get("acc")));
    assertTrue("Fields in extras not persisted", "String".equals(op2.extras.getString("str")));
  }
Exemple #5
0
  private String getAccounts() {
    String result = "<table>";
    List<SyncAdapterType> unknownAdapters = new ArrayList<SyncAdapterType>();

    result +=
        "<tr><th>Name</th><th>Type</th><th>Authority</th><th>Writable</th><th>Default</th></tr>\n";

    Account defaultContactsAccount = ToolsAccounts.identifyDefaultContactAccount(_context);

    // all online accounts defined by system & user.
    Account[] deviceAccounts = AccountManager.get(_context).getAccounts();

    List<Account> listAccounts = new ArrayList<Account>();
    for (Account account : deviceAccounts) {
      if (defaultContactsAccount == null || !account.equals(defaultContactsAccount)) {
        listAccounts.add(account);
      }
    }

    if (defaultContactsAccount != null) {
      listAccounts.add(0, defaultContactsAccount);
    }

    for (SyncAdapterType syncAdapterType : ContentResolver.getSyncAdapterTypes()) {
      unknownAdapters.add(syncAdapterType);
    }

    boolean bEven = true;
    // walk through all user online accounts, and keep contacts sync accounts
    for (Account account : listAccounts) {
      boolean bFound = false;

      // search SyncAdapter details
      for (SyncAdapterType syncAdapterType : ContentResolver.getSyncAdapterTypes()) {
        String defaultAuth = "";

        if (syncAdapterType.accountType.equals(account.type)) {
          unknownAdapters.remove(syncAdapterType);

          bFound = true;
          // match: this account has contact authority.
          boolean bWritable = syncAdapterType.supportsUploading();

          String rowClass = "account" + (bEven ? "Even" : "Odd");
          if (account.equals(defaultContactsAccount)
              && ContactsContract.AUTHORITY.equals(syncAdapterType.authority)) {
            rowClass = "accountDefault";
            defaultAuth = "contacts (syncadapter)";
          }
          result += "<tr class='" + rowClass + "'>";
          result +=
              "<td>"
                  + account.name
                  + "</td><td>"
                  + account.type
                  + "</td><td>"
                  + syncAdapterType.authority
                  + "</td><td>"
                  + bWritable
                  + "</td><td>"
                  + defaultAuth
                  + "</td></tr>\n";
        }
      }

      if (!bFound) {
        if (account.equals(defaultContactsAccount)) {
          result += "<tr class='accountDefault'>";
          result +=
              "<td>"
                  + account.name
                  + "</td><td>"
                  + account.type
                  + "</td><td>"
                  + ContactsContract.AUTHORITY
                  + "</td><td>true</td><td>contacts (no syncadapter)</td></tr>\n";
        } else {
          String rowClass = "account" + (bEven ? "Even" : "Odd");
          result += "<tr class='" + rowClass + "'>";
          result +=
              "<td>"
                  + account.name
                  + "</td><td>"
                  + account.type
                  + "</td><td>unknown</td><td>unknown</td><td></td></tr>\n";
        }
      }

      bEven = !bEven;
    }

    for (SyncAdapterType syncAdapterType : unknownAdapters) {
      String rowClass = "account" + (bEven ? "Even" : "Odd");
      result += "<tr class='" + rowClass + "'>";
      result +=
          "<td>(unknown)</td><td>"
              + syncAdapterType.accountType
              + "</td><td>"
              + syncAdapterType.authority
              + "</td><td>"
              + syncAdapterType.supportsUploading()
              + "</td><td></td></tr>\n";
    }

    result += "</table>";

    return result;
  }
Exemple #6
0
  /**
   * Core upload method: sends the file(s) to upload
   *
   * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
   */
  public void uploadFile(String uploadKey) {

    synchronized (mPendingUploads) {
      mCurrentUpload = mPendingUploads.get(uploadKey);
    }

    if (mCurrentUpload != null) {

      notifyUploadStart(mCurrentUpload);

      RemoteOperationResult uploadResult = null, grantResult = null;

      try {
        /// prepare client object to send requests to the ownCloud server
        if (mUploadClient == null || !mLastAccount.equals(mCurrentUpload.getAccount())) {
          mLastAccount = mCurrentUpload.getAccount();
          mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
          mUploadClient =
              OwnCloudClientFactory.createOwnCloudClient(mLastAccount, getApplicationContext());
        }

        /// check the existence of the parent folder for the file to upload
        String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
        remoteParentPath =
            remoteParentPath.endsWith(OCFile.PATH_SEPARATOR)
                ? remoteParentPath
                : remoteParentPath + OCFile.PATH_SEPARATOR;
        grantResult = grantFolderExistence(remoteParentPath);

        /// perform the upload
        if (grantResult.isSuccess()) {
          OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
          mCurrentUpload.getFile().setParentId(parent.getFileId());
          uploadResult = mCurrentUpload.execute(mUploadClient);
          if (uploadResult.isSuccess()) {
            saveUploadedFile();
          }
        } else {
          uploadResult = grantResult;
        }

      } catch (AccountsException e) {
        Log_OC.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
        uploadResult = new RemoteOperationResult(e);

      } catch (IOException e) {
        Log_OC.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
        uploadResult = new RemoteOperationResult(e);

      } finally {
        synchronized (mPendingUploads) {
          mPendingUploads.remove(uploadKey);
          Log_OC.i(TAG, "Remove CurrentUploadItem from pending upload Item Map.");
        }
        if (uploadResult.isException()) {
          // enforce the creation of a new client object for next uploads; this grant that a new
          // socket will
          // be created in the future if the current exception is due to an abrupt lose of network
          // connection
          mUploadClient = null;
        }
      }

      /// notify result

      notifyUploadResult(uploadResult, mCurrentUpload);
      sendFinalBroadcast(mCurrentUpload, uploadResult);
    }
  }