Android Open Source - picasso Contacts Photo Request Handler






From Project

Back to project page picasso.

License

The source code is released under:

Apache License

If you think the Android project picasso listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 Square, Inc./*w ww  .j  a  v  a  2s.c  om*/
 *
 * 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.squareup.picasso;

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.content.UriMatcher;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;

import java.io.IOException;
import java.io.InputStream;

import static android.content.ContentResolver.SCHEME_CONTENT;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static android.provider.ContactsContract.Contacts.openContactPhotoInputStream;
import static com.squareup.picasso.Picasso.LoadedFrom.DISK;

class ContactsPhotoRequestHandler extends RequestHandler {
  /** A lookup uri (e.g. content://com.android.contacts/contacts/lookup/3570i61d948d30808e537) */
  private static final int ID_LOOKUP = 1;
  /** A contact thumbnail uri (e.g. content://com.android.contacts/contacts/38/photo) */
  private static final int ID_THUMBNAIL = 2;
  /** A contact uri (e.g. content://com.android.contacts/contacts/38) */
  private static final int ID_CONTACT = 3;
  /**
   * A contact display photo (high resolution) uri
   * (e.g. content://com.android.contacts/display_photo/5)
   */
  private static final int ID_DISPLAY_PHOTO = 4;

  private static final UriMatcher matcher;

  static {
    matcher = new UriMatcher(UriMatcher.NO_MATCH);
    matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", ID_LOOKUP);
    matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", ID_LOOKUP);
    matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", ID_THUMBNAIL);
    matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", ID_CONTACT);
    matcher.addURI(ContactsContract.AUTHORITY, "display_photo/#", ID_DISPLAY_PHOTO);
  }

  final Context context;

  ContactsPhotoRequestHandler(Context context) {
    this.context = context;
  }

  @Override public boolean canHandleRequest(Request data) {
    final Uri uri = data.uri;
    return (SCHEME_CONTENT.equals(uri.getScheme())
        && ContactsContract.Contacts.CONTENT_URI.getHost().equals(uri.getHost())
        && !uri.getPathSegments().contains(ContactsContract.Contacts.Photo.CONTENT_DIRECTORY));
  }

  @Override public Result load(Request data, int networkPolicy) throws IOException {
    InputStream is = null;
    try {
      is = getInputStream(data);
      return new Result(decodeStream(is, data), DISK);
    } finally {
      Utils.closeQuietly(is);
    }
  }

  private InputStream getInputStream(Request data) throws IOException {
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = data.uri;
    switch (matcher.match(uri)) {
      case ID_LOOKUP:
        uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
        if (uri == null) {
          return null;
        }
        // Resolved the uri to a contact uri, intentionally fall through to process the resolved uri
      case ID_CONTACT:
        if (SDK_INT < ICE_CREAM_SANDWICH) {
          return openContactPhotoInputStream(contentResolver, uri);
        } else {
          return ContactPhotoStreamIcs.get(contentResolver, uri);
        }
      case ID_THUMBNAIL:
      case ID_DISPLAY_PHOTO:
        return contentResolver.openInputStream(uri);
      default:
        throw new IllegalStateException("Invalid uri: " + uri);
    }
  }

  private Bitmap decodeStream(InputStream stream, Request data) throws IOException {
    if (stream == null) {
      return null;
    }
    final BitmapFactory.Options options = createBitmapOptions(data);
    if (requiresInSampleSize(options)) {
      InputStream is = getInputStream(data);
      try {
        BitmapFactory.decodeStream(is, null, options);
      } finally {
        Utils.closeQuietly(is);
      }
      calculateInSampleSize(data.targetWidth, data.targetHeight, options, data);
    }
    return BitmapFactory.decodeStream(stream, null, options);
  }

  @TargetApi(ICE_CREAM_SANDWICH)
  private static class ContactPhotoStreamIcs {
    static InputStream get(ContentResolver contentResolver, Uri uri) {
      return openContactPhotoInputStream(contentResolver, uri, true);
    }
  }
}




Java Source Code List

com.example.picasso.Data.java
com.example.picasso.GrayscaleTransformation.java
com.example.picasso.PicassoSampleActivity.java
com.example.picasso.PicassoSampleAdapter.java
com.example.picasso.SampleContactsActivity.java
com.example.picasso.SampleContactsAdapter.java
com.example.picasso.SampleGalleryActivity.java
com.example.picasso.SampleGridViewActivity.java
com.example.picasso.SampleGridViewAdapter.java
com.example.picasso.SampleListDetailActivity.java
com.example.picasso.SampleListDetailAdapter.java
com.example.picasso.SampleScrollListener.java
com.example.picasso.SampleWidgetProvider.java
com.example.picasso.SquaredImageView.java
com.squareup.picasso.Action.java
com.squareup.picasso.AssetRequestHandler.java
com.squareup.picasso.BitmapHunter.java
com.squareup.picasso.Cache.java
com.squareup.picasso.Callback.java
com.squareup.picasso.ContactsPhotoRequestHandler.java
com.squareup.picasso.ContentStreamRequestHandler.java
com.squareup.picasso.DeferredRequestCreator.java
com.squareup.picasso.Dispatcher.java
com.squareup.picasso.Downloader.java
com.squareup.picasso.FetchAction.java
com.squareup.picasso.FileRequestHandler.java
com.squareup.picasso.GetAction.java
com.squareup.picasso.ImageViewAction.java
com.squareup.picasso.LruCache.java
com.squareup.picasso.MarkableInputStream.java
com.squareup.picasso.MediaStoreRequestHandler.java
com.squareup.picasso.MemoryPolicy.java
com.squareup.picasso.NetworkPolicy.java
com.squareup.picasso.NetworkRequestHandler.java
com.squareup.picasso.OkHttpDownloader.java
com.squareup.picasso.PicassoDrawable.java
com.squareup.picasso.PicassoExecutorService.java
com.squareup.picasso.Picasso.java
com.squareup.picasso.RemoteViewsAction.java
com.squareup.picasso.RequestCreator.java
com.squareup.picasso.RequestHandler.java
com.squareup.picasso.Request.java
com.squareup.picasso.ResourceRequestHandler.java
com.squareup.picasso.StatsSnapshot.java
com.squareup.picasso.Stats.java
com.squareup.picasso.TargetAction.java
com.squareup.picasso.Target.java
com.squareup.picasso.Transformation.java
com.squareup.picasso.UrlConnectionDownloader.java
com.squareup.picasso.Utils.java
com.squareup.picasso.pollexor.PollexorRequestTransformer.java