Search in sources :

Example 56 with Tile

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);
                    }
                }
            });
        }
    }
}
Also used : IContentProvider(android.content.IContentProvider) ArrayMap(android.util.ArrayMap) Tile(com.android.settingslib.drawer.Tile) Handler(android.os.Handler) Intent(android.content.Intent) PackageManager(android.content.pm.PackageManager) Preference(android.support.v7.preference.Preference) Resources(android.content.res.Resources) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 57 with Tile

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);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) Preference(android.support.v7.preference.Preference) Drawable(android.graphics.drawable.Drawable) Tile(com.android.settingslib.drawer.Tile) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 58 with Tile

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));
}
Also used : Context(android.content.Context) RemoteViews(android.widget.RemoteViews) ViewGroup(android.view.ViewGroup) Tile(com.android.settingslib.drawer.Tile) TextView(android.widget.TextView) Intent(android.content.Intent) Test(org.junit.Test)

Example 59 with Tile

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));
}
Also used : Context(android.content.Context) RemoteViews(android.widget.RemoteViews) ContextThemeWrapper(android.view.ContextThemeWrapper) Button(android.widget.Button) ViewGroup(android.view.ViewGroup) FrameLayout(android.widget.FrameLayout) Tile(com.android.settingslib.drawer.Tile) Intent(android.content.Intent) Test(org.junit.Test)

Example 60 with Tile

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;
}
Also used : ArrayList(java.util.ArrayList) Tile(com.android.settingslib.drawer.Tile) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Icon(android.graphics.drawable.Icon)

Aggregations

Tile (com.android.settingslib.drawer.Tile)506 Test (org.junit.Test)345 Intent (android.content.Intent)160 DashboardCategory (com.android.settingslib.drawer.DashboardCategory)117 Bundle (android.os.Bundle)108 Preference (android.support.v7.preference.Preference)98 ComponentName (android.content.ComponentName)93 ArrayList (java.util.ArrayList)87 Context (android.content.Context)70 ActivityTile (com.android.settingslib.drawer.ActivityTile)50 ProviderTile (com.android.settingslib.drawer.ProviderTile)45 UserHandle (android.os.UserHandle)39 Preference (androidx.preference.Preference)39 RecyclerView (android.support.v7.widget.RecyclerView)31 View (android.view.View)31 Icon (android.graphics.drawable.Icon)30 Activity (android.app.Activity)29 VisibleForTesting (android.support.annotation.VisibleForTesting)29 PackageManager (android.content.pm.PackageManager)25 ViewGroup (android.view.ViewGroup)24