Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2013 Square, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | package com.squareup.picasso; |
michael@0 | 17 | |
michael@0 | 18 | import android.annotation.TargetApi; |
michael@0 | 19 | import android.content.ContentResolver; |
michael@0 | 20 | import android.content.Context; |
michael@0 | 21 | import android.content.UriMatcher; |
michael@0 | 22 | import android.graphics.Bitmap; |
michael@0 | 23 | import android.graphics.BitmapFactory; |
michael@0 | 24 | import android.net.Uri; |
michael@0 | 25 | import android.provider.ContactsContract; |
michael@0 | 26 | |
michael@0 | 27 | import java.io.IOException; |
michael@0 | 28 | import java.io.InputStream; |
michael@0 | 29 | |
michael@0 | 30 | import static android.os.Build.VERSION.SDK_INT; |
michael@0 | 31 | import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH; |
michael@0 | 32 | import static android.provider.ContactsContract.Contacts.openContactPhotoInputStream; |
michael@0 | 33 | import static com.squareup.picasso.Picasso.LoadedFrom.DISK; |
michael@0 | 34 | |
michael@0 | 35 | class ContactsPhotoBitmapHunter extends BitmapHunter { |
michael@0 | 36 | /** A lookup uri (e.g. content://com.android.contacts/contacts/lookup/3570i61d948d30808e537) */ |
michael@0 | 37 | private static final int ID_LOOKUP = 1; |
michael@0 | 38 | /** A contact thumbnail uri (e.g. content://com.android.contacts/contacts/38/photo) */ |
michael@0 | 39 | private static final int ID_THUMBNAIL = 2; |
michael@0 | 40 | /** A contact uri (e.g. content://com.android.contacts/contacts/38) */ |
michael@0 | 41 | private static final int ID_CONTACT = 3; |
michael@0 | 42 | /** |
michael@0 | 43 | * A contact display photo (high resolution) uri |
michael@0 | 44 | * (e.g. content://com.android.contacts/display_photo/5) |
michael@0 | 45 | */ |
michael@0 | 46 | private static final int ID_DISPLAY_PHOTO = 4; |
michael@0 | 47 | |
michael@0 | 48 | private static final UriMatcher matcher; |
michael@0 | 49 | |
michael@0 | 50 | static { |
michael@0 | 51 | matcher = new UriMatcher(UriMatcher.NO_MATCH); |
michael@0 | 52 | matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", ID_LOOKUP); |
michael@0 | 53 | matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", ID_LOOKUP); |
michael@0 | 54 | matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", ID_THUMBNAIL); |
michael@0 | 55 | matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", ID_CONTACT); |
michael@0 | 56 | matcher.addURI(ContactsContract.AUTHORITY, "display_photo/#", ID_DISPLAY_PHOTO); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | final Context context; |
michael@0 | 60 | |
michael@0 | 61 | ContactsPhotoBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, |
michael@0 | 62 | Stats stats, Action action) { |
michael@0 | 63 | super(picasso, dispatcher, cache, stats, action); |
michael@0 | 64 | this.context = context; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | @Override Bitmap decode(Request data) throws IOException { |
michael@0 | 68 | InputStream is = null; |
michael@0 | 69 | try { |
michael@0 | 70 | is = getInputStream(); |
michael@0 | 71 | return decodeStream(is, data); |
michael@0 | 72 | } finally { |
michael@0 | 73 | Utils.closeQuietly(is); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | @Override Picasso.LoadedFrom getLoadedFrom() { |
michael@0 | 78 | return DISK; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | private InputStream getInputStream() throws IOException { |
michael@0 | 82 | ContentResolver contentResolver = context.getContentResolver(); |
michael@0 | 83 | Uri uri = getData().uri; |
michael@0 | 84 | switch (matcher.match(uri)) { |
michael@0 | 85 | case ID_LOOKUP: |
michael@0 | 86 | uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); |
michael@0 | 87 | if (uri == null) { |
michael@0 | 88 | return null; |
michael@0 | 89 | } |
michael@0 | 90 | // Resolved the uri to a contact uri, intentionally fall through to process the resolved uri |
michael@0 | 91 | case ID_CONTACT: |
michael@0 | 92 | if (SDK_INT < ICE_CREAM_SANDWICH) { |
michael@0 | 93 | return openContactPhotoInputStream(contentResolver, uri); |
michael@0 | 94 | } else { |
michael@0 | 95 | return ContactPhotoStreamIcs.get(contentResolver, uri); |
michael@0 | 96 | } |
michael@0 | 97 | case ID_THUMBNAIL: |
michael@0 | 98 | case ID_DISPLAY_PHOTO: |
michael@0 | 99 | return contentResolver.openInputStream(uri); |
michael@0 | 100 | default: |
michael@0 | 101 | throw new IllegalStateException("Invalid uri: " + uri); |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | private Bitmap decodeStream(InputStream stream, Request data) throws IOException { |
michael@0 | 106 | if (stream == null) { |
michael@0 | 107 | return null; |
michael@0 | 108 | } |
michael@0 | 109 | BitmapFactory.Options options = null; |
michael@0 | 110 | if (data.hasSize()) { |
michael@0 | 111 | options = new BitmapFactory.Options(); |
michael@0 | 112 | options.inJustDecodeBounds = true; |
michael@0 | 113 | InputStream is = getInputStream(); |
michael@0 | 114 | try { |
michael@0 | 115 | BitmapFactory.decodeStream(is, null, options); |
michael@0 | 116 | } finally { |
michael@0 | 117 | Utils.closeQuietly(is); |
michael@0 | 118 | } |
michael@0 | 119 | calculateInSampleSize(data.targetWidth, data.targetHeight, options); |
michael@0 | 120 | } |
michael@0 | 121 | return BitmapFactory.decodeStream(stream, null, options); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | @TargetApi(ICE_CREAM_SANDWICH) |
michael@0 | 125 | private static class ContactPhotoStreamIcs { |
michael@0 | 126 | static InputStream get(ContentResolver contentResolver, Uri uri) { |
michael@0 | 127 | return openContactPhotoInputStream(contentResolver, uri, true); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | } |