예제 #1
0
  /** Build editors for all current {@link #mState} rows. */
  public void rebuildFromState() {
    // Remove any existing editors
    mEditors.removeAllViews();

    // Check if we are displaying anything here
    boolean hasEntries = mState.hasMimeEntries(mKind.mimeType);

    if (!mKind.isList) {
      if (hasEntries) {
        // we might have no visible entries. check that, too
        for (ValuesDelta entry : mState.getMimeEntries(mKind.mimeType)) {
          if (!entry.isVisible()) {
            hasEntries = false;
            break;
          }
        }
      }

      if (!hasEntries) {
        EntityModifier.insertChild(mState, mKind);
        hasEntries = true;
      }
    }

    if (hasEntries) {
      int entryIndex = 0;
      for (ValuesDelta entry : mState.getMimeEntries(mKind.mimeType)) {
        // Skip entries that aren't visible
        if (!entry.isVisible()) continue;

        final GenericEditorView editor =
            (GenericEditorView) mInflater.inflate(R.layout.item_generic_editor, mEditors, false);
        editor.setValues(mKind, entry, mState, mReadOnly, mViewIdGenerator);
        // older versions of android had lists where we now have a single value
        // in these cases we should show the remove button for all but the first value
        // to ensure that nothing is removed
        editor.mDelete.setVisibility(
            (mKind.isList || (entryIndex != 0)) ? View.VISIBLE : View.GONE);
        editor.setEditorListener(this);
        mEditors.addView(editor);
        entryIndex++;
      }
    }
  }
  public void testParseExtrasIgnoreUnhandled() {
    final ContactsSource source = getSource();
    final EntityDelta state = getEntity(TEST_ID);

    // We should silently ignore types unsupported by source
    final Bundle extras = new Bundle();
    extras.putString(Insert.POSTAL, TEST_POSTAL);
    EntityModifier.parseExtras(mContext, source, state, extras);

    assertNull("Broke source rules", state.getMimeEntries(StructuredPostal.CONTENT_ITEM_TYPE));
  }
  public void testParseExtrasJobTitle() {
    final ContactsSource source = getSource();
    final EntityDelta state = getEntity(TEST_ID);

    // Make sure that we create partial Organizations
    final Bundle extras = new Bundle();
    extras.putString(Insert.JOB_TITLE, TEST_NAME);
    EntityModifier.parseExtras(mContext, source, state, extras);

    final int count = state.getMimeEntries(Organization.CONTENT_ITEM_TYPE).size();
    assertEquals("Expected to create organization", 1, count);
  }
  public void testParseExtrasIgnoreLimit() {
    final ContactsSource source = getSource();
    final DataKind kindIm = source.getKindForMimetype(Im.CONTENT_ITEM_TYPE);

    // Build "before" IM
    final ContentValues first = new ContentValues();
    first.put(Data._ID, TEST_ID);
    first.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
    first.put(Im.DATA, TEST_IM);

    final EntityDelta state = getEntity(TEST_ID, first);
    final int beforeCount = state.getMimeEntries(Im.CONTENT_ITEM_TYPE).size();

    // We should ignore data that doesn't fit source rules, since source
    // only allows single Im
    final Bundle extras = new Bundle();
    extras.putInt(Insert.IM_PROTOCOL, Im.PROTOCOL_GOOGLE_TALK);
    extras.putString(Insert.IM_HANDLE, TEST_IM);
    EntityModifier.parseExtras(mContext, source, state, extras);

    final int afterCount = state.getMimeEntries(Im.CONTENT_ITEM_TYPE).size();
    assertEquals("Broke source rules", beforeCount, afterCount);
  }
  /**
   * Set the internal state for this view, given a current {@link EntityDelta} state and the {@link
   * ContactsSource} that apply to that state.
   *
   * <p>TODO: make this more generic using data from the source
   */
  @Override
  public void setState(EntityDelta state, ContactsSource source, ViewIdGenerator vig) {
    // Remove any existing sections
    mGeneral.removeAllViews();

    // Bail if invalid state or source
    if (state == null || source == null) return;

    // Make sure we have StructuredName
    EntityModifier.ensureKindExists(state, source, StructuredName.CONTENT_ITEM_TYPE);

    // Fill in the header info
    ValuesDelta values = state.getValues();
    String accountName = values.getAsString(RawContacts.ACCOUNT_NAME);
    CharSequence accountType = source.getDisplayLabel(mContext);
    if (TextUtils.isEmpty(accountType)) {
      accountType = mContext.getString(R.string.account_phone);
    }
    if (!TextUtils.isEmpty(accountName)) {
      mHeaderAccountName.setText(mContext.getString(R.string.from_account_format, accountName));
    }
    mHeaderAccountType.setText(mContext.getString(R.string.account_type_format, accountType));
    mHeaderIcon.setImageDrawable(source.getDisplayIcon(mContext));

    mRawContactId = values.getAsLong(RawContacts._ID);

    ValuesDelta primary;

    // Photo
    DataKind kind = source.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
    if (kind != null) {
      EntityModifier.ensureKindExists(state, source, Photo.CONTENT_ITEM_TYPE);
      mHasPhotoEditor = (source.getKindForMimetype(Photo.CONTENT_ITEM_TYPE) != null);
      primary = state.getPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
      mPhoto.setValues(kind, primary, state, source.readOnly, vig);
      if (!mHasPhotoEditor || !mPhoto.hasSetPhoto()) {
        mPhotoStub.setVisibility(View.GONE);
      } else {
        mPhotoStub.setVisibility(View.VISIBLE);
      }
    } else {
      mPhotoStub.setVisibility(View.VISIBLE);
    }

    // Name
    primary = state.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE);
    mName.setText(primary.getAsString(StructuredName.DISPLAY_NAME));

    // Read only warning
    mReadOnlyWarning.setText(mContext.getString(R.string.contact_read_only, accountType));

    // Phones
    ArrayList<ValuesDelta> phones = state.getMimeEntries(Phone.CONTENT_ITEM_TYPE);
    if (phones != null) {
      for (ValuesDelta phone : phones) {
        View field = mInflater.inflate(R.layout.item_read_only_field, mGeneral, false);
        TextView v;
        v = (TextView) field.findViewById(R.id.label);
        v.setText(mContext.getText(R.string.phoneLabelsGroup));
        v = (TextView) field.findViewById(R.id.data);
        v.setText(PhoneNumberFormatUtilEx.formatNumber(phone.getAsString(Phone.NUMBER)));
        mGeneral.addView(field);
      }
    }

    // Emails
    ArrayList<ValuesDelta> emails = state.getMimeEntries(Email.CONTENT_ITEM_TYPE);
    if (emails != null) {
      for (ValuesDelta email : emails) {
        View field = mInflater.inflate(R.layout.item_read_only_field, mGeneral, false);
        TextView v;
        v = (TextView) field.findViewById(R.id.label);
        v.setText(mContext.getText(R.string.emailLabelsGroup));
        v = (TextView) field.findViewById(R.id.data);
        v.setText(email.getAsString(Email.DATA));
        mGeneral.addView(field);
      }
    }

    // Hide mGeneral if it's empty
    if (mGeneral.getChildCount() > 0) {
      mGeneral.setVisibility(View.VISIBLE);
    } else {
      mGeneral.setVisibility(View.GONE);
    }
  }