示例#1
0
 /**
  * Based on <a href="http://www.screaming-penguin.com/node/7749">Backing up your Android SQLite
  * database to the SD card</a>
  *
  * @param src
  * @param dst
  * @return true if success
  * @throws IOException
  */
 boolean copyFile(File src, File dst) throws IOException {
   long sizeIn = -1;
   long sizeCopied = 0;
   boolean ok = false;
   if (src != null && src.exists()) {
     sizeIn = src.length();
     if (!dst.createNewFile()) {
       MyLog.e(this, "New file was not created: '" + dst.getCanonicalPath() + "'");
     } else if (src.getCanonicalPath().compareTo(dst.getCanonicalPath()) == 0) {
       MyLog.d(this, "Cannot copy to itself: '" + src.getCanonicalPath() + "'");
     } else {
       FileInputStream fileInputStream = null;
       java.nio.channels.FileChannel inChannel = null;
       FileOutputStream fileOutputStream = null;
       java.nio.channels.FileChannel outChannel = null;
       try {
         fileInputStream = new FileInputStream(src);
         inChannel = fileInputStream.getChannel();
         fileOutputStream = new FileOutputStream(dst);
         outChannel = fileOutputStream.getChannel();
         sizeCopied = inChannel.transferTo(0, inChannel.size(), outChannel);
         ok = (sizeIn == sizeCopied);
       } finally {
         DbUtils.closeSilently(outChannel);
         DbUtils.closeSilently(fileOutputStream);
         DbUtils.closeSilently(inChannel);
         DbUtils.closeSilently(fileInputStream);
       }
     }
   }
   MyLog.d(this, "Copied " + sizeCopied + " bytes of " + sizeIn);
   return ok;
 }
 private Cursor queryDatabase() {
   final String method = "queryDatabase";
   Cursor cursor = null;
   for (int attempt = 0; attempt < 3 && !isCancelled(); attempt++) {
     try {
       cursor =
           MyContextHolder.get()
               .context()
               .getContentResolver()
               .query(
                   getParams().mContentUri,
                   getParams().mProjection,
                   getParams().mSa.selection,
                   getParams().mSa.selectionArgs,
                   getParams().mSortOrder);
       break;
     } catch (IllegalStateException e) {
       logD(method, "Attempt " + attempt + " to prepare cursor", e);
       DbUtils.closeSilently(cursor);
       try {
         Thread.sleep(500);
       } catch (InterruptedException e2) {
         logD(method, "Attempt " + attempt + " to prepare cursor was interrupted", e2);
         break;
       }
     }
   }
   return cursor;
 }
 private void loadMessageFromDatabase(ConversationOneMessage oMsg) {
   Uri uri = MyProvider.getTimelineMsgUri(ma.getUserId(), TimelineTypeEnum.ALL, true, oMsg.msgId);
   Cursor cursor = null;
   try {
     cursor = context.getContentResolver().query(uri, PROJECTION, null, null, null);
     if (cursor != null && cursor.moveToFirst()) {
       loadMessageFromCursor(oMsg, cursor);
     }
   } finally {
     DbUtils.closeSilently(cursor);
   }
 }
 private void deliverResultsAndClean(Cursor cursor) {
   Cursor cursorPrev = null;
   try {
     if (this.mCursor != cursor) {
       cursorPrev = this.mCursor;
       this.mCursor = cursor;
     }
     if (getParams().cancelled || cursor == null) {
       deliverCancellation();
     } else {
       deliverResult(cursor);
     }
   } finally {
     DbUtils.closeSilently(cursorPrev, "asyncLoaderEnded");
     synchronized (asyncLoaderLock) {
       asyncLoader = null;
     }
   }
 }
 private void disposeResult() {
   DbUtils.closeSilently(mCursor, "disposeResult");
   mCursor = null;
 }