session-android/src/org/thoughtcrime/securesms/database/CanonicalSessionMigrator.java

71 lines
3.0 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 18:20:44 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2011-12-20 18:20:44 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.database;
import android.content.Context;
import android.util.Log;
import java.io.File;
2011-12-20 18:20:44 +00:00
public class CanonicalSessionMigrator {
private static void migrateSession(File sessionFile, File sessionsDirectory, long canonicalAddress) {
2011-12-20 18:20:44 +00:00
File canonicalSessionFile = new File(sessionsDirectory.getAbsolutePath() + File.separatorChar + canonicalAddress);
sessionFile.renameTo(canonicalSessionFile);
2011-12-20 18:20:44 +00:00
Log.w("CanonicalSessionMigrator", "Moving: " + sessionFile.toString() + " to " + canonicalSessionFile.toString());
2011-12-20 18:20:44 +00:00
File canonicalSessionFileLocal = new File(sessionsDirectory.getAbsolutePath() + File.separatorChar + canonicalAddress + "-local");
File localFile = new File(sessionFile.getAbsolutePath() + "-local");
if (localFile.exists())
localFile.renameTo(canonicalSessionFileLocal);
2011-12-20 18:20:44 +00:00
Log.w("CanonicalSessionMigrator", "Moving " + localFile + " to " + canonicalSessionFileLocal);
2011-12-20 18:20:44 +00:00
File canonicalSessionFileRemote = new File(sessionsDirectory.getAbsolutePath() + File.separatorChar + canonicalAddress + "-remote");
File remoteFile = new File(sessionFile.getAbsolutePath() + "-remote");
if (remoteFile.exists())
remoteFile.renameTo(canonicalSessionFileRemote);
2011-12-20 18:20:44 +00:00
Log.w("CanonicalSessionMigrator", "Moving " + remoteFile + " to " + canonicalSessionFileRemote);
}
public static void migrateSessions(Context context) {
if (context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).getBoolean("canonicalized", false))
return;
2011-12-20 18:20:44 +00:00
CanonicalAddressDatabase canonicalDb = CanonicalAddressDatabase.getInstance(context);
File rootDirectory = context.getFilesDir();
File sessionsDirectory = new File(rootDirectory.getAbsolutePath() + File.separatorChar + "sessions");
2011-12-20 18:20:44 +00:00
sessionsDirectory.mkdir();
2011-12-20 18:20:44 +00:00
String[] files = rootDirectory.list();
2011-12-20 18:20:44 +00:00
for (int i=0;i<files.length;i++) {
File item = new File(rootDirectory.getAbsolutePath() + File.separatorChar + files[i]);
2011-12-20 18:20:44 +00:00
if (!item.isDirectory() && files[i].matches("[0-9]+")) {
long canonicalAddress = canonicalDb.getCanonicalAddress(files[i]);
migrateSession(item, sessionsDirectory, canonicalAddress);
2011-12-20 18:20:44 +00:00
}
}
context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).edit().putBoolean("canonicalized", true).commit();
}
2011-12-20 18:20:44 +00:00
}