Fix shortcut icon in launcher

When building icon for shortcut, check if the icon is LayerDrawable.
- If yes, only take the second layer (foreground).

Also move the class to shortcut package

Change-Id: I3513dbeb6509f11aa70ab3230d441e268ca9356d
Fixes: 72553870
Test: atest
This commit is contained in:
Fan Zhang
2018-01-29 11:23:52 -08:00
parent 499efd06ab
commit 409eab14de
5 changed files with 31 additions and 21 deletions

View File

@@ -146,7 +146,7 @@
android:parentActivityName="Settings"> android:parentActivityName="Settings">
</activity> </activity>
<activity android:name="CreateShortcut" <activity android:name=".shortcut.CreateShortcut"
android:label="@string/settings_shortcut"> android:label="@string/settings_shortcut">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/> <action android:name="android.intent.action.CREATE_SHORTCUT"/>

View File

@@ -34,6 +34,8 @@ import static android.content.pm.PackageManager.GET_META_DATA;
import static android.content.pm.PackageManager.GET_RESOLVED_FILTER; import static android.content.pm.PackageManager.GET_RESOLVED_FILTER;
import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
import com.android.settings.shortcut.CreateShortcut;
/** /**
* Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED} * Listens to {@link Intent.ACTION_PRE_BOOT_COMPLETED} and {@link Intent.ACTION_USER_INITIALIZED}
* performs setup steps for a managed profile (disables the launcher icon of the Settings app, * performs setup steps for a managed profile (disables the launcher icon of the Settings app,

View File

@@ -34,7 +34,7 @@ import android.widget.CompoundButton;
import com.android.internal.app.LocalePicker; import com.android.internal.app.LocalePicker;
import com.android.internal.app.LocaleStore; import com.android.internal.app.LocaleStore;
import com.android.settings.CreateShortcut; import com.android.settings.shortcut.CreateShortcut;
import com.android.settings.R; import com.android.settings.R;
import java.text.NumberFormat; import java.text.NumberFormat;

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings; package com.android.settings.shortcut;
import android.app.LauncherActivity; import android.app.LauncherActivity;
import android.content.ComponentName; import android.content.ComponentName;
@@ -28,7 +28,9 @@ import android.content.pm.ShortcutManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Bitmap.Config; import android.graphics.Bitmap.Config;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.graphics.drawable.LayerDrawable;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
@@ -40,6 +42,7 @@ import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.Settings.TetherSettingsActivity; import com.android.settings.Settings.TetherSettingsActivity;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
@@ -65,7 +68,8 @@ public class CreateShortcut extends LauncherActivity {
finish(); finish();
} }
protected Intent createResultIntent(Intent shortcutIntent, ResolveInfo resolveInfo, @VisibleForTesting
Intent createResultIntent(Intent shortcutIntent, ResolveInfo resolveInfo,
CharSequence label) { CharSequence label) {
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
ShortcutManager sm = getSystemService(ShortcutManager.class); ShortcutManager sm = getSystemService(ShortcutManager.class);
@@ -94,8 +98,8 @@ public class CreateShortcut extends LauncherActivity {
if (activityInfo.icon != 0) { if (activityInfo.icon != 0) {
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(activityInfo.icon, intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(activityInfo.icon,
R.layout.shortcut_badge, R.layout.shortcut_badge,
getResources().getDimensionPixelSize(R.dimen.shortcut_size))); getResources().getDimensionPixelSize(R.dimen.shortcut_size)));
} }
return intent; return intent;
} }
@@ -112,7 +116,11 @@ public class CreateShortcut extends LauncherActivity {
private Bitmap createIcon(int resource, int layoutRes, int size) { private Bitmap createIcon(int resource, int layoutRes, int size) {
Context context = new ContextThemeWrapper(this, android.R.style.Theme_Material); Context context = new ContextThemeWrapper(this, android.R.style.Theme_Material);
View view = LayoutInflater.from(context).inflate(layoutRes, null); View view = LayoutInflater.from(context).inflate(layoutRes, null);
((ImageView) view.findViewById(android.R.id.icon)).setImageResource(resource); Drawable iconDrawable = getDrawable(resource);
if (iconDrawable instanceof LayerDrawable) {
iconDrawable = ((LayerDrawable) iconDrawable).getDrawable(1);
}
((ImageView) view.findViewById(android.R.id.icon)).setImageDrawable(iconDrawable);
int spec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); int spec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
view.measure(spec, spec); view.measure(spec, spec);

View File

@@ -14,12 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings; package com.android.settings.shortcut;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
@@ -39,9 +37,13 @@ import android.content.pm.ResolveInfo;
import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import com.android.settings.R;
import com.android.settings.Settings;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -55,12 +57,6 @@ import java.util.List;
/** /**
* Tests for {@link CreateShortcutTest} * Tests for {@link CreateShortcutTest}
*
m SettingsTests &&
adb install \
-r -g ${ANDROID_PRODUCT_OUT}/data/app/SettingsTests/SettingsTests.apk &&
adb shell am instrument -e class com.android.settings.CreateShortcutTest \
-w com.android.settings.tests/android.support.test.runner.AndroidJUnitRunner
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@SmallTest @SmallTest
@@ -71,8 +67,10 @@ public class CreateShortcutTest {
private Instrumentation mInstrumentation; private Instrumentation mInstrumentation;
private Context mContext; private Context mContext;
@Mock ShortcutManager mShortcutManager; @Mock
@Captor ArgumentCaptor<List<ShortcutInfo>> mListCaptor; ShortcutManager mShortcutManager;
@Captor
ArgumentCaptor<List<ShortcutInfo>> mListCaptor;
@Before @Before
public void setup() { public void setup() {
@@ -84,15 +82,17 @@ public class CreateShortcutTest {
@Test @Test
public void test_layoutDoesNotHaveCancelButton() { public void test_layoutDoesNotHaveCancelButton() {
mInstrumentation.startActivitySync(new Intent(Intent.ACTION_CREATE_SHORTCUT) mInstrumentation.startActivitySync(new Intent(Intent.ACTION_CREATE_SHORTCUT)
.setClassName(mContext, CreateShortcut.class.getName())); .setClassName(mContext, CreateShortcut.class.getName())
onView(withText(R.string.cancel)).check(doesNotExist()); .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
onView(ViewMatchers.withText(R.string.cancel)).check(doesNotExist());
} }
@Test @Test
public void createResultIntent() { public void createResultIntent() {
CreateShortcut orgActivity = (CreateShortcut) mInstrumentation.startActivitySync( CreateShortcut orgActivity = (CreateShortcut) mInstrumentation.startActivitySync(
new Intent(Intent.ACTION_CREATE_SHORTCUT) new Intent(Intent.ACTION_CREATE_SHORTCUT)
.setClassName(mContext, CreateShortcut.class.getName())); .setClassName(mContext, CreateShortcut.class.getName())
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
CreateShortcut activity = spy(orgActivity); CreateShortcut activity = spy(orgActivity);
doReturn(mShortcutManager).when(activity).getSystemService(eq(Context.SHORTCUT_SERVICE)); doReturn(mShortcutManager).when(activity).getSystemService(eq(Context.SHORTCUT_SERVICE));