예제 #1
1
 private int deleteMessages(SQLiteDatabase db, String selection, String[] selectionArgs) {
   int count = 0;
   String sqlDesc = "";
   db.beginTransaction();
   try {
     // Delete all related records from MyDatabase.MsgOfUser for these messages
     String selectionG =
         " EXISTS ("
             + "SELECT * FROM "
             + Msg.TABLE_NAME
             + " WHERE ("
             + Msg.TABLE_NAME
             + "."
             + BaseColumns._ID
             + "="
             + MsgOfUser.TABLE_NAME
             + "."
             + MyDatabase.MsgOfUser.MSG_ID
             + ") AND ("
             + selection
             + "))";
     String descSuffix = "; args=" + Arrays.toString(selectionArgs);
     sqlDesc = selectionG + descSuffix;
     count = db.delete(MsgOfUser.TABLE_NAME, selectionG, selectionArgs);
     // Now delete messages themselves
     sqlDesc = selection + descSuffix;
     count = db.delete(Msg.TABLE_NAME, selection, selectionArgs);
     db.setTransactionSuccessful();
   } catch (Exception e) {
     MyLog.d(TAG, "; SQL='" + sqlDesc + "'", e);
   } finally {
     db.endTransaction();
   }
   return count;
 }
예제 #2
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;
 }
예제 #3
0
    private void moveAll(TaskResult result) {
      boolean useExternalStorageOld = MyStorage.isStorageExternal();
      if (mUseExternalStorageNew
          && !MyStorage.isWritableExternalStorageAvailable(result.messageBuilder)) {
        mUseExternalStorageNew = false;
      }

      MyLog.d(
          this,
          "About to move data from " + useExternalStorageOld + " to " + mUseExternalStorageNew);

      if (mUseExternalStorageNew == useExternalStorageOld) {
        result.messageBuilder.append(" Nothing to do.");
        result.success = true;
        return;
      }
      try {
        result.success =
            moveDatabase(
                mUseExternalStorageNew, result.messageBuilder, DatabaseHolder.DATABASE_NAME);
        if (result.success) {
          result.moved = true;
          moveDatabase(
              mUseExternalStorageNew,
              result.messageBuilder,
              TimelineSearchSuggestionsProvider.DATABASE_NAME);
          moveDownloads(mUseExternalStorageNew, result.messageBuilder);
        }
      } finally {
        if (result.success) {
          saveNewSettings(mUseExternalStorageNew, result.messageBuilder);
        }
      }
    }
예제 #4
0
 @Override
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   if (action.equals(MyAction.SERVICE_STATE.getAction())) {
     MyContextHolder.initialize(context, this);
     synchronized (mServiceState) {
       stateQueuedTime = System.nanoTime();
       waitingForServiceState = false;
       mServiceState = MyServiceState.load(intent.getStringExtra(IntentExtra.SERVICE_STATE.key));
     }
     MyLog.d(this, "Notification received: Service state=" + mServiceState);
   } else if ("android.intent.action.BOOT_COMPLETED".equals(action)) {
     MyLog.d(this, "Trying to start service on boot");
     sendCommand(CommandData.getEmpty());
   } else if ("android.intent.action.ACTION_SHUTDOWN".equals(action)) {
     // We need this to persist unsaved data in the service
     MyLog.d(this, "Stopping service on Shutdown");
     setServiceUnavailable();
     stopService();
   }
 }
 @Override
 void executeOnUiThread(MessageContextMenu menu, MessageEditorData editorData) {
   Uri uri =
       MatchedUri.getUserListUri(
           editorData.ma.getUserId(),
           UserListType.USERS_OF_MESSAGE,
           menu.messageList.isTimelineCombined(),
           menu.getMsgId());
   if (MyLog.isLoggable(this, MyLog.DEBUG)) {
     MyLog.d(this, "onItemClick, startActivity=" + uri);
   }
   menu.messageList.getActivity().startActivity(MyAction.VIEW_USERS.getIntent(uri));
 }
 @Override
 void executeOnUiThread(MessageContextMenu menu, MessageEditorData editorData) {
   Uri uri =
       MatchedUri.getTimelineItemUri(
           editorData.ma.getUserId(),
           menu.messageList.getTimelineType(),
           menu.messageList.isTimelineCombined(),
           menu.messageList.getSelectedUserId(),
           menu.getMsgId());
   String action = menu.messageList.getActivity().getIntent().getAction();
   if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {
     if (MyLog.isLoggable(this, MyLog.DEBUG)) {
       MyLog.d(this, "onItemClick, setData=" + uri);
     }
     menu.messageList.getActivity().setResult(Activity.RESULT_OK, new Intent().setData(uri));
   } else {
     if (MyLog.isLoggable(this, MyLog.DEBUG)) {
       MyLog.d(this, "onItemClick, startActivity=" + uri);
     }
     menu.messageList.getActivity().startActivity(MyAction.VIEW_CONVERSATION.getIntent(uri));
   }
 }
예제 #7
0
    // This is in the UI thread, so we can mess with the UI
    @Override
    protected void onPostExecute(TaskResult result) {
      DialogFactory.dismissSafely(dlg);
      if (result == null) {
        MyLog.e(this, "Result is Null");
        return;
      }
      MyLog.d(
          this,
          this.getClass().getSimpleName()
              + " ended, "
              + (result.success ? (result.moved ? "moved" : "didn't move") : "failed"));

      if (!result.success) {
        result.messageBuilder.insert(0, mContext.getString(R.string.error) + ": ");
      }
      Toast.makeText(mContext, result.getMessage(), Toast.LENGTH_LONG).show();
      parentFragment.showUseExternalStorage();
    }
예제 #8
0
    private void moveDownloads(boolean useExternalStorageNew, StringBuilder messageToAppend) {
      String method = "moveDownloads";
      boolean succeeded = false;
      boolean done = false;
      boolean didWeCopyAnything = false;
      File dirOld = null;
      File dirNew = null;
      try {

        if (!done) {
          dirOld = MyStorage.getDataFilesDir(MyStorage.DIRECTORY_DOWNLOADS);
          dirNew =
              MyStorage.getDataFilesDir(
                  MyStorage.DIRECTORY_DOWNLOADS, TriState.fromBoolean(useExternalStorageNew));
          if (dirOld == null || !dirOld.exists()) {
            messageToAppend.append(" No old avatars. ");
            done = true;
            succeeded = true;
          }
          if (dirNew == null) {
            messageToAppend.append(" No directory for new avatars?! ");
            done = true;
          }
        }
        if (!done) {
          if (MyLog.isVerboseEnabled()) {
            MyLog.v(this, method + " from: " + dirOld.getPath());
            MyLog.v(this, method + " to: " + dirNew.getPath());
          }
          String filename = "";
          try {
            for (File fileOld : dirOld.listFiles()) {
              if (fileOld.isFile()) {
                filename = fileOld.getName();
                File fileNew = new File(dirNew, filename);
                if (copyFile(fileOld, fileNew)) {
                  didWeCopyAnything = true;
                }
              }
            }
            succeeded = true;
          } catch (Exception e) {
            String logMsg = method + " couldn't copy'" + filename + "'";
            MyLog.v(this, logMsg, e);
            messageToAppend.insert(0, " " + logMsg + ": " + e.getMessage());
          }
          done = true;
        }
      } catch (Exception e) {
        MyLog.v(this, e);
        messageToAppend.append(method + " error: " + e.getMessage() + ". ");
        succeeded = false;
      } finally {
        // Delete unnecessary files
        try {
          if (succeeded) {
            if (didWeCopyAnything) {
              for (File fileOld : dirOld.listFiles()) {
                if (fileOld.isFile() && !fileOld.delete()) {
                  messageToAppend.append(method + " couldn't delete old file " + fileOld.getName());
                }
              }
            }
          } else {
            if (dirNew != null && dirNew.exists()) {
              for (File fileNew : dirNew.listFiles()) {
                if (fileNew.isFile() && !fileNew.delete()) {
                  messageToAppend.append(method + " couldn't delete new file " + fileNew.getName());
                }
              }
            }
          }
        } catch (Exception e) {
          String logMsg = method + " deleting unnecessary files";
          MyLog.v(this, logMsg, e);
          messageToAppend.append(logMsg + ": " + e.getMessage());
        }
      }
      MyLog.d(this, method + " " + (succeeded ? "succeeded" : "failed"));
    }
예제 #9
0
 private boolean moveDatabase(
     boolean useExternalStorageNew, StringBuilder messageToAppend, String databaseName) {
   final String method = "moveDatabase";
   boolean succeeded = false;
   boolean done = false;
   /** Did we actually copied database? */
   boolean copied = false;
   File dbFileOld = null;
   File dbFileNew = null;
   try {
     dbFileOld = MyContextHolder.get().context().getDatabasePath(databaseName);
     dbFileNew =
         MyStorage.getDatabasePath(databaseName, TriState.fromBoolean(useExternalStorageNew));
     if (dbFileOld == null) {
       messageToAppend.append(" No old database " + databaseName);
       done = true;
     }
     if (!done) {
       if (dbFileNew == null) {
         messageToAppend.append(" No new database " + databaseName);
         done = true;
       } else {
         if (!dbFileOld.exists()) {
           messageToAppend.append(" No old database " + databaseName);
           done = true;
           succeeded = true;
         } else if (dbFileNew.exists()) {
           messageToAppend.insert(0, " Database already exists " + databaseName);
           if (!dbFileNew.delete()) {
             messageToAppend.insert(0, " Couldn't delete already existed files. ");
             done = true;
           }
         }
       }
     }
     if (!done) {
       if (MyLog.isVerboseEnabled()) {
         MyLog.v(this, method + " from: " + dbFileOld.getPath());
         MyLog.v(this, method + " to: " + dbFileNew.getPath());
       }
       try {
         if (copyFile(dbFileOld, dbFileNew)) {
           copied = true;
           succeeded = true;
         }
       } catch (Exception e) {
         MyLog.v(this, "Copy database " + databaseName, e);
         messageToAppend.insert(
             0, " Couldn't copy database " + databaseName + ": " + e.getMessage() + ". ");
       }
     }
   } catch (Exception e) {
     MyLog.v(this, e);
     messageToAppend.append(method + " error: " + e.getMessage() + ". ");
     succeeded = false;
   } finally {
     // Delete unnecessary files
     try {
       if (succeeded) {
         if (copied && dbFileOld != null && dbFileOld.exists() && !dbFileOld.delete()) {
           messageToAppend.append(method + " couldn't delete old files. ");
         }
       } else {
         if (dbFileNew != null && dbFileNew.exists() && !dbFileNew.delete()) {
           messageToAppend.append(method + " couldn't delete new files. ");
         }
       }
     } catch (Exception e) {
       MyLog.v(this, method + " Delete old file", e);
       messageToAppend.append(method + " couldn't delete old files. " + e.getMessage() + ". ");
     }
   }
   MyLog.d(this, method + "; " + databaseName + " " + (succeeded ? "succeeded" : "failed"));
   return succeeded;
 }
예제 #10
0
 private void logD(String method, String message, Throwable tr) {
   MyLog.d(this, String.valueOf(instanceId) + " " + method + "; " + message, tr);
 }