Change print setting from a dynamic tile to static pref
...because dynamic tile is a lot harder to index correctly. - Removed metadata that makes PrintSettings a dynamic tile. - Added PrintSettings into connected_device xml. - Added a new PreferenceController - all summary updating logic is copied from PrintSettingsFragment Change-Id: I41e7c9d23e97ecd5a043ac7c33f2d404260c92e7 Fixes: 73128944 Test: robotests
This commit is contained in:
@@ -1984,11 +1984,6 @@
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.VOICE_LAUNCH" />
|
||||
</intent-filter>
|
||||
<intent-filter android:priority="2">
|
||||
<action android:name="com.android.settings.action.SETTINGS" />
|
||||
</intent-filter>
|
||||
<meta-data android:name="com.android.settings.category"
|
||||
android:value="com.android.settings.category.ia.device" />
|
||||
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
|
||||
android:value="com.android.settings.print.PrintSettingsFragment" />
|
||||
</activity>
|
||||
|
@@ -1,5 +1,3 @@
|
||||
[Hook Scripts]
|
||||
checkstyle_hook = ${REPO_ROOT}/prebuilts/checkstyle/checkstyle.py --sha ${PREUPLOAD_COMMIT}
|
||||
-fw src/com/android/settings/print/
|
||||
checkcolor_hook = ${REPO_ROOT}/prebuilts/checkcolor/checkcolor.py -p .
|
||||
|
||||
|
@@ -50,6 +50,14 @@
|
||||
settings:controller="com.android.settings.connecteddevice.BluetoothOnWhileDrivingPreferenceController"
|
||||
android:order="-2"/>
|
||||
|
||||
<Preference
|
||||
android:key="connected_device_printing"
|
||||
android:title="@string/print_settings"
|
||||
android:summary="@string/summary_placeholder"
|
||||
android:icon="@drawable/ic_settings_print"
|
||||
android:fragment="com.android.settings.print.PrintSettingsFragment"
|
||||
android:order="-1"/>
|
||||
|
||||
<Preference
|
||||
android:key="bt_received_files"
|
||||
android:icon="@drawable/ic_folder_vd_theme_24"
|
||||
|
@@ -18,7 +18,7 @@
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:settings="http://schemas.android.com/apk/res-auto"
|
||||
android:key="print_settings_screen"
|
||||
android:title="@string/print_settings_title"
|
||||
android:title="@string/print_settings"
|
||||
settings:keywords="@string/keywords_printing">
|
||||
|
||||
<PreferenceCategory
|
||||
|
@@ -802,11 +802,6 @@ public class SettingsActivity extends SettingsDrawerActivity
|
||||
!UserManager.isDeviceInDemoMode(this), isAdmin)
|
||||
|| somethingChanged;
|
||||
|
||||
somethingChanged = setTileEnabled(new ComponentName(packageName,
|
||||
Settings.PrintSettingsActivity.class.getName()),
|
||||
pm.hasSystemFeature(PackageManager.FEATURE_PRINTING), isAdmin)
|
||||
|| somethingChanged;
|
||||
|
||||
final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
|
||||
&& !Utils.isMonkeyRunning();
|
||||
|
||||
|
@@ -29,6 +29,7 @@ import com.android.settings.connecteddevice.usb.UsbModePreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.nfc.NfcPreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.print.PrintSettingPreferenceController;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
@@ -43,7 +44,6 @@ import java.util.List;
|
||||
public class AdvancedConnectedDeviceDashboardFragment extends DashboardFragment {
|
||||
|
||||
private static final String TAG = "AdvancedConnectedDeviceFrag";
|
||||
private UsbModePreferenceController mUsbPrefController;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
@@ -67,22 +67,32 @@ public class AdvancedConnectedDeviceDashboardFragment extends DashboardFragment
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
return buildControllers(context, getLifecycle());
|
||||
}
|
||||
|
||||
private static List<AbstractPreferenceController> buildControllers(Context context,
|
||||
Lifecycle lifecycle) {
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
final Lifecycle lifecycle = getLifecycle();
|
||||
final NfcPreferenceController nfcPreferenceController =
|
||||
new NfcPreferenceController(context);
|
||||
lifecycle.addObserver(nfcPreferenceController);
|
||||
controllers.add(nfcPreferenceController);
|
||||
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
|
||||
lifecycle.addObserver(mUsbPrefController);
|
||||
controllers.add(mUsbPrefController);
|
||||
controllers.add(new UsbModePreferenceController(
|
||||
context, new UsbBackend(context), lifecycle));
|
||||
final BluetoothSwitchPreferenceController bluetoothPreferenceController =
|
||||
new BluetoothSwitchPreferenceController(context);
|
||||
lifecycle.addObserver(bluetoothPreferenceController);
|
||||
controllers.add(bluetoothPreferenceController);
|
||||
|
||||
controllers.add(new BluetoothFilesPreferenceController(context));
|
||||
controllers.add(new BluetoothOnWhileDrivingPreferenceController(context));
|
||||
final PrintSettingPreferenceController printerController =
|
||||
new PrintSettingPreferenceController(context);
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(printerController);
|
||||
lifecycle.addObserver(nfcPreferenceController);
|
||||
lifecycle.addObserver(bluetoothPreferenceController);
|
||||
}
|
||||
controllers.add(printerController);
|
||||
|
||||
return controllers;
|
||||
}
|
||||
|
||||
@@ -111,5 +121,11 @@ public class AdvancedConnectedDeviceDashboardFragment extends DashboardFragment
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractPreferenceController> getPreferenceControllers(
|
||||
Context context) {
|
||||
return buildControllers(context, null /* lifecycle */);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@@ -80,8 +80,8 @@ public class ConnectedDeviceDashboardFragmentOld extends DashboardFragment {
|
||||
new NfcPreferenceController(context);
|
||||
lifecycle.addObserver(nfcPreferenceController);
|
||||
controllers.add(nfcPreferenceController);
|
||||
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
|
||||
lifecycle.addObserver(mUsbPrefController);
|
||||
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context),
|
||||
lifecycle);
|
||||
controllers.add(mUsbPrefController);
|
||||
final BluetoothMasterSwitchPreferenceController bluetoothPreferenceController =
|
||||
new BluetoothMasterSwitchPreferenceController(
|
||||
|
@@ -23,6 +23,7 @@ import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
@@ -37,12 +38,16 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
||||
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||
private Preference mUsbPreference;
|
||||
|
||||
public UsbModePreferenceController(Context context, UsbBackend usbBackend) {
|
||||
public UsbModePreferenceController(Context context, UsbBackend usbBackend,
|
||||
Lifecycle lifecycle) {
|
||||
super(context);
|
||||
mUsbBackend = usbBackend;
|
||||
mUsbReceiver = new UsbConnectionBroadcastReceiver(mContext, (connected, newMode) -> {
|
||||
updateSummary(mUsbPreference, connected, newMode);
|
||||
}, mUsbBackend);
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -21,6 +21,7 @@ import android.util.Log;
|
||||
import com.android.settings.search.ResultPayload;
|
||||
import com.android.settings.search.SearchIndexableRaw;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -70,6 +71,8 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl
|
||||
|
||||
protected final String mPreferenceKey;
|
||||
|
||||
protected Lifecycle mLifecycle;
|
||||
|
||||
public BasePreferenceController(Context context, String preferenceKey) {
|
||||
super(context);
|
||||
mPreferenceKey = preferenceKey;
|
||||
|
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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.print;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintJobId;
|
||||
import android.print.PrintJobInfo;
|
||||
import android.print.PrintManager;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.wrapper.PrintManagerWrapper;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link BasePreferenceController} for Print settings.
|
||||
*/
|
||||
public class PrintSettingPreferenceController extends BasePreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
|
||||
|
||||
private final PackageManager mPackageManager;
|
||||
private PrintManagerWrapper mPrintManager;
|
||||
private Preference mPreference;
|
||||
|
||||
public PrintSettingPreferenceController(Context context) {
|
||||
super(context, "connected_device_printing" /* preferenceKey */);
|
||||
mPackageManager = context.getPackageManager();
|
||||
mPrintManager = new PrintManagerWrapper(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)
|
||||
? AVAILABLE : DISABLED_UNSUPPORTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mPrintManager.addPrintJobStateChanegListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mPrintManager.removePrintJobStateChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrintJobStateChanged(PrintJobId printJobId) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
if (preference == null) {
|
||||
return;
|
||||
}
|
||||
preference.setSummary(getSummary());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSummary() {
|
||||
final List<PrintJob> printJobs = mPrintManager.getPrintJobs();
|
||||
|
||||
int numActivePrintJobs = 0;
|
||||
if (printJobs != null) {
|
||||
for (PrintJob job : printJobs) {
|
||||
if (shouldShowToUser(job.getInfo())) {
|
||||
numActivePrintJobs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numActivePrintJobs > 0) {
|
||||
return mContext.getResources().getQuantityString(
|
||||
R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs);
|
||||
} else {
|
||||
final List<PrintServiceInfo> services =
|
||||
mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
|
||||
if (services == null || services.isEmpty()) {
|
||||
return mContext.getString(R.string.print_settings_summary_no_service);
|
||||
} else {
|
||||
final int count = services.size();
|
||||
return mContext.getResources().getQuantityString(
|
||||
R.plurals.print_settings_summary, count, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the print job the shown to the user in the settings app.
|
||||
*
|
||||
* @param printJob The print job in question.
|
||||
* @return true iff the print job should be shown.
|
||||
*/
|
||||
static boolean shouldShowToUser(PrintJobInfo printJob) {
|
||||
switch (printJob.getState()) {
|
||||
case PrintJobInfo.STATE_QUEUED:
|
||||
case PrintJobInfo.STATE_STARTED:
|
||||
case PrintJobInfo.STATE_BLOCKED:
|
||||
case PrintJobInfo.STATE_FAILED: {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.print;
|
||||
|
||||
import static com.android.settings.print.PrintSettingPreferenceController.shouldShowToUser;
|
||||
|
||||
import android.app.LoaderManager.LoaderCallbacks;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.AsyncTaskLoader;
|
||||
@@ -37,7 +39,6 @@ import android.print.PrintServicesLoader;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.text.TextUtils;
|
||||
@@ -52,7 +53,6 @@ import android.widget.TextView;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settings.utils.ProfileSettingsPreferenceFragment;
|
||||
@@ -130,11 +130,6 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
startSubSettingsIfNeeded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
@@ -359,7 +354,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
printJob.getCreationTime(), printJob.getCreationTime(),
|
||||
DateFormat.SHORT, DateFormat.SHORT)));
|
||||
|
||||
TypedArray a = getActivity().obtainStyledAttributes(new int[]{
|
||||
TypedArray a = getActivity().obtainStyledAttributes(new int[] {
|
||||
android.R.attr.colorControlNormal});
|
||||
int tintColor = a.getColor(0, 0);
|
||||
a.recycle();
|
||||
@@ -494,136 +489,17 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the print job the shown to the user in the settings app.
|
||||
*
|
||||
* @param printJob The print job in question.
|
||||
* @return true iff the print job should be shown.
|
||||
*/
|
||||
private static boolean shouldShowToUser(PrintJobInfo printJob) {
|
||||
switch (printJob.getState()) {
|
||||
case PrintJobInfo.STATE_QUEUED:
|
||||
case PrintJobInfo.STATE_STARTED:
|
||||
case PrintJobInfo.STATE_BLOCKED:
|
||||
case PrintJobInfo.STATE_FAILED: {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for the print settings summary
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static class PrintSummaryProvider
|
||||
implements SummaryLoader.SummaryProvider, PrintJobStateChangeListener {
|
||||
private final Context mContext;
|
||||
private final PrintManagerWrapper mPrintManager;
|
||||
private final SummaryLoader mSummaryLoader;
|
||||
|
||||
/**
|
||||
* Create a new {@link PrintSummaryProvider}.
|
||||
*
|
||||
* @param context The context this provider is for
|
||||
* @param summaryLoader The summary load using this provider
|
||||
*/
|
||||
PrintSummaryProvider(Context context, SummaryLoader summaryLoader,
|
||||
PrintManagerWrapper printManager) {
|
||||
mContext = context;
|
||||
mSummaryLoader = summaryLoader;
|
||||
mPrintManager = printManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setListening(boolean isListening) {
|
||||
if (mPrintManager != null) {
|
||||
if (isListening) {
|
||||
mPrintManager.addPrintJobStateChanegListner(this);
|
||||
onPrintJobStateChanged(null);
|
||||
} else {
|
||||
mPrintManager.removePrintJobStateChangeListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrintJobStateChanged(PrintJobId printJobId) {
|
||||
final List<PrintJob> printJobs = mPrintManager.getPrintJobs();
|
||||
|
||||
int numActivePrintJobs = 0;
|
||||
if (printJobs != null) {
|
||||
for (PrintJob job : printJobs) {
|
||||
if (shouldShowToUser(job.getInfo())) {
|
||||
numActivePrintJobs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numActivePrintJobs > 0) {
|
||||
mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
|
||||
R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs));
|
||||
} else {
|
||||
List<PrintServiceInfo> services =
|
||||
mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
|
||||
if (services == null || services.isEmpty()) {
|
||||
mSummaryLoader.setSummary(this,
|
||||
mContext.getString(R.string.print_settings_summary_no_service));
|
||||
} else {
|
||||
final int count = services.size();
|
||||
mSummaryLoader.setSummary(this,
|
||||
mContext.getResources().getQuantityString(
|
||||
R.plurals.print_settings_summary, count, count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class PrintManagerWrapper {
|
||||
|
||||
private final PrintManager mPrintManager;
|
||||
|
||||
PrintManagerWrapper(Context context) {
|
||||
mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
|
||||
.getGlobalPrintManagerForUser(context.getUserId());
|
||||
}
|
||||
|
||||
public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
|
||||
return mPrintManager.getPrintServices(selectionFlags);
|
||||
}
|
||||
|
||||
public void addPrintJobStateChanegListner(PrintJobStateChangeListener listener) {
|
||||
mPrintManager.addPrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePrintJobStateChangeListener(PrintJobStateChangeListener listener) {
|
||||
mPrintManager.removePrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public List<PrintJob> getPrintJobs() {
|
||||
return mPrintManager.getPrintJobs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for {@link PrintSummaryProvider providers} the settings app can use to read the
|
||||
* print summary.
|
||||
*/
|
||||
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
|
||||
(activity, summaryLoader) -> new PrintSummaryProvider(activity, summaryLoader,
|
||||
new PrintSummaryProvider.PrintManagerWrapper(activity));
|
||||
|
||||
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
List<SearchIndexableResource> indexables = new ArrayList<>();
|
||||
SearchIndexableResource indexable = new SearchIndexableResource(context);
|
||||
indexable.xmlResId = R.xml.print_settings;
|
||||
indexables.add(indexable);
|
||||
return indexables;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
List<SearchIndexableResource> indexables = new ArrayList<>();
|
||||
SearchIndexableResource indexable = new SearchIndexableResource(context);
|
||||
indexable.xmlResId = R.xml.print_settings;
|
||||
indexables.add(indexable);
|
||||
return indexables;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
54
src/com/android/settings/wrapper/PrintManagerWrapper.java
Normal file
54
src/com/android/settings/wrapper/PrintManagerWrapper.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.wrapper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintManager;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Wrapper class for {@link PrintManager}. This is necessary to increase testability in Robolectric.
|
||||
*/
|
||||
public class PrintManagerWrapper {
|
||||
|
||||
private final PrintManager mPrintManager;
|
||||
|
||||
public PrintManagerWrapper(Context context) {
|
||||
mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
|
||||
.getGlobalPrintManagerForUser(context.getUserId());
|
||||
}
|
||||
|
||||
public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
|
||||
return mPrintManager.getPrintServices(selectionFlags);
|
||||
}
|
||||
|
||||
public void addPrintJobStateChanegListener(PrintManager.PrintJobStateChangeListener listener) {
|
||||
mPrintManager.addPrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePrintJobStateChangeListener(
|
||||
PrintManager.PrintJobStateChangeListener listener) {
|
||||
mPrintManager.removePrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public List<PrintJob> getPrintJobs() {
|
||||
return mPrintManager.getPrintJobs();
|
||||
}
|
||||
}
|
@@ -16,7 +16,6 @@
|
||||
package com.android.settings.connecteddevice;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -24,9 +23,6 @@ import android.content.pm.PackageManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.nfc.NfcPreferenceController;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
import com.android.settingslib.drawer.CategoryKey;
|
||||
@@ -52,16 +48,14 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
|
||||
@Mock
|
||||
private PackageManager mManager;
|
||||
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private AdvancedConnectedDeviceDashboardFragment mFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
|
||||
mFragment = new AdvancedConnectedDeviceDashboardFragment();
|
||||
when(mContext.getPackageManager()).thenReturn(mManager);
|
||||
when(mContext.getApplicationContext().getPackageManager()).thenReturn(mManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,28 +73,6 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
|
||||
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchIndexProvider_NoNfc_KeyAdded() {
|
||||
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
|
||||
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
|
||||
mContext);
|
||||
|
||||
assertThat(keys).isNotNull();
|
||||
assertThat(keys).contains(NfcPreferenceController.KEY_TOGGLE_NFC);
|
||||
assertThat(keys).contains(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchIndexProvider_NFC_KeyNotAdded() {
|
||||
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
|
||||
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
|
||||
mContext);
|
||||
|
||||
assertThat(keys).isNotNull();
|
||||
assertThat(keys).doesNotContain(NfcPreferenceController.KEY_TOGGLE_NFC);
|
||||
assertThat(keys).doesNotContain(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCategoryKey_returnCategoryDevice() {
|
||||
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
|
||||
|
@@ -1,12 +1,15 @@
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -16,20 +19,12 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class UsbModePreferenceControllerTest {
|
||||
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private UsbBackend mUsbBackend;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private UsbConnectionBroadcastReceiver mUsbConnectionBroadcastReceiver;
|
||||
|
||||
@@ -40,7 +35,7 @@ public class UsbModePreferenceControllerTest {
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = ShadowApplication.getInstance().getApplicationContext();
|
||||
mController = new UsbModePreferenceController(mContext, mUsbBackend);
|
||||
mController = new UsbModePreferenceController(mContext, mUsbBackend, null /* lifecycle */);
|
||||
mController.mUsbReceiver = mUsbConnectionBroadcastReceiver;
|
||||
}
|
||||
|
||||
|
@@ -16,14 +16,11 @@
|
||||
package com.android.settings.core;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController
|
||||
.DISABLED_DEPENDENT_SETTING;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
|
||||
import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
|
||||
import static com.android.settings.core.BasePreferenceController.UNAVAILABLE_UNKNOWN;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
@@ -41,7 +38,7 @@ import org.robolectric.annotation.Config;
|
||||
public class BasePreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
BasePreferenceController mPreferenceController;
|
||||
private BasePreferenceController mPreferenceController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.print;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.Resources;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintJobInfo;
|
||||
import android.print.PrintManager;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.dashboard.SummaryLoader;
|
||||
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.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class PrintSettingsFragmentTest {
|
||||
|
||||
@Mock
|
||||
private PrintSettingsFragment.PrintSummaryProvider.PrintManagerWrapper mPrintManager;
|
||||
@Mock
|
||||
private Activity mActivity;
|
||||
@Mock
|
||||
private Resources mRes;
|
||||
@Mock
|
||||
private SummaryLoader mSummaryLoader;
|
||||
private SummaryLoader.SummaryProvider mSummaryProvider;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mActivity.getResources()).thenReturn(mRes);
|
||||
mSummaryProvider = new PrintSettingsFragment.PrintSummaryProvider(mActivity, mSummaryLoader,
|
||||
mPrintManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSummary_hasActiveJob_shouldSetSummaryToNumberOfJobs() {
|
||||
final List<PrintJob> printJobs = new ArrayList<>();
|
||||
final PrintJob job = mock(PrintJob.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
printJobs.add(job);
|
||||
when(job.getInfo().getState()).thenReturn(PrintJobInfo.STATE_STARTED);
|
||||
when(mPrintManager.getPrintJobs()).thenReturn(printJobs);
|
||||
|
||||
mSummaryProvider.setListening(true);
|
||||
|
||||
verify(mRes).getQuantityString(R.plurals.print_jobs_summary, 1, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSummary_shouldSetSummaryToNumberOfPrintServices() {
|
||||
final List<PrintServiceInfo> printServices = mock(List.class);
|
||||
when(printServices.isEmpty()).thenReturn(false);
|
||||
when(printServices.size()).thenReturn(2);
|
||||
// 2 services
|
||||
when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES))
|
||||
.thenReturn(printServices);
|
||||
|
||||
mSummaryProvider.setListening(true);
|
||||
|
||||
verify(mRes).getQuantityString(R.plurals.print_settings_summary, 2, 2);
|
||||
|
||||
// No service
|
||||
when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)).thenReturn(null);
|
||||
|
||||
mSummaryProvider.setListening(true);
|
||||
|
||||
verify(mActivity).getString(R.string.print_settings_summary_no_service);
|
||||
}
|
||||
}
|
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* 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.print;
|
||||
|
||||
import static android.arch.lifecycle.Lifecycle.Event.ON_START;
|
||||
import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.arch.lifecycle.LifecycleOwner;
|
||||
import android.content.Context;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintJobInfo;
|
||||
import android.print.PrintManager;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.wrapper.PrintManagerWrapper;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class PrintSettingsPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private PrintManagerWrapper mPrintManager;
|
||||
private Context mContext;
|
||||
private Preference mPreference;
|
||||
private PrintSettingPreferenceController mController;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private Lifecycle mLifecycle;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPreference = new Preference(mContext);
|
||||
mController = new PrintSettingPreferenceController(mContext);
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
ReflectionHelpers.setField(mController, "mPrintManager", mPrintManager);
|
||||
mLifecycle.addObserver(mController);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStartStop_shouldRegisterPrintStateListener() {
|
||||
mLifecycle.handleLifecycleEvent(ON_START);
|
||||
mLifecycle.handleLifecycleEvent(ON_STOP);
|
||||
|
||||
verify(mPrintManager).addPrintJobStateChanegListener(mController);
|
||||
verify(mPrintManager).removePrintJobStateChangeListener(mController);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_hasActiveJob_shouldSetSummaryToNumberOfJobs() {
|
||||
final List<PrintJob> printJobs = new ArrayList<>();
|
||||
final PrintJob job = mock(PrintJob.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
printJobs.add(job);
|
||||
when(job.getInfo().getState()).thenReturn(PrintJobInfo.STATE_STARTED);
|
||||
when(mPrintManager.getPrintJobs()).thenReturn(printJobs);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(mContext.getResources()
|
||||
.getQuantityString(R.plurals.print_jobs_summary, 1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_shouldSetSummaryToNumberOfPrintServices() {
|
||||
final List<PrintServiceInfo> printServices = mock(List.class);
|
||||
when(printServices.isEmpty()).thenReturn(false);
|
||||
when(printServices.size()).thenReturn(2);
|
||||
// 2 services
|
||||
when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES))
|
||||
.thenReturn(printServices);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(mContext.getResources()
|
||||
.getQuantityString(R.plurals.print_settings_summary, 2, 2));
|
||||
|
||||
// No service
|
||||
when(mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES)).thenReturn(null);
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary())
|
||||
.isEqualTo(mContext.getString(R.string.print_settings_summary_no_service));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user