move PreferenceController into settingsLib/core
Test: moved Change-Id: I6578efc4d51ef5a9202b6a8ad1c7197aeda2d6bb
This commit is contained in:
@@ -16,56 +16,20 @@
|
|||||||
package com.android.settings.core;
|
package com.android.settings.core;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.v7.preference.Preference;
|
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
import com.android.settings.search.SearchIndexableRaw;
|
import com.android.settings.search.SearchIndexableRaw;
|
||||||
import com.android.settings.search2.ResultPayload;
|
import com.android.settings.search2.ResultPayload;
|
||||||
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A controller that manages event for preference.
|
* A controller that manages event for preference.
|
||||||
*/
|
*/
|
||||||
public abstract class PreferenceController {
|
public abstract class PreferenceController extends AbstractPreferenceController {
|
||||||
|
|
||||||
protected final Context mContext;
|
|
||||||
|
|
||||||
public PreferenceController(Context context) {
|
public PreferenceController(Context context) {
|
||||||
mContext = context;
|
super(context);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays preference in this controller.
|
|
||||||
*/
|
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
|
||||||
if (isAvailable()) {
|
|
||||||
if (this instanceof Preference.OnPreferenceChangeListener) {
|
|
||||||
final Preference preference = screen.findPreference(getPreferenceKey());
|
|
||||||
preference.setOnPreferenceChangeListener(
|
|
||||||
(Preference.OnPreferenceChangeListener) this);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
removePreference(screen, getPreferenceKey());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the current status of preference (summary, switch state, etc)
|
|
||||||
*/
|
|
||||||
public void updateState(Preference preference) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates non-indexable keys for search provider.
|
|
||||||
*
|
|
||||||
* Called by SearchIndexProvider#getNonIndexableKeys
|
|
||||||
*/
|
|
||||||
public void updateNonIndexableKeys(List<String> keys) {
|
|
||||||
if (!isAvailable()) {
|
|
||||||
keys.add(getPreferenceKey());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,36 +40,6 @@ public abstract class PreferenceController {
|
|||||||
public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
|
public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if preference is available (should be displayed)
|
|
||||||
*/
|
|
||||||
public abstract boolean isAvailable();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles preference tree click
|
|
||||||
*
|
|
||||||
* @param preference the preference being clicked
|
|
||||||
* @return true if click is handled
|
|
||||||
*/
|
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the key for this preference.
|
|
||||||
*/
|
|
||||||
public abstract String getPreferenceKey();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes preference from screen.
|
|
||||||
*/
|
|
||||||
protected final void removePreference(PreferenceScreen screen, String key) {
|
|
||||||
Preference pref = screen.findPreference(key);
|
|
||||||
if (pref != null) {
|
|
||||||
screen.removePreference(pref);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the {@link ResultPayload} corresponding to the search result type for the preference.
|
* @return the {@link ResultPayload} corresponding to the search result type for the preference.
|
||||||
*/
|
*/
|
||||||
|
@@ -1,113 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 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.core;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.support.v7.preference.Preference;
|
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
|
||||||
import com.android.settings.TestConfig;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
import org.robolectric.annotation.Config;
|
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
|
||||||
public class PreferenceControllerTest {
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private Context mContext;
|
|
||||||
@Mock
|
|
||||||
private PreferenceScreen mScreen;
|
|
||||||
@Mock
|
|
||||||
private Preference mPreference;
|
|
||||||
|
|
||||||
private TestPrefController mTestPrefController;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
mTestPrefController = new TestPrefController(mContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void removeExistingPref_shouldBeRemoved() {
|
|
||||||
when(mScreen.findPreference(TestPrefController.KEY_PREF)).thenReturn(mPreference);
|
|
||||||
|
|
||||||
mTestPrefController.removePreference(mScreen, TestPrefController.KEY_PREF);
|
|
||||||
|
|
||||||
verify(mScreen).removePreference(mPreference);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void removeNonExistingPref_shouldNotRemoveAnything() {
|
|
||||||
mTestPrefController.removePreference(mScreen, TestPrefController.KEY_PREF);
|
|
||||||
|
|
||||||
verify(mScreen, never()).removePreference(any(Preference.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void displayPref_ifAvailable() {
|
|
||||||
mTestPrefController.isAvailable = true;
|
|
||||||
|
|
||||||
mTestPrefController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
verify(mScreen, never()).removePreference(any(Preference.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void doNotDisplayPref_ifNotAvailable() {
|
|
||||||
when(mScreen.findPreference(TestPrefController.KEY_PREF)).thenReturn(mPreference);
|
|
||||||
mTestPrefController.isAvailable = false;
|
|
||||||
|
|
||||||
mTestPrefController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
verify(mScreen).removePreference(any(Preference.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
private class TestPrefController extends PreferenceController {
|
|
||||||
private static final String KEY_PREF = "test_pref";
|
|
||||||
public boolean isAvailable;
|
|
||||||
|
|
||||||
public TestPrefController(Context context) {
|
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable() {
|
|
||||||
return isAvailable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_PREF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Reference in New Issue
Block a user