summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/editor/CompactPhotoEditorView.java
blob: 3481a332fca67c3d5b9676309f84bfeb62f4754c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.editor;

import com.android.contacts.R;
import com.android.contacts.common.ContactPhotoManager;
import com.android.contacts.common.ContactPhotoManager.DefaultImageProvider;
import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
import com.android.contacts.common.ContactsUtils;
import com.android.contacts.common.model.RawContactDelta;
import com.android.contacts.common.model.ValuesDelta;
import com.android.contacts.common.model.dataitem.DataKind;
import com.android.contacts.common.util.MaterialColorMapUtils;
import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
import com.android.contacts.editor.CompactContactEditorFragment.PhotoHandler;
import com.android.contacts.util.ContactPhotoUtils;
import com.android.contacts.util.SchedulingUtils;
import com.android.contacts.widget.QuickContactImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.DisplayPhoto;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Displays the primary photo.
 */
public class CompactPhotoEditorView extends LinearLayout implements View.OnClickListener {

    private static final String TAG = CompactContactEditorFragment.TAG;

    private ContactPhotoManager mContactPhotoManager;
    private PhotoHandler mPhotoHandler;

    private final float mLandscapePhotoRatio;
    private final boolean mIsTwoPanel;

    private ValuesDelta mValuesDelta;
    private boolean mReadOnly;
    private boolean mIsPhotoSet;
    private MaterialPalette mMaterialPalette;

    private QuickContactImageView mPhotoImageView;

    public CompactPhotoEditorView(Context context) {
        this(context, null);
    }

    public CompactPhotoEditorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mLandscapePhotoRatio = getTypedFloat(R.dimen.quickcontact_landscape_photo_ratio);
        mIsTwoPanel = getResources().getBoolean(R.bool.quickcontact_two_panel);
    }

    private float getTypedFloat(int resourceId) {
        final TypedValue typedValue = new TypedValue();
        getResources().getValue(resourceId, typedValue, /* resolveRefs =*/ true);
        return typedValue.getFloat();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContactPhotoManager = ContactPhotoManager.getInstance(getContext());

        mPhotoImageView = (QuickContactImageView) findViewById(R.id.photo);
        mPhotoImageView.setOnClickListener(this);
    }

    public void setValues(DataKind dataKind, ValuesDelta valuesDelta,
            RawContactDelta rawContactDelta, boolean readOnly, MaterialPalette materialPalette,
            ViewIdGenerator viewIdGenerator) {
        mValuesDelta = valuesDelta;
        mReadOnly = readOnly;
        mMaterialPalette = materialPalette;

        setId(viewIdGenerator.getId(rawContactDelta, dataKind, valuesDelta, /* viewIndex =*/ 0));

        if (valuesDelta == null) {
            setDefaultPhoto();
        } else {
            final byte[] bytes = valuesDelta.getAsByteArray(Photo.PHOTO);
            if (bytes == null) {
                setDefaultPhoto();
            } else {
                final Bitmap bitmap = BitmapFactory.decodeByteArray(
                        bytes, /* offset =*/ 0, bytes.length);
                mPhotoImageView.setImageBitmap(bitmap);
                mIsPhotoSet = true;
                mValuesDelta.setFromTemplate(false);

                // Check if we can update to the full size photo immediately
                if (valuesDelta.getAfter() == null
                        || valuesDelta.getAfter().get(Photo.PHOTO) == null) {
                    // If the user hasn't updated the PHOTO value, then PHOTO_FILE_ID may contain
                    // a reference to a larger version of PHOTO that we can bind to the UI.
                    // Otherwise, we need to wait for a call to #setFullSizedPhoto() to update
                    // our full sized image.
                    final Integer fileId = valuesDelta.getAsInteger(Photo.PHOTO_FILE_ID);
                    if (fileId != null) {
                        final Uri photoUri = DisplayPhoto.CONTENT_URI.buildUpon()
                                .appendPath(fileId.toString()).build();
                        setFullSizedPhoto(photoUri);
                    }
                }
            }
        }

        if (!mIsPhotoSet) {
            setDefaultPhotoTint();
        }

        // Adjust the photo dimensions following the same logic as in
        // MultiShrinkScroll.initialize
        SchedulingUtils.doOnPreDraw(this, /* drawNextFrame =*/ false, new Runnable() {
            @Override
            public void run() {
                final int photoHeight, photoWidth;
                if (mIsTwoPanel) {
                    // Make the photo slightly more narrow than it is tall
                    photoHeight = getHeight();
                    photoWidth = (int) (photoHeight * mLandscapePhotoRatio);
                } else {
                    // Make the photo a square
                    photoWidth = getWidth();
                    photoHeight = photoWidth;
                }
                final ViewGroup.LayoutParams layoutParams = getLayoutParams();
                layoutParams.height = photoHeight;
                layoutParams.width = photoWidth;
                setLayoutParams(layoutParams);
            }
        });
    }

    /**
     * Set the {@link PhotoHandler} to forward clicks (i.e. requests to edit the photo) to.
     */
    public void setPhotoHandler(PhotoHandler photoHandler) {
        mPhotoHandler = photoHandler;
    }

    /**
     * Whether a writable {@link Photo} has been set.
     */
    public boolean isWritablePhotoSet() {
        return mIsPhotoSet && !mReadOnly;
    }

    /**
     * Set the given {@link Bitmap} as the photo in the underlying {@link ValuesDelta}
     * and bind a thumbnail to the UI.
     */
    public void setPhoto(Bitmap bitmap) {
        if (mReadOnly) {
            Log.w(TAG, "Attempted to set read only photo. Aborting");
            return;
        }
        if (bitmap == null) {
            mValuesDelta.put(ContactsContract.CommonDataKinds.Photo.PHOTO, (byte[]) null);
            setDefaultPhoto();
            return;
        }

        final int thumbnailSize = ContactsUtils.getThumbnailSize(getContext());
        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
                bitmap, thumbnailSize, thumbnailSize, /* filter =*/ false);

        mPhotoImageView.setImageBitmap(scaledBitmap);
        mIsPhotoSet = true;
        mValuesDelta.setFromTemplate(false);

        // When the user chooses a new photo mark it as super primary
        mValuesDelta.setSuperPrimary(true);

        // Even though high-res photos cannot be saved by passing them via
        // an EntityDeltaList (since they cause the Bundle size limit to be
        // exceeded), we still pass a low-res thumbnail. This simplifies
        // code all over the place, because we don't have to test whether
        // there is a change in EITHER the delta-list OR a changed photo...
        // this way, there is always a change in the delta-list.
        final byte[] compressed = ContactPhotoUtils.compressBitmap(scaledBitmap);
        if (compressed != null) {
            mValuesDelta.setPhoto(compressed);
        }
    }

    /**
     * Show the default "add photo" place holder.
     */
    private void setDefaultPhoto() {
        mPhotoImageView.setImageDrawable(ContactPhotoManager.getDefaultAvatarDrawableForContact(
                getResources(), /* hires =*/ false, /* defaultImageRequest =*/ null));
        setDefaultPhotoTint();
        mIsPhotoSet = false;
        mValuesDelta.setFromTemplate(true);
    }

    private void setDefaultPhotoTint() {
        final int color = mMaterialPalette == null
                ? MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(
                getResources()).mPrimaryColor
                : mMaterialPalette.mPrimaryColor;
        mPhotoImageView.setTint(color);
    }

    /**
     * Bind the photo at the given Uri to the UI but do not set the photo on the underlying
     * {@link ValuesDelta}.
     */
    public void setFullSizedPhoto(Uri photoUri) {
        if (photoUri != null) {
            final DefaultImageProvider fallbackToPreviousImage = new DefaultImageProvider() {
                @Override
                public void applyDefaultImage(ImageView view, int extent, boolean darkTheme,
                        DefaultImageRequest defaultImageRequest) {
                    // Before we finish setting the full sized image, don't change the current
                    // image that is set in any way.
                }
            };
            mContactPhotoManager.loadPhoto(mPhotoImageView, photoUri,
                    mPhotoImageView.getWidth(), /* darkTheme =*/ false, /* isCircular =*/ false,
                    /* defaultImageRequest =*/ null, fallbackToPreviousImage);
        }
    }

    @Override
    public void onClick(View view) {
        if (mPhotoHandler != null) {
            mPhotoHandler.onClick(view);
        }
    }
}