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