Search in sources :

Example 1 with SubscriptionsTable

use of com.thebluealliance.androidclient.database.tables.SubscriptionsTable in project the-blue-alliance-android by the-blue-alliance.

the class CreateSubscriptionPanel method doInBackground.

@Override
protected Void doInBackground(String... params) {
    String modelKey = params[0];
    FavoritesTable favTable = db.getFavoritesTable();
    SubscriptionsTable subTable = db.getSubscriptionsTable();
    String currentUser = accountController.getSelectedAccount();
    String myKey = MyTBAHelper.createKey(currentUser, modelKey);
    favExists = favTable.exists(myKey);
    currentSettings = "";
    if (subTable.exists(myKey)) {
        currentSettings = subTable.get(myKey).getNotificationSettings();
    }
    return null;
}
Also used : FavoritesTable(com.thebluealliance.androidclient.database.tables.FavoritesTable) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable)

Example 2 with SubscriptionsTable

use of com.thebluealliance.androidclient.database.tables.SubscriptionsTable in project the-blue-alliance-android by the-blue-alliance.

the class GCMMessageHandler method followsTeam.

@Override
public boolean followsTeam(Context context, String teamNumber, String matchKey, String notificationType) {
    String currentUser = mAccountController.getSelectedAccount();
    // "frc111"
    String teamKey = TeamHelper.baseTeamKey("frc" + teamNumber);
    // "r@gmail.com:frc111"
    String teamInterestKey = MyTBAHelper.createKey(currentUser, teamKey);
    String teamAtEventKey = EventTeamHelper.generateKey(MatchHelper.getEventKeyFromMatchKey(matchKey), // "2016calb_frc111"
    teamKey);
    String teamAtEventInterestKey = MyTBAHelper.createKey(currentUser, teamAtEventKey);
    FavoritesTable favTable = mDb.getFavoritesTable();
    SubscriptionsTable subTable = mDb.getSubscriptionsTable();
    return favTable.exists(teamInterestKey) || favTable.exists(teamAtEventInterestKey) || subTable.hasNotificationType(teamInterestKey, notificationType) || subTable.hasNotificationType(teamAtEventInterestKey, notificationType);
}
Also used : FavoritesTable(com.thebluealliance.androidclient.database.tables.FavoritesTable) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable)

Example 3 with SubscriptionsTable

use of com.thebluealliance.androidclient.database.tables.SubscriptionsTable in project the-blue-alliance-android by the-blue-alliance.

the class DatabaseMocker method mockSubscriptionsTable.

public static SubscriptionsTable mockSubscriptionsTable(Database database) {
    SQLiteDatabase db = mock(SQLiteDatabase.class);
    SubscriptionsTable table = new SubscriptionsTable(db);
    when(database.getSubscriptionsTable()).thenReturn(table);
    when(database.getWritableDatabase()).thenReturn(db);
    return table;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable)

Example 4 with SubscriptionsTable

use of com.thebluealliance.androidclient.database.tables.SubscriptionsTable in project the-blue-alliance-android by the-blue-alliance.

the class MyTbaDatafeed method updateUserSubscriptions.

@WorkerThread
public void updateUserSubscriptions() {
    String currentUser = mAccountController.getSelectedAccount();
    String prefString = String.format(LAST_SUBSCRIPTIONS_UPDATE, currentUser);
    Date now = new Date();
    Date futureTime = new Date(mPrefs.getLong(prefString, 0) + Constants.MY_TBA_UPDATE_TIMEOUT);
    if (now.before(futureTime)) {
        TbaLogger.d("Not updating myTBA subscriptions. Too soon since last update");
        return;
    }
    if (!ConnectionDetector.isConnectedToInternet(mApplicationContext)) {
        return;
    }
    TbaLogger.d("Updating myTBA subscriptions");
    if (!mAccountController.isMyTbaEnabled()) {
        TbaLogger.e("MyTBA is not enabled");
        Handler mainHandler = new Handler(mApplicationContext.getMainLooper());
        mainHandler.post(() -> Toast.makeText(mApplicationContext, mRes.getString(R.string.mytba_error_no_account), Toast.LENGTH_SHORT).show());
        return;
    }
    String authHeader = mAuthController.getAuthHeader();
    Response<ModelsMobileApiMessagesSubscriptionCollection> subscriptionResponse;
    try {
        subscriptionResponse = mSubscriptionApi.list(authHeader).execute();
    } catch (IOException e) {
        TbaLogger.w("Unable to update myTBA favorites");
        e.printStackTrace();
        return;
    }
    List<Subscription> subscriptionModels = new ArrayList<>();
    if (subscriptionResponse != null) {
        try {
            ModelsMobileApiMessagesSubscriptionCollection subscriptionCollection = subscriptionResponse.body();
            if (subscriptionCollection != null && subscriptionCollection.subscriptions != null) {
                SubscriptionsTable subscriptions = mDb.getSubscriptionsTable();
                subscriptions.recreate(currentUser);
                for (int i = 0; i < subscriptionCollection.subscriptions.size(); i++) {
                    ModelsMobileApiMessagesSubscriptionMessage s = subscriptionCollection.subscriptions.get(i);
                    subscriptionModels.add(new Subscription(currentUser, s.model_key, s.notifications, s.model_type));
                }
                subscriptions.add(subscriptionModels);
                TbaLogger.d("Added " + subscriptionModels.size() + " subscriptions");
            }
            mPrefs.edit().putLong(prefString, now.getTime()).apply();
        } catch (Exception ex) {
            if (ex instanceof SQLiteDatabaseLockedException) {
                TbaLogger.i("Database locked: " + ex.getMessage());
            } else {
                TbaLogger.e("Unable to update mytba subscriptions", ex);
            }
        }
    }
}
Also used : SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) ArrayList(java.util.ArrayList) Handler(android.os.Handler) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable) IOException(java.io.IOException) ModelsMobileApiMessagesSubscriptionMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionMessage) Date(java.util.Date) SQLiteDatabaseLockedException(android.database.sqlite.SQLiteDatabaseLockedException) IOException(java.io.IOException) ModelsMobileApiMessagesSubscriptionCollection(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionCollection) Subscription(com.thebluealliance.androidclient.models.Subscription) WorkerThread(androidx.annotation.WorkerThread)

Example 5 with SubscriptionsTable

use of com.thebluealliance.androidclient.database.tables.SubscriptionsTable in project the-blue-alliance-android by the-blue-alliance.

the class MyTbaDatafeed method updateModelSettings.

@WorkerThread
@ModelPrefsResult
public int updateModelSettings(ModelNotificationFavoriteSettings settings) {
    String modelKey = settings.modelKey;
    List<String> notifications = settings.enabledNotifications;
    boolean isFavorite = settings.isFavorite;
    String user = mAccountController.getSelectedAccount();
    String key = MyTBAHelper.createKey(user, modelKey);
    ModelType modelType = settings.modelType;
    ModelsMobileApiMessagesModelPreferenceMessage request = new ModelsMobileApiMessagesModelPreferenceMessage();
    request.model_key = modelKey;
    request.device_key = mGcmController.getRegistrationId();
    request.notifications = notifications;
    request.favorite = isFavorite;
    request.model_type = modelType.getEnum();
    SubscriptionsTable subscriptionsTable = mDb.getSubscriptionsTable();
    FavoritesTable favoritesTable = mDb.getFavoritesTable();
    // Determine if we have to do anything
    List<String> existingNotificationsList = new ArrayList<>();
    Subscription existingSubscription = subscriptionsTable.get(key);
    if (existingSubscription != null) {
        existingNotificationsList = existingSubscription.getNotificationList();
    }
    Collections.sort(notifications);
    Collections.sort(existingNotificationsList);
    TbaLogger.d("New notifications: " + notifications.toString());
    TbaLogger.d("Existing notifications: " + existingNotificationsList.toString());
    boolean notificationsHaveChanged = !(notifications.equals(existingNotificationsList));
    // and if the existing notification mSettings equal the new ones, do nothing.
    if (((isFavorite && favoritesTable.exists(key)) || (!isFavorite && !favoritesTable.exists(key))) && !notificationsHaveChanged) {
        // nothing has changed, no-op
        return MODEL_PREF_NOOP;
    } else {
        try {
            String authHeader = mAuthController.getAuthHeader();
            Response<ModelsMobileApiMessagesBaseResponse> response = mModelApi.setPreferences(authHeader, request).execute();
            if (response == null) {
                TbaLogger.w("Null response for mytba update");
                return MODEL_PREF_FAIL;
            }
            if (!response.isSuccessful()) {
                TbaLogger.w("mytba update error " + response.code() + ": " + response.message());
                return MODEL_PREF_FAIL;
            }
            ModelsMobileApiMessagesBaseResponse prefResponse = response.body();
            TbaLogger.d("Mytba result: " + prefResponse.code + "/" + prefResponse.message);
            if (response.code() == 401 || prefResponse.code == 401) {
                TbaLogger.e(prefResponse.message);
                return MODEL_PREF_FAIL;
            }
            JsonObject responseJson = JSONHelper.getasJsonObject(prefResponse.message);
            if (responseJson == null || responseJson.isJsonNull()) {
                return MODEL_PREF_FAIL;
            }
            JsonObject fav = responseJson.get("favorite").getAsJsonObject(), sub = responseJson.get("subscription").getAsJsonObject();
            int favCode = fav.get("code").getAsInt(), subCode = sub.get("code").getAsInt();
            if (subCode == 200) {
                // Request was successful, update the local databases
                if (notifications.isEmpty()) {
                    subscriptionsTable.remove(key);
                } else if (subscriptionsTable.exists(key)) {
                    subscriptionsTable.update(key, new Subscription(user, modelKey, notifications, modelType.getEnum()));
                } else {
                    subscriptionsTable.add(new Subscription(user, modelKey, notifications, modelType.getEnum()));
                }
            } else if (subCode == 500) {
                Toast.makeText(mApplicationContext, mApplicationContext.getString(R.string.mytba_error, subCode, sub.get("message").getAsString()), Toast.LENGTH_SHORT).show();
            }
            if (favCode == 200) {
                if (!isFavorite) {
                    favoritesTable.remove(key);
                } else if (!favoritesTable.exists(key)) {
                    favoritesTable.add(new Favorite(user, modelKey, modelType.getEnum()));
                }
            } else if (favCode == 500) {
                Toast.makeText(mApplicationContext, mApplicationContext.getString(R.string.mytba_error, favCode, fav.get("message").getAsString()), Toast.LENGTH_SHORT).show();
            }
            return MODEL_PREF_SUCCESS;
        } catch (IOException e) {
            TbaLogger.e("IO Exception while updating model preferences!", e);
            e.printStackTrace();
            return MODEL_PREF_FAIL;
        }
    }
}
Also used : Favorite(com.thebluealliance.androidclient.models.Favorite) ModelsMobileApiMessagesModelPreferenceMessage(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesModelPreferenceMessage) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) SubscriptionsTable(com.thebluealliance.androidclient.database.tables.SubscriptionsTable) IOException(java.io.IOException) FavoritesTable(com.thebluealliance.androidclient.database.tables.FavoritesTable) ModelsMobileApiMessagesBaseResponse(com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse) ModelType(com.thebluealliance.androidclient.types.ModelType) Subscription(com.thebluealliance.androidclient.models.Subscription) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

SubscriptionsTable (com.thebluealliance.androidclient.database.tables.SubscriptionsTable)5 FavoritesTable (com.thebluealliance.androidclient.database.tables.FavoritesTable)3 WorkerThread (androidx.annotation.WorkerThread)2 Subscription (com.thebluealliance.androidclient.models.Subscription)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)1 Handler (android.os.Handler)1 ModelsMobileApiMessagesBaseResponse (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesBaseResponse)1 ModelsMobileApiMessagesModelPreferenceMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesModelPreferenceMessage)1 ModelsMobileApiMessagesSubscriptionCollection (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionCollection)1 ModelsMobileApiMessagesSubscriptionMessage (com.appspot.tbatv_prod_hrd.model.ModelsMobileApiMessagesSubscriptionMessage)1 JsonObject (com.google.gson.JsonObject)1 Favorite (com.thebluealliance.androidclient.models.Favorite)1 ModelType (com.thebluealliance.androidclient.types.ModelType)1 Date (java.util.Date)1