Merge "WallpaperTypeSettings use DashboardFragment"

This commit is contained in:
TreeHugger Robot
2018-04-17 20:55:26 +00:00
committed by Android (Google) Code Review
5 changed files with 237 additions and 113 deletions

View File

@@ -14,7 +14,10 @@
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/wallpaper_settings_fragment_title">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="wallpaper_type"
android:title="@string/wallpaper_settings_fragment_title"
settings:controller="com.android.settings.wallpaper.WallpaperTypePreferenceController">
</PreferenceScreen>

View File

@@ -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<ResolveInfo> 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<ResolveInfo> 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<ResolveInfo> result = rList.stream().filter(
rInfo -> TextUtils.equals(pref.getKey(),
rInfo.activityInfo.packageName)).collect(Collectors.toList());
if (result.isEmpty()) {
mScreen.removePreference(pref);
}
}
}
}

View File

@@ -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();
}
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<ResolveInfo> 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);
}
protected String getLogTag() {
return TAG;
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getIntent() == null) {
return super.onPreferenceTreeClick(preference);
}
startActivity(preference.getIntent());
finish();
return true;
protected int getPreferenceScreenResId() {
return R.xml.wallpaper_settings;
}
@Override
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<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
final List<SearchIndexableRaw> 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(

View File

@@ -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();
}
}

View File

@@ -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> 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);
}
}