From f4b328e7239255a8b95e62e4c5b6d1a5637964ec Mon Sep 17 00:00:00 2001 From: Mill Chen Date: Thu, 12 Apr 2018 13:53:22 +0000 Subject: [PATCH] WallpaperTypeSettings use DashboardFragment - Build a controller to generate/manage a list of preferences. - Move some logics to the controller and add tests. Bug: 73899467 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.wallpaper Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.core Test: atest SettingsGatewayTest UniquePreferenceTest Change-Id: I519a76ff34bcd4195b0ad6ad406a8f66371923d2 --- res/xml/wallpaper_settings.xml | 9 +- .../WallpaperTypePreferenceController.java | 122 +++++++++++++++++++++ .../settings/wallpaper/WallpaperTypeSettings.java | 61 +++-------- .../WallpaperTypePreferenceControllerTest.java | 91 +++++++++++++++ .../wallpaper/WallpaperTypeSettingsTest.java | 63 ----------- 5 files changed, 235 insertions(+), 111 deletions(-) create mode 100644 src/com/android/settings/wallpaper/WallpaperTypePreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/wallpaper/WallpaperTypePreferenceControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/wallpaper/WallpaperTypeSettingsTest.java diff --git a/res/xml/wallpaper_settings.xml b/res/xml/wallpaper_settings.xml index bb9e809984..195f4bce24 100644 --- a/res/xml/wallpaper_settings.xml +++ b/res/xml/wallpaper_settings.xml @@ -14,7 +14,10 @@ limitations under the License. --> - - + diff --git a/src/com/android/settings/wallpaper/WallpaperTypePreferenceController.java b/src/com/android/settings/wallpaper/WallpaperTypePreferenceController.java new file mode 100644 index 0000000000..5b8d723987 --- /dev/null +++ b/src/com/android/settings/wallpaper/WallpaperTypePreferenceController.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.wallpaper; + +import android.app.Fragment; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.text.TextUtils; + +import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; + +import java.util.List; +import java.util.stream.Collectors; + +public class WallpaperTypePreferenceController extends BasePreferenceController + implements LifecycleObserver, OnStart { + private Fragment mParentFragment; + private PreferenceScreen mScreen; + + public WallpaperTypePreferenceController(Context context, String key) { + super(context, key); + } + + public void setParentFragment(Fragment parentFragment) { + mParentFragment = parentFragment; + } + + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mScreen = screen; + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (preference.getIntent() == null) { + return super.handlePreferenceTreeClick(preference); + } + mParentFragment.startActivity(preference.getIntent()); + mParentFragment.getActivity().finish(); + return true; + } + + @Override + public void onStart() { + populateWallpaperTypes(); + } + + private void populateWallpaperTypes() { + // Search for activities that satisfy the ACTION_SET_WALLPAPER action + final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); + final PackageManager pm = mContext.getPackageManager(); + final List rList = pm.queryIntentActivities(intent, + PackageManager.MATCH_DEFAULT_ONLY); + + removeUselessExistingPreference(rList); + mScreen.setOrderingAsAdded(false); + // Add Preference items for each of the matching activities + for (ResolveInfo info : rList) { + final String packageName = info.activityInfo.packageName; + Preference pref = mScreen.findPreference(packageName); + if (pref == null) { + pref = new Preference(mScreen.getContext()); + } + final Intent prefIntent = new Intent(intent).addFlags( + Intent.FLAG_ACTIVITY_FORWARD_RESULT); + prefIntent.setComponent(new ComponentName(packageName, info.activityInfo.name)); + pref.setIntent(prefIntent); + pref.setKey(packageName); + CharSequence label = info.loadLabel(pm); + if (label == null) { + label = packageName; + } + pref.setTitle(label); + pref.setIcon(info.loadIcon(pm)); + mScreen.addPreference(pref); + } + } + + private void removeUselessExistingPreference(List rList) { + final int count = mScreen.getPreferenceCount(); + if (count <= 0) { + return; + } + for (int i = count - 1; i >= 0; i--) { + final Preference pref = mScreen.getPreference(i); + final List result = rList.stream().filter( + rInfo -> TextUtils.equals(pref.getKey(), + rInfo.activityInfo.packageName)).collect(Collectors.toList()); + if (result.isEmpty()) { + mScreen.removePreference(pref); + } + } + } +} \ No newline at end of file diff --git a/src/com/android/settings/wallpaper/WallpaperTypeSettings.java b/src/com/android/settings/wallpaper/WallpaperTypeSettings.java index 9d565e4aa7..ca42ddaef4 100644 --- a/src/com/android/settings/wallpaper/WallpaperTypeSettings.java +++ b/src/com/android/settings/wallpaper/WallpaperTypeSettings.java @@ -16,20 +16,15 @@ package com.android.settings.wallpaper; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.os.Bundle; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.dashboard.DashboardFragment; import com.android.settings.search.BaseSearchIndexProvider; -import com.android.settings.search.Indexable; import com.android.settings.search.SearchIndexableRaw; import com.android.settingslib.search.SearchIndexable; @@ -37,7 +32,8 @@ import java.util.ArrayList; import java.util.List; @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) -public class WallpaperTypeSettings extends SettingsPreferenceFragment implements Indexable { +public class WallpaperTypeSettings extends DashboardFragment { + private static final String TAG = "WallpaperTypeSettings"; @Override public int getMetricsCategory() { @@ -50,52 +46,26 @@ public class WallpaperTypeSettings extends SettingsPreferenceFragment implements } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - addPreferencesFromResource(R.xml.wallpaper_settings); - populateWallpaperTypes(); + protected String getLogTag() { + return TAG; } - private void populateWallpaperTypes() { - // Search for activities that satisfy the ACTION_SET_WALLPAPER action - final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); - final PackageManager pm = getPackageManager(); - final List rList = pm.queryIntentActivities(intent, - PackageManager.MATCH_DEFAULT_ONLY); - - final PreferenceScreen parent = getPreferenceScreen(); - parent.setOrderingAsAdded(false); - // Add Preference items for each of the matching activities - for (ResolveInfo info : rList) { - Preference pref = new Preference(getPrefContext()); - Intent prefIntent = new Intent(intent).addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); - prefIntent.setComponent(new ComponentName( - info.activityInfo.packageName, info.activityInfo.name)); - pref.setIntent(prefIntent); - CharSequence label = info.loadLabel(pm); - if (label == null) label = info.activityInfo.packageName; - pref.setTitle(label); - pref.setIcon(info.loadIcon(pm)); - parent.addPreference(pref); - } + @Override + protected int getPreferenceScreenResId() { + return R.xml.wallpaper_settings; } @Override - public boolean onPreferenceTreeClick(Preference preference) { - if (preference.getIntent() == null) { - return super.onPreferenceTreeClick(preference); - } - startActivity(preference.getIntent()); - finish(); - return true; + public void onAttach(Context context) { + super.onAttach(context); + use(WallpaperTypePreferenceController.class).setParentFragment(this); } public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = new BaseSearchIndexProvider() { @Override public List getRawDataToIndex(Context context, boolean enabled) { - final List result = new ArrayList(); + final List result = new ArrayList<>(); final Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER); final PackageManager pm = context.getPackageManager(); @@ -110,9 +80,10 @@ public class WallpaperTypeSettings extends SettingsPreferenceFragment implements continue; } CharSequence label = info.loadLabel(pm); - if (label == null) label = info.activityInfo.packageName; - - SearchIndexableRaw data = new SearchIndexableRaw(context); + if (label == null) { + label = info.activityInfo.packageName; + } + final SearchIndexableRaw data = new SearchIndexableRaw(context); data.title = label.toString(); data.key = "wallpaper_type_settings"; data.screenTitle = context.getResources().getString( diff --git a/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypePreferenceControllerTest.java new file mode 100644 index 0000000000..fa6ecaf58d --- /dev/null +++ b/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypePreferenceControllerTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.wallpaper; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.content.Intent; +import android.support.v7.preference.Preference; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; + +@RunWith(SettingsRobolectricTestRunner.class) +public class WallpaperTypePreferenceControllerTest { + + @Mock + private Fragment mFragment; + + private Context mContext; + private Activity mActivity; + private WallpaperTypePreferenceController mController; + private Preference mPreference; + private Intent mIntent; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mActivity = spy(Robolectric.buildActivity(Activity.class).get()); + mController = new WallpaperTypePreferenceController(mContext, "pref_key"); + mController.setParentFragment(mFragment); + mIntent = new Intent(); + mPreference = new Preference(mContext); + } + + @Test + public void getAvailabilityStatus_byDefault_shouldBeShown() { + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void testhandlePreferenceTreeClick_intentNull_shouldDoNothing() { + mPreference.setIntent(null); + + final boolean handled = mController.handlePreferenceTreeClick(mPreference); + + assertThat(handled).isFalse(); + } + + @Test + public void testhandlePreferenceTreeClick_shouldLaunchIntent() { + mPreference.setIntent(mIntent); + doNothing().when(mFragment).startActivity(any(Intent.class)); + when(mFragment.getActivity()).thenReturn(mActivity); + doNothing().when(mActivity).finish(); + + final boolean handled = mController.handlePreferenceTreeClick(mPreference); + + assertThat(handled).isTrue(); + } +} diff --git a/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypeSettingsTest.java b/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypeSettingsTest.java deleted file mode 100644 index a39ad9fec4..0000000000 --- a/tests/robotests/src/com/android/settings/wallpaper/WallpaperTypeSettingsTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.android.settings.wallpaper; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.robolectric.RuntimeEnvironment.application; - -import android.app.Activity; -import android.content.Intent; -import android.support.v7.preference.Preference; - -import com.android.settings.testutils.SettingsRobolectricTestRunner; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.robolectric.Robolectric; - -@RunWith(SettingsRobolectricTestRunner.class) -public class WallpaperTypeSettingsTest { - - private Preference mPreference; - - private Intent mIntent; - - @Before - public void setUp() { - mIntent = new Intent(); - mPreference = new Preference(application); - } - - @Test - public void testOnPreferenceTreeClick_intentNull_shouldDoNothing() { - Activity activity = Robolectric.setupActivity(Activity.class); - WallpaperTypeSettings fragment = spy(new WallpaperTypeSettings()); - doReturn(activity).when(fragment).getActivity(); - - boolean handled = fragment.onPreferenceTreeClick(mPreference); - - assertThat(handled).isFalse(); - } - - @Test - public void testOnPreferenceTreeClick_shouldLaunchIntentAndFinish() { - Activity activity = Robolectric.setupActivity(Activity.class); - WallpaperTypeSettings fragment = spy(new WallpaperTypeSettings()); - doReturn(activity).when(fragment).getActivity(); - mPreference.setIntent(mIntent); - doNothing().when(fragment).finish(); - ArgumentCaptor intent = ArgumentCaptor.forClass(Intent.class); - doNothing().when(fragment).startActivity(intent.capture()); - - boolean handled = fragment.onPreferenceTreeClick(mPreference); - - assertThat(handled).isTrue(); - verify(fragment, times(1)).finish(); - assertThat(intent.getValue()).isSameAs(mIntent); - } -} -- 2.11.0