Home | History | Annotate | Download | only in bitmapsample
      1 /*
      2  * Copyright (C) 2013 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.example.bitmapsample;
     18 
     19 import com.android.bitmap.RequestKey;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.net.MalformedURLException;
     24 import java.net.URL;
     25 
     26 public class BitmapRequestKeyImpl implements RequestKey {
     27     public final String mUriString;
     28     public final URL mUrl;
     29 
     30     private boolean mSlept;
     31 
     32     public BitmapRequestKeyImpl(String uriString) {
     33         mUriString = uriString;
     34         URL url = null;
     35         try {
     36             url = new URL(uriString);
     37         } catch (MalformedURLException e) {
     38             e.printStackTrace();
     39         }
     40         mUrl = url;
     41         mSlept = false;
     42     }
     43 
     44     @Override
     45     public boolean equals(Object o) {
     46         if (o == null || !(o instanceof BitmapRequestKeyImpl)) {
     47             return false;
     48         }
     49         final BitmapRequestKeyImpl other = (BitmapRequestKeyImpl) o;
     50         return mUriString.equals(other.mUriString);
     51     }
     52 
     53     @Override
     54     public int hashCode() {
     55         int hash = 17;
     56         hash += 31 * hash + mUriString.hashCode();
     57         return hash;
     58     }
     59 
     60     @Override
     61     public String toString() {
     62         final StringBuilder sb = new StringBuilder("[");
     63         sb.append(mUriString);
     64         sb.append("]");
     65         return sb.toString();
     66     }
     67 
     68     @Override
     69     public Cancelable createFileDescriptorFactoryAsync(final RequestKey key,
     70             final Callback callback) {
     71         return null;
     72     }
     73 
     74     @Override
     75     public InputStream createInputStream() throws IOException {
     76         // Artificially sleep for (deterministically) random amount of time.
     77         if (!mSlept) {
     78             // Character difference between shortest and longest uri.
     79             final long spread = 26;
     80             // Maximum amount of time to sleep.
     81             final long max = 3;
     82             final long duration = (long) ((float) (mUriString.length() % spread) / spread * max
     83                     * 1000);
     84             try {
     85                 Thread.sleep(duration);
     86             } catch (InterruptedException ignored) {
     87             }
     88             mSlept = true;
     89         }
     90         return mUrl.openStream();
     91     }
     92 
     93     @Override
     94     public boolean hasOrientationExif() throws IOException {
     95         return false;
     96     }
     97 
     98 }