Search in sources :

Example 1 with RecommendationDatabaseHelper

use of com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper in project zype-firebuilder by zype.

the class RecommendationSenderTest method testUpdateRecommendations.

/**
 * Test updating recommendations.
 */
@Test
public void testUpdateRecommendations() throws Exception {
    RecommendationDatabaseHelper databaseHelper = RecommendationDatabaseHelper.getInstance();
    Context context = InstrumentationRegistry.getContext();
    String type = RecommendationRecord.GLOBAL;
    RecommendationRecord r1 = new RecommendationRecord("1", 1, RecommendationRecord.RELATED);
    assertNotNull(databaseHelper);
    databaseHelper.addRecord(context, r1);
    List<RecommendationRecord> list = new ArrayList<>();
    list.add(r1);
    List<Integer> idsForNewRecs = new ArrayList<>(Arrays.asList(1, 2, 3));
    List<String> contentIdsForNewRecs = new ArrayList<>(Arrays.asList("1", "2", "3"));
    assertTrue("Recommendations should have been updated successfully", mSender.updateExistingRecommendations(type, list, contentIdsForNewRecs, idsForNewRecs));
    assertFalse("Id should have been removed.", idsForNewRecs.contains(1));
    assertFalse("Id should have been removed.", contentIdsForNewRecs.contains("1"));
    RecommendationRecord updatedR1 = databaseHelper.getRecord(context, "1");
    assertTrue("Record should have been updated", updatedR1.getType().equals(RecommendationRecord.GLOBAL));
    assertFalse("Recommendations should not be updated with null input parameters", mSender.updateExistingRecommendations(type, null, null, null));
    databaseHelper.getDatabase(context).close();
}
Also used : Context(android.content.Context) RecommendationDatabaseHelper(com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper) ArrayList(java.util.ArrayList) RecommendationRecord(com.amazon.android.contentbrowser.database.records.RecommendationRecord) Test(org.junit.Test)

Example 2 with RecommendationDatabaseHelper

use of com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper in project zype-firebuilder by zype.

the class RecommendationSenderTest method testGetRecordsToDelete.

/**
 * Tests getting the list of records to delete.
 */
@Test
public void testGetRecordsToDelete() throws Exception {
    RecommendationDatabaseHelper databaseHelper = RecommendationDatabaseHelper.getInstance();
    Context context = InstrumentationRegistry.getContext();
    String type = RecommendationRecord.GLOBAL;
    // Case 1:
    // Database empty; new 5; update 0; max 5
    assertNotNull(databaseHelper);
    databaseHelper.clearDatabase(context);
    List<RecommendationRecord> toDelete = mSender.getRecordsToDelete(type, 5, new ArrayList<>(), 5);
    assertTrue("No recommendations should be deleted", toDelete.isEmpty());
    // Case 2:
    // Database 3; new 4; update 0; max 5; expecting to deleteByContentId 2
    RecommendationRecord r1 = new RecommendationRecord("1", 1, RecommendationRecord.GLOBAL);
    RecommendationRecord r2 = new RecommendationRecord("2", 2, RecommendationRecord.GLOBAL);
    RecommendationRecord r3 = new RecommendationRecord("3", 3, RecommendationRecord.GLOBAL);
    databaseHelper.addRecord(context, r1);
    databaseHelper.addRecord(context, r2);
    databaseHelper.addRecord(context, r3);
    toDelete = mSender.getRecordsToDelete(type, 4, new ArrayList<>(), 5);
    assertEquals("3 recommendations should be deleted", 2, toDelete.size());
    assertTrue("Rec with id 1 should be deleted", toDelete.contains(r1));
    assertTrue("Rec with id 2 should be deleted", toDelete.contains(r2));
    // Case 3:
    // Database 3, new 5, update 3; max 5; expected to deleteByContentId 0
    List<RecommendationRecord> toUpdate = new ArrayList<>();
    toUpdate.add(r1);
    toUpdate.add(r2);
    toUpdate.add(r3);
    toDelete = mSender.getRecordsToDelete(type, 5, toUpdate, 5);
    assertTrue("No recommendations should be deleted", toDelete.isEmpty());
    // Case 4
    // Database 3, new 10, update 0; max 5
    toDelete = mSender.getRecordsToDelete(type, 10, new ArrayList<>(), 5);
    assertEquals("3 recommendations should be deleted", 3, toDelete.size());
    // Case 5
    // Database 3, new 1, update 0; max 5
    toDelete = mSender.getRecordsToDelete(type, 1, new ArrayList<>(), 5);
    assertTrue("No recommendations should be deleted", toDelete.isEmpty());
    databaseHelper.getDatabase(context).close();
}
Also used : Context(android.content.Context) RecommendationDatabaseHelper(com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper) ArrayList(java.util.ArrayList) RecommendationRecord(com.amazon.android.contentbrowser.database.records.RecommendationRecord) Test(org.junit.Test)

Example 3 with RecommendationDatabaseHelper

use of com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper in project zype-firebuilder by zype.

the class RecommendationSender method buildRecommendationIdList.

/**
 * Generates of list of recommendation ids to use from 1 to the n, the max number of
 * recommendations to send at one time.
 *
 * @param max      The max number of recommendations to send at one time.
 * @return List of recommendation ids.
 */
private ArrayList<Integer> buildRecommendationIdList(int max) {
    ArrayList<Integer> ids = new ArrayList<>();
    RecommendationDatabaseHelper databaseHelper = RecommendationDatabaseHelper.getInstance();
    List<Integer> usedIds = databaseHelper.getAllRecommendationsIds(mContext);
    int i = 1;
    while (ids.size() < max) {
        if (!usedIds.contains(i)) {
            ids.add(i);
        }
        i++;
    }
    return ids;
}
Also used : RecommendationDatabaseHelper(com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper) ArrayList(java.util.ArrayList)

Example 4 with RecommendationDatabaseHelper

use of com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper in project zype-firebuilder by zype.

the class RecommendationSender method updateExistingRecommendations.

/**
 * Updates the given recommendations records with a new type. Removes the updated
 * recommendation id from the list of possible ids for new recommendations. Builds the new
 * notification and sends it to notification manager if ({@link #mSendToNotificationManager}
 * is true.
 *
 * If the app runs into an error during this process, it is possible that the app's
 * database becomes out of sync with the device's database regarding notifications. However,
 * this does not pose much of a threat to user experience.
 *
 * @param type                The type of recommendation.
 * @param recsToUpdate        The recommendation records to update in the database.
 * @param contentIdsOfNewRecs A list of content ids that need to be recommended.
 * @param idsForNewRecs       A list of ids to use for new recommendations.
 * @return False if the parameters were bad; true otherwise.
 */
boolean updateExistingRecommendations(String type, List<RecommendationRecord> recsToUpdate, List<String> contentIdsOfNewRecs, List<Integer> idsForNewRecs) {
    RecommendationDatabaseHelper databaseHelper = RecommendationDatabaseHelper.getInstance();
    if (recsToUpdate == null || databaseHelper == null || idsForNewRecs == null || contentIdsOfNewRecs == null) {
        Log.e(TAG, "Parameters should not be null when updating recommendations");
        return false;
    }
    for (RecommendationRecord record : recsToUpdate) {
        // Update the record data. We can use same rec id but should update the type.
        record.setType(type);
        databaseHelper.updateRecord(mContext, record);
        // Remove the rec id so its not used later.
        idsForNewRecs.remove(Integer.valueOf(record.getRecommendationId()));
        // Remove the content id so another recommendation isn't sent later.
        contentIdsOfNewRecs.remove(record.getContentId());
        // Build new notification
        Notification notification = buildRecommendation(record.getContentId(), record.getRecommendationId(), type);
        // Cancel old notification and send the new
        if (mSendToNotificationManager) {
            mNotificationManager.cancel(record.getRecommendationId());
            if (notification != null) {
                mNotificationManager.notify(record.getRecommendationId(), notification);
            }
        }
    }
    return true;
}
Also used : RecommendationDatabaseHelper(com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper) RecommendationRecord(com.amazon.android.contentbrowser.database.records.RecommendationRecord) Notification(android.app.Notification)

Example 5 with RecommendationDatabaseHelper

use of com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper in project zype-firebuilder by zype.

the class RecommendationSender method sendNewRecommendations.

/**
 * Creates recommendations, stores them to the database, and sends them to notification
 * manager (if {@link #mSendToNotificationManager} is true. If the list of recommendation ids
 * is smaller than the list of content ids, no recommendations are sent. We need an id for each
 * new recommendation to send.
 *
 * If the app runs into an error during this process, it is possible that the app's
 * database becomes out of sync with the device's database regarding notifications. However,
 * this does not pose much of a threat to user experience.
 *
 * @param type                The type of recommendation.
 * @param idsForNewRecs       A list of available content ids.
 * @param contentIdsOfNewRecs Ids of the content to recommend.
 * @return True if recommendations were sent; false otherwise.
 */
boolean sendNewRecommendations(String type, List<Integer> idsForNewRecs, List<String> contentIdsOfNewRecs) {
    RecommendationDatabaseHelper databaseHelper = RecommendationDatabaseHelper.getInstance();
    if (contentIdsOfNewRecs == null || idsForNewRecs == null || databaseHelper == null) {
        Log.e(TAG, "Parameters should not be null");
        return false;
    }
    if (contentIdsOfNewRecs.size() > idsForNewRecs.size()) {
        Log.e(TAG, "Not enough recommendation ids sent to send all new recommendations. Not " + "sending any.");
        return false;
    }
    for (String contentId : contentIdsOfNewRecs) {
        Integer recommendationId = idsForNewRecs.remove(0);
        Notification notification = buildRecommendation(contentId, recommendationId, type);
        databaseHelper.addRecord(mContext, contentId, recommendationId, type);
        if (mSendToNotificationManager) {
            sendToNotificationManager(mContext, recommendationId, notification);
        }
    }
    return true;
}
Also used : RecommendationDatabaseHelper(com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper) Notification(android.app.Notification)

Aggregations

RecommendationDatabaseHelper (com.amazon.android.contentbrowser.database.helpers.RecommendationDatabaseHelper)15 RecommendationRecord (com.amazon.android.contentbrowser.database.records.RecommendationRecord)8 ArrayList (java.util.ArrayList)7 Context (android.content.Context)6 Test (org.junit.Test)6 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 RecentDatabaseHelper (com.amazon.android.contentbrowser.database.helpers.RecentDatabaseHelper)1 Content (com.amazon.android.model.content.Content)1 ContentContainer (com.amazon.android.model.content.ContentContainer)1 ContentUpdateEvent (com.amazon.android.model.event.ContentUpdateEvent)1