Example usage for android.content ContentResolver getStreamTypes

List of usage examples for android.content ContentResolver getStreamTypes

Introduction

In this page you can find the example usage for android.content ContentResolver getStreamTypes.

Prototype

public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter) 

Source Link

Document

Query for the possible MIME types for the representations the given content URL can be returned when opened as as stream with #openTypedAssetFileDescriptor .

Usage

From source file:eu.faircode.netguard.ActivitySettings.java

private void handleImport(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override/*from w w w.  ja va  2s. co m*/
        protected Throwable doInBackground(Object... objects) {
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                xmlImport(in);
                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    ServiceSinkhole.reloadStats("import", ActivitySettings.this);
                    // Update theme, request permissions
                    recreate();
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:eu.faircode.netguard.ActivitySettings.java

private void handleHosts(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override//  w  w  w .j a  v a  2  s  . com
        protected Throwable doInBackground(Object... objects) {
            File hosts = new File(getFilesDir(), "hosts.txt");

            FileOutputStream out = null;
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                out = new FileOutputStream(hosts);

                int len;
                long total = 0;
                byte[] buf = new byte[4096];
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                    total += len;
                }
                Log.i(TAG, "Copied bytes=" + total);

                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(ActivitySettings.this);
                    String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
                    prefs.edit().putString("hosts_last_import", last).apply();

                    if (running) {
                        getPreferenceScreen().findPreference("hosts_import")
                                .setSummary(getString(R.string.msg_import_last, last));
                        Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    }

                    ServiceSinkhole.reload("hosts import", ActivitySettings.this, false);
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void handleImport(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override/*from w  w w  .  j ava  2 s . c  o  m*/
        protected Throwable doInBackground(Object... objects) {
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                xmlImport(in);
                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    ServiceSinkhole.reloadStats("import", ActivitySettings.this);
                    // Update theme, request permissions
                    recreate();
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void handleHosts(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override/*  w w  w  .j av a 2 s  . com*/
        protected Throwable doInBackground(Object... objects) {
            File hosts = new File(getFilesDir(), "hosts.txt");

            FileOutputStream out = null;
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                out = new FileOutputStream(hosts);

                int len;
                long total = 0;
                byte[] buf = new byte[4096];
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                    total += len;
                }
                Log.i(TAG, "Copied bytes=" + total);

                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(ActivitySettings.this);
                    String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
                    prefs.edit().putString("hosts_last_import", last).apply();

                    if (running) {
                        getPreferenceScreen().findPreference("hosts_import")
                                .setSummary(getString(R.string.msg_import_last, last));
                        Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    }

                    ServiceSinkhole.reload("hosts import", ActivitySettings.this);
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}