List of usage examples for android.database.sqlite SQLiteDatabase yieldIfContendedSafely
public boolean yieldIfContendedSafely()
From source file:com.geecko.QuickLyric.tasks.WriteToDatabaseTask.java
@Override public Boolean doInBackground(Object... params) { lyricsArray = new Lyrics[params.length - 2]; SQLiteDatabase database; if (params[0] instanceof Fragment) { fragment = (Fragment) params[0]; mContext = fragment.getActivity(); if (mContext == null || !(mContext instanceof MainActivity)) cancel(true);//from w w w . j a v a 2 s. co m database = DatabaseHelper.getInstance(mContext).getWritableDatabase(); } else database = (SQLiteDatabase) params[0]; item = (MenuItem) params[1]; if (params[2] instanceof Lyrics[]) lyricsArray = (Lyrics[]) params[2]; else for (int i = 0; i < lyricsArray.length; i++) { lyricsArray[i] = (Lyrics) params[i + 2]; } boolean result = true; String[] columns = DatabaseHelper.columns; if (database != null && database.isOpen()) { database.beginTransaction(); try { for (Lyrics lyrics : lyricsArray) { Lyrics storedLyrics = DatabaseHelper.getInstance(mContext) .get(new String[] { lyrics.getArtist(), lyrics.getTitle(), lyrics.getOriginalArtist(), lyrics.getOriginalTrack() }); if ((storedLyrics == null || (!storedLyrics.isLRC() && lyrics.isLRC())) && !"Storage".equals(lyrics.getSource())) { ContentValues values = new ContentValues(2); values.put(columns[0], lyrics.getArtist()); values.put(columns[1], lyrics.getTitle()); values.put(columns[2], lyrics.getText()); values.put(columns[3], lyrics.getURL()); values.put(columns[4], lyrics.getSource()); if (lyrics.getCoverURL() != null && lyrics.getCoverURL().startsWith("http://")) values.put(columns[5], lyrics.getCoverURL()); values.put(columns[6], lyrics.getOriginalArtist()); values.put(columns[7], lyrics.getOriginalTrack()); values.put(columns[8], lyrics.isLRC() ? 1 : 0); values.put(columns[9], lyrics.getWriter()); values.put(columns[10], lyrics.getCopyright()); database.delete(DatabaseHelper.TABLE_NAME, String.format("%s=? AND %s=?", columns[0], columns[1]), new String[] { lyrics.getArtist(), lyrics.getTitle() }); database.insert(DatabaseHelper.TABLE_NAME, null, values); if (fragment instanceof LyricsViewFragment) ((LyricsViewFragment) fragment).lyricsPresentInDB = true; result = true; } else if (mContext != null) { // if called from activity, not service database.delete(DatabaseHelper.TABLE_NAME, String.format("%s=? AND %s=?", columns[0], columns[1]), new String[] { lyrics.getArtist(), lyrics.getTitle() }); if (fragment instanceof LyricsViewFragment) ((LyricsViewFragment) fragment).lyricsPresentInDB = false; result = false; } database.yieldIfContendedSafely(); } database.setTransactionSuccessful(); } finally { database.endTransaction(); } } return result; }