Merge "Log.wtf when databases go to be downgraded." into kraken
This commit is contained in:
committed by
Android (Google) Code Review
commit
9ac3743dbd
@ -28,6 +28,12 @@ import android.util.Log;
|
||||
* Transactions are used to make sure the database is always in a sensible state.
|
||||
* <p>For an example, see the NotePadProvider class in the NotePad sample application,
|
||||
* in the <em>samples/</em> directory of the SDK.</p>
|
||||
*
|
||||
* <p class="note"><strong>Note:</strong> this class assumes
|
||||
* monotonically increasing version numbers for upgrades. Also, there
|
||||
* is no concept of a database downgrade; installing a new version of
|
||||
* your app which uses a lower version number than a
|
||||
* previously-installed version will result in undefined behavior.</p>
|
||||
*/
|
||||
public abstract class SQLiteOpenHelper {
|
||||
private static final String TAG = SQLiteOpenHelper.class.getSimpleName();
|
||||
@ -105,6 +111,10 @@ public abstract class SQLiteOpenHelper {
|
||||
if (version == 0) {
|
||||
onCreate(db);
|
||||
} else {
|
||||
if (version > mNewVersion) {
|
||||
Log.wtf(TAG, "Can't downgrade read-only database from version " +
|
||||
version + " to " + mNewVersion + ": " + db.getPath());
|
||||
}
|
||||
onUpgrade(db, version, mNewVersion);
|
||||
}
|
||||
db.setVersion(mNewVersion);
|
||||
|
@ -88,6 +88,21 @@ public final class Log {
|
||||
TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface to handle terrible failures from {@link #wtf()}.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public interface TerribleFailureHandler {
|
||||
void onTerribleFailure(String tag, TerribleFailure what);
|
||||
}
|
||||
|
||||
private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
|
||||
public void onTerribleFailure(String tag, TerribleFailure what) {
|
||||
RuntimeInit.wtf(tag, what);
|
||||
}
|
||||
};
|
||||
|
||||
private Log() {
|
||||
}
|
||||
|
||||
@ -257,12 +272,28 @@ public final class Log {
|
||||
* @param tr An exception to log. May be null.
|
||||
*/
|
||||
public static int wtf(String tag, String msg, Throwable tr) {
|
||||
tr = new TerribleFailure(msg, tr);
|
||||
TerribleFailure what = new TerribleFailure(msg, tr);
|
||||
int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, getStackTraceString(tr));
|
||||
RuntimeInit.wtf(tag, tr);
|
||||
sWtfHandler.onTerribleFailure(tag, what);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the terrible failure handler, for testing.
|
||||
*
|
||||
* @return the old handler
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
|
||||
if (handler == null) {
|
||||
throw new NullPointerException("handler == null");
|
||||
}
|
||||
TerribleFailureHandler oldHandler = sWtfHandler;
|
||||
sWtfHandler = handler;
|
||||
return oldHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handy function to get a loggable stack trace from a Throwable
|
||||
* @param tr An exception to log
|
||||
|
Reference in New Issue
Block a user