Example usage for android.net.http HttpResponseCache install

List of usage examples for android.net.http HttpResponseCache install

Introduction

In this page you can find the example usage for android.net.http HttpResponseCache install.

Prototype

public static synchronized HttpResponseCache install(File directory, long maxSize) throws IOException 

Source Link

Document

Creates a new HTTP response cache and sets it as the system default cache.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private static HttpResponseCache installHttpResponseCache(final File cacheDir) throws IOException {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache == null) {
        final long maxSize = calculateDiskCacheSize(cacheDir);
        cache = HttpResponseCache.install(cacheDir, maxSize);
    }/*from w w w .  j a  v  a 2  s . com*/
    return cache;
}

From source file:in.rab.ordboken.Ordboken.java

private Ordboken(Context context) {
    mContext = context;//w  ww. java  2  s . c  o m

    try {
        // Ends up only fully caching the search results. Documents use ETag
        // and are conditionally cached.
        HttpResponseCache.install(new File(context.getCacheDir(), "ordboken"), 1024 * 1024);
    } catch (IOException e) {
        // Oh well...
    }

    mPrefs = context.getSharedPreferences("ordboken", Context.MODE_PRIVATE);
    mLastWhere = Where.valueOf(mPrefs.getString("lastWhere", Where.MAIN.toString()));
    mLastWhat = mPrefs.getString("lastWhat", "ordbok");

    mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    mNeClient = new NeClient(NeClient.Auth.BASIC);
    mNeClient.setPersistentAuthData(mPrefs.getString("persistentAuthData", null));
    mNeClient.setUsername(mPrefs.getString("username", null));
    mNeClient.setPassword(mPrefs.getString("password", null));
}

From source file:com.fastbootmobile.encore.app.OmniMusic.java

@Override
public void onCreate() {
    super.onCreate();

    // We need to filter here whether we're initializing the main app or an aux attached plugin
    String appName = Utils.getAppNameByPID(this, android.os.Process.myPid());
    final String process = appName.substring(appName.indexOf(':') + 1);

    Sentry.setCaptureListener(new Sentry.SentryEventCaptureListener() {
        @Override//from   w  w w. j a  va 2s  . c  o m
        public Sentry.SentryEventBuilder beforeCapture(Sentry.SentryEventBuilder sentryEventBuilder) {
            JSONObject tags = sentryEventBuilder.getTags();
            try {
                tags.put("OS", "Android " + Build.VERSION.RELEASE);
                tags.put("OSCodename", Build.VERSION.CODENAME);
                tags.put("Device", Build.DEVICE);
                tags.put("Model", Build.MODEL);
                tags.put("Manufacturer", Build.MANUFACTURER);
                tags.put("AppVersionCode", String.valueOf(BuildConfig.VERSION_CODE));
                tags.put("AppVersionName", BuildConfig.VERSION_NAME);
                tags.put("AppFlavor", BuildConfig.FLAVOR);
            } catch (JSONException e) {
                Log.e(TAG, "Failed to put a tag into Sentry", e);
            }

            sentryEventBuilder.addModule(process, BuildConfig.VERSION_NAME);
            return sentryEventBuilder;
        }
    });
    Sentry.init(this,
            "https://4dc1acbdb1cb423282e2a59f553e1153:9415087b9e1348c3ba4bed44be599f6a@sentry.fastboot.mobi/2");

    // Setup LeakCanary
    mRefWatcher = LeakCanary.install(this);

    if (PROCESS_APP.equals(process)) {
        // Setup the plugins system
        ProviderAggregator.getDefault().setContext(getApplicationContext());
        PluginsLookup.getDefault().initialize(getApplicationContext());

        /**
         * Note about the cache and EchoNest: The HTTP cache would sometimes cache request
         * we didn't want (such as status query for Taste Profile update). We're using
         * a hacked jEN library that doesn't cache these requests.
         */
        // Setup network cache
        try {
            final File httpCacheDir = new File(getCacheDir(), "http");
            final long httpCacheSize = 100 * 1024 * 1024; // 100 MiB
            final HttpResponseCache cache = HttpResponseCache.install(httpCacheDir, httpCacheSize);

            Log.i(TAG, "HTTP Cache size: " + cache.size() / 1024 / 1024 + "MB");
        } catch (IOException e) {
            Log.w(TAG, "HTTP response cache installation failed", e);
        }

        // Setup image cache
        ImageCache.getDefault().initialize(getApplicationContext());

        // Setup Automix system
        AutoMixManager.getDefault().initialize(getApplicationContext());

        // Setup custom fonts
        CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);
    }
}

From source file:com.morlins.artists.MainActivity.java

private void installCache(long cacheSize) {
    final File httpCacheDir = new File(this.getCacheDir(), "http");
    try {/* w ww.  j a v  a2s .co  m*/
        Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class)
                .invoke(null, httpCacheDir, cacheSize);
        Log.v("cache", "cache set up");
    } catch (Exception httpResponseCacheNotAvailable) {
        Log.v("cache",
                "android.net.http.HttpResponseCache not available, "
                        + "probably because we're running on a pre-ICS version of Android. "
                        + "Using com.integralblue.httpresponsecache.HttpHttpResponseCache.");
    }
    try {
        HttpResponseCache.install(httpCacheDir, cacheSize);
    } catch (Exception e) {
        Log.v("cache", "Failed to set up ");
        e.printStackTrace();
    }
}

From source file:com.androchill.call411.MainActivity.java

private void enableHttpResponseCache() {
    try {/*from  ww w.j ava  2 s  . co m*/
        long httpCacheSize = 25 * 1024 * 1024; // 10 MiB
        File httpCacheDir = new File(getCacheDir(), "http");
        HttpResponseCache.install(httpCacheDir, httpCacheSize);
    } catch (Exception httpResponseCacheNotAvailable) {
        Log.d("MainActivity", "HTTP response cache is unavailable.");
    }
}

From source file:com.irccloud.android.activity.UploadsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (ColorFormatter.file_uri_template != null)
        template = UriTemplate.fromTemplate(ColorFormatter.file_uri_template);
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 21) {
        Bitmap cloud = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        setTaskDescription(new ActivityManager.TaskDescription(getResources().getString(R.string.app_name),
                cloud, 0xFFF2F7FC));/*  ww w  . j  a v a2  s.c o m*/
        cloud.recycle();
    }

    if (Build.VERSION.SDK_INT >= 14) {
        try {
            java.io.File httpCacheDir = new java.io.File(getCacheDir(), "http");
            long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
            HttpResponseCache.install(httpCacheDir, httpCacheSize);
        } catch (IOException e) {
            Log.i("IRCCloud", "HTTP response cache installation failed:" + e);
        }
    }
    setContentView(R.layout.ignorelist);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
        getSupportActionBar().setElevation(0);
    }

    if (savedInstanceState != null && savedInstanceState.containsKey("cid")) {
        cid = savedInstanceState.getInt("cid");
        to = savedInstanceState.getString("to");
        msg = savedInstanceState.getString("msg");
        page = savedInstanceState.getInt("page");
        File[] files = (File[]) savedInstanceState.getSerializable("adapter");
        for (File f : files) {
            adapter.addFile(f);
        }
        adapter.notifyDataSetChanged();
    }

    footer = getLayoutInflater().inflate(R.layout.messageview_header, null);
    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(adapter);
    listView.addFooterView(footer);
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (canLoadMore && firstVisibleItem + visibleItemCount > totalItemCount - 4) {
                canLoadMore = false;
                new FetchFilesTask().execute((Void) null);
            }
        }
    });
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            final File f = (File) adapter.getItem(i);

            AlertDialog.Builder builder = new AlertDialog.Builder(UploadsActivity.this);
            builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB);
            final View v = getLayoutInflater().inflate(R.layout.dialog_upload, null);
            final EditText messageinput = (EditText) v.findViewById(R.id.message);
            messageinput.setText(msg);
            final ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);

            v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (messageinput.hasFocus()) {
                        v.post(new Runnable() {
                            @Override
                            public void run() {
                                v.scrollTo(0, v.getBottom());
                            }
                        });
                    }
                }
            });

            if (f.mime_type.startsWith("image/")) {
                try {
                    thumbnail.setImageBitmap(f.image);
                    thumbnail.setVisibility(View.VISIBLE);
                    thumbnail.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(UploadsActivity.this, ImageViewerActivity.class);
                            i.setData(Uri.parse(f.url));
                            startActivity(i);
                        }
                    });
                    thumbnail.setClickable(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                thumbnail.setVisibility(View.GONE);
            }

            ((TextView) v.findViewById(R.id.filesize)).setText(f.metadata);
            v.findViewById(R.id.filename).setVisibility(View.GONE);
            v.findViewById(R.id.filename_heading).setVisibility(View.GONE);

            builder.setTitle("Send A File To " + to);
            builder.setView(v);
            builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String message = messageinput.getText().toString();
                    if (message.length() > 0)
                        message += " ";
                    message += f.url;

                    dialog.dismiss();
                    if (getParent() == null) {
                        setResult(Activity.RESULT_OK);
                    } else {
                        getParent().setResult(Activity.RESULT_OK);
                    }
                    finish();

                    NetworkConnection.getInstance().say(cid, to, message);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            AlertDialog d = builder.create();
            d.setOwnerActivity(UploadsActivity.this);
            d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            d.show();
        }
    });
}