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.editor; 18 19 import com.android.contacts.R; 20 import com.android.contacts.model.AccountType; 21 import com.android.contacts.model.AccountType.EditType; 22 import com.android.contacts.model.EntityDelta; 23 import com.android.contacts.model.EntityDelta.ValuesDelta; 24 import com.android.contacts.model.EntityModifier; 25 26 import android.content.Context; 27 import android.content.Entity; 28 import android.database.Cursor; 29 import android.graphics.Bitmap; 30 import android.provider.ContactsContract.CommonDataKinds.Photo; 31 import android.provider.ContactsContract.Data; 32 import android.provider.ContactsContract.RawContacts; 33 import android.util.AttributeSet; 34 import android.view.View; 35 import android.view.ViewGroup; 36 import android.widget.LinearLayout; 37 38 /** 39 * Base view that provides common code for the editor interaction for a specific 40 * RawContact represented through an {@link EntityDelta}. 41 * <p> 42 * Internal updates are performed against {@link ValuesDelta} so that the 43 * source {@link Entity} can be swapped out. Any state-based changes, such as 44 * adding {@link Data} rows or changing {@link EditType}, are performed through 45 * {@link EntityModifier} to ensure that {@link AccountType} are enforced. 46 */ 47 public abstract class BaseRawContactEditorView extends LinearLayout { 48 49 private PhotoEditorView mPhoto; 50 private boolean mHasPhotoEditor = false; 51 52 private View mBody; 53 private View mDivider; 54 55 private boolean mExpanded = true; 56 BaseRawContactEditorView(Context context)57 public BaseRawContactEditorView(Context context) { 58 super(context); 59 } 60 BaseRawContactEditorView(Context context, AttributeSet attrs)61 public BaseRawContactEditorView(Context context, AttributeSet attrs) { 62 super(context, attrs); 63 } 64 65 @Override onFinishInflate()66 protected void onFinishInflate() { 67 super.onFinishInflate(); 68 69 mBody = findViewById(R.id.body); 70 mDivider = findViewById(R.id.divider); 71 72 mPhoto = (PhotoEditorView)findViewById(R.id.edit_photo); 73 mPhoto.setEnabled(isEnabled()); 74 } 75 setGroupMetaData(Cursor groupMetaData)76 public void setGroupMetaData(Cursor groupMetaData) { 77 } 78 79 /** 80 * Assign the given {@link Bitmap} to the internal {@link PhotoEditorView} 81 * for the {@link EntityDelta} currently being edited. 82 */ setPhotoBitmap(Bitmap bitmap)83 public void setPhotoBitmap(Bitmap bitmap) { 84 mPhoto.setPhotoBitmap(bitmap); 85 } 86 setHasPhotoEditor(boolean hasPhotoEditor)87 protected void setHasPhotoEditor(boolean hasPhotoEditor) { 88 mHasPhotoEditor = hasPhotoEditor; 89 mPhoto.setVisibility(hasPhotoEditor ? View.VISIBLE : View.GONE); 90 } 91 92 /** 93 * Return true if the current {@link RawContacts} supports {@link Photo}, 94 * which means that {@link PhotoEditorView} is enabled. 95 */ hasPhotoEditor()96 public boolean hasPhotoEditor() { 97 return mHasPhotoEditor; 98 } 99 100 /** 101 * Return true if internal {@link PhotoEditorView} has a {@link Photo} set. 102 */ hasSetPhoto()103 public boolean hasSetPhoto() { 104 return mPhoto.hasSetPhoto(); 105 } 106 getPhotoEditor()107 public PhotoEditorView getPhotoEditor() { 108 return mPhoto; 109 } 110 111 /** 112 * @return the RawContact ID that this editor is editing. 113 */ getRawContactId()114 public abstract long getRawContactId(); 115 116 /** 117 * Set the internal state for this view, given a current 118 * {@link EntityDelta} state and the {@link AccountType} that 119 * apply to that state. 120 */ setState(EntityDelta state, AccountType source, ViewIdGenerator vig, boolean isProfile)121 public abstract void setState(EntityDelta state, AccountType source, ViewIdGenerator vig, 122 boolean isProfile); 123 setExpanded(boolean value)124 /* package */ void setExpanded(boolean value) { 125 // only allow collapsing if we are one of several children 126 final boolean newValue; 127 if (getParent() instanceof ViewGroup && ((ViewGroup) getParent()).getChildCount() == 1) { 128 newValue = true; 129 } else { 130 newValue = value; 131 } 132 133 if (newValue == mExpanded) return; 134 mExpanded = newValue; 135 mBody.setVisibility(newValue ? View.VISIBLE : View.GONE); 136 mDivider.setVisibility(newValue ? View.GONE : View.VISIBLE); 137 } 138 } 139