Example usage for android.content Context getDatabasePath

List of usage examples for android.content Context getDatabasePath

Introduction

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

Prototype

public abstract File getDatabasePath(String name);

Source Link

Document

Returns the absolute path on the filesystem where a database created with #openOrCreateDatabase is stored.

Usage

From source file:com.mobshep.mobileshepherd.CSInjection1.java

public void generateKey(Context context, String password) {
    try {/* w  w w  .  j  av a2 s . c  om*/
        try {
            SQLiteDatabase.loadLibs(context);

            String dbPath = context.getDatabasePath("key1.db").getPath();

            File dbPathFile = new File(dbPath);
            if (!dbPathFile.exists())
                dbPathFile.getParentFile().mkdirs();

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, dbPassword, null);

            db.execSQL("DROP TABLE IF EXISTS key1");
            db.execSQL("CREATE TABLE key1(key VARCHAR)");

            db.execSQL("INSERT INTO key1 VALUES('The Key is SourHatsAndAngryCats.')");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast error = Toast.makeText(CSInjection1.this, "An error occurred.", Toast.LENGTH_LONG);
            error.show();

        }

    } catch (SQLiteException e) {
        Toast error = Toast.makeText(CSInjection1.this, "An database error occurred.", Toast.LENGTH_LONG);
        error.show();
    }
}

From source file:com.mobshep.mobileshepherd.CSInjection1.java

public void populateTable(Context context, String dbpassword) {
    try {/*w  ww . ja  va  2s. co m*/

        try {
            SQLiteDatabase.loadLibs(context);

            String dbPath = context.getDatabasePath("Users.db").getPath();

            File dbPathFile = new File(dbPath);
            if (!dbPathFile.exists())
                dbPathFile.getParentFile().mkdirs();

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, dbpassword, null);

            db.execSQL("DROP TABLE IF EXISTS Users");
            db.execSQL(
                    "CREATE TABLE Users(memID INTEGER PRIMARY KEY AUTOINCREMENT, memName TEXT, memAge INTEGER, memPass VARCHAR)");

            db.execSQL("INSERT INTO Users VALUES( 1,'Admin',20,'49c3a17ad8d8ccd93885e6a28661480d')");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast error = Toast.makeText(CSInjection1.this, "An error occurred.", Toast.LENGTH_LONG);
            error.show();

        }

    } catch (SQLiteException e) {
        Toast error = Toast.makeText(CSInjection1.this, "An database error occurred.", Toast.LENGTH_LONG);
        error.show();
    }
}

From source file:com.mobshep.mobileshepherd.CSInjection2.java

public void generateKey(Context context, String password) {
    try {/*  w  w  w.  j  a va2 s  .  c o  m*/
        try {
            SQLiteDatabase.loadLibs(context);

            String dbPath = context.getDatabasePath("key.db").getPath();

            File dbPathFile = new File(dbPath);
            if (!dbPathFile.exists())
                dbPathFile.getParentFile().mkdirs();

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, dbPassword, null);

            db.execSQL("DROP TABLE IF EXISTS key");
            db.execSQL("CREATE TABLE key(key VARCHAR)");

            db.execSQL("INSERT INTO key VALUES('The Key is BurpingChimneys.')");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast error = Toast.makeText(CSInjection2.this, "An error occurred, key was not generated",
                    Toast.LENGTH_LONG);
            error.show();

        }

    } catch (SQLiteException e) {
        Toast error = Toast.makeText(CSInjection2.this, "An database error occurred.", Toast.LENGTH_LONG);
        error.show();
    }
}

From source file:com.mobshep.mobileshepherd.CSInjection2.java

public void populateTable(Context context, String dbPassword) {
    try {/*from w  w w  .  j  av a 2  s  .  co m*/

        try {
            SQLiteDatabase.loadLibs(context);

            String dbPath = context.getDatabasePath("Players.db").getPath();

            File dbPathFile = new File(dbPath);
            if (!dbPathFile.exists())
                dbPathFile.getParentFile().mkdirs();

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, dbPassword, null);

            db.execSQL("DROP TABLE IF EXISTS Players");
            db.execSQL(
                    "CREATE TABLE Players(memID INTEGER PRIMARY KEY AUTOINCREMENT, memName TEXT, memAge INTEGER, memPass VARCHAR)");

            db.execSQL("INSERT INTO Players VALUES( 1,'Admin',20,'49c3a17ad8d8ccd93885e6a28661480d')");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Toast error = Toast.makeText(CSInjection2.this, "An error occurred, table not initialized.",
                    Toast.LENGTH_LONG);
            error.show();

        }

    } catch (SQLiteException e) {
        Toast error = Toast.makeText(CSInjection2.this, "An database error occurred.", Toast.LENGTH_LONG);
        error.show();
    }
}

From source file:com.denel.facepatrol.MainActivity.java

public void copydatabase(Context mcontext) {

    String dbname = "DenelDB";
    File outfile = mcontext.getDatabasePath(dbname);
    if (outfile.exists()) {
        return;/* www.  ja  v  a  2 s  .com*/
    }
    outfile = mcontext.getDatabasePath(dbname + ".temp");
    // parent directory for his file if it doesn't exist,
    // in this case it returns a false.
    outfile.getParentFile().mkdirs();
    try {
        InputStream instream = mcontext.getAssets().open(dbname);
        OutputStream outstream = new FileOutputStream(outfile);
        //transfer bytes from instream to outstream
        byte[] buffer = new byte[1024];
        int length;
        while ((length = instream.read(buffer)) > 0) {
            outstream.write(buffer, 0, length);
        }
        outstream.flush();
        outstream.close();
        instream.close();
        outfile.renameTo(mcontext.getDatabasePath(dbname));
    } catch (IOException e) {
        if (outfile.exists()) {
            outfile.delete();
        }
    }
}

From source file:com.denel.facepatrol.MainActivity.java

private void decryptfile(Context mcontext, SecretKey key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

    File infile = mcontext.getDatabasePath(dbname_en);
    InputStream fis = new FileInputStream(infile);
    File outfile = mcontext.getDatabasePath(dbname);
    // parent directory for his file if it doesn't exist,
    // in this case it returns a false.
    outfile.getParentFile().mkdirs();// w  ww .j  ava  2 s .  co m
    // This stream write the decrypted text. This stream will be wrapped by another stream. 
    FileOutputStream fos = new FileOutputStream(outfile);
    // Length is 16 byte // Careful when taking user input!!! 
    // http://stackoverflow.com/a/3452620/1188357 
    SecretKeySpec sks = new SecretKeySpec(key.getEncoded(), "AES");
    // Create cipher 
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    // Wrap the output stream 
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes 
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    } // Flush and close streams. 
    cos.flush();
    cos.close();
    fis.close();
    // delete the encrypted file
    if (infile.exists()) {
        infile.delete();
    }
}

From source file:holidayiq.com.geofenced.geofence.GeofenceTransitionsIntentService.java

public static void SendNotificationForEntry(Context mContext, String geoId, String geoName,
        String notificationStringTitle, String notificationStringContent, String notificationStringDeeplink,
        String objType, Context context, String eventType, boolean isBanner) {
    String currentTime = String.valueOf(System.currentTimeMillis());
    if (notificationStringTitle != null) {
        notificationStringTitle = notificationStringTitle.replace("$name$", geoName);
        notificationStringContent = notificationStringContent.replace("$name$", geoName);
        notificationStringDeeplink = notificationStringDeeplink.replace("$name$", Underscored(geoName));
        notificationStringDeeplink = notificationStringDeeplink.replace("$id$", geoId);
        Calendar c = new GregorianCalendar();
        c.set(Calendar.HOUR_OF_DAY, 6);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date d1 = c.getTime();//ww  w  . j  a va2 s.  c o  m
        boolean isNotification = false;
        File dbFile = mContext.getDatabasePath("hiq_in_app.sqlite");
        SQLiteDatabase inAppDb = SQLiteDatabase.openDatabase(dbFile.getPath(), null,
                SQLiteDatabase.OPEN_READWRITE);
        if (geoName.equalsIgnoreCase("home")) {
            String lastTriggeredHome = HIQSharedPrefrence.getString("lastTriggeredHome", mContext);
            if (lastTriggeredHome == null) {
                sendNotification(notificationStringTitle, notificationStringContent, notificationStringDeeplink,
                        context, isBanner);
                HIQSharedPrefrence.putString("lastTriggeredHome", System.currentTimeMillis() + "", mContext);
                isNotification = true;
            } else {
                long previouslastTriggeredHome = Long
                        .parseLong(HIQSharedPrefrence.getString("lastTriggeredHome", mContext));
                if (previouslastTriggeredHome < d1.getTime()) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    HIQSharedPrefrence.putString("lastTriggeredHome", System.currentTimeMillis() + "",
                            mContext);
                    isNotification = true;
                }
            }

        } else if (objType.equalsIgnoreCase("Hotel")) {
            String lastTriggeredHome = HIQSharedPrefrence.getString("lastTriggeredHotel", mContext);
            if (lastTriggeredHome == null) {
                sendNotification(notificationStringTitle, notificationStringContent, notificationStringDeeplink,
                        context, isBanner);
                HIQSharedPrefrence.putString("lastTriggeredHotel", System.currentTimeMillis() + "", mContext);
                isNotification = true;
            } else {
                long previouslastTriggeredHome = Long
                        .parseLong(HIQSharedPrefrence.getString("lastTriggeredHotel", mContext));
                if (previouslastTriggeredHome < d1.getTime()) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    HIQSharedPrefrence.putString("lastTriggeredHotel", System.currentTimeMillis() + "",
                            mContext);
                    isNotification = true;
                }
            }
        } else if (objType.equalsIgnoreCase("SS")) {
            Cursor res = null;
            try {
                String lastNotificationTriggerd = null;
                res = inAppDb.rawQuery("Select * from " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME + " where "
                        + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                if (res != null) {
                    if (res.moveToFirst()) {
                        lastNotificationTriggerd = res.getString(res.getColumnIndex("lastTimeStamp"));
                    }
                }
                if (lastNotificationTriggerd == null) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    isNotification = true;
                    Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME
                            + " SET lastTimeStamp = '" + currentTime + "' where "
                            + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                    cur.moveToFirst();
                    cur.close();
                } else {
                    long time = Long.parseLong(lastNotificationTriggerd);
                    if (time < d1.getTime()) {
                        sendNotification(notificationStringTitle, notificationStringContent,
                                notificationStringDeeplink, context, isBanner);
                        isNotification = true;
                        Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME
                                + " SET lastTimeStamp = '" + currentTime + "' where "
                                + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                        cur.moveToFirst();
                        cur.close();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                ExceptionUtils.logException(e);
            } finally {
                if (res != null && !res.isClosed()) {
                    res.close();
                }
                res = null;
                if (inAppDb != null && inAppDb.isOpen()) {
                    inAppDb.close();
                }
                inAppDb = null;
            }
        } else if (objType.equalsIgnoreCase("Destination")) {
            Cursor res = null;
            try {
                String lastNotificationTriggerd = null;
                res = inAppDb.rawQuery("Select * from " + GeoDbHelper.DESTINATION_TABLE_NAME + " where "
                        + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                if (res != null) {
                    try {
                        if (res.moveToFirst()) {
                            lastNotificationTriggerd = res.getString(res.getColumnIndex("lastTimeStamp"));
                        }
                    } catch (Exception e) {

                    }
                }
                if (lastNotificationTriggerd == null) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    isNotification = true;
                    Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.DESTINATION_TABLE_NAME
                            + " SET lastTimeStamp = '" + currentTime + "' where "
                            + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                    cur.moveToFirst();
                    cur.close();
                } else {
                    long time = Long.parseLong(lastNotificationTriggerd);
                    if (time < d1.getTime()) {
                        sendNotification(notificationStringTitle, notificationStringContent,
                                notificationStringDeeplink, context, isBanner);
                        isNotification = true;
                        Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.DESTINATION_TABLE_NAME
                                + " SET lastTimeStamp = '" + currentTime + "' where "
                                + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                        cur.moveToFirst();
                        cur.close();
                    }
                }

            } catch (Exception e) {
                ExceptionUtils.logException(e);
            } finally {
                if (res != null && !res.isClosed()) {
                    res.close();
                }
                res = null;
                if (inAppDb != null && inAppDb.isOpen()) {
                    inAppDb.close();
                }
                inAppDb = null;
            }
        }
        HashMap<String, Object> oMap = new HashMap<String, Object>();
        oMap.put("Type", eventType);
        oMap.put("Name", geoName);
        oMap.put("ID", geoId);
        oMap.put("Object Type", objType);
        oMap.put("Date", new Date());

        if (isNotification) {
        }
    }

    // HIQUtil.sendEventToGAFromObject("GeoFenceEvent", "GeoFenceEvent in Android", oMap);

}

From source file:com.denel.facepatrol.MainActivity.java

private void encryptfile(Context mcontext, SecretKey key)
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

    // This will probably change when I will the database will be downloaded from the server
    boolean db_file_exists = mcontext.getDatabasePath(dbname).exists();
    InputStream fis = null;//from   w w w .ja v a 2  s.  c  om
    File infile = mcontext.getDatabasePath(dbname);
    // check if database file exists to prevent downloading the file each start
    if (db_file_exists) {
        fis = new FileInputStream(infile);
    } else {
        fis = mcontext.getAssets().open(dbname);
    }
    // This stream write the encrypted text. This stream will be wrapped by another stream. 
    FileOutputStream fos = new FileOutputStream(mcontext.getDatabasePath(dbname_en).getAbsolutePath());
    // Length is 16 byte // Careful when taking user input!!! http://stackoverflow.com/a/3452620/1188357 
    SecretKeySpec sks = new SecretKeySpec(key.getEncoded(), "AES");
    // Create cipher 
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream 
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes 
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    } // Flush and close streams. 
    cos.flush();
    cos.close();
    fis.close();
    // delete the decrypted file
    if (infile.exists()) {
        infile.delete();
    }
}

From source file:com.miz.functions.MizLib.java

public static void copyDatabase(Context context) {
    try {//ww  w .  j ava2s. co  m
        FileUtils.copyFile(context.getDatabasePath("mizuu_data"),
                new File(Environment.getExternalStorageDirectory(), "mizuu_data.db"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

private synchronized boolean deleteDB(final Context context) {
    if (context == null)
        return false;

    Log.i(TAG, "Deleting Database as requested by preferences.");
    File f = context.getDatabasePath(DATABASE_NAME);
    if (f.exists()) {
        if (getOpenHelper() != null) {
            closeDB();//from  w  w w .j a  va  2 s  . c o  m
        }
        return f.delete();
    }

    return false;
}