Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion;
  
  import  android.annotation.TargetApi;
  import  android.app.Fragment;
  import  android.content.Context;
  import  android.net.Uri;
  import  android.os.Build;
  import  android.os.Handler;
  import  android.os.Looper;
 import  android.text.TextUtils;
 import  android.util.Log;
 import  android.widget.ImageView;
 
 
 import  org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
 
 import java.io.File;
 import java.util.List;
 
Created by koush on 5/21/13.
 
 public class Ion {
     static final Handler mainHandler = new Handler(Looper.getMainLooper());
     static int availableProcessors = Runtime.getRuntime().availableProcessors();
     static ExecutorService ioExecutorService = Executors.newFixedThreadPool(4);
     static ExecutorService bitmapExecutorService  =  > 2 ? Executors.newFixedThreadPool( - 1) : Executors.newFixedThreadPool(1);
     static HashMap<StringIoninstances = new HashMap<StringIon>();

    
Get the default Ion object instance and begin building a request

Parameters:
context
Returns:
 
     public static LoadBuilder<Builders.Any.Bwith(Context context) {
         return getDefault(context).build(context);
     }

    
the default Ion object instance and begin building a request

Parameters:
fragment
Returns:
 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
     public static LoadBuilder<Builders.Any.Bwith(Fragment fragment) {
         return getDefault(fragment.getActivity()).build(fragment);
     }

    
the default Ion object instance and begin building a request

Parameters:
fragment
Returns:
 
     public static LoadBuilder<Builders.Any.Bwith(android.support.v4.app.Fragment fragment) {
         return getDefault(fragment.getActivity()).build(fragment);
     }

    
Get the default Ion instance

Parameters:
context
Returns:
 
     public static Ion getDefault(Context context) {
        return getInstance(context"ion");
    }

    
Get the given Ion instance by name

Parameters:
context
name
Returns:
    public static Ion getInstance(Context contextString name) {
        if (context == null)
            throw new NullPointerException("Can not pass null context in to retrieve ion instance");
        Ion instance = .get(name);
        if (instance == null)
            .put(nameinstance = new Ion(contextname));
        return instance;
    }

    
Create a ImageView bitmap request builder

Parameters:
imageView
Returns:
    public static Builders.IV.F<? extends Builders.IV.F<?>> with(ImageView imageView) {
        return getDefault(imageView.getContext()).build(imageView);
    }
    String logtag;
    int logLevel;
    Gson gson;
    ArrayList<Loaderloaders = new ArrayList<Loader>();
    String name;
    Config config = new Config();
    Context context;
    private Ion(Context contextString name) {
        this. = context = context.getApplicationContext();
        this. = name;
         = new AsyncHttpClient(new AsyncServer("ion-" + name));
        .getSSLSocketMiddleware().setHostnameVerifier(new BrowserCompatHostnameVerifier());
        File ionCacheDir = new File(context.getCacheDir(), name);
        try {
             = ResponseCacheMiddleware.addCache(ionCacheDir, 10L * 1024L * 1024L);
        }
        catch (IOException e) {
            IonLog.w("unable to set up response cache, clearing"e);
            FileUtility.deleteDirectory(ionCacheDir);
            try {
                 = ResponseCacheMiddleware.addCache(ionCacheDir, 10L * 1024L * 1024L);
            }
            catch (IOException ex) {
                IonLog.w("unable to set up response cache, failing"e);
            }
        }
         = new FileCache(new File(context.getFilesDir(), name), .false);
        // TODO: Support pre GB?
        if (Build.VERSION.SDK_INT >= 9)
            addCookieMiddleware();
         = new IonBitmapCache(this);
        configure()
                .addLoader( = new VideoLoader())
                .addLoader( = new PackageIconLoader())
                .addLoader( = new HttpLoader())
                .addLoader( = new ContentLoader())
                .addLoader( = new ResourceLoader())
                .addLoader( = new AssetLoader())
                .addLoader( = new FileLoader());
    }
    public static ExecutorService getBitmapLoadExecutorService() {
        return ;
    }
    public static ExecutorService getIoExecutorService() {
        return ;
    }

    
Begin building a request

Parameters:
context
Returns:
    public LoadBuilder<Builders.Any.Bbuild(Context context) {
        return new IonRequestBuilder(ContextReference.fromContext(context), this);
    }

    
Begin building a request

Parameters:
fragment
Returns:
    public LoadBuilder<Builders.Any.Bbuild(Fragment fragment) {
        return new IonRequestBuilder(new ContextReference.FragmentContextReference(fragment), this);
    }

    
Begin building a request

Parameters:
fragment
Returns:
    public LoadBuilder<Builders.Any.Bbuild(android.support.v4.app.Fragment fragment) {
        return new IonRequestBuilder(new ContextReference.SupportFragmentContextReference(fragment), this);
    }

    
Create a builder that can be used to build an network request

Parameters:
imageView
Returns:
    public Builders.IV.F<? extends Builders.IV.F<?>> build(ImageView imageView) {
        if (Thread.currentThread() != Looper.getMainLooper().getThread())
            throw new IllegalStateException("must be called from UI thread");
        .reset();
        . = this;
        return .withImageView(imageView);
    }
    int groupCount(Object group) {
        FutureSet members;
        synchronized (this) {
            members = .get(group);
        }
        if (members == null)
            return 0;
        return members.size();
    }
        @Override
        public int compare(DeferredLoadBitmap lhsDeferredLoadBitmap rhs) {
            // higher is more recent
            if (lhs.priority == rhs.priority)
                return 0;
            if (lhs.priority < rhs.priority)
                return 1;
            return -1;
        }
    };
    private Runnable processDeferred = new Runnable() {
        @Override
        public void run() {
            if (BitmapFetcher.shouldDeferImageView(Ion.this))
                return;
            ArrayList<DeferredLoadBitmapdeferred = null;
            for (String key.keySet()) {
                Object owner = .tag(key);
                if (owner instanceof DeferredLoadBitmap) {
                    DeferredLoadBitmap deferredLoadBitmap = (DeferredLoadBitmap)owner;
                    if (deferred == null)
                        deferred = new ArrayList<DeferredLoadBitmap>();
                    deferred.add(deferredLoadBitmap);
                }
            }
            if (deferred == null)
                return;
            int count = 0;
            Collections.sort(deferred);
            for (DeferredLoadBitmap deferredLoadBitmapdeferred) {
                .tag(deferredLoadBitmap.keynull);
                .tag(deferredLoadBitmap.fetcher.bitmapKeynull);
                deferredLoadBitmap.fetcher.execute();
                count++;
                // do MAX_IMAGEVIEW_LOAD max. this may end up going over the MAX_IMAGEVIEW_LOAD threshhold
                if (count > .)
                    return;
            }
        }
    };
    void processDeferred() {
        .removeCallbacks();
        .post();
    }

    
Cancel all pending requests associated with the request group

Parameters:
group
    public void cancelAll(Object group) {
        FutureSet members;
        synchronized (this) {
            members = .remove(group);
        }
        if (members == null)
            return;
        for (Future futuremembers.keySet()) {
            if (future != null)
                future.cancel();
        }
    }
    void addFutureInFlight(Future futureObject group) {
        if (group == null || future == null || future.isDone() || future.isCancelled())
            return;
        FutureSet members;
        synchronized (this) {
            members = .get(group);
            if (members == null) {
                members = new FutureSet();
                .put(groupmembers);
            }
        }
        members.put(futuretrue);
    }

    
Cancel all pending requests
    public void cancelAll() {
        ArrayList<Objectgroups;
        synchronized (this) {
            groups = new ArrayList<Object>(.keySet());
        }
        for (Object groupgroups)
            cancelAll(group);
    }

    
Cancel all pending requests associated with the given context

Parameters:
context
    public void cancelAll(Context context) {
        cancelAll((Object)context);
    }
    public int getPendingRequestCount(Object group) {
        synchronized (this) {
            FutureSet members = .get(group);
            if (members == null)
                return 0;
            int ret = 0;
            for (Future futuremembers.keySet()) {
                if (!future.isCancelled() && !future.isDone())
                    ret++;
            }
            return ret;
        }
    }
    public void dump() {
        .dump();
        Log.i("Pending bitmaps: " + .size());
        Log.i("Groups: " + .size());
        for (FutureSet futures.values()) {
            Log.i("Group size: " + futures.size());
        }
    }

    
Get the application Context object in use by this Ion instance

Returns:
    public Context getContext() {
        return ;
    }
    static class FutureSet extends WeakHashMap<FutureBoolean> {
    }
    // maintain a list of futures that are in being processed, allow for bulk cancellation
    private void addCookieMiddleware() {
    }

    
Get or put an item from the cache

Returns:
    public FileCacheStore cache(String key) {
        return new FileCacheStore(this.getFileCache(), key);
    }
    public FileCache getCache() {
        return .getFileCache();
    }

    
Get or put an item in the persistent store

Returns:
    public FileCacheStore store(String key) {
        return new FileCacheStore(thiskey);
    }
    public FileCache getStore() {
        return ;
    }
    public String getName() {
        return ;
    }

    
Get the Cookie middleware that is attached to the AsyncHttpClient instance.

Returns:
        return ;
    }
        return ;
    }

    
Get the AsyncHttpClient object in use by this Ion instance

Returns:
    public AsyncHttpClient getHttpClient() {
        return ;
    }

    
Get the AsyncServer reactor in use by this Ion instance

Returns:
    public AsyncServer getServer() {
        return .getServer();
    }
    public class Config {
        public HttpLoader getHttpLoader() {
            return ;
        }
        public VideoLoader getVideoLoader() {
            return ;
        }
        public PackageIconLoader getPackageIconLoader() {
            return ;
        }
        public ContentLoader getContentLoader() {
            return ;
        }
        public FileLoader getFileLoader() {
            return ;
        }
        public ResponseCacheMiddleware getResponseCache() {
            return ;
        }
        public SSLContext createSSLContext(String algorithmthrows NoSuchAlgorithmException {
            .initialize();
            return SSLContext.getInstance(algorithm);
        }

        
Get the Gson object in use by this Ion instance. This can be used to customize serialization and deserialization from java objects.

Returns:
        public synchronized Gson getGson() {
            if ( == null)
                 = new Gson();
            return ;
        }

        
Set the log level for all requests made by Ion.

Parameters:
logtag
logLevel
Returns:
        public Config setLogging(String logtagint logLevel) {
            Ion.this. = logtag;
            Ion.this. = logLevel;
            return this;
        }

        
Route all http requests through the given proxy.

Parameters:
host
port
        public void proxy(String hostint port) {
            .getSocketMiddleware().enableProxy(hostport);
        }

        
Route all https requests through the given proxy. Note that https proxying requires that the Android device has the appropriate root certificate installed to function properly.

Parameters:
host
port
        public void proxySecure(String hostint port) {
            .getSSLSocketMiddleware().enableProxy(hostport);
        }

        
Disable routing of http requests through a previous provided proxy
        public void disableProxy() {
            .getSocketMiddleware().disableProxy();
        }

        
Disable routing of https requests through a previous provided proxy
        public void disableSecureProxy() {
            .getSocketMiddleware().disableProxy();
        }

        
Set the Gson object in use by this Ion instance. This can be used to customize serialization and deserialization from java objects.

Parameters:
gson
        public void setGson(Gson gson) {
            Ion.this. = gson;
        }
            @Override
            public AsyncHttpRequest createAsyncHttpRequest(Uri uriString methodHeaders headers) {
                AsyncHttpRequest request = new AsyncHttpRequest(urimethodheaders);
                if (!TextUtils.isEmpty())
                    request.getHeaders().set("User-Agent");
                return request;
            }
        };
            return ;
        }
        public Config setAsyncHttpRequestFactory(AsyncHttpRequestFactory asyncHttpRequestFactory) {
            this. = asyncHttpRequestFactory;
            return this;
        }
        public String userAgent() {
            return ;
        }
        public Config userAgent(String userAgent) {
            Ion.this. = userAgent;
            return this;
        }
        public Config addLoader(int indexLoader loader) {
            .add(indexloader);
            return this;
        }
        public Config insertLoader(Loader loader) {
            .add(0, loader);
            return this;
        }
        public Config addLoader(Loader loader) {
            .add(loader);
            return this;
        }
        public List<LoadergetLoaders() {
            return ;
        }
    }
    public Config configure() {
        return ;
    }

    
Return the bitmap cache used by this Ion instance

Returns:
    public IonBitmapCache getBitmapCache() {
        return ;
    }
New to GrepCode? Check out our FAQ X