|
|
@ -10,6 +10,7 @@ import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.core.util.ObjectsCompat; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
@ -19,38 +20,60 @@ import awaisomereport.LogCollector; |
|
|
|
import static awais.instagrabber.utils.Utils.logCollector; |
|
|
|
|
|
|
|
public final class DataBox extends SQLiteOpenHelper { |
|
|
|
private static final String TAG = "DataBox"; |
|
|
|
|
|
|
|
private static DataBox sInstance; |
|
|
|
private final static int VERSION = 1; |
|
|
|
|
|
|
|
private final static int VERSION = 2; |
|
|
|
private final static String TABLE_COOKIES = "cookies"; |
|
|
|
private final static String TABLE_FAVORITES = "favorites"; |
|
|
|
private final static String KEY_DATE_ADDED = "date_added"; |
|
|
|
private final static String KEY_QUERY_TEXT = "query_text"; |
|
|
|
private final static String KEY_QUERY_DISPLAY = "query_display"; |
|
|
|
|
|
|
|
private final static String KEY_ID = "id"; |
|
|
|
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; |
|
|
|
private final static String KEY_FULL_NAME = "full_name"; |
|
|
|
private final static String KEY_PROFILE_PIC = "profile_pic"; |
|
|
|
|
|
|
|
private final Context c; |
|
|
|
|
|
|
|
public static synchronized DataBox getInstance(final Context context) { |
|
|
|
if (sInstance == null) sInstance = new DataBox(context.getApplicationContext()); |
|
|
|
return sInstance; |
|
|
|
} |
|
|
|
|
|
|
|
public DataBox(@Nullable final Context context) { |
|
|
|
private DataBox(@Nullable final Context context) { |
|
|
|
super(context, "cookiebox.db", null, VERSION); |
|
|
|
c = context; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onCreate(@NonNull final SQLiteDatabase db) { |
|
|
|
db.execSQL("CREATE TABLE cookies (id INTEGER PRIMARY KEY, uid TEXT, username TEXT, cookie TEXT)"); |
|
|
|
Log.i(TAG, "Creating tables..."); |
|
|
|
db.execSQL("CREATE TABLE " + TABLE_COOKIES + " (" |
|
|
|
+ KEY_ID + " INTEGER PRIMARY KEY," |
|
|
|
+ KEY_UID + " TEXT," |
|
|
|
+ KEY_USERNAME + " TEXT," |
|
|
|
+ KEY_COOKIE + " TEXT," |
|
|
|
+ KEY_FULL_NAME + " TEXT," |
|
|
|
+ KEY_PROFILE_PIC + " TEXT)"); |
|
|
|
db.execSQL("CREATE TABLE favorites (id INTEGER PRIMARY KEY, query_text TEXT, date_added INTEGER, query_display TEXT)"); |
|
|
|
Log.i(TAG, "Tables created!"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) { } |
|
|
|
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) { |
|
|
|
Log.i(TAG, String.format("Updating DB from v%d to v%d", oldVersion, newVersion)); |
|
|
|
if (oldVersion == 1) { |
|
|
|
db.execSQL("ALTER TABLE " + TABLE_COOKIES + " ADD " + KEY_FULL_NAME + " TEXT"); |
|
|
|
db.execSQL("ALTER TABLE " + TABLE_COOKIES + " ADD " + KEY_PROFILE_PIC + " TEXT"); |
|
|
|
} |
|
|
|
Log.i(TAG, String.format("DB update from v%d to v%d completed!", oldVersion, newVersion)); |
|
|
|
} |
|
|
|
|
|
|
|
///////////////////////////////////////// YOUR FAVORITES! HERE ///////////////////////////////////////// |
|
|
|
public final void addFavorite(@NonNull final FavoriteModel favoriteModel) { |
|
|
|
final String query = favoriteModel.getQuery(); |
|
|
|
final String display = favoriteModel.getDisplayName(); |
|
|
@ -87,16 +110,16 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final int rowsDeleted = db.delete(TABLE_FAVORITES, "query_text=? AND date_added=?", |
|
|
|
new String[]{query, Long.toString(favoriteModel.getDate())}); |
|
|
|
new String[]{query, Long.toString(favoriteModel.getDate())}); |
|
|
|
|
|
|
|
final int rowsDeletedTwo = db.delete(TABLE_FAVORITES, "query_text=? AND date_added=?", |
|
|
|
new String[]{query.replaceAll("@", ""), Long.toString(favoriteModel.getDate())}); |
|
|
|
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"); |
|
|
|
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
|
|
if (BuildConfig.DEBUG) Log.e(TAG, "Error", e); |
|
|
|
} finally { |
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
@ -117,12 +140,13 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
do { |
|
|
|
tempFav = new FavoriteModel( |
|
|
|
(cursor.getString(0).charAt(0) == '@' || cursor.getString(0).charAt(0) == '#' || cursor.getString(0).contains("/")) |
|
|
|
? cursor.getString(0) |
|
|
|
: "@" + cursor.getString(0), // query text |
|
|
|
? cursor.getString(0) |
|
|
|
: "@" + cursor.getString(0), // query text |
|
|
|
cursor.getLong(1), // date added |
|
|
|
cursor.getString(2) == null ? (cursor.getString(0).charAt(0) == '@' || cursor.getString(0).charAt(0) == '#' || cursor.getString(0).contains("/")) |
|
|
|
? cursor.getString(0) |
|
|
|
: "@" + cursor.getString(0) : cursor.getString(2) // display |
|
|
|
cursor.getString(2) == null ? (cursor.getString(0).charAt(0) == '@' || 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 { |
|
|
@ -154,7 +178,7 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
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); |
|
|
|
if (BuildConfig.DEBUG) Log.e(TAG, "", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -162,11 +186,9 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
} |
|
|
|
|
|
|
|
public final String getFavorite(@NonNull final String query) { |
|
|
|
ArrayList<FavoriteModel> favorites = null; |
|
|
|
|
|
|
|
try (final SQLiteDatabase db = getReadableDatabase(); |
|
|
|
final Cursor cursor = db.rawQuery("SELECT query_text, date_added FROM favorites WHERE " |
|
|
|
+KEY_QUERY_TEXT+"='"+query+"' ORDER BY date_added DESC", null)) { |
|
|
|
+ KEY_QUERY_TEXT + "='" + query + "' ORDER BY date_added DESC", null)) { |
|
|
|
if (cursor != null && cursor.moveToFirst()) { |
|
|
|
return cursor.getString(0) + "/" + String.valueOf(cursor.getLong(1)); |
|
|
|
} |
|
|
@ -174,31 +196,33 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
///////////////////////////////////// YOUR COOKIES FOR COOKIE MONSTER ARE HERE ///////////////////////////////////// |
|
|
|
public final void addUserCookie(@NonNull final CookieModel cookieModel) { |
|
|
|
final String cookieModelUid = cookieModel.getUid(); |
|
|
|
if (!Utils.isEmpty(cookieModelUid)) { |
|
|
|
try (final SQLiteDatabase db = getWritableDatabase()) { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final ContentValues values = new ContentValues(); |
|
|
|
values.put(KEY_USERNAME, cookieModel.getUsername()); |
|
|
|
values.put(KEY_COOKIE, cookieModel.getCookie()); |
|
|
|
values.put(KEY_UID, cookieModelUid); |
|
|
|
public final void addOrUpdateUser(final String uid, |
|
|
|
final String username, |
|
|
|
final String cookie, |
|
|
|
final String fullName, |
|
|
|
final String profilePicUrl) { |
|
|
|
if (Utils.isEmpty(uid)) return; |
|
|
|
try (final SQLiteDatabase db = getWritableDatabase()) { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final ContentValues values = new ContentValues(); |
|
|
|
values.put(KEY_USERNAME, username); |
|
|
|
values.put(KEY_COOKIE, cookie); |
|
|
|
values.put(KEY_UID, uid); |
|
|
|
values.put(KEY_FULL_NAME, fullName); |
|
|
|
values.put(KEY_PROFILE_PIC, profilePicUrl); |
|
|
|
|
|
|
|
final int rows = db.update(TABLE_COOKIES, values, KEY_UID + "=?", new String[]{cookieModelUid}); |
|
|
|
final int rows = db.update(TABLE_COOKIES, values, KEY_UID + "=?", new String[]{uid}); |
|
|
|
|
|
|
|
if (rows != 1) |
|
|
|
db.insertOrThrow(TABLE_COOKIES, null, values); |
|
|
|
if (rows != 1) |
|
|
|
db.insertOrThrow(TABLE_COOKIES, null, values); |
|
|
|
|
|
|
|
db.setTransactionSuccessful(); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
|
|
} finally { |
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
|
db.setTransactionSuccessful(); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (BuildConfig.DEBUG) Log.e(TAG, "Error", e); |
|
|
|
} finally { |
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -210,7 +234,7 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final int rowsDeleted = db.delete(TABLE_COOKIES, KEY_UID + "=? AND " + KEY_USERNAME + "=? AND " + KEY_COOKIE + "=?", |
|
|
|
new String[]{cookieModelUid, cookieModel.getUsername(), cookieModel.getCookie()}); |
|
|
|
new String[]{cookieModelUid, cookieModel.getUsername(), cookieModel.getCookie()}); |
|
|
|
|
|
|
|
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
|
|
} catch (final Exception e) { |
|
|
@ -222,6 +246,21 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public final synchronized void deleteAllUserCookies() { |
|
|
|
try (final SQLiteDatabase db = getWritableDatabase()) { |
|
|
|
db.beginTransaction(); |
|
|
|
try { |
|
|
|
final int rowsDeleted = db.delete(TABLE_COOKIES, null, null); |
|
|
|
|
|
|
|
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
|
|
} catch (final Exception e) { |
|
|
|
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
|
|
} finally { |
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public final int getCookieCount() { |
|
|
|
int cookieCount = 0; |
|
|
|
try (final SQLiteDatabase db = getReadableDatabase(); |
|
|
@ -235,12 +274,24 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
public final CookieModel getCookie(final String uid) { |
|
|
|
CookieModel cookie = null; |
|
|
|
try (final SQLiteDatabase db = getReadableDatabase(); |
|
|
|
final Cursor cursor = db.rawQuery("SELECT uid, username, cookie FROM cookies WHERE uid = ?", new String[]{uid})) { |
|
|
|
final Cursor cursor = db.rawQuery( |
|
|
|
"SELECT " |
|
|
|
+ KEY_UID + "," |
|
|
|
+ KEY_USERNAME + "," |
|
|
|
+ KEY_COOKIE + "," |
|
|
|
+ KEY_FULL_NAME + "," |
|
|
|
+ KEY_PROFILE_PIC |
|
|
|
+ " FROM " + TABLE_COOKIES |
|
|
|
+ " WHERE " + KEY_UID + " = ?", |
|
|
|
new String[]{uid}) |
|
|
|
) { |
|
|
|
if (cursor != null && cursor.moveToFirst()) |
|
|
|
cookie = new CookieModel( |
|
|
|
cursor.getString(0), // uid |
|
|
|
cursor.getString(1), // username |
|
|
|
cursor.getString(2) // cookie |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_UID)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_USERNAME)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_COOKIE)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_FULL_NAME)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_PROFILE_PIC)) |
|
|
|
); |
|
|
|
} |
|
|
|
return cookie; |
|
|
@ -251,14 +302,24 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
ArrayList<CookieModel> cookies = null; |
|
|
|
|
|
|
|
try (final SQLiteDatabase db = getReadableDatabase(); |
|
|
|
final Cursor cursor = db.rawQuery("SELECT uid, username, cookie FROM cookies", null)) { |
|
|
|
final Cursor cursor = db.rawQuery( |
|
|
|
"SELECT " |
|
|
|
+ KEY_UID + "," |
|
|
|
+ KEY_USERNAME + "," |
|
|
|
+ KEY_COOKIE + "," |
|
|
|
+ KEY_FULL_NAME + "," |
|
|
|
+ KEY_PROFILE_PIC |
|
|
|
+ " FROM " + TABLE_COOKIES, null) |
|
|
|
) { |
|
|
|
if (cursor != null && cursor.moveToFirst()) { |
|
|
|
cookies = new ArrayList<>(); |
|
|
|
do { |
|
|
|
cookies.add(new CookieModel( |
|
|
|
cursor.getString(0), // uid |
|
|
|
cursor.getString(1), // username |
|
|
|
cursor.getString(2) // cookie |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_UID)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_USERNAME)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_COOKIE)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_FULL_NAME)), |
|
|
|
cursor.getString(cursor.getColumnIndex(KEY_PROFILE_PIC)) |
|
|
|
)); |
|
|
|
} while (cursor.moveToNext()); |
|
|
|
} |
|
|
@ -266,16 +327,25 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
|
|
|
|
return cookies; |
|
|
|
} |
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
public static class CookieModel { |
|
|
|
private final String uid, username, cookie; |
|
|
|
private final String uid; |
|
|
|
private final String username; |
|
|
|
private final String cookie; |
|
|
|
private final String fullName; |
|
|
|
private final String profilePic; |
|
|
|
private boolean selected; |
|
|
|
|
|
|
|
public CookieModel(final String uid, final String username, final String cookie) { |
|
|
|
public CookieModel(final String uid, |
|
|
|
final String username, |
|
|
|
final String cookie, |
|
|
|
final String fullName, |
|
|
|
final String profilePic) { |
|
|
|
this.uid = uid; |
|
|
|
this.username = username; |
|
|
|
this.cookie = cookie; |
|
|
|
this.fullName = fullName; |
|
|
|
this.profilePic = profilePic; |
|
|
|
} |
|
|
|
|
|
|
|
public String getUid() { |
|
|
@ -290,6 +360,14 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
return cookie; |
|
|
|
} |
|
|
|
|
|
|
|
public String getFullName() { |
|
|
|
return fullName; |
|
|
|
} |
|
|
|
|
|
|
|
public String getProfilePic() { |
|
|
|
return profilePic; |
|
|
|
} |
|
|
|
|
|
|
|
public boolean isSelected() { |
|
|
|
return selected; |
|
|
|
} |
|
|
@ -298,6 +376,21 @@ public final class DataBox extends SQLiteOpenHelper { |
|
|
|
this.selected = selected; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean equals(final Object o) { |
|
|
|
if (this == o) return true; |
|
|
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
final CookieModel that = (CookieModel) o; |
|
|
|
return ObjectsCompat.equals(uid, that.uid) && |
|
|
|
ObjectsCompat.equals(username, that.username) && |
|
|
|
ObjectsCompat.equals(cookie, that.cookie); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int hashCode() { |
|
|
|
return ObjectsCompat.hash(uid, username, cookie); |
|
|
|
} |
|
|
|
|
|
|
|
@NonNull |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|