SimpleRecipientChip.java revision 78f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7
16a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp/*
26a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * Copyright (C) 2011 The Android Open Source Project
36a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp *
46a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * Licensed under the Apache License, Version 2.0 (the "License");
56a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * you may not use this file except in compliance with the License.
66a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * You may obtain a copy of the License at
76a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp *
86a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp *      http://www.apache.org/licenses/LICENSE-2.0
96a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp *
106a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * Unless required by applicable law or agreed to in writing, software
116a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * distributed under the License is distributed on an "AS IS" BASIS,
126a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * See the License for the specific language governing permissions and
146a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * limitations under the License.
156a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp */
166a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
176a8c8d837e53897f19539e0c2455e3d5fc579b5emindyppackage com.android.ex.chips;
186a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
196a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.graphics.Canvas;
206a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.graphics.Rect;
216a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.graphics.drawable.Drawable;
226a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.text.TextUtils;
236a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.text.style.DynamicDrawableSpan;
246a8c8d837e53897f19539e0c2455e3d5fc579b5emindypimport android.text.style.ImageSpan;
256a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
266a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp/**
276a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * VisibleRecipientChip defines an ImageSpan that contains information relevant to a
286a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp * particular recipient and renders a background asset to go with it.
296a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp */
306a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp/* package */class VisibleRecipientChip extends ImageSpan implements RecipientChip {
316a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private final CharSequence mDisplay;
326a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
336a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private final CharSequence mValue;
346a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
356a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private final long mContactId;
366a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
376a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private final long mDataId;
386a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
396a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private RecipientEntry mEntry;
406a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
416a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private boolean mSelected = false;
426a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
436a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    private CharSequence mOriginalText;
446a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
456a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public VisibleRecipientChip(Drawable drawable, RecipientEntry entry, int offset) {
466a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        super(drawable, DynamicDrawableSpan.ALIGN_BOTTOM);
476a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mDisplay = entry.getDisplayName();
486a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mValue = entry.getDestination().trim();
496a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mContactId = entry.getContactId();
506a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mDataId = entry.getDataId();
516a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mEntry = entry;
526a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
536a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
546a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
556a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Set the selected state of the chip.
566a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * @param selected
576a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
586a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public void setSelected(boolean selected) {
596a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mSelected = selected;
606a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
616a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
626a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
636a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Return true if the chip is selected.
646a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
656a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public boolean isSelected() {
666a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mSelected;
676a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
686a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
696a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
706a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Get the text displayed in the chip.
716a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
726a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public CharSequence getDisplay() {
736a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mDisplay;
746a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
756a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
766a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
776a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Get the text value this chip represents.
786a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
796a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public CharSequence getValue() {
806a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mValue;
816a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
826a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
836a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
846a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Get the id of the contact associated with this chip.
856a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
866a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public long getContactId() {
876a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mContactId;
886a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
896a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
906a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
916a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Get the id of the data associated with this chip.
926a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
936a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public long getDataId() {
946a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mDataId;
956a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
966a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
976a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    /**
986a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     * Get associated RecipientEntry.
996a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp     */
1006a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public RecipientEntry getEntry() {
1016a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return mEntry;
1026a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
1036a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
1046a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public void setOriginalText(String text) {
1056a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        if (!TextUtils.isEmpty(text)) {
1066a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp            text = text.trim();
1076a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        }
1086a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        mOriginalText = text;
1096a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
1106a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
1116a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public CharSequence getOriginalText() {
1126a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return !TextUtils.isEmpty(mOriginalText) ? mOriginalText : mEntry.getDestination();
1136a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
1146a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
1156a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    @Override
1166a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public Rect getBounds() {
1176a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        return getDrawable().getBounds();
1186a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
1196a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp
1206a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    @Override
1216a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    public void draw(Canvas canvas) {
1226a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp        getDrawable().draw(canvas);
1236a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp    }
12478f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy
12578f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy    @Override
12678f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy    public String toString() {
12778f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy        return mDisplay + " <" + mValue + ">";
12878f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy    }
1296a8c8d837e53897f19539e0c2455e3d5fc579b5emindyp}
130