Example usage for android.content Context getFileStreamPath

List of usage examples for android.content Context getFileStreamPath

Introduction

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

Prototype

public abstract File getFileStreamPath(String name);

Source Link

Document

Returns the absolute path on the filesystem where a file created with #openFileOutput is stored.

Usage

From source file:Main.java

public static boolean checkIfDllInstalled(Context context) {
    boolean isInstalled = false;
    try {/*from w  ww . java 2  s .  c om*/
        InputStream inputStream = context.getAssets().open("Disdll.dll");
        File file = context.getFileStreamPath("Disdll.dll");
        if (file != null) {
            if (file.length() == inputStream.available()) {
                isInstalled = true;
            }
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return isInstalled;
}

From source file:org.wahtod.wififixer.utility.LogUtil.java

private static boolean hasStackTrace(Context context) {
    return context.getFileStreamPath(DefaultExceptionHandler.EXCEPTIONS_FILENAME).exists();
}

From source file:org.rapidandroid.ApplicationGlobals.java

/**
 * Creates a global settings file if does not exist. For default values, all interactive features disabled.
 * @param context//  w w  w .  j  ava2 s .  c o  m
 */
public static void checkGlobals(Context context) {
    File f = context.getFileStreamPath(SETTINGS_FILE);
    if (!f.exists()) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        saveGlobalSettings(context, false, false, false, "Message parsing in progress", false,
                "Message parsed successfully, thank you", false,
                "Unable to understand your message, please try again");

    }
}

From source file:com.github.ajasmin.telususageandroidwidget.TelusReportFetcher.java

public static void retriveUsageSummaryData(int appWidgetId)
        throws InvalidCredentialsException, NetworkErrorException {
    Context context = MyApp.getContext();
    String fileName = "" + appWidgetId;

    TelusWidgetPreferences.PreferencesData prefs = TelusWidgetPreferences.getPreferences(appWidgetId);

    if (!context.getFileStreamPath(fileName).exists()
            || System.currentTimeMillis() - prefs.lastUpdateTime > CACHE_LIFETIME) {
        fetchFromTelusSite(prefs);/*from   ww  w.j  a v a2  s.c  o m*/
    }
}

From source file:org.openjira.jira.utils.LoadImageAsync.java

public static FileInputStream openStream(Context context, String tag, String uuid) throws Exception {
    File fDir = context.getFileStreamPath(tag);
    String sDir = fDir.getAbsolutePath();
    File file = new File(sDir + "/" + uuid + ".png");
    FileInputStream is = new FileInputStream(file);
    return is;//  w w  w. j  av  a 2  s.co  m
}

From source file:org.openjira.jira.utils.LoadImageAsync.java

public static boolean imageExists(Context ctx, String tag, String uuid) {
    // TODO Auto-generated method stub
    String fileName = tag + "_" + uuid + ".png";
    FileInputStream fis = null;/*from w  w w . j a v  a2  s. c o m*/

    File fDir = ctx.getFileStreamPath(tag);
    String sDir = fDir.getAbsolutePath();
    File file = new File(sDir + "/" + uuid + ".png");

    try {
        fis = new FileInputStream(file);
        fis.close();
    } catch (FileNotFoundException e1) {
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}

From source file:com.xiaomi.account.utils.SysHelper.java

public static Bitmap getImageBitmap(Context context, String imageName) {
    File cachedImageFile = context.getFileStreamPath(imageName);
    if (cachedImageFile.isFile() && cachedImageFile.exists()) {
        return android.graphics.BitmapFactory.decodeFile(cachedImageFile.getAbsolutePath());
    }/*from w  w  w.ja  va 2 s  .  c  om*/
    return null;
}

From source file:org.wso2.emm.agent.utils.CommonUtils.java

/**
 * Generates keys, CSR and certificates for the devices.
 * @param context - Application context.
 * @param listener - DeviceCertCreationListener which provide device .
 *//*from  w w w.j  a  v  a2 s. co  m*/
public static void generateDeviceCertificate(final Context context, final DeviceCertCreationListener listener)
        throws AndroidAgentException {

    if (context.getFileStreamPath(Constants.DEVICE_CERTIFCATE_NAME).exists()) {
        try {
            listener.onDeviceCertCreated(
                    new BufferedInputStream(context.openFileInput(Constants.DEVICE_CERTIFCATE_NAME)));
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }
    } else {

        try {
            ServerConfig utils = new ServerConfig();
            final KeyPair deviceKeyPair = KeyPairGenerator.getInstance(Constants.DEVICE_KEY_TYPE)
                    .generateKeyPair();
            X500Principal subject = new X500Principal(Constants.DEVICE_CSR_INFO);
            PKCS10CertificationRequest csr = new PKCS10CertificationRequest(Constants.DEVICE_KEY_ALGO, subject,
                    deviceKeyPair.getPublic(), null, deviceKeyPair.getPrivate());

            EndPointInfo endPointInfo = new EndPointInfo();
            endPointInfo.setHttpMethod(org.wso2.emm.agent.proxy.utils.Constants.HTTP_METHODS.POST);
            endPointInfo.setEndPoint(utils.getAPIServerURL(context) + Constants.SCEP_ENDPOINT);
            endPointInfo.setRequestParams(Base64.encodeToString(csr.getEncoded(), Base64.DEFAULT));

            new APIController().invokeAPI(endPointInfo, new APIResultCallBack() {
                @Override
                public void onReceiveAPIResult(Map<String, String> result, int requestCode) {
                    try {
                        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                        InputStream in = new ByteArrayInputStream(
                                Base64.decode(result.get("response"), Base64.DEFAULT));
                        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        KeyStore keyStore = KeyStore.getInstance("PKCS12");
                        keyStore.load(null);
                        keyStore.setKeyEntry(Constants.DEVICE_CERTIFCATE_ALIAS,
                                (Key) deviceKeyPair.getPrivate(),
                                Constants.DEVICE_CERTIFCATE_PASSWORD.toCharArray(),
                                new java.security.cert.Certificate[] { cert });
                        keyStore.store(byteArrayOutputStream,
                                Constants.DEVICE_CERTIFCATE_PASSWORD.toCharArray());
                        FileOutputStream outputStream = context.openFileOutput(Constants.DEVICE_CERTIFCATE_NAME,
                                Context.MODE_PRIVATE);
                        outputStream.write(byteArrayOutputStream.toByteArray());
                        byteArrayOutputStream.close();
                        outputStream.close();
                        try {
                            listener.onDeviceCertCreated(new BufferedInputStream(
                                    context.openFileInput(Constants.DEVICE_CERTIFCATE_NAME)));
                        } catch (FileNotFoundException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    } catch (CertificateException e) {
                        Log.e(TAG, e.getMessage());
                    } catch (KeyStoreException e) {
                        e.printStackTrace();
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }, Constants.SCEP_REQUEST_CODE, context, true);

        } catch (NoSuchAlgorithmException e) {
            throw new AndroidAgentException("No algorithm for key generation", e);
        } catch (SignatureException e) {
            throw new AndroidAgentException("Invalid Signature", e);
        } catch (NoSuchProviderException e) {
            throw new AndroidAgentException("Invalid provider", e);
        } catch (InvalidKeyException e) {
            throw new AndroidAgentException("Invalid key", e);
        }

    }

}

From source file:com.entertailion.android.slideshow.utils.Utils.java

/**
 * Get string version of HTML for a web page. Cache HTML for future acccess.
 * /*from w ww . ja  v a2s  .  c  o m*/
 * @param context
 * @param url
 * @param refresh
 * @return
 */
public synchronized static final String getCachedData(Context context, String url, boolean refresh) {
    Log.d(LOG_TAG, "getCachedData: " + url);
    String data = null;
    boolean exists = false;
    String cleanUrl = "cache." + clean(url);
    File file = context.getFileStreamPath(cleanUrl);
    if (file != null && file.exists()) {
        exists = true;
    }

    if (!refresh && exists) {
        try {
            FileInputStream fis = context.openFileInput(cleanUrl);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            StringBuffer buffer = new StringBuffer();
            for (String line; (line = br.readLine()) != null;) {
                buffer.append(line);
            }
            fis.close();
            data = buffer.toString();
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error getData: " + url, e);
        }
    } else {
        boolean found = false;
        StringBuilder builder = new StringBuilder();
        try {
            InputStream stream = new HttpRequestHelper().getHttpStream(url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            stream.close();
            found = true;
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error getData: " + url, e);
        } catch (Exception e) {
            Log.e(LOG_TAG, "stream is NULL");
        }
        data = builder.toString();
        if (!found && exists) {
            try {
                FileInputStream fis = context.openFileInput(cleanUrl);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                StringBuffer buffer = new StringBuffer();
                for (String line; (line = br.readLine()) != null;) {
                    buffer.append(line);
                }
                fis.close();
                data = buffer.toString();
            } catch (Exception e) {
                Log.e(LOG_TAG, "Error getData: " + url, e);
            }
        }
        if (data != null && data.trim().length() > 0) {
            try {
                FileOutputStream fos = context.openFileOutput(cleanUrl, Context.MODE_PRIVATE);
                fos.write(data.getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e(LOG_TAG, "Error getData: " + url, e);
            } catch (IOException e) {
                Log.e(LOG_TAG, "Error getData: " + url, e);
            }
        }
    }
    return data;
}

From source file:org.wso2.iot.agent.utils.CommonUtils.java

/**
 * Generates keys, CSR and certificates for the devices.
 * @param context - Application context.
 * @param listener - DeviceCertCreationListener which provide device .
 *//*  www  . j a  va 2  s. c  o m*/
public static void generateDeviceCertificate(final Context context, final DeviceCertCreationListener listener)
        throws AndroidAgentException {

    if (context.getFileStreamPath(Constants.DEVICE_CERTIFCATE_NAME).exists()) {
        try {
            listener.onDeviceCertCreated(
                    new BufferedInputStream(context.openFileInput(Constants.DEVICE_CERTIFCATE_NAME)));
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }
    } else {

        try {
            ServerConfig utils = new ServerConfig();
            final KeyPair deviceKeyPair = KeyPairGenerator.getInstance(Constants.DEVICE_KEY_TYPE)
                    .generateKeyPair();
            X500Principal subject = new X500Principal(Constants.DEVICE_CSR_INFO);
            PKCS10CertificationRequest csr = new PKCS10CertificationRequest(Constants.DEVICE_KEY_ALGO, subject,
                    deviceKeyPair.getPublic(), null, deviceKeyPair.getPrivate());

            EndPointInfo endPointInfo = new EndPointInfo();
            endPointInfo.setHttpMethod(org.wso2.iot.agent.proxy.utils.Constants.HTTP_METHODS.POST);
            endPointInfo.setEndPoint(utils.getAPIServerURL(context) + Constants.SCEP_ENDPOINT);
            endPointInfo.setRequestParams(Base64.encodeToString(csr.getEncoded(), Base64.DEFAULT));

            new APIController().invokeAPI(endPointInfo, new APIResultCallBack() {
                @Override
                public void onReceiveAPIResult(Map<String, String> result, int requestCode) {
                    try {
                        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
                        InputStream in = new ByteArrayInputStream(
                                Base64.decode(result.get("response"), Base64.DEFAULT));
                        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        KeyStore keyStore = KeyStore.getInstance("PKCS12");
                        keyStore.load(null);
                        keyStore.setKeyEntry(Constants.DEVICE_CERTIFCATE_ALIAS,
                                (Key) deviceKeyPair.getPrivate(),
                                Constants.DEVICE_CERTIFCATE_PASSWORD.toCharArray(),
                                new java.security.cert.Certificate[] { cert });
                        keyStore.store(byteArrayOutputStream,
                                Constants.DEVICE_CERTIFCATE_PASSWORD.toCharArray());
                        FileOutputStream outputStream = context.openFileOutput(Constants.DEVICE_CERTIFCATE_NAME,
                                Context.MODE_PRIVATE);
                        outputStream.write(byteArrayOutputStream.toByteArray());
                        byteArrayOutputStream.close();
                        outputStream.close();
                        try {
                            listener.onDeviceCertCreated(new BufferedInputStream(
                                    context.openFileInput(Constants.DEVICE_CERTIFCATE_NAME)));
                        } catch (FileNotFoundException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    } catch (CertificateException | KeyStoreException | NoSuchAlgorithmException
                            | IOException e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
                }
            }, Constants.SCEP_REQUEST_CODE, context, true);

        } catch (NoSuchAlgorithmException e) {
            throw new AndroidAgentException("No algorithm for key generation", e);
        } catch (SignatureException e) {
            throw new AndroidAgentException("Invalid Signature", e);
        } catch (NoSuchProviderException e) {
            throw new AndroidAgentException("Invalid provider", e);
        } catch (InvalidKeyException e) {
            throw new AndroidAgentException("Invalid key", e);
        }
    }
}