import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
-import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import androidx.annotation.VisibleForTesting;
import androidx.slice.Slice;
-import com.android.settings.homepage.contextualcards.deviceinfo.BatterySlice;
import com.android.settings.homepage.contextualcards.slices.ConnectedDeviceSlice;
import com.android.settings.wifi.WifiSlice;
import com.android.settingslib.utils.AsyncLoaderCompat;
private static final String TAG = "ContextualCardLoader";
- private Context mContext;
+ private final ContentObserver mObserver = new ContentObserver(
+ new Handler(Looper.getMainLooper())) {
+ @Override
+ public void onChange(boolean selfChange) {
+ if (isStarted()) {
+ forceLoad();
+ }
+ }
+ };
- public interface CardContentLoaderListener {
- void onFinishCardLoading(List<ContextualCard> contextualCards);
- }
+ private Context mContext;
ContextualCardLoader(Context context) {
super(context);
public List<ContextualCard> loadInBackground() {
final List<ContextualCard> result = new ArrayList<>();
try (Cursor cursor = getContextualCardsFromProvider()) {
- if (cursor.getCount() == 0) {
- result.addAll(createStaticCards());
- } else {
+ if (cursor.getCount() > 0) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
final ContextualCard card = new ContextualCard(cursor);
if (card.isCustomCard()) {
}
@VisibleForTesting
- List<ContextualCard> createStaticCards() {
- final long appVersionCode = getAppVersionCode();
- final String packageName = mContext.getPackageName();
- final double rankingScore = 0.0;
- final List<ContextualCard> result = new ArrayList();
- result.add(new ContextualCard.Builder()
- .setSliceUri(BatterySlice.BATTERY_CARD_URI)
- .setName(BatterySlice.PATH_BATTERY_INFO)
- .setPackageName(packageName)
- .setRankingScore(rankingScore)
- .setAppVersion(appVersionCode)
- .setCardType(ContextualCard.CardType.SLICE)
- .setIsHalfWidth(false)
- .build());
- return result;
- }
-
- @VisibleForTesting
List<ContextualCard> filterEligibleCards(List<ContextualCard> candidates) {
return candidates.stream().filter(card -> isCardEligibleToDisplay(card))
.collect(Collectors.toList());
.count();
}
- private long getAppVersionCode() {
- try {
- return mContext.getPackageManager().getPackageInfo(mContext.getPackageName(),
- 0 /* flags */).getLongVersionCode();
- } catch (PackageManager.NameNotFoundException e) {
- Log.e(TAG, "Invalid package name for context", e);
- }
- return -1L;
+ public interface CardContentLoaderListener {
+ void onFinishCardLoading(List<ContextualCard> contextualCards);
}
-
- private final ContentObserver mObserver = new ContentObserver(
- new Handler(Looper.getMainLooper())) {
- @Override
- public void onChange(boolean selfChange) {
- if (isStarted()) {
- forceLoad();
- }
- }
- };
-}
+}
\ No newline at end of file
import android.annotation.Nullable;
-import com.android.settings.homepage.contextualcards.deviceinfo.BatterySlice;
import com.android.settings.homepage.contextualcards.slices.BatteryFixSlice;
import com.android.settings.homepage.contextualcards.slices.ConnectedDeviceSlice;
import com.android.settings.homepage.contextualcards.slices.LowStorageSlice;
.setCardName(KEY_WIFI)
.setCardCategory(ContextualCard.Category.IMPORTANT)
.build();
- final ContextualCard batteryInfoCard =
- ContextualCard.newBuilder()
- .setSliceUri(BatterySlice.BATTERY_CARD_URI.toString())
- .setCardName(BatterySlice.PATH_BATTERY_INFO)
- .setCardCategory(ContextualCard.Category.DEFAULT)
- .build();
final ContextualCard connectedDeviceCard =
ContextualCard.newBuilder()
.setSliceUri(ConnectedDeviceSlice.CONNECTED_DEVICE_URI.toString())
.build();
final ContextualCardList cards = ContextualCardList.newBuilder()
.addCard(wifiCard)
- .addCard(batteryInfoCard)
.addCard(connectedDeviceCard)
.addCard(lowStorageCard)
.addCard(batteryFixCard)
return cards;
}
-}
+}
\ No newline at end of file
import org.robolectric.shadows.ShadowContentResolver;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
}
@Test
- public void createStaticCards_shouldContainCorrectCards() {
- final Uri batteryInfo = BatterySlice.BATTERY_CARD_URI;
- final List<Uri> expectedUris = Arrays.asList(batteryInfo);
-
- final List<Uri> actualCardUris = mContextualCardLoader.createStaticCards().stream().map(
- ContextualCard::getSliceUri).collect(Collectors.toList());
-
- assertThat(actualCardUris).containsExactlyElementsIn(expectedUris);
- }
-
- @Test
public void isCardEligibleToDisplay_customCard_returnTrue() {
final ContextualCard customCard = new ContextualCard.Builder()
.setName("custom_card")
.build());
return cards;
}
-}
+}
\ No newline at end of file