use of com.android.settingslib.drawer.Tile in project android_packages_apps_Settings by omnirom.
the class SecurityFeatureProviderImpl method updatePreferencesToRunOnWorkerThread.
@VisibleForTesting
void updatePreferencesToRunOnWorkerThread(Context context, PreferenceScreen preferenceScreen, DashboardCategory dashboardCategory) {
int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0;
Map<String, IContentProvider> providerMap = new ArrayMap<>();
for (int i = 0; i < tilesCount; i++) {
Tile tile = dashboardCategory.getTile(i);
// If the tile does not have a key or appropriate meta data, skip it.
if (TextUtils.isEmpty(tile.key) || (tile.metaData == null)) {
continue;
}
Preference matchingPref = preferenceScreen.findPreference(tile.key);
// If the tile does not have a matching preference, skip it.
if (matchingPref == null) {
continue;
}
// Check if the tile has content providers for dynamically updatable content.
final String iconUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_ICON_URI, null);
final String summaryUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, null);
if (!TextUtils.isEmpty(iconUri)) {
String packageName = null;
if (tile.intent != null) {
Intent intent = tile.intent;
if (!TextUtils.isEmpty(intent.getPackage())) {
packageName = intent.getPackage();
} else if (intent.getComponent() != null) {
packageName = intent.getComponent().getPackageName();
}
}
Pair<String, Integer> icon = TileUtils.getIconFromUri(context, packageName, iconUri, providerMap);
if (icon != null) {
sIconCache.put(iconUri, icon);
// Icon is only returned if the icon belongs to Settings or the target app.
// setIcon must be called on the UI thread.
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
try {
matchingPref.setIcon(context.getPackageManager().getResourcesForApplication(icon.first).getDrawable(icon.second, /* res id */
context.getTheme()));
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
// Intentionally ignored. If icon resources cannot be found, do not
// update.
}
}
});
}
}
if (!TextUtils.isEmpty(summaryUri)) {
String summary = TileUtils.getTextFromUri(context, summaryUri, providerMap, TileUtils.META_DATA_PREFERENCE_SUMMARY);
sSummaryCache.put(summaryUri, summary);
// setSummary must be called on UI thread.
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// Only update the summary if it has actually changed.
if (summary == null) {
if (matchingPref.getSummary() != null) {
matchingPref.setSummary(summary);
}
} else if (!summary.equals(matchingPref.getSummary())) {
matchingPref.setSummary(summary);
}
}
});
}
}
}
use of com.android.settingslib.drawer.Tile in project android_packages_apps_Settings by omnirom.
the class SecurityFeatureProviderImpl method initPreferences.
@VisibleForTesting
static void initPreferences(Context context, PreferenceScreen preferenceScreen, DashboardCategory dashboardCategory) {
int tilesCount = (dashboardCategory != null) ? dashboardCategory.getTilesCount() : 0;
for (int i = 0; i < tilesCount; i++) {
Tile tile = dashboardCategory.getTile(i);
// If the tile does not have a key or appropriate meta data, skip it.
if (TextUtils.isEmpty(tile.key) || (tile.metaData == null)) {
continue;
}
Preference matchingPref = preferenceScreen.findPreference(tile.key);
// If the tile does not have a matching preference, skip it.
if (matchingPref == null) {
continue;
}
// Either remove an icon by replacing them with nothing, or use the cached one since
// there is a delay in fetching the injected icon, and we don't want an inappropriate
// icon to be displayed while waiting for the injected icon.
final String iconUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_ICON_URI, null);
Drawable drawable = DEFAULT_ICON;
if ((iconUri != null) && sIconCache.containsKey(iconUri)) {
Pair<String, Integer> icon = sIconCache.get(iconUri);
try {
drawable = context.getPackageManager().getResourcesForApplication(icon.first).getDrawable(icon.second, /* res id */
context.getTheme());
} catch (PackageManager.NameNotFoundException e) {
// Ignore and just load the default icon.
}
}
matchingPref.setIcon(drawable);
// Either reserve room for the summary or load the cached one. This prevents the title
// from shifting when the final summary is injected.
final String summaryUri = tile.metaData.getString(TileUtils.META_DATA_PREFERENCE_SUMMARY_URI, null);
String summary = context.getString(R.string.summary_placeholder);
if ((summaryUri != null) && sSummaryCache.containsKey(summaryUri)) {
summary = sSummaryCache.get(summaryUri);
}
matchingPref.setSummary(summary);
}
}
use of com.android.settingslib.drawer.Tile in project android_packages_apps_Settings by omnirom.
the class SuggestionAdapterTest method onBindViewHolder_shouldInflateRemoteView.
@Test
public void onBindViewHolder_shouldInflateRemoteView() {
List<Tile> packages = makeSuggestions("pkg1");
RemoteViews remoteViews = mock(RemoteViews.class);
TextView textView = new TextView(RuntimeEnvironment.application);
doReturn(textView).when(remoteViews).apply(any(Context.class), any(ViewGroup.class));
packages.get(0).remoteViews = remoteViews;
setupSuggestions(mActivity, packages);
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
assertThat(textView.getParent()).isSameAs(mSuggestionHolder.itemView);
mSuggestionHolder.itemView.performClick();
verify(mActivity).startSuggestion(any(Intent.class));
}
use of com.android.settingslib.drawer.Tile in project android_packages_apps_Settings by omnirom.
the class SuggestionAdapterTest method onBindViewHolder_primaryViewShouldHandleClick.
@Test
public void onBindViewHolder_primaryViewShouldHandleClick() {
Context context = new ContextThemeWrapper(RuntimeEnvironment.application, R.style.Theme_Settings);
List<Tile> packages = makeSuggestions("pkg1");
RemoteViews remoteViews = mock(RemoteViews.class);
FrameLayout layout = new FrameLayout(context);
Button primary = new Button(context);
primary.setId(android.R.id.primary);
layout.addView(primary);
doReturn(layout).when(remoteViews).apply(any(Context.class), any(ViewGroup.class));
packages.get(0).remoteViews = remoteViews;
setupSuggestions(mActivity, packages);
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
mSuggestionHolder.itemView.performClick();
assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isNull();
verify(mActivity, never()).startSuggestion(any(Intent.class));
primary.performClick();
verify(mActivity).startSuggestion(any(Intent.class));
}
use of com.android.settingslib.drawer.Tile in project android_packages_apps_Settings by omnirom.
the class SuggestionAdapterTest method makeSuggestions.
private List<Tile> makeSuggestions(String... pkgNames) {
final List<Tile> suggestions = new ArrayList<>();
for (String pkgName : pkgNames) {
Tile suggestion = new Tile();
suggestion.intent = new Intent("action");
suggestion.intent.setComponent(new ComponentName(pkgName, "cls"));
suggestions.add(suggestion);
suggestion.icon = mock(Icon.class);
}
return suggestions;
}
Aggregations