Example usage for android.content Context getAssets

List of usage examples for android.content Context getAssets

Introduction

In this page you can find the example usage for android.content Context getAssets.

Prototype

public abstract AssetManager getAssets();

Source Link

Document

Returns an AssetManager instance for the application's package.

Usage

From source file:com.google.android.apps.iosched.io.StubHttpClient.java

/**
 * Build a stub {@link HttpResponse}, probably for use with
 * {@link #setResponse(HttpResponse)}.//from ww w . ja  va2  s.  com
 *
 * @param statusCode {@link HttpStatus} code to use.
 * @param assetName Name of asset that should be included as a single
 *            {@link HttpEntity} to be included in the response, loaded
 *            through {@link AssetManager}.
 */
public static HttpResponse buildResponse(int statusCode, String assetName, Context context) throws IOException {
    final StatusLine status = new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, null);
    final HttpResponse response = new BasicHttpResponse(status);
    if (assetName != null) {
        final InputStream entity = context.getAssets().open(assetName);
        response.setEntity(new InputStreamEntity(entity, entity.available()));
    }
    return response;
}

From source file:org.ametro.app.ApplicationEx.java

public static void extractEULA(Context context) {
    if (!Constants.EULA_FILE.exists()) {
        try {/*from w  w  w.  j  a v  a 2  s.c o  m*/
            FileUtil.writeToStream(context.getAssets().open("gpl.html"),
                    new FileOutputStream(Constants.EULA_FILE), true);
        } catch (Exception e) {
            // do nothing!
        }
    }
}

From source file:org.dolphinemu.dolphinemu.utils.DirectoryInitialization.java

private static void copyAssetFolder(String assetFolder, File outputFolder, Boolean overwrite, Context context) {
    Log.verbose("[DirectoryInitialization] Copying Folder " + assetFolder + " to " + outputFolder);

    try {/*from   w w w  .ja  v a  2 s.co  m*/
        boolean createdFolder = false;
        for (String file : context.getAssets().list(assetFolder)) {
            if (!createdFolder) {
                outputFolder.mkdir();
                createdFolder = true;
            }
            copyAssetFolder(assetFolder + File.separator + file, new File(outputFolder, file), overwrite,
                    context);
            copyAsset(assetFolder + File.separator + file, new File(outputFolder, file), overwrite, context);
        }
    } catch (IOException e) {
        Log.error("[DirectoryInitialization] Failed to copy asset folder: " + assetFolder + e.getMessage());
    }
}

From source file:com.openatlas.android.initializer.BundleParser.java

public static void parser(Context mContext) {
    InputStream is;//from   ww  w.j  a va2  s.  c  o m
    ArrayList<BundleInfoList.BundleInfo> bundleInfos = new ArrayList<BundleInfoList.BundleInfo>();
    ArrayList<BundleListing.Component> mComponents = new ArrayList<BundleListing.Component>();

    try {
        is = mContext.getAssets().open("bundle-info.json");
        int size = is.available();

        // Read the entire asset into a local byte buffer.  
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        JSONArray jsonArray = new JSONArray(new String(buffer));
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject tmp = jsonArray.optJSONObject(index);
            BundleInfo mBundleInfo = new BundleInfo();
            BundleListing.Component mComponent = new BundleListing.Component();
            mBundleInfo.bundleName = tmp.optString("pkgName");
            mBundleInfo.hasSO = tmp.optBoolean("hasSO");
            mComponent.setPkgName(mBundleInfo.bundleName);
            mComponent.setHasSO(mBundleInfo.hasSO);
            ArrayList<String> components = new ArrayList<String>();

            JSONArray activities = tmp.optJSONArray("activities");
            for (int j = 0; j < activities.length(); j++) {
                components.add(activities.getString(j));
                mComponent.getActivities().add(activities.getString(j));
            }

            JSONArray receivers = tmp.optJSONArray("receivers");
            for (int j = 0; j < receivers.length(); j++) {
                components.add(receivers.getString(j));
                mComponent.getReceivers().add(receivers.getString(j));
            }

            JSONArray services = tmp.optJSONArray("services");
            for (int j = 0; j < services.length(); j++) {
                components.add(services.getString(j));
                mComponent.getServices().add(services.getString(j));
            }

            JSONArray contentProviders = tmp.optJSONArray("contentProviders");
            for (int j = 0; j < contentProviders.length(); j++) {
                components.add(contentProviders.getString(j));
                mComponent.getContentProviders().add(contentProviders.getString(j));

            }

            JSONArray dependencys = tmp.optJSONArray("dependency");
            for (int j = 0; j < dependencys.length(); j++) {
                mBundleInfo.DependentBundles.add(dependencys.getString(j));
                mComponent.getDependency().add(dependencys.getString(j));

            }
            mBundleInfo.Components = components;
            bundleInfos.add(mBundleInfo);
            mComponents.add(mComponent);
            BundleInfoList.getInstance().init(bundleInfos);
        }
        BundleListing.getInstance().setBundles(mComponents);
        //BundleInfoList.getInstance().init(bundleInfos);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:gov.wa.wsdot.android.wsdot.ui.traveltimes.TravelTimesFragment.java

public static View makeTravelTimeView(TravelTimeEntity time, Context context) {

    Typeface tfb = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
    LayoutInflater li = LayoutInflater.from(context);
    View cv = li.inflate(R.layout.trip_view, null);

    // set via label
    ((TextView) cv.findViewById(R.id.title)).setText("Via " + time.getVia());

    TextView currentTimeTextView = cv.findViewById(R.id.current_value);
    currentTimeTextView.setTypeface(tfb);

    // set updated
    ((TextView) cv.findViewById(R.id.updated))
            .setText(ParserUtils.relativeTime(time.getUpdated(), "yyyy-MM-dd HH:mm a", false));

    if (time.getStatus().toLowerCase().equals("closed")) {
        currentTimeTextView.setText("Closed");
        currentTimeTextView.setTextColor(Color.RED);
        cv.findViewById(R.id.subtitle).setVisibility(View.GONE);
    } else {/*from   www .j  av a  2  s.  c  o m*/

        // set distance and avg time text view
        String average_time;
        String distance = time.getDistance();
        int average = time.getAverage();

        if (average == 0) {
            average_time = "Not Available";
        } else {
            average_time = average + " min";
        }

        ((TextView) cv.findViewById(R.id.subtitle)).setText(distance + " / " + average_time);

        // set current travel time. Set to closed if status is closed.
        int current = time.getCurrent();

        if (current < average) {
            currentTimeTextView.setTextColor(0xFF008060);
        } else if ((current > average) && (average != 0)) {
            currentTimeTextView.setTextColor(Color.RED);
        } else {
            currentTimeTextView.setTextColor(Color.BLACK);
        }

        currentTimeTextView.setText(current + " min");

    }
    return cv;

}

From source file:com.dv.Utils.Tools.java

/**
 * getTypeFace description/*from w  ww.ja va2s.  c  o m*/
 * Description:
 *
 * @param context
 * @return
 */
public static Typeface getTypeFace(Context context) {
    Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/STHeiti-Light.ttc");

    return typeFace;
}

From source file:Main.java

/**
 * This method copies a binary from asset directory to working directory.
 * //from w ww  .  jav  a2s .c  o m
 * @param assetPath
 * @param localPath
 * @param context
 * @return true == copied, false == text busy
 */
private static boolean copyFile(String assetPath, String localPath, Context context) {
    try {
        // detect architecture
        if (isARM())
            assetPath += "_arm";
        else if (isX86())
            assetPath += "_x86";
        else if (isMIPS())
            assetPath += "_mips";
        else
            assetPath += "_arm";

        InputStream binary = context.getAssets().open(assetPath);
        FileOutputStream execute = new FileOutputStream(localPath);

        int read = 0;
        byte[] buffer = new byte[4096];

        while ((read = binary.read(buffer)) > 0) {
            execute.write(buffer, 0, read);
        }

        execute.close();
        binary.close();

        execute = null;
        binary = null;

    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static File writeAssetToFile(Context ctx, String assetName, String destinationFile) throws IOException {
    File destination = new File(destinationFile);
    if (!destination.getParentFile().exists())
        destination.getParentFile().mkdirs();
    if (destination.exists())
        destination.delete();/*from  ww w .  j  a  v  a2  s .com*/
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = ctx.getAssets().open(assetName);
        fos = new FileOutputStream(destination);
        int read;
        byte[] buf = new byte[65536];
        while ((read = is.read(buf, 0, buf.length)) != -1) {
            fos.write(buf, 0, read);
        }
    } finally {
        if (is != null)
            is.close();
        if (fos != null)
            fos.close();
    }
    return destination;
}

From source file:Main.java

private static void loadICUData(Context context, File destDir) throws IOException {
    OutputStream out = null;//  ww  w. j  a  va  2s.  c  om
    ZipInputStream in = null;
    File icuDir = new File(destDir, "icu");
    File icuDataFile = new File(icuDir, "icudt53l.dat");
    try {
        if (!icuDir.exists())
            icuDir.mkdirs();
        if (!icuDataFile.exists()) {
            in = new ZipInputStream(context.getAssets().open("icudt53l.zip"));
            in.getNextEntry();
            out = new FileOutputStream(icuDataFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    } catch (IOException e) {
        if (icuDataFile.exists())
            icuDataFile.delete();
        throw e;
    } finally {
        if (in != null)
            in.close();
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:org.anhonesteffort.flock.sync.AppSecureSocketFactory.java

private static SSLContext createAppStoreSSLContext(Context appContext, boolean useFlockTrustStore)
        throws HttpClientError {
    if (appContext == null)
        throw new HttpClientError("application context is null :(");

    KeyStore trustStore;/*from  w w w .j  a  va 2 s  .  com*/

    try {

        if (useFlockTrustStore) {
            AssetManager assetManager = appContext.getAssets();
            InputStream keyStoreInputStream = assetManager.open("flock.store");
            trustStore = KeyStore.getInstance("BKS");

            trustStore.load(keyStoreInputStream, "owsflock".toCharArray());
        } else {
            trustStore = KeyStore.getInstance("AndroidCAStore");
            trustStore.load(null, null);
        }

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);

        return sslContext;

    } catch (Exception e) {
        Log.e(TAG, "createAppStoreSSLContext() - flock store? " + useFlockTrustStore, e);
        throw new HttpClientError(e.toString());
    }
}