Commit 9378f673 authored by Michael W's avatar Michael W
Browse files

DeskClock: Remove references to org.lineageos.platform.internal

* Was used for a feature that isn't used anymore
* We need to create new tables because dropping columns isn't supported
  -> create temp tables, copy over values, remove old tables, rename temp
     tables
* Also fixes alarm inserts, which partially failed due to increasing
  volume and profile being indexed wrong:
"CursorWindow: Failed to read row 0, column 11 from a CursorWindow which
has 1 rows, 11 columns."
* Above fix is required for "new Alarms(cursor)" to not fail so we can
  actually get a valid Alarms object to insert into our (temp) database

Change-Id: I80e495792dcb65955de09268c7df9c6846cee559
parent 2369e050
......@@ -25,7 +25,6 @@ android_app {
"androidx.appcompat_appcompat",
"androidx.gridlayout_gridlayout",
"androidx.recyclerview_recyclerview",
"org.lineageos.platform.internal",
],
required: [
"com.android.deskclock_whitelist"
......
......@@ -32,7 +32,6 @@ import androidx.loader.content.CursorLoader;
import com.android.deskclock.R;
import com.android.deskclock.data.DataModel;
import com.android.deskclock.data.Weekdays;
import lineageos.app.ProfileManager;
import java.util.Calendar;
import java.util.LinkedList;
......@@ -64,7 +63,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
RINGTONE,
DELETE_AFTER_USE,
INCREASING_VOLUME,
PROFILE
};
private static final String[] QUERY_ALARMS_WITH_INSTANCES_COLUMNS = {
......@@ -78,7 +76,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
ClockDatabaseHelper.ALARMS_TABLE_NAME + "." + RINGTONE,
ClockDatabaseHelper.ALARMS_TABLE_NAME + "." + DELETE_AFTER_USE,
ClockDatabaseHelper.ALARMS_TABLE_NAME + "." + INCREASING_VOLUME,
ClockDatabaseHelper.ALARMS_TABLE_NAME + "." + PROFILE,
ClockDatabaseHelper.INSTANCES_TABLE_NAME + "."
+ ClockContract.InstancesColumns.ALARM_STATE,
ClockDatabaseHelper.INSTANCES_TABLE_NAME + "." + ClockContract.InstancesColumns._ID,
......@@ -104,8 +101,8 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
private static final int LABEL_INDEX = 6;
private static final int RINGTONE_INDEX = 7;
private static final int DELETE_AFTER_USE_INDEX = 8;
private static final int INCREASING_VOLUME_INDEX = 10;
private static final int PROFILE_INDEX = 11;
private static final int INCREASING_VOLUME_INDEX = 9;
private static final int INSTANCE_STATE_INDEX = 9;
public static final int INSTANCE_ID_INDEX = 10;
public static final int INSTANCE_YEAR_INDEX = 11;
......@@ -116,7 +113,7 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
public static final int INSTANCE_LABEL_INDEX = 16;
public static final int INSTANCE_VIBRATE_INDEX = 17;
private static final int COLUMN_COUNT = PROFILE_INDEX + 1;
private static final int COLUMN_COUNT = INCREASING_VOLUME_INDEX + 1;
private static final int ALARM_JOIN_INSTANCE_COLUMN_COUNT = INSTANCE_VIBRATE_INDEX + 1;
public static ContentValues createContentValues(Alarm alarm) {
......@@ -133,7 +130,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
values.put(LABEL, alarm.label);
values.put(DELETE_AFTER_USE, alarm.deleteAfterUse);
values.put(INCREASING_VOLUME, alarm.increasingVolume ? 1 : 0);
values.put(PROFILE, alarm.profile.toString());
if (alarm.alert == null) {
// We want to put null, so default alarm changes
values.putNull(RINGTONE);
......@@ -296,7 +292,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
public Uri alert;
public boolean deleteAfterUse;
public boolean increasingVolume;
public UUID profile;
public int instanceState;
public int instanceId;
......@@ -315,7 +310,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
this.alert = DataModel.getDataModel().getDefaultAlarmRingtoneUri();
this.deleteAfterUse = false;
this.increasingVolume = false;
this.profile = ProfileManager.NO_PROFILE;
}
public Alarm(Cursor c) {
......@@ -328,7 +322,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
label = c.getString(LABEL_INDEX);
deleteAfterUse = c.getInt(DELETE_AFTER_USE_INDEX) == 1;
increasingVolume = c.getInt(INCREASING_VOLUME_INDEX) == 1;
profile = getProfileFromCursor(c);
if (c.getColumnCount() == ALARM_JOIN_INSTANCE_COLUMN_COUNT) {
instanceState = c.getInt(INSTANCE_STATE_INDEX);
......@@ -355,7 +348,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
alert = p.readParcelable(null);
deleteAfterUse = p.readInt() == 1;
increasingVolume = p.readInt() == 1;
profile = ParcelUuid.CREATOR.createFromParcel(p).getUuid();
}
/**
......@@ -365,17 +357,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
return getContentUri(id);
}
private static UUID getProfileFromCursor(Cursor c) {
if (!c.isNull(PROFILE_INDEX)) {
try {
return UUID.fromString(c.getString(PROFILE_INDEX));
} catch (IllegalArgumentException ex) {
// fall back to no profile
}
}
return ProfileManager.NO_PROFILE;
}
public String getLabelOrDefault(Context context) {
return label.isEmpty() ? context.getString(R.string.default_label) : label;
}
......@@ -402,7 +383,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
p.writeParcelable(alert, flags);
p.writeInt(deleteAfterUse ? 1 : 0);
p.writeInt(increasingVolume ? 1 : 0);
p.writeParcelable(new ParcelUuid(profile), 0);
}
public int describeContents() {
......@@ -416,7 +396,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
result.mLabel = label;
result.mRingtone = alert;
result.mIncreasingVolume = increasingVolume;
result.mProfile = profile;
return result;
}
......@@ -498,7 +477,6 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
", label='" + label + '\'' +
", deleteAfterUse=" + deleteAfterUse +
", increasingVolume=" + increasingVolume +
", profile=" + profile +
'}';
}
}
......@@ -29,7 +29,6 @@ import com.android.deskclock.LogUtils;
import com.android.deskclock.R;
import com.android.deskclock.alarms.AlarmStateManager;
import com.android.deskclock.data.DataModel;
import lineageos.app.ProfileManager;
import java.util.Calendar;
import java.util.LinkedList;
......@@ -69,8 +68,7 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
RINGTONE,
ALARM_ID,
ALARM_STATE,
INCREASING_VOLUME,
PROFILE
INCREASING_VOLUME
};
/**
......@@ -89,9 +87,8 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
private static final int ALARM_ID_INDEX = 9;
private static final int ALARM_STATE_INDEX = 10;
private static final int INCREASING_VOLUME_INDEX = 11;
private static final int PROFILE_INDEX = 12;
private static final int COLUMN_COUNT = PROFILE_INDEX + 1;
private static final int COLUMN_COUNT = INCREASING_VOLUME_INDEX + 1;
public static ContentValues createContentValues(AlarmInstance instance) {
ContentValues values = new ContentValues(COLUMN_COUNT);
......@@ -116,7 +113,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
values.put(ALARM_ID, instance.mAlarmId);
values.put(ALARM_STATE, instance.mAlarmState);
values.put(INCREASING_VOLUME, instance.mIncreasingVolume ? 1 : 0);
values.put(PROFILE, instance.mProfile.toString());
return values;
}
......@@ -305,7 +301,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
public Long mAlarmId;
public int mAlarmState;
public boolean mIncreasingVolume;
public UUID mProfile;
public AlarmInstance(Calendar calendar, Long alarmId) {
this(calendar);
......@@ -320,7 +315,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
mRingtone = null;
mAlarmState = SILENT_STATE;
mIncreasingVolume = false;
mProfile = ProfileManager.NO_PROFILE;
}
public AlarmInstance(AlarmInstance instance) {
......@@ -336,7 +330,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
this.mAlarmId = instance.mAlarmId;
this.mAlarmState = instance.mAlarmState;
this.mIncreasingVolume = instance.mIncreasingVolume;
this.mProfile = instance.mProfile;
}
public AlarmInstance(Cursor c, boolean joinedTable) {
......@@ -372,15 +365,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
}
mAlarmState = c.getInt(ALARM_STATE_INDEX);
mIncreasingVolume = c.getInt(INCREASING_VOLUME_INDEX) == 1;
if (c.isNull(PROFILE_INDEX)) {
mProfile = ProfileManager.NO_PROFILE;
} else {
try {
mProfile = UUID.fromString(c.getString(PROFILE_INDEX));
} catch (IllegalArgumentException ex) {
mProfile = ProfileManager.NO_PROFILE;
}
}
}
/**
......@@ -497,7 +481,6 @@ public final class AlarmInstance implements ClockContract.InstancesColumns {
", mAlarmId=" + mAlarmId +
", mAlarmState=" + mAlarmState +
", mIncreasingVolume=" + mIncreasingVolume +
", mProfile=" + mProfile +
'}';
}
}
......@@ -90,12 +90,6 @@ public final class ClockContract {
* <P>Type: BOOLEAN</P>
*/
public static final String INCREASING_VOLUME = "incvol";
/**
* Profile to change to when alarm triggers
* <P>Type: STRING</P>
*/
public static final String PROFILE = "profile";
}
/**
......
......@@ -28,7 +28,6 @@ import android.text.TextUtils;
import com.android.deskclock.LogUtils;
import com.android.deskclock.data.Weekdays;
import lineageos.app.ProfileManager;
import java.util.Calendar;
import java.util.UUID;
......@@ -70,13 +69,16 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
*/
private static final int VERSION_10 = 11;
/**
* Removed change profile
*/
private static final int VERSION_11 = 12;
// This creates a default alarm at 8:30 for every Mon,Tue,Wed,Thu,Fri
private static final String DEFAULT_ALARM_1 = "(8, 30, 31, 0, 1, '', NULL, 0, 0, "
+ ProfileManager.NO_PROFILE.toString() + ");";
private static final String DEFAULT_ALARM_1 = "(8, 30, 31, 0, 1, '', NULL, 0, 0);";
// This creates a default alarm at 9:30 for every Sat,Sun
private static final String DEFAULT_ALARM_2 = "(9, 00, 96, 0, 1, '', NULL, 0, 0, "
+ ProfileManager.NO_PROFILE.toString() + ");";
private static final String DEFAULT_ALARM_2 = "(9, 00, 96, 0, 1, '', NULL, 0, 0);";
// Database and table names
static final String DATABASE_NAME = "alarms.db";
......@@ -85,8 +87,8 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
static final String INSTANCES_TABLE_NAME = "alarm_instances";
private static final String SELECTED_CITIES_TABLE_NAME = "selected_cities";
private static void createAlarmsTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + ALARMS_TABLE_NAME + " (" +
private static void createAlarmsTable(SQLiteDatabase db, String alarmsTableName) {
db.execSQL("CREATE TABLE " + alarmsTableName + " (" +
ClockContract.AlarmsColumns._ID + " INTEGER PRIMARY KEY," +
ClockContract.AlarmsColumns.HOUR + " INTEGER NOT NULL, " +
ClockContract.AlarmsColumns.MINUTES + " INTEGER NOT NULL, " +
......@@ -96,14 +98,12 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
ClockContract.AlarmsColumns.LABEL + " TEXT NOT NULL, " +
ClockContract.AlarmsColumns.RINGTONE + " TEXT, " +
ClockContract.AlarmsColumns.DELETE_AFTER_USE + " INTEGER NOT NULL DEFAULT 0, " +
ClockContract.AlarmsColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0, " +
ClockContract.AlarmsColumns.PROFILE + " TEXT NOT NULL DEFAULT '" +
ProfileManager.NO_PROFILE.toString() + "');");
ClockContract.AlarmsColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0);");
LogUtils.i("Alarms Table created");
}
private static void createInstanceTable(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + INSTANCES_TABLE_NAME + " (" +
private static void createInstanceTable(SQLiteDatabase db, String instanceTableName) {
db.execSQL("CREATE TABLE " + instanceTableName + " (" +
ClockContract.InstancesColumns._ID + " INTEGER PRIMARY KEY," +
ClockContract.InstancesColumns.YEAR + " INTEGER NOT NULL, " +
ClockContract.InstancesColumns.MONTH + " INTEGER NOT NULL, " +
......@@ -117,20 +117,18 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
ClockContract.InstancesColumns.ALARM_ID + " INTEGER REFERENCES " +
ALARMS_TABLE_NAME + "(" + ClockContract.AlarmsColumns._ID + ") " +
"ON UPDATE CASCADE ON DELETE CASCADE, " +
ClockContract.InstancesColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0, " +
ClockContract.InstancesColumns.PROFILE + " TEXT NOT NULL DEFAULT '" +
ProfileManager.NO_PROFILE.toString() + "');");
ClockContract.InstancesColumns.INCREASING_VOLUME + " INTEGER NOT NULL DEFAULT 0);");
LogUtils.i("Instance table created");
}
public ClockDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION_10);
super(context, DATABASE_NAME, null, VERSION_11);
}
@Override
public void onCreate(SQLiteDatabase db) {
createAlarmsTable(db);
createInstanceTable(db);
createAlarmsTable(db, ALARMS_TABLE_NAME);
createInstanceTable(db, INSTANCES_TABLE_NAME);
// insert default alarms
LogUtils.i("Inserting default alarms");
......@@ -144,8 +142,7 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
ClockContract.AlarmsColumns.LABEL + cs +
ClockContract.AlarmsColumns.RINGTONE + cs +
ClockContract.AlarmsColumns.DELETE_AFTER_USE + cs +
ClockContract.AlarmsColumns.INCREASING_VOLUME + cs +
ClockContract.AlarmsColumns.PROFILE + ") VALUES ";
ClockContract.AlarmsColumns.INCREASING_VOLUME + ") VALUES ";
db.execSQL(insertMe + DEFAULT_ALARM_1);
db.execSQL(insertMe + DEFAULT_ALARM_2);
}
......@@ -164,8 +161,8 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";");
// Create new alarms table and copy over the data
createAlarmsTable(db);
createInstanceTable(db);
createAlarmsTable(db, ALARMS_TABLE_NAME);
createInstanceTable(db, INSTANCES_TABLE_NAME);
LogUtils.i("Copying old alarms to new table");
final String[] OLD_TABLE_COLUMNS = {
......@@ -177,8 +174,7 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
"vibrate",
"message",
"alert",
"incvol",
"profile"
"incvol"
};
try (Cursor cursor = db.query(OLD_ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS,
null, null, null, null, null)) {
......@@ -202,16 +198,6 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
}
alarm.increasingVolume = cursor.getInt(8) == 1;
if (cursor.isNull(9)) {
alarm.profile = ProfileManager.NO_PROFILE;
} else {
try {
alarm.profile = UUID.fromString(cursor.getString(9));
} catch (IllegalArgumentException ex) {
alarm.profile = ProfileManager.NO_PROFILE;
}
}
// Save new version of alarm and create alarm instance for it
db.insert(ALARMS_TABLE_NAME, null, Alarm.createContentValues(alarm));
if (alarm.enabled) {
......@@ -238,11 +224,53 @@ class ClockDatabaseHelper extends SQLiteOpenHelper {
if (oldVersion < VERSION_10) {
db.execSQL("ALTER TABLE " + ALARMS_TABLE_NAME
+ " ADD COLUMN " + ClockContract.AlarmsColumns.PROFILE
+ " TEXT NOT NULL DEFAULT '" + ProfileManager.NO_PROFILE + "';");
+ " ADD COLUMN profile"
+ " TEXT NOT NULL DEFAULT '';");
db.execSQL("ALTER TABLE " + INSTANCES_TABLE_NAME
+ " ADD COLUMN " + ClockContract.InstancesColumns.PROFILE
+ " TEXT NOT NULL DEFAULT '" + ProfileManager.NO_PROFILE + "';");
+ " ADD COLUMN profile"
+ " TEXT NOT NULL DEFAULT '';");
}
if (oldVersion < VERSION_11) {
LogUtils.i("Copying alarms to temporary table");
final String TEMP_ALARMS_TABLE_NAME = ALARMS_TABLE_NAME + "_temp";
final String TEMP_INSTANCES_TABLE_NAME = INSTANCES_TABLE_NAME + "_temp";
createAlarmsTable(db, TEMP_ALARMS_TABLE_NAME);
createInstanceTable(db, TEMP_INSTANCES_TABLE_NAME);
final String[] OLD_TABLE_COLUMNS = {
ClockContract.AlarmsColumns._ID,
ClockContract.AlarmsColumns.HOUR,
ClockContract.AlarmsColumns.MINUTES,
ClockContract.AlarmsColumns.DAYS_OF_WEEK,
ClockContract.AlarmsColumns.ENABLED,
ClockContract.AlarmsColumns.VIBRATE,
ClockContract.AlarmsColumns.LABEL,
ClockContract.AlarmsColumns.RINGTONE,
ClockContract.AlarmsColumns.DELETE_AFTER_USE,
ClockContract.AlarmsColumns.INCREASING_VOLUME
};
try (Cursor cursor = db.query(ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS,
null, null, null, null, null)) {
final Calendar currentTime = Calendar.getInstance();
while (cursor != null && cursor.moveToNext()) {
final Alarm alarm = new Alarm(cursor);
// Save new version of alarm and create alarm instance for it
db.insert(TEMP_ALARMS_TABLE_NAME, null,
Alarm.createContentValues(alarm));
if (alarm.enabled) {
AlarmInstance newInstance = alarm.createInstanceAfter(currentTime);
db.insert(TEMP_INSTANCES_TABLE_NAME, null,
AlarmInstance.createContentValues(newInstance));
}
}
}
db.execSQL("DROP TABLE IF EXISTS " + ALARMS_TABLE_NAME + ";");
db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";");
db.execSQL("ALTER TABLE " + TEMP_ALARMS_TABLE_NAME
+ " RENAME TO " + ALARMS_TABLE_NAME + ";");
db.execSQL("ALTER TABLE " + TEMP_INSTANCES_TABLE_NAME
+ " RENAME TO " + INSTANCES_TABLE_NAME + ";");
}
}
......
......@@ -77,8 +77,6 @@ public class ClockProvider extends ContentProvider {
ALARMS_TABLE_NAME + "." + AlarmsColumns.DELETE_AFTER_USE);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.INCREASING_VOLUME,
ALARMS_TABLE_NAME + "." + AlarmsColumns.INCREASING_VOLUME);
sAlarmsWithInstancesProjection.put(ALARMS_TABLE_NAME + "." + AlarmsColumns.PROFILE,
ALARMS_TABLE_NAME + "." + AlarmsColumns.PROFILE);
sAlarmsWithInstancesProjection.put(INSTANCES_TABLE_NAME + "."
+ InstancesColumns.ALARM_STATE,
INSTANCES_TABLE_NAME + "." + InstancesColumns.ALARM_STATE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment