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:ru.gkpromtech.exhibition.db.DbHelper.java

public static String getDbPath(Context context, String dbName) {
    return context.getDatabasePath(dbName).getAbsolutePath();
}

From source file:com.csipsimple.backup.SipProfilesHelper.java

SipProfilesHelper(Context ctxt) {
    mContext = ctxt;
    databaseFile = ctxt.getDatabasePath(SipManager.AUTHORITY);
}

From source file:uk.org.rivernile.edinburghbustracker.android.Application.java

/**
 * Download the stop database from the server and put it in the
 * application's working data directory.
 *
 * @param context The context to use this method with.
 * @param url The URL of the bus stop database to download.
 *//* w  w w  .j  a  v a  2  s.  co  m*/
private static void updateStopsDB(final Context context, final String url, final String checksum) {
    if (context == null || url == null || url.length() == 0 || checksum == null || checksum.length() == 0)
        return;
    try {
        // Connect to the server.
        final URL u = new URL(url);
        final HttpURLConnection con = (HttpURLConnection) u.openConnection();
        final InputStream in = con.getInputStream();

        // Make sure the URL is what we expect.
        if (!u.getHost().equals(con.getURL().getHost())) {
            in.close();
            con.disconnect();
            return;
        }

        // The location the file should be downloaded to.
        final File temp = context.getDatabasePath(BusStopDatabase.STOP_DB_NAME + "_temp");
        // The eventual destination of the file.
        final File dest = context.getDatabasePath(BusStopDatabase.STOP_DB_NAME);
        final FileOutputStream out = new FileOutputStream(temp);

        // Get the file from the server.
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        // Make sure the stream is flushed then close resources and
        // disconnect.
        out.flush();
        out.close();
        in.close();
        con.disconnect();

        // Do a MD5 checksum on the downloaded file. Make sure it matches
        // what the server reported.
        if (!md5Checksum(temp).equalsIgnoreCase(checksum)) {
            // If it doesn't match, delete the downloaded file.
            temp.delete();
            return;
        }

        try {
            // Open the temp database and execute the index operation on it.
            final SQLiteDatabase db = SQLiteDatabase.openDatabase(temp.getAbsolutePath(), null,
                    SQLiteDatabase.OPEN_READWRITE);
            BusStopDatabase.setUpIndexes(db);
            db.close();
        } catch (SQLiteException e) {
            // If we couldn't create the index, continue anyway. The user
            // will still be able to use the database, it will just run
            // slowly if they want route lines.
        }

        // Close a currently open database. Delete the old database then
        // move the downloaded file in to its place. Do this while
        // synchronized to make sure noting else uses the database in this
        // time.
        final BusStopDatabase bsd = BusStopDatabase.getInstance(context.getApplicationContext());
        synchronized (bsd) {
            try {
                bsd.getReadableDatabase().close();
            } catch (SQLiteException e) {
                // Nothing to do here. Assume it's already closed.
            }

            dest.delete();
            temp.renameTo(dest);
        }

        // Delete the associated journal file because we no longer need it.
        final File journalFile = context.getDatabasePath(BusStopDatabase.STOP_DB_NAME + "_temp-journal");
        if (journalFile.exists())
            journalFile.delete();

        // Alert the user that the database has been updated.
        Looper.prepare();
        Toast.makeText(context, R.string.bus_stop_db_updated, Toast.LENGTH_LONG).show();
        Looper.loop();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
}

From source file:de.measite.contactmerger.MergeActivity.java

public void updateList() {
    progressContainer = findViewById(R.id.progress_bar_container);
    progressBar = (ProgressBar) findViewById(R.id.analyze_progress);
    progressContainer.setVisibility(View.GONE);

    loadText = (TextView) findViewById(R.id.load_text);

    TextView stopScan = (TextView) findViewById(R.id.stop_scan);
    Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
    stopScan.setTypeface(font);//from   ww  w  .  j  av  a  2  s  .com
    stopScan.setClickable(true);
    stopScan.setOnClickListener(this);

    startScan = (Button) findViewById(R.id.start_scan);
    startScan.setOnClickListener(this);

    ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.switcher);
    ViewSwitcher switcher_list = (ViewSwitcher) findViewById(R.id.switcher_list);

    Context context = getApplicationContext();
    File path = context.getDatabasePath("contactsgraph");
    File modelFile = new File(path, "model.kryo.gz");

    if (path.exists() && modelFile.exists()) {
        this.adapter.update();
        while (switcher.getCurrentView().getId() != R.id.switcher_list) {
            switcher.showNext();
        }
        if (adapter.getCount() == 0) {
            while (switcher_list.getCurrentView().getId() != R.id.all_done) {
                switcher_list.showNext();
            }
        } else {
            while (switcher_list.getCurrentView().getId() != R.id.contact_merge_list) {
                switcher_list.showPrevious();
            }
        }
        switcher_list.postInvalidate();
    } else {
        if (switcher.getCurrentView().getId() == R.id.contact_merge_list) {
            switcher.showPrevious();
        }
        Intent intent = new Intent(getApplicationContext(), AnalyzerService.class);
        intent.putExtra("forceRunning", true);
        startService(intent);
    }
    switcher.postInvalidate();
}

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

public void outputKey(Context context, String password) {
    SQLiteDatabase.loadLibs(context);/*from   w  ww  .ja v a  2  s.  c  o m*/

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

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

    String query = ("SELECT * FROM key0;");

    Cursor cursor = db.rawQuery(query, null);

    if (cursor != null) {

        try {
            if (cursor.moveToFirst())
                key.setText(cursor.getString(0));
        } finally {
            cursor.close();

        }
    }
}

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

public void outputKey(Context context, String password) {
    SQLiteDatabase.loadLibs(context);//from   w  ww  .j a  v  a 2s. com

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

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

    String query = ("SELECT * FROM key1;");

    Cursor cursor = db.rawQuery(query, null);

    if (cursor != null) {

        try {
            if (cursor.moveToFirst())
                key.setText(cursor.getString(0));
        } finally {
            cursor.close();

        }
    }
}

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

public void outputKey(Context context, String password) {
    SQLiteDatabase.loadLibs(context);/*  w  w  w.  j ava 2s. c o  m*/

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

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

    String query = ("SELECT * FROM key;");

    Cursor cursor = db.rawQuery(query, null);

    if (cursor != null) {

        try {
            if (cursor.moveToFirst())
                key.setText(cursor.getString(0));
        } finally {
            cursor.close();

        }
    }
}

From source file:com.momock.util.JsonDatabase.java

public static JsonDatabase get(final Context context, final String dbname) {
    return new JsonDatabase() {
        Map<String, Collection> cols = new HashMap<String, Collection>();
        SQLiteDatabase db = null;//  w w  w  .  ja v a2 s  . c o m
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(context, dbname);

        @Override
        public Collection getCollection(String name) {
            if (!cols.containsKey(name)) {
                cols.put(name, new Collection(this, helper, name));
            }
            return cols.get(name);
        }

        @Override
        public void forceClose() {
            if (db != null) {
                db.close();
                db = null;
            }
        }

        @Override
        SQLiteDatabase getNativeDatabase() {
            if (db == null || !db.isOpen()) {
                try {
                    db = helper.getWritableDatabase();
                } catch (Exception e) {
                    Logger.error(e);
                    Logger.debug("Database Path :" + context.getDatabasePath(dbname).getPath());
                    try {
                        helper = new MySQLiteOpenHelper(context, null);
                        db = helper.getWritableDatabase();
                    } catch (Exception ex) {
                        Logger.error(ex);
                        db = null;
                    }
                }
            }
            return db;
        }

    };
}

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

public void generateKey(Context context, String password) {
    try {//www  .j ava  2 s  . co  m
        try {
            SQLiteDatabase.loadLibs(context);

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

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

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

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

            db.execSQL("INSERT INTO key0 VALUES('The Key is VolcanicEruptionsAbruptInterruptions.')");

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

        }

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

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

public void populateTable(Context context, String password) {
    try {/*from   w w  w .j  a va2s . c  o  m*/
        try {
            SQLiteDatabase.loadLibs(context);

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

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

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

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

            db.execSQL("INSERT INTO encrypted VALUES( 1,'Admin',20,'A3B922DF010PQSI827')");

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

        }

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