Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.ui.widget;
     18 
     19 import com.android.contacts.R;
     20 import com.android.contacts.model.ContactsSource;
     21 import com.android.contacts.model.EntityDelta;
     22 import com.android.contacts.model.EntityModifier;
     23 import com.android.contacts.model.ContactsSource.DataKind;
     24 import com.android.contacts.model.ContactsSource.EditType;
     25 import com.android.contacts.model.Editor.EditorListener;
     26 import com.android.contacts.model.EntityDelta.ValuesDelta;
     27 import com.android.contacts.ui.ViewIdGenerator;
     28 
     29 import android.content.Context;
     30 import android.content.Entity;
     31 import android.provider.ContactsContract.Contacts;
     32 import android.provider.ContactsContract.Data;
     33 import android.provider.ContactsContract.RawContacts;
     34 import android.provider.ContactsContract.CommonDataKinds.Email;
     35 import android.provider.ContactsContract.CommonDataKinds.Phone;
     36 import android.provider.ContactsContract.CommonDataKinds.Photo;
     37 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
     38 import android.telephony.PhoneNumberUtils;
     39 import android.text.TextUtils;
     40 import android.util.AttributeSet;
     41 import android.view.LayoutInflater;
     42 import android.view.View;
     43 import android.view.ViewGroup;
     44 import android.widget.ImageView;
     45 import android.widget.TextView;
     46 
     47 import java.util.ArrayList;
     48 
     49 /**
     50  * Custom view that displays read-only contacts in the edit screen.
     51  */
     52 class ReadOnlyContactEditorView extends BaseContactEditorView {
     53 
     54     private View mPhotoStub;
     55     private TextView mName;
     56     private TextView mReadOnlyWarning;
     57     private ViewGroup mGeneral;
     58 
     59     private View mHeaderColorBar;
     60     private View mSideBar;
     61     private ImageView mHeaderIcon;
     62     private TextView mHeaderAccountType;
     63     private TextView mHeaderAccountName;
     64 
     65     private long mRawContactId = -1;
     66 
     67     public ReadOnlyContactEditorView(Context context) {
     68         super(context);
     69     }
     70 
     71     public ReadOnlyContactEditorView(Context context, AttributeSet attrs) {
     72         super(context, attrs);
     73     }
     74 
     75     /** {@inheritDoc} */
     76     @Override
     77     protected void onFinishInflate() {
     78         super.onFinishInflate();
     79 
     80         mInflater = (LayoutInflater)getContext().getSystemService(
     81                 Context.LAYOUT_INFLATER_SERVICE);
     82 
     83         mPhoto = (PhotoEditorView)findViewById(R.id.edit_photo);
     84         mPhotoStub = findViewById(R.id.stub_photo);
     85 
     86         mName = (TextView) findViewById(R.id.read_only_name);
     87         mReadOnlyWarning = (TextView) findViewById(R.id.read_only_warning);
     88         mGeneral = (ViewGroup)findViewById(R.id.sect_general);
     89 
     90         mHeaderColorBar = findViewById(R.id.header_color_bar);
     91         mSideBar = findViewById(R.id.color_bar);
     92         mHeaderIcon = (ImageView) findViewById(R.id.header_icon);
     93         mHeaderAccountType = (TextView) findViewById(R.id.header_account_type);
     94         mHeaderAccountName = (TextView) findViewById(R.id.header_account_name);
     95     }
     96 
     97     /**
     98      * Set the internal state for this view, given a current
     99      * {@link EntityDelta} state and the {@link ContactsSource} that
    100      * apply to that state.
    101      *
    102      * TODO: make this more generic using data from the source
    103      */
    104     @Override
    105     public void setState(EntityDelta state, ContactsSource source, ViewIdGenerator vig) {
    106         // Remove any existing sections
    107         mGeneral.removeAllViews();
    108 
    109         // Bail if invalid state or source
    110         if (state == null || source == null) return;
    111 
    112         // Make sure we have StructuredName
    113         EntityModifier.ensureKindExists(state, source, StructuredName.CONTENT_ITEM_TYPE);
    114 
    115         // Fill in the header info
    116         ValuesDelta values = state.getValues();
    117         String accountName = values.getAsString(RawContacts.ACCOUNT_NAME);
    118         CharSequence accountType = source.getDisplayLabel(mContext);
    119         if (TextUtils.isEmpty(accountType)) {
    120             accountType = mContext.getString(R.string.account_phone);
    121         }
    122         if (!TextUtils.isEmpty(accountName)) {
    123             mHeaderAccountName.setText(
    124                     mContext.getString(R.string.from_account_format, accountName));
    125         }
    126         mHeaderAccountType.setText(mContext.getString(R.string.account_type_format, accountType));
    127         mHeaderIcon.setImageDrawable(source.getDisplayIcon(mContext));
    128 
    129         mRawContactId = values.getAsLong(RawContacts._ID);
    130 
    131         ValuesDelta primary;
    132 
    133         // Photo
    134         DataKind kind = source.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
    135         if (kind != null) {
    136             EntityModifier.ensureKindExists(state, source, Photo.CONTENT_ITEM_TYPE);
    137             mHasPhotoEditor = (source.getKindForMimetype(Photo.CONTENT_ITEM_TYPE) != null);
    138             primary = state.getPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
    139             mPhoto.setValues(kind, primary, state, source.readOnly, vig);
    140             if (!mHasPhotoEditor || !mPhoto.hasSetPhoto()) {
    141                 mPhotoStub.setVisibility(View.GONE);
    142             } else {
    143                 mPhotoStub.setVisibility(View.VISIBLE);
    144             }
    145         } else {
    146             mPhotoStub.setVisibility(View.VISIBLE);
    147         }
    148 
    149         // Name
    150         primary = state.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE);
    151         mName.setText(primary.getAsString(StructuredName.DISPLAY_NAME));
    152 
    153         // Read only warning
    154         mReadOnlyWarning.setText(mContext.getString(R.string.contact_read_only, accountType));
    155 
    156         // Phones
    157         ArrayList<ValuesDelta> phones = state.getMimeEntries(Phone.CONTENT_ITEM_TYPE);
    158         if (phones != null) {
    159             for (ValuesDelta phone : phones) {
    160                 View field = mInflater.inflate(
    161                         R.layout.item_read_only_field, mGeneral, false);
    162                 TextView v;
    163                 v = (TextView) field.findViewById(R.id.label);
    164                 v.setText(mContext.getText(R.string.phoneLabelsGroup));
    165                 v = (TextView) field.findViewById(R.id.data);
    166                 v.setText(PhoneNumberUtils.formatNumber(phone.getAsString(Phone.NUMBER)));
    167                 mGeneral.addView(field);
    168             }
    169         }
    170 
    171         // Emails
    172         ArrayList<ValuesDelta> emails = state.getMimeEntries(Email.CONTENT_ITEM_TYPE);
    173         if (emails != null) {
    174             for (ValuesDelta email : emails) {
    175                 View field = mInflater.inflate(
    176                         R.layout.item_read_only_field, mGeneral, false);
    177                 TextView v;
    178                 v = (TextView) field.findViewById(R.id.label);
    179                 v.setText(mContext.getText(R.string.emailLabelsGroup));
    180                 v = (TextView) field.findViewById(R.id.data);
    181                 v.setText(email.getAsString(Email.DATA));
    182                 mGeneral.addView(field);
    183             }
    184         }
    185 
    186         // Hide mGeneral if it's empty
    187         if (mGeneral.getChildCount() > 0) {
    188             mGeneral.setVisibility(View.VISIBLE);
    189         } else {
    190             mGeneral.setVisibility(View.GONE);
    191         }
    192     }
    193 
    194     /**
    195      * Sets the {@link EditorListener} on the name field
    196      */
    197     @Override
    198     public void setNameEditorListener(EditorListener listener) {
    199         // do nothing
    200     }
    201 
    202     @Override
    203     public long getRawContactId() {
    204         return mRawContactId;
    205     }
    206 }
    207