Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion;
  
  import java.io.File;
  import java.net.URI;
  import java.util.HashMap;
  import java.util.List;
 
 import  android.content.Context;
 import  android.os.Build;
 import  android.os.Handler;
 import  android.os.Looper;
 import  android.text.TextUtils;
 import  android.util.Log;
 import  android.view.ContextThemeWrapper;
 import  android.widget.ImageView;
 
Created by koush on 5/21/13.
 
 public class Ion {
     public static final Handler mainHandler = new Handler(Looper.getMainLooper());
     private static ExecutorService singleExecutorService  = Runtime.getRuntime().availableProcessors() < 2 ? null : Executors.newFixedThreadPool(1);
 
     // todo: make this static by moving the server's executor service to static
         ExecutorService executorService = ;
         if (executorService == null) {
             executorService = getServer().getExecutorService();
         }
         return executorService;
     }


    
Get the default Ion object instance and begin building a request with the given uri

Parameters:
context
uri
Returns:
 
     public static Builders.Any.B with(Context contextString uri) {
         return getDefault(context).build(contexturi);
     }

    
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);
     }

    
Get the default Ion object instance and begin building an operation on the given file

Parameters:
context
file
Returns:
 
     public static FutureBuilder with(Context contextFile file) {
         return getDefault(context).build(contextfile);
     }

    
Begin building an operation on the given file

Parameters:
context
file
Returns:
 
     public FutureBuilder build(Context contextFile file) {
         return new IonRequestBuilder(contextthis).load(file);
     }

    
Get the default Ion instance

Parameters:
context
Returns:
    public static Ion getDefault(Context context) {
        return getInstance(context"ion");
    }
    private static HashMap<StringIoninstances = new HashMap<StringIon>();

    
Get the given Ion instance by name

Parameters:
context
name
Returns:
    public static Ion getInstance(Context contextString name) {
        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.ImageView.F<? extends Builders.ImageView.F<?>> with(ImageView imageView) {
        Ion ion = getDefault(imageView.getContext());
        return ion.build(imageView);
    }

    
Begin building a request with the given uri

Parameters:
context
uri
Returns:
    public Builders.Any.B build(Context contextString uri) {
        return new IonRequestBuilder(contextthis).load(uri);
    }

    
Begin building a request

Parameters:
context
Returns:
    public LoadBuilder<Builders.Any.Bbuild(Context context) {
        return new IonRequestBuilder(contextthis);
    }
Create a builder that can be used to build an network request

Parameters:
imageView
Returns:
    public Builders.ImageView.F<? extends Builders.ImageView.F<?>> build(ImageView imageView) {
        if (Thread.currentThread() != Looper.getMainLooper().getThread())
            throw new IllegalStateException("must be called from UI thread");
        .reset();
        . = this;
        return .withImageView(imageView);
    }

    
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();
        }
    }

    
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() {
    }

    
Disable routing of https requests through a previous provided proxy
    public void disableSecureProxy() {
    }
    void removeFutureInFlight(Future futureObject group) {
    }
    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() {
    }
    public HttpLoader getHttpLoader() {
        return ;
    }
    public ContentLoader getContentLoader() {
        return ;
    }
    public FileLoader getFileLoader() {
        return ;
    }
        return ;
    }
    String name;
    public String getName() {
        return ;
    }
    Context context;
    private Ion(Context contextString name) {
         = new AsyncHttpClient(new AsyncServer());
        this. = context = context.getApplicationContext();
        this. = name;
        try {
             = ResponseCacheMiddleware.addCache(new File(context.getCacheDir(), name), 10L * 1024L * 1024L);
        }
        catch (Exception e) {
            IonLog.w("unable to set up response cache"e);
        }
        // TODO: Support pre GB?
        if (Build.VERSION.SDK_INT >= 9)
            addCookieMiddleware();
         = new IonBitmapCache(this);
        configure()
        .addLoader(new PackageIconLoader())
        .addLoader( = new HttpLoader())
        .addLoader( = new ContentLoader())
        .addLoader( = new FileLoader());
    }

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

Returns:
        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 static class Config {
        private Gson gson = new Gson();
        
Get the Gson object in use by this Ion instance. This can be used to customize serialization and deserialization from java objects.

Returns:
        public Gson getGson() {
            return ;
        }

        
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) {
            this. = gson;
        }
            @Override
            public AsyncHttpRequest createAsyncHttpRequest(URI uriString methodRawHeaders headers) {
                if (!TextUtils.isEmpty())
                    headers.set("User-Agent");
                return new AsyncHttpRequest(urimethodheaders);
            }
        };
            return ;
        }
        public Config setAsyncHttpRequestFactory(AsyncHttpRequestFactory asyncHttpRequestFactory) {
            this. = asyncHttpRequestFactory;
            return this;
        }
        private String userAgent;
        public String userAgent() {
            return ;
        }
        public Config userAgent(String userAgent) {
            this. = userAgent;
            return this;
        }
        ArrayList<Loaderloaders = new ArrayList<Loader>();
        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 ;
        }
    }
    String LOGTAG;
    int logLevel;
    
Set the log level for all requests made by Ion.

Parameters:
logtag
logLevel
    public void setLogging(String logtagint logLevel) {
         = logtag;
        this. = logLevel;
    }
    Config config = new Config();
    public Config configure() {
        return ;
    }
    IonBitmapCache bitmapCache;
    
Return the bitmap cache used by this Ion instance

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