Merge "Change print setting from a dynamic tile to static pref"
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user