1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.homepage.contextualcards; 18 19 import static com.android.settings.homepage.contextualcards.CardDatabaseHelper.CARD_TABLE; 20 21 import android.content.ContentValues; 22 import android.content.Context; 23 import android.database.Cursor; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.os.Build; 26 import android.text.format.DateUtils; 27 import android.util.Log; 28 29 import androidx.annotation.VisibleForTesting; 30 31 import com.android.settingslib.utils.ThreadUtils; 32 33 public class ContextualCardFeatureProviderImpl implements ContextualCardFeatureProvider { 34 private static final String TAG = "ContextualCardFeatureProvider"; 35 36 private final Context mContext; 37 ContextualCardFeatureProviderImpl(Context context)38 public ContextualCardFeatureProviderImpl(Context context) { 39 mContext = context; 40 } 41 42 @Override getContextualCards()43 public Cursor getContextualCards() { 44 final SQLiteDatabase db = CardDatabaseHelper.getInstance(mContext).getReadableDatabase(); 45 //TODO(b/149542061): Make the dismissal duration configurable. 46 final long threshold = System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS; 47 final String selection = CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP + " < ? OR " 48 + CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP + " IS NULL"; 49 final String[] selectionArgs = {String.valueOf(threshold)}; 50 final Cursor cursor = db.query(CARD_TABLE, null /* columns */, selection, 51 selectionArgs /* selectionArgs */, null /* groupBy */, null /* having */, 52 CardDatabaseHelper.CardColumns.SCORE + " DESC" /* orderBy */); 53 ThreadUtils.postOnBackgroundThread(() -> resetDismissedTime(threshold)); 54 return cursor; 55 } 56 57 @Override markCardAsDismissed(Context context, String cardName)58 public int markCardAsDismissed(Context context, String cardName) { 59 final SQLiteDatabase db = CardDatabaseHelper.getInstance(mContext).getWritableDatabase(); 60 final ContentValues values = new ContentValues(); 61 values.put(CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP, System.currentTimeMillis()); 62 final String selection = CardDatabaseHelper.CardColumns.NAME + "=?"; 63 final String[] selectionArgs = {cardName}; 64 final int rowsUpdated = db.update(CARD_TABLE, values, selection, selectionArgs); 65 context.getContentResolver().notifyChange(CardContentProvider.DELETE_CARD_URI, null); 66 return rowsUpdated; 67 } 68 69 @VisibleForTesting resetDismissedTime(long threshold)70 int resetDismissedTime(long threshold) { 71 final SQLiteDatabase database = 72 CardDatabaseHelper.getInstance(mContext).getWritableDatabase(); 73 final ContentValues values = new ContentValues(); 74 values.putNull(CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP); 75 final String selection = CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP + " < ? AND " 76 + CardDatabaseHelper.CardColumns.DISMISSED_TIMESTAMP + " IS NOT NULL"; 77 final String[] selectionArgs = {String.valueOf(threshold)}; 78 final int rowsUpdated = database.update(CARD_TABLE, values, selection, selectionArgs); 79 if (Build.IS_DEBUGGABLE) { 80 Log.d(TAG, "Reset " + rowsUpdated + " records of dismissed time."); 81 } 82 return rowsUpdated; 83 } 84 } 85