Commit 0ad7c15e authored by Ehimare Okoyomon's avatar Ehimare Okoyomon Committed by Commit Bot

[Android] Add blocked permission icons programmatically

Create function to add a strikethough overlay on top of icon if the
permission is blocked.

Screenshots: crbug.com/1077766#c96
Bug: 1077766
Change-Id: I6d081ad69c4817be3158aacca9f8d6997a12d36d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2526302
Commit-Queue: Ehimare Okoyomon <eokoyomon@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827767}
parent 0d7b4c7e
......@@ -145,20 +145,22 @@ public class PageInfoViewTest {
WebsitePreferenceBridge.setContentSettingForPattern(Profile.getLastUsedRegularProfile(),
ContentSettingsType.GEOLOCATION, url, "*", ContentSettingValues.ALLOW);
WebsitePreferenceBridge.setContentSettingForPattern(Profile.getLastUsedRegularProfile(),
ContentSettingsType.NOTIFICATIONS, url, "*", ContentSettingValues.ALLOW);
ContentSettingsType.NOTIFICATIONS, url, "*", ContentSettingValues.BLOCK);
});
}
private void expectHasPermissions(String url, boolean hasPermissions) {
// The default value for these types is ask.
@ContentSettingValues
int expected = hasPermissions ? ContentSettingValues.ALLOW : ContentSettingValues.ASK;
int expectAllow = hasPermissions ? ContentSettingValues.ALLOW : ContentSettingValues.ASK;
@ContentSettingValues
int expectBlock = hasPermissions ? ContentSettingValues.BLOCK : ContentSettingValues.ASK;
TestThreadUtils.runOnUiThreadBlocking(() -> {
assertEquals(expected,
assertEquals(expectBlock,
WebsitePreferenceBridgeJni.get().getSettingForOrigin(
Profile.getLastUsedRegularProfile(), ContentSettingsType.NOTIFICATIONS,
url, url));
assertEquals(expected,
assertEquals(expectAllow,
WebsitePreferenceBridgeJni.get().getSettingForOrigin(
Profile.getLastUsedRegularProfile(), ContentSettingsType.GEOLOCATION,
url, "*"));
......
......@@ -6,7 +6,14 @@ package org.chromium.components.browser_ui.site_settings;
import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
......@@ -251,6 +258,45 @@ public class ContentSettingsResources {
return icon;
}
/**
* @return A {@link Drawable} that is the blocked version of the icon passed in. Achieved by
* adding a diagonal strike through the icon.
*/
public static Drawable getBlockedIcon(Resources resources, Drawable icon) {
if (icon == null) return null;
// Save color filter in order to re-apply later
ColorFilter filter = icon.getColorFilter();
// Create bitmap from drawable
Bitmap iconBitmap = Bitmap.createBitmap(
icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(iconBitmap);
int width = canvas.getWidth();
int height = canvas.getHeight();
icon.setBounds(0, 0, width, height);
icon.draw(canvas);
// Draw diagonal transparent line
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
paint.setStrokeWidth(12);
canvas.drawLine(0, 0, width, height, paint);
// Draw a strikethrough on top
paint.setColor(Color.BLACK);
paint.setXfermode(null);
paint.setStrokeWidth(4);
canvas.drawLine(
width * 0.1f, height * 0.1f, width - width * 0.1f, height - height * 0.1f, paint);
Drawable blocked = new BitmapDrawable(resources, iconBitmap);
// Re-apply color filter
blocked.setColorFilter(filter);
return blocked;
}
/**
* Returns the resource id of the title (short version), shown on the Site Settings page
* and in the global toggle at the top of a Website Settings page for a content type.
......
......@@ -9,6 +9,7 @@ import static org.chromium.components.browser_ui.site_settings.WebsitePreference
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
......@@ -353,6 +354,19 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
|| SITE_WILDCARD.equals(info.getEmbedderSafe()));
}
private Drawable getContentSettingsIcon(@ContentSettingsType int contentSettingsType,
@ContentSettingValues @Nullable Integer value, boolean enabled) {
Drawable icon = enabled
? SettingsUtils.getTintedIcon(
getActivity(), ContentSettingsResources.getIcon(contentSettingsType))
: ContentSettingsResources.getDisabledIcon(contentSettingsType, getResources());
if (getSiteSettingsClient().isPageInfoV2Enabled() && value != null
&& value == ContentSettingValues.BLOCK) {
return ContentSettingsResources.getBlockedIcon(getResources(), icon);
}
return icon;
}
/**
* Updates the permissions displayed in the UI by fetching them from mSite.
* Must only be called once mSite is set.
......@@ -858,8 +872,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
preference.setTitle(titleResourceId);
}
if (!preference.isEnabled()) {
preference.setIcon(
ContentSettingsResources.getDisabledIcon(contentType, getResources()));
preference.setIcon(getContentSettingsIcon(contentType, value, false));
return;
}
SiteSettingsCategory category = SiteSettingsCategory.createFromContentSettingsType(
......@@ -869,8 +882,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
preference.setIcon(category.getDisabledInAndroidIcon(getActivity()));
preference.setEnabled(false);
} else {
preference.setIcon(SettingsUtils.getTintedIcon(
getActivity(), ContentSettingsResources.getIcon(contentType)));
preference.setIcon(getContentSettingsIcon(contentType, value, true));
}
}
......@@ -1045,6 +1057,7 @@ public class SingleWebsiteSettings extends SiteSettingsPreferenceFragment
preference.setSummary(
getString(ContentSettingsResources.getCategorySummary(permission)));
}
preference.setIcon(getContentSettingsIcon(type, permission, true));
if (mWebsiteSettingsObserver != null) {
mWebsiteSettingsObserver.onPermissionChanged();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment