|
|
@ -6,6 +6,7 @@ import android.database.Cursor; |
|
|
|
import android.database.sqlite.SQLiteDatabase; |
|
|
|
import android.database.sqlite.SQLiteOpenHelper; |
|
|
|
import android.util.Log; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
@ -16,7 +17,6 @@ import awais.instagrabber.BuildConfig; |
|
|
|
import awaisomereport.LogCollector; |
|
|
|
|
|
|
|
import static awais.instagrabber.utils.Utils.logCollector; |
|
|
|
import static awais.instagrabber.utils.Utils.settingsHelper; |
|
|
|
|
|
|
|
public final class DataBox extends SQLiteOpenHelper { |
|
|
|
private static DataBox sInstance; |
|
|
@ -29,6 +29,7 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
private final static String KEY_USERNAME = Constants.EXTRAS_USERNAME; |
|
|
|
private final static String KEY_COOKIE = "cookie"; |
|
|
|
private final static String KEY_UID = "uid"; |
|
|
|
private Context c; |
|
|
|
|
|
|
|
public static synchronized DataBox getInstance(final Context context) { |
|
|
|
if (sInstance == null) sInstance = new DataBox(context.getApplicationContext()); |
|
|
@ -37,11 +38,11 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
public DataBox(@Nullable final Context context) { |
|
|
|
super(context, "cookiebox.db", null, VERSION); |
|
|
|
c = context; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onCreate(@NonNull final SQLiteDatabase db) { |
|
|
|
settingsHelper.putBoolean(Constants.DB_TO_MIGRATE, false); |
|
|
|
db.execSQL("CREATE TABLE cookies (id INTEGER PRIMARY KEY, uid TEXT, username TEXT, cookie TEXT)"); |
|
|
|
db.execSQL("CREATE TABLE favorites (id INTEGER PRIMARY KEY, query_text TEXT, date_added INTEGER, query_display TEXT)"); |
|
|
|
} |
|
|
@ -81,17 +82,19 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
public final synchronized void delFavorite(@NonNull final FavoriteModel favoriteModel) { |
|
|
|
final String query = favoriteModel.getQuery(); |
|
|
|
final String display = favoriteModel.getDisplayName(); |
|
|
|
if (!Utils.isEmpty(query)) { |
|
|
|
try (final SQLiteDatabase db = getWritableDatabase()) { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final int rowsDeleted = db.delete(TABLE_FAVORITES, KEY_QUERY_TEXT + "=? AND " |
|
|
|
+ KEY_DATE_ADDED + "=? AND " |
|
|
|
+ KEY_QUERY_DISPLAY + "=?", |
|
|
|
new String[]{query, Long.toString(favoriteModel.getDate()), display}); |
|
|
|
+ KEY_DATE_ADDED + "=?", |
|
|
|
new String[]{query, Long.toString(favoriteModel.getDate())}); |
|
|
|
|
|
|
|
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
|
|
final int rowsDeletedTwo = db.delete(TABLE_FAVORITES, KEY_QUERY_TEXT + "=? AND " |
|
|
|
+ KEY_DATE_ADDED + "=?", |
|
|
|
new String[]{query.replaceAll("@", ""), Long.toString(favoriteModel.getDate())}); |
|
|
|
|
|
|
|
if (rowsDeleted > 0 || rowsDeletedTwo > 0) db.setTransactionSuccessful(); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (logCollector != null) |
|
|
|
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "delFavorite"); |
|
|
@ -107,33 +110,21 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
public final ArrayList<FavoriteModel> getAllFavorites() { |
|
|
|
ArrayList<FavoriteModel> favorites = null; |
|
|
|
FavoriteModel tempFav; |
|
|
|
final SQLiteDatabase db = getWritableDatabase(); |
|
|
|
|
|
|
|
if (Utils.settingsHelper.getBoolean(Constants.DB_TO_MIGRATE) == true) { |
|
|
|
try (final SQLiteDatabase db = getWritableDatabase()) { |
|
|
|
try { |
|
|
|
db.beginTransaction(); |
|
|
|
db.execSQL("ALTER TABLE favorites ADD query_display TEXT"); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (logCollector != null) |
|
|
|
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "migrate"); |
|
|
|
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
|
|
} finally { |
|
|
|
db.endTransaction(); |
|
|
|
settingsHelper.putBoolean(Constants.DB_TO_MIGRATE, false); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try (final SQLiteDatabase db = getWritableDatabase(); |
|
|
|
final Cursor cursor = db.rawQuery("SELECT query_text, date_added, query_display FROM favorites ORDER BY date_added DESC", null)) { |
|
|
|
try (final Cursor cursor = db.rawQuery("SELECT query_text, date_added, query_display FROM favorites ORDER BY date_added DESC", null)) { |
|
|
|
if (cursor != null && cursor.moveToFirst()) { |
|
|
|
db.beginTransaction(); |
|
|
|
favorites = new ArrayList<>(); |
|
|
|
do { |
|
|
|
tempFav = new FavoriteModel( |
|
|
|
cursor.getString(0), // query text |
|
|
|
(cursor.getString(0).charAt(0) == '@' || cursor.getString(0).contains("/")) |
|
|
|
? cursor.getString(0) |
|
|
|
: "@" + cursor.getString(0), // query text |
|
|
|
cursor.getLong(1), // date added |
|
|
|
cursor.getString(2) == null ? cursor.getString(0) : cursor.getString(2) // display |
|
|
|
cursor.getString(2) == null ? (cursor.getString(0).charAt(0) == '@' || cursor.getString(0).contains("/")) |
|
|
|
? cursor.getString(0) |
|
|
|
: "@" + cursor.getString(0) : cursor.getString(2) // display |
|
|
|
); |
|
|
|
if (cursor.getString(2) == null) { |
|
|
|
try { |
|
|
@ -156,6 +147,17 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
} while (cursor.moveToNext()); |
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
|
} catch (final Exception x) { |
|
|
|
Log.e("austin_debug", "", x); |
|
|
|
try { |
|
|
|
db.execSQL("ALTER TABLE favorites ADD query_display TEXT"); |
|
|
|
Toast.makeText(c, "DB has migrated, launch quick access again.", Toast.LENGTH_SHORT).show(); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (logCollector != null) |
|
|
|
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "migrate"); |
|
|
|
Toast.makeText(c, "DB migration failed, contact maintainer.", Toast.LENGTH_SHORT).show(); |
|
|
|
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return favorites; |
|
|
|