예제 #1
0
  private void setupAccounts(
      final Bundle savedInstanceState, final Collection<Account> accounts, final Config conf) {
    String accountId = null;
    Account account = null;

    if (savedInstanceState != null) accountId = savedInstanceState.getString(ARG_ACCOUNT_ID);
    if (accountId == null) accountId = this.intentExtras.getString(ARG_ACCOUNT_ID);
    if (accountId != null) {
      account = conf.getAccount(accountId);
      final List<String> svcs = this.intentExtras.getStringArrayList(ARG_SVCS);
      LOG.i("accountId=%s svcs=%s", account.getId(), svcs);

      this.enabledPostToAccounts.setAccount(account);
      this.enabledPostToAccounts.fromBundle(savedInstanceState);
      if (svcs != null && !this.enabledPostToAccounts.isServicesPreSpecified()) {
        for (String svc : svcs) {
          this.enabledPostToAccounts.enable(ServiceRef.parseServiceMeta(svc));
        }
        this.enabledPostToAccounts.setServicesPreSpecified(true);
      }
    } else {
      this.askAccountOnActivate = true;
    }

    this.spnAccount = (Spinner) findViewById(R.id.spnAccount);
    this.accountAdaptor = new AccountAdaptor(getBaseContext(), accounts);
    this.spnAccount.setAdapter(this.accountAdaptor);
    setSelectedAccount(account);
    this.spnAccount.setOnItemSelectedListener(this.accountOnItemSelectedListener);

    this.llSubAccounts = (ViewGroup) findViewById(R.id.llSubAccounts);
  }
예제 #2
0
 protected void accountSelected(final Account account) {
   switch (account.getProvider()) {
     case SUCCESSWHALE:
     case BUFFER:
       this.enabledPostToAccounts.setAccount(account);
       new PostToAccountLoaderTask(
               getApplicationContext(), this.llSubAccounts, this.enabledPostToAccounts)
           .executeOnExecutor(this.exec, account);
       break;
     default:
       this.llSubAccounts.setVisibility(View.GONE);
   }
 }
예제 #3
0
  protected void askPost() {
    final Account account = getSelectedAccount();
    final Set<ServiceRef> svcs = this.enabledPostToAccounts.copyOfServices();
    final AlertDialog.Builder dlgBld = new AlertDialog.Builder(this);

    String msg;
    switch (account.getProvider()) {
      case SUCCESSWHALE:
      case BUFFER:
        msg =
            String.format(
                "Post to these %s accounts?%n%s",
                account.getUiTitle(), ServiceRef.humanList(svcs, "\n"));
        break;
      default:
        msg = String.format("Post to %s?", account.getUiTitle());
    }
    dlgBld.setMessage(msg);

    dlgBld.setPositiveButton(
        "Post",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(final DialogInterface dialog, final int which) {
            submitPost(account, svcs);
          }
        });

    dlgBld.setNegativeButton(
        "Cancel",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(final DialogInterface dialog, final int whichButton) {
            dialog.cancel();
          }
        });

    dlgBld.show();
  }
예제 #4
0
  protected void submitPost(final Account account, final Set<ServiceRef> svcs) {
    final String body = this.txtBody.getText().toString();
    final Intent recoveryIntent =
        new Intent(getBaseContext(), PostActivity.class)
            .putExtra(ARG_ACCOUNT_ID, account.getId())
            .putExtra(ARG_IN_REPLY_TO_UID, this.inReplyToUid)
            .putExtra(ARG_IN_REPLY_TO_SID, this.inReplyToSid)
            .putExtra(ARG_ATTACHMENT, this.attachment)
            .putExtra(ARG_BODY, body);

    final ArrayList<String> svcsLst = new ArrayList<String>();
    for (ServiceRef svc : svcs) {
      svcsLst.add(svc.toServiceMeta());
    }
    recoveryIntent.putStringArrayListExtra(ARG_SVCS, svcsLst);

    new PostTask(
            getApplicationContext(),
            new PostRequest(
                account, svcs, body, this.inReplyToSid, this.attachment, recoveryIntent))
        .execute();
    finish();
  }
예제 #5
0
  @Override
  protected void doWork(final Intent i) {
    final Prefs prefs = new Prefs(getBaseContext());
    final Config conf;
    try {
      conf = prefs.asConfig();
    } catch (final JSONException e) {
      LOG.w("Can not send to Hosaka: %s", e.toString());
      return;
    }

    // XXX Currently this assumes only one Hosaka account.
    // TODO Make UI stop user adding more than one Hosaka account.

    final Account account = conf.firstAccountOfType(AccountProvider.HOSAKA);
    if (account == null) {
      LOG.i("Not sending to Hosaka: no account found.");
      return;
    }

    if (!waitForDbReady()) return;
    final DbInterface db = getDb();

    SaveScrollNow.requestAndWaitForUiToSaveScroll(db);

    final Map<String, Column> hashToCol = new HashMap<String, Column>();
    final Map<String, HosakaColumn> toPush = new HashMap<String, HosakaColumn>();
    for (final Column col : conf.getColumns()) {
      if (InternalColumnType.fromColumn(col) != null) continue; // Do not sync internal columns.

      final String hash = HosakaColumn.columnHash(col, conf);
      hashToCol.put(hash, col);
      final ScrollState ss = db.getScroll(col.getId());
      if (ss == null) continue; // In case of (new) empty columns.
      // Always add all columns, even if sent before new values.
      // - Old / regressed values will be filtered server side.
      // - Values sent can be used to filter response.
      // - Also useful as do not know state of remote DB.
      toPush.put(
          hash,
          new HosakaColumn(
              null /* ss.getItemId(); TODO ScrollState to also store sid? */,
              ss.getItemTime(),
              ss.getUnreadTime(),
              ss.getScrollDirection()));
    }

    final HosakaProvider prov = new HosakaProvider();
    try {
      // Make POST even if not really sending anything new, as may be fetching new state.
      final long startTime = now();
      final Map<String, HosakaColumn> returnedColumns = prov.sendColumns(account, toPush);
      final long durationMillis = TimeUnit.NANOSECONDS.toMillis(now() - startTime);
      LOG.i("Sent %s in %d millis: %s", account.getAccessToken(), durationMillis, toPush);

      final boolean syncScroll =
          prefs.getSharedPreferences().getBoolean(FetchingPrefFragment.KEY_SYNC_SCROLL, false);

      final Map<Column, ScrollState> colToNewScroll = new HashMap<Column, ScrollState>();
      for (final Entry<String, HosakaColumn> e : returnedColumns.entrySet()) {
        final String hash = e.getKey();
        final Column col = hashToCol.get(hash);
        final HosakaColumn before = toPush.get(hash);
        final HosakaColumn after = e.getValue();
        if (col != null
            && before != null
            && (after.getUnreadTime() > before.getUnreadTime()
                || (syncScroll
                    && before.getScrollDirection() == ScrollDirection.UP
                    && after.getItemTime() > before.getItemTime()))) {
          colToNewScroll.put(col, after.toScrollState());
        }
      }
      db.mergeAndStoreScrolls(
          colToNewScroll,
          syncScroll ? ScrollChangeType.UNREAD_AND_SCROLL : ScrollChangeType.UNREAD);
      LOG.i("Merged %s columns: %s.", colToNewScroll.size(), colToNewScroll);

      storeResult(db, toPush.size(), colToNewScroll.size(), null);
    } catch (final IOException e) {
      storeResult(db, toPush.size(), 0, e);
    } catch (final JSONException e) {
      storeResult(db, toPush.size(), 0, e);
    } finally {
      prov.shutdown();
    }
  }