fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,47 @@
/*
* 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.print.PrintJob;
import android.print.PrintJobInfo;
import android.text.TextUtils;
public class PrintJobMessagePreferenceController extends PrintJobPreferenceControllerBase {
public PrintJobMessagePreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected void updateUi() {
final PrintJob printJob = getPrintJob();
if (printJob == null) {
mFragment.finish();
return;
}
if (printJob.isCancelled() || printJob.isCompleted()) {
mFragment.finish();
return;
}
final PrintJobInfo info = printJob.getInfo();
final CharSequence status = info.getStatus(mContext.getPackageManager());
mPreference.setVisible(!TextUtils.isEmpty(status));
mPreference.setSummary(status);
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.print.PrintJob;
import android.print.PrintJobInfo;
import android.text.format.DateUtils;
import com.android.settings.R;
import java.text.DateFormat;
public class PrintJobPreferenceController extends PrintJobPreferenceControllerBase {
public PrintJobPreferenceController(Context context, String key) {
super(context, key);
}
@Override
protected void updateUi() {
final PrintJob printJob = getPrintJob();
if (printJob == null) {
mFragment.finish();
return;
}
if (printJob.isCancelled() || printJob.isCompleted()) {
mFragment.finish();
return;
}
PrintJobInfo info = printJob.getInfo();
switch (info.getState()) {
case PrintJobInfo.STATE_CREATED: {
mPreference.setTitle(mContext.getString(
R.string.print_configuring_state_title_template, info.getLabel()));
}
break;
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED: {
if (!printJob.getInfo().isCancelling()) {
mPreference.setTitle(mContext.getString(
R.string.print_printing_state_title_template, info.getLabel()));
} else {
mPreference.setTitle(mContext.getString(
R.string.print_cancelling_state_title_template, info.getLabel()));
}
}
break;
case PrintJobInfo.STATE_FAILED: {
mPreference.setTitle(mContext.getString(
R.string.print_failed_state_title_template, info.getLabel()));
}
break;
case PrintJobInfo.STATE_BLOCKED: {
if (!printJob.getInfo().isCancelling()) {
mPreference.setTitle(mContext.getString(
R.string.print_blocked_state_title_template, info.getLabel()));
} else {
mPreference.setTitle(mContext.getString(
R.string.print_cancelling_state_title_template, info.getLabel()));
}
}
break;
}
mPreference.setSummary(mContext.getString(R.string.print_job_summary,
info.getPrinterName(), DateUtils.formatSameDayTime(
info.getCreationTime(), info.getCreationTime(), DateFormat.SHORT,
DateFormat.SHORT)));
TypedArray a = mContext.obtainStyledAttributes(new int[]{
android.R.attr.colorControlNormal});
int tintColor = a.getColor(0, 0);
a.recycle();
switch (info.getState()) {
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED: {
Drawable icon = mContext.getDrawable(com.android.internal.R.drawable.ic_print);
icon.setTint(tintColor);
mPreference.setIcon(icon);
break;
}
case PrintJobInfo.STATE_FAILED:
case PrintJobInfo.STATE_BLOCKED: {
Drawable icon = mContext.getDrawable(
com.android.internal.R.drawable.ic_print_error);
icon.setTint(tintColor);
mPreference.setIcon(icon);
break;
}
}
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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.print.PrintJob;
import android.print.PrintJobId;
import android.print.PrintManager;
import android.util.Log;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
public abstract class PrintJobPreferenceControllerBase extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
private static final String TAG = "PrintJobPrefCtrlBase";
private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
private final PrintManager mPrintManager;
protected Preference mPreference;
protected PrintJobSettingsFragment mFragment;
protected PrintJobId mPrintJobId;
public PrintJobPreferenceControllerBase(Context context, String key) {
super(context, key);
mPrintManager = ((PrintManager) mContext.getSystemService(
Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(mContext.getUserId());
}
@Override
public void onStart() {
mPrintManager.addPrintJobStateChangeListener(this);
updateUi();
}
@Override
public void onStop() {
mPrintManager.removePrintJobStateChangeListener(this);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void onPrintJobStateChanged(PrintJobId printJobId) {
updateUi();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
public void init(PrintJobSettingsFragment fragment) {
mFragment = fragment;
processArguments();
}
protected PrintJob getPrintJob() {
return mPrintManager.getPrintJob(mPrintJobId);
}
protected abstract void updateUi();
private void processArguments() {
String printJobId = mFragment.getArguments().getString(EXTRA_PRINT_JOB_ID);
if (printJobId == null) {
printJobId = mFragment.getActivity().getIntent().getStringExtra(EXTRA_PRINT_JOB_ID);
if (printJobId == null) {
Log.w(TAG, EXTRA_PRINT_JOB_ID + " not set");
mFragment.finish();
return;
}
}
mPrintJobId = PrintJobId.unflattenFromString(printJobId);
}
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2013 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.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.print.PrintJob;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
/**
* Fragment for management of a print job.
*/
public class PrintJobSettingsFragment extends DashboardFragment {
private static final String TAG = "PrintJobSettingsFragment";
private static final int MENU_ITEM_ID_CANCEL = 1;
private static final int MENU_ITEM_ID_RESTART = 2;
@Override
protected int getPreferenceScreenResId() {
return R.xml.print_job_settings;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(PrintJobPreferenceController.class).init(this);
use(PrintJobMessagePreferenceController.class).init(this);
}
@Override
public int getMetricsCategory() {
return SettingsEnums.PRINT_JOB_SETTINGS;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setEnabled(false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final PrintJob printJob = use(PrintJobPreferenceController.class).getPrintJob();
if (printJob == null) {
return;
}
if (!printJob.getInfo().isCancelling()) {
MenuItem cancel = menu.add(0, MENU_ITEM_ID_CANCEL, Menu.NONE,
getString(R.string.print_cancel));
cancel.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
if (printJob.isFailed()) {
MenuItem restart = menu.add(0, MENU_ITEM_ID_RESTART, Menu.NONE,
getString(R.string.print_restart));
restart.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final PrintJob printJob = use(PrintJobPreferenceController.class).getPrintJob();
if (printJob != null) {
switch (item.getItemId()) {
case MENU_ITEM_ID_CANCEL: {
printJob.cancel();
finish();
return true;
}
case MENU_ITEM_ID_RESTART: {
printJob.restart();
finish();
return true;
}
}
}
return super.onOptionsItemSelected(item);
}
}

View File

@@ -0,0 +1,743 @@
/*
* Copyright (C) 2013 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.app.Activity;
import android.app.ActivityOptions;
import android.app.settings.SettingsEnums;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.print.PrintManager;
import android.print.PrinterDiscoverySession;
import android.print.PrinterDiscoverySession.OnPrintersChangeListener;
import android.print.PrinterId;
import android.print.PrinterInfo;
import android.printservice.PrintServiceInfo;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.SettingsMainSwitchBar;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Fragment with print service settings.
*/
public class PrintServiceSettingsFragment extends SettingsPreferenceFragment
implements OnCheckedChangeListener,
LoaderManager.LoaderCallbacks<List<PrintServiceInfo>> {
private static final String LOG_TAG = "PrintServiceSettings";
private static final int LOADER_ID_PRINTERS_LOADER = 1;
private static final int LOADER_ID_PRINT_SERVICE_LOADER = 2;
private final AdapterDataObserver mDataObserver = new AdapterDataObserver() {
@Override
public void onChanged() {
invalidateOptionsMenuIfNeeded();
updateEmptyView();
}
private void invalidateOptionsMenuIfNeeded() {
final int unfilteredItemCount = mPrintersAdapter.getUnfilteredCount();
if ((mLastUnfilteredItemCount <= 0 && unfilteredItemCount > 0)
|| mLastUnfilteredItemCount > 0 && unfilteredItemCount <= 0) {
getActivity().invalidateOptionsMenu();
}
mLastUnfilteredItemCount = unfilteredItemCount;
}
};
private SettingsMainSwitchBar mSwitchBar;
private String mPreferenceKey;
private Intent mSettingsIntent;
private Intent mAddPrintersIntent;
private ComponentName mComponentName;
private PrintersAdapter mPrintersAdapter;
private int mLastUnfilteredItemCount;
private boolean mServiceEnabled;
private SearchView mSearchView;
@Override
public int getMetricsCategory() {
return SettingsEnums.PRINT_SERVICE_SETTINGS;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String title = getArguments().getString(PrintSettingsFragment.EXTRA_TITLE);
if (!TextUtils.isEmpty(title)) {
getActivity().setTitle(title);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
mServiceEnabled = getArguments().getBoolean(PrintSettingsFragment.EXTRA_CHECKED);
return root;
}
@Override
public void onStart() {
super.onStart();
initComponents();
updateUiForArguments();
updateEmptyView();
updateUiForServiceState();
}
@Override
public void onPause() {
if (mSearchView != null) {
mSearchView.setOnQueryTextListener(null);
}
super.onPause();
}
@Override
public void onStop() {
super.onStop();
mSwitchBar.removeOnSwitchChangeListener(this);
mSwitchBar.hide();
mPrintersAdapter.unregisterAdapterDataObserver(mDataObserver);
}
private void onPreferenceToggled(String preferenceKey, boolean enabled) {
((PrintManager) getContext().getSystemService(Context.PRINT_SERVICE))
.setPrintServiceEnabled(mComponentName, enabled);
}
private void updateEmptyView() {
ViewGroup contentRoot = (ViewGroup) getListView().getParent();
View emptyView = getEmptyView();
if (!mSwitchBar.isChecked()) {
if (emptyView != null) {
contentRoot.removeView(emptyView);
emptyView = null;
}
if (emptyView == null) {
emptyView = getActivity().getLayoutInflater().inflate(
R.layout.empty_print_state, contentRoot, false);
TextView textView = (TextView) emptyView.findViewById(R.id.message);
textView.setText(R.string.print_service_disabled);
contentRoot.addView(emptyView);
setEmptyView(emptyView);
}
} else if (mPrintersAdapter.getUnfilteredCount() <= 0) {
if (emptyView != null) {
contentRoot.removeView(emptyView);
emptyView = null;
}
if (emptyView == null) {
emptyView = getActivity().getLayoutInflater().inflate(
R.layout.empty_printers_list_service_enabled, contentRoot, false);
contentRoot.addView(emptyView);
setEmptyView(emptyView);
}
} else if (mPrintersAdapter.getItemCount() <= 0) {
if (emptyView != null) {
contentRoot.removeView(emptyView);
emptyView = null;
}
if (emptyView == null) {
emptyView = getActivity().getLayoutInflater().inflate(
R.layout.empty_print_state, contentRoot, false);
TextView textView = (TextView) emptyView.findViewById(R.id.message);
textView.setText(R.string.print_no_printers_found);
contentRoot.addView(emptyView);
setEmptyView(emptyView);
}
} else if (mPrintersAdapter.getItemCount() > 0) {
if (emptyView != null) {
contentRoot.removeView(emptyView);
}
}
}
private void updateUiForServiceState() {
if (mServiceEnabled) {
mSwitchBar.setCheckedInternal(true);
mPrintersAdapter.enable();
} else {
mSwitchBar.setCheckedInternal(false);
mPrintersAdapter.disable();
}
getActivity().invalidateOptionsMenu();
}
private void initComponents() {
mPrintersAdapter = new PrintersAdapter();
mPrintersAdapter.registerAdapterDataObserver(mDataObserver);
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mSwitchBar.setTitle(
getContext().getString(R.string.default_print_service_main_switch_title));
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.show();
mSwitchBar.setOnBeforeCheckedChangeListener((checked) -> {
onPreferenceToggled(mPreferenceKey, checked);
return false;
});
getListView().setAdapter(mPrintersAdapter);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateEmptyView();
}
private void updateUiForArguments() {
Bundle arguments = getArguments();
// Component name.
mComponentName = ComponentName.unflattenFromString(arguments
.getString(PrintSettingsFragment.EXTRA_SERVICE_COMPONENT_NAME));
// Key.
mPreferenceKey = mComponentName.flattenToString();
// Enabled.
final boolean enabled = arguments.getBoolean(PrintSettingsFragment.EXTRA_CHECKED);
mSwitchBar.setCheckedInternal(enabled);
getLoaderManager().initLoader(LOADER_ID_PRINT_SERVICE_LOADER, null, this);
setHasOptionsMenu(true);
}
@Override
public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
return new SettingsPrintServicesLoader(
(PrintManager) getContext().getSystemService(Context.PRINT_SERVICE), getContext(),
PrintManager.ALL_SERVICES);
}
@Override
public void onLoadFinished(Loader<List<PrintServiceInfo>> loader,
List<PrintServiceInfo> services) {
PrintServiceInfo service = null;
if (services != null) {
final int numServices = services.size();
for (int i = 0; i < numServices; i++) {
if (services.get(i).getComponentName().equals(mComponentName)) {
service = services.get(i);
break;
}
}
}
if (service == null) {
// The print service was uninstalled
finishFragment();
}
mServiceEnabled = service.isEnabled();
if (service.getSettingsActivityName() != null) {
Intent settingsIntent = new Intent(Intent.ACTION_MAIN);
settingsIntent.setComponent(
new ComponentName(service.getComponentName().getPackageName(),
service.getSettingsActivityName()));
List<ResolveInfo> resolvedActivities = getPackageManager().queryIntentActivities(
settingsIntent, 0);
if (!resolvedActivities.isEmpty()) {
// The activity is a component name, therefore it is one or none.
if (resolvedActivities.get(0).activityInfo.exported) {
mSettingsIntent = settingsIntent;
}
}
} else {
mSettingsIntent = null;
}
if (service.getAddPrintersActivityName() != null) {
Intent addPrintersIntent = new Intent(Intent.ACTION_MAIN);
addPrintersIntent.setComponent(
new ComponentName(service.getComponentName().getPackageName(),
service.getAddPrintersActivityName()));
List<ResolveInfo> resolvedActivities = getPackageManager().queryIntentActivities(
addPrintersIntent, 0);
if (!resolvedActivities.isEmpty()) {
// The activity is a component name, therefore it is one or none.
if (resolvedActivities.get(0).activityInfo.exported) {
mAddPrintersIntent = addPrintersIntent;
}
}
} else {
mAddPrintersIntent = null;
}
updateUiForServiceState();
}
@Override
public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) {
updateUiForServiceState();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.print_service_settings, menu);
MenuItem addPrinters = menu.findItem(R.id.print_menu_item_add_printer);
if (mServiceEnabled && mAddPrintersIntent != null) {
addPrinters.setIntent(mAddPrintersIntent);
} else {
menu.removeItem(R.id.print_menu_item_add_printer);
}
MenuItem settings = menu.findItem(R.id.print_menu_item_settings);
if (mServiceEnabled && mSettingsIntent != null) {
settings.setIntent(mSettingsIntent);
} else {
menu.removeItem(R.id.print_menu_item_settings);
}
MenuItem searchItem = menu.findItem(R.id.print_menu_item_search);
if (mServiceEnabled && mPrintersAdapter.getUnfilteredCount() > 0) {
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setMaxWidth(Integer.MAX_VALUE);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String searchString) {
mPrintersAdapter.getFilter().filter(searchString);
return true;
}
});
mSearchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
if (AccessibilityManager.getInstance(getActivity()).isEnabled()) {
view.announceForAccessibility(getString(
R.string.print_search_box_shown_utterance));
}
}
@Override
public void onViewDetachedFromWindow(View view) {
Activity activity = getActivity();
if (activity != null && !activity.isFinishing()
&& AccessibilityManager.getInstance(activity).isEnabled()) {
view.announceForAccessibility(getString(
R.string.print_search_box_hidden_utterance));
}
}
});
} else {
menu.removeItem(R.id.print_menu_item_search);
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
private final class PrintersAdapter extends RecyclerView.Adapter<ViewHolder>
implements LoaderManager.LoaderCallbacks<List<PrinterInfo>>, Filterable {
private final Object mLock = new Object();
private final List<PrinterInfo> mPrinters = new ArrayList<PrinterInfo>();
private final List<PrinterInfo> mFilteredPrinters = new ArrayList<PrinterInfo>();
private CharSequence mLastSearchString;
public void enable() {
getLoaderManager().initLoader(LOADER_ID_PRINTERS_LOADER, null, this);
}
public void disable() {
getLoaderManager().destroyLoader(LOADER_ID_PRINTERS_LOADER);
mPrinters.clear();
}
public int getUnfilteredCount() {
return mPrinters.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
synchronized (mLock) {
if (TextUtils.isEmpty(constraint)) {
return null;
}
FilterResults results = new FilterResults();
List<PrinterInfo> filteredPrinters = new ArrayList<PrinterInfo>();
String constraintLowerCase = constraint.toString().toLowerCase();
final int printerCount = mPrinters.size();
for (int i = 0; i < printerCount; i++) {
PrinterInfo printer = mPrinters.get(i);
String name = printer.getName();
if (name != null && name.toLowerCase().contains(constraintLowerCase)) {
filteredPrinters.add(printer);
}
}
results.values = filteredPrinters;
results.count = filteredPrinters.size();
return results;
}
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
synchronized (mLock) {
mLastSearchString = constraint;
mFilteredPrinters.clear();
if (results == null) {
mFilteredPrinters.addAll(mPrinters);
} else {
List<PrinterInfo> printers = (List<PrinterInfo>) results.values;
mFilteredPrinters.addAll(printers);
}
}
notifyDataSetChanged();
}
};
}
@Override
public int getItemCount() {
synchronized (mLock) {
return mFilteredPrinters.size();
}
}
private Object getItem(int position) {
synchronized (mLock) {
return mFilteredPrinters.get(position);
}
}
@Override
public long getItemId(int position) {
return position;
}
/**
* Checks if a printer can be used for printing
*
* @param position The position of the printer in the list
* @return true iff the printer can be used for printing.
*/
public boolean isActionable(int position) {
PrinterInfo printer = (PrinterInfo) getItem(position);
return printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.printer_dropdown_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.itemView.setEnabled(isActionable(position));
final PrinterInfo printer = (PrinterInfo) getItem(position);
CharSequence title = printer.getName();
CharSequence subtitle = printer.getDescription();
Drawable icon = printer.loadIcon(getActivity());
TextView titleView = holder.itemView.findViewById(R.id.title);
titleView.setText(title);
TextView subtitleView = holder.itemView.findViewById(R.id.subtitle);
if (!TextUtils.isEmpty(subtitle)) {
subtitleView.setText(subtitle);
subtitleView.setVisibility(View.VISIBLE);
} else {
subtitleView.setText(null);
subtitleView.setVisibility(View.GONE);
}
LinearLayout moreInfoView = holder.itemView.findViewById(R.id.more_info);
if (printer.getInfoIntent() != null) {
moreInfoView.setVisibility(View.VISIBLE);
moreInfoView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Bundle options = ActivityOptions.makeBasic()
.setPendingIntentBackgroundActivityStartMode(
ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
.toBundle();
getActivity().startIntentSender(
printer.getInfoIntent().getIntentSender(), null, 0, 0, 0,
options);
} catch (SendIntentException e) {
Log.e(LOG_TAG, "Could not execute pending info intent: %s", e);
}
}
});
} else {
moreInfoView.setVisibility(View.GONE);
}
ImageView iconView = holder.itemView.findViewById(R.id.icon);
if (icon != null) {
iconView.setVisibility(View.VISIBLE);
if (!isActionable(position)) {
icon.mutate();
TypedValue value = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.disabledAlpha, value,
true);
icon.setAlpha((int) (value.getFloat() * 255));
}
iconView.setImageDrawable(icon);
} else {
iconView.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(v -> {
PrinterInfo pi = (PrinterInfo) getItem(position);
if (pi.getInfoIntent() != null) {
try {
getActivity().startIntentSender(pi.getInfoIntent().getIntentSender(),
null, 0, 0, 0);
} catch (SendIntentException e) {
Log.e(LOG_TAG, "Could not execute info intent: %s", e);
}
}
});
}
@Override
public Loader<List<PrinterInfo>> onCreateLoader(int id, Bundle args) {
if (id == LOADER_ID_PRINTERS_LOADER) {
return new PrintersLoader(getContext());
}
return null;
}
@Override
public void onLoadFinished(Loader<List<PrinterInfo>> loader,
List<PrinterInfo> printers) {
synchronized (mLock) {
mPrinters.clear();
final int printerCount = printers.size();
for (int i = 0; i < printerCount; i++) {
PrinterInfo printer = printers.get(i);
if (printer.getId().getServiceName().equals(mComponentName)) {
mPrinters.add(printer);
}
}
mFilteredPrinters.clear();
mFilteredPrinters.addAll(mPrinters);
if (!TextUtils.isEmpty(mLastSearchString)) {
getFilter().filter(mLastSearchString);
}
}
notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<List<PrinterInfo>> loader) {
synchronized (mLock) {
mPrinters.clear();
mFilteredPrinters.clear();
mLastSearchString = null;
}
notifyDataSetChanged();
}
}
private static class PrintersLoader extends Loader<List<PrinterInfo>> {
private static final String LOG_TAG = "PrintersLoader";
private static final boolean DEBUG = false;
private final Map<PrinterId, PrinterInfo> mPrinters =
new LinkedHashMap<PrinterId, PrinterInfo>();
private PrinterDiscoverySession mDiscoverySession;
public PrintersLoader(Context context) {
super(context);
}
@Override
public void deliverResult(List<PrinterInfo> printers) {
if (isStarted()) {
super.deliverResult(printers);
}
}
@Override
protected void onStartLoading() {
if (DEBUG) {
Log.i(LOG_TAG, "onStartLoading()");
}
// The contract is that if we already have a valid,
// result the we have to deliver it immediately.
if (!mPrinters.isEmpty()) {
deliverResult(new ArrayList<PrinterInfo>(mPrinters.values()));
}
// We want to start discovery at this point.
onForceLoad();
}
@Override
protected void onStopLoading() {
if (DEBUG) {
Log.i(LOG_TAG, "onStopLoading()");
}
onCancelLoad();
}
@Override
protected void onForceLoad() {
if (DEBUG) {
Log.i(LOG_TAG, "onForceLoad()");
}
loadInternal();
}
@Override
protected boolean onCancelLoad() {
if (DEBUG) {
Log.i(LOG_TAG, "onCancelLoad()");
}
return cancelInternal();
}
@Override
protected void onReset() {
if (DEBUG) {
Log.i(LOG_TAG, "onReset()");
}
onStopLoading();
mPrinters.clear();
if (mDiscoverySession != null) {
mDiscoverySession.destroy();
mDiscoverySession = null;
}
}
@Override
protected void onAbandon() {
if (DEBUG) {
Log.i(LOG_TAG, "onAbandon()");
}
onStopLoading();
}
private boolean cancelInternal() {
if (mDiscoverySession != null
&& mDiscoverySession.isPrinterDiscoveryStarted()) {
mDiscoverySession.stopPrinterDiscovery();
return true;
}
return false;
}
private void loadInternal() {
if (mDiscoverySession == null) {
PrintManager printManager = (PrintManager) getContext()
.getSystemService(Context.PRINT_SERVICE);
mDiscoverySession = printManager.createPrinterDiscoverySession();
mDiscoverySession.setOnPrintersChangeListener(new OnPrintersChangeListener() {
@Override
public void onPrintersChanged() {
deliverResult(new ArrayList<PrinterInfo>(
mDiscoverySession.getPrinters()));
}
});
}
mDiscoverySession.startPrinterDiscovery(null);
}
}
}

View File

@@ -0,0 +1,145 @@
/*
* 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.os.UserManager;
import android.print.PrintJob;
import android.print.PrintJobId;
import android.print.PrintJobInfo;
import android.print.PrintManager;
import android.printservice.PrintServiceInfo;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.utils.StringUtil;
import java.util.List;
/**
* {@link BasePreferenceController} for Print settings.
*/
public class PrintSettingPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
private static final String KEY_PRINTING_SETTINGS = "connected_device_printing";
private final PackageManager mPackageManager;
private final PrintManager mPrintManager;
private Preference mPreference;
public PrintSettingPreferenceController(Context context) {
super(context, KEY_PRINTING_SETTINGS);
mPackageManager = context.getPackageManager();
mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
.getGlobalPrintManagerForUser(context.getUserId());
}
@Override
public int getAvailabilityStatus() {
return mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)
&& mPrintManager != null
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void onStart() {
if (mPrintManager != null) {
mPrintManager.addPrintJobStateChangeListener(this);
}
}
@Override
public void onStop() {
if (mPrintManager != null) {
mPrintManager.removePrintJobStateChangeListener(this);
}
}
@Override
public void onPrintJobStateChanged(PrintJobId printJobId) {
updateState(mPreference);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
((RestrictedPreference) preference).checkRestrictionAndSetDisabled(
UserManager.DISALLOW_PRINTING);
}
@Override
public CharSequence 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 StringUtil.getIcuPluralsString(mContext, numActivePrintJobs,
R.string.print_jobs_summary);
} else {
final List<PrintServiceInfo> services =
mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
if (services == null || services.isEmpty()) {
return mContext.getText(R.string.print_settings_summary_no_service);
} else {
return StringUtil.getIcuPluralsString(mContext, services.size(),
R.string.print_settings_summary);
}
}
}
/**
* 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;
}
}

View File

@@ -0,0 +1,537 @@
/*
* Copyright (C) 2013 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 com.android.settings.print.PrintSettingPreferenceController.shouldShowToUser;
import android.app.settings.SettingsEnums;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserManager;
import android.print.PrintJob;
import android.print.PrintJobId;
import android.print.PrintJobInfo;
import android.print.PrintManager;
import android.print.PrintManager.PrintJobStateChangeListener;
import android.printservice.PrintServiceInfo;
import android.provider.Settings;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager.LoaderCallbacks;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.widget.AppPreference;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Fragment with the top level print settings.
*/
@SearchIndexable
public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
implements Indexable, OnClickListener {
public static final String TAG = "PrintSettingsFragment";
private static final int LOADER_ID_PRINT_JOBS_LOADER = 1;
private static final int LOADER_ID_PRINT_SERVICES = 2;
private static final String PRINT_JOBS_CATEGORY = "print_jobs_category";
private static final String PRINT_SERVICES_CATEGORY = "print_services_category";
static final String EXTRA_CHECKED = "EXTRA_CHECKED";
static final String EXTRA_TITLE = "EXTRA_TITLE";
static final String EXTRA_SERVICE_COMPONENT_NAME = "EXTRA_SERVICE_COMPONENT_NAME";
static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
private static final String EXTRA_PRINT_SERVICE_COMPONENT_NAME =
"EXTRA_PRINT_SERVICE_COMPONENT_NAME";
private static final int ORDER_LAST = Preference.DEFAULT_ORDER - 1;
private PreferenceCategory mActivePrintJobsCategory;
private PreferenceCategory mPrintServicesCategory;
private PrintJobsController mPrintJobsController;
private PrintServicesController mPrintServicesController;
private Button mAddNewServiceButton;
@VisibleForTesting
boolean mIsUiRestricted;
public PrintSettingsFragment() {
super(UserManager.DISALLOW_PRINTING);
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.print_settings;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.PRINT_SETTINGS;
}
@Override
public int getHelpResource() {
return R.string.help_uri_printing;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
mIsUiRestricted = isUiRestricted();
setupPreferences();
return root;
}
@VisibleForTesting
void setupPreferences() {
if (mIsUiRestricted) {
return;
}
mActivePrintJobsCategory = (PreferenceCategory) findPreference(PRINT_JOBS_CATEGORY);
mPrintServicesCategory = (PreferenceCategory) findPreference(PRINT_SERVICES_CATEGORY);
getPreferenceScreen().removePreference(mActivePrintJobsCategory);
mPrintJobsController = new PrintJobsController();
getLoaderManager().initLoader(LOADER_ID_PRINT_JOBS_LOADER, null, mPrintJobsController);
mPrintServicesController = new PrintServicesController();
getLoaderManager().initLoader(LOADER_ID_PRINT_SERVICES, null, mPrintServicesController);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupEmptyViews();
}
@VisibleForTesting
void setupEmptyViews() {
if (mIsUiRestricted) {
return;
}
ViewGroup contentRoot = (ViewGroup) getListView().getParent();
View emptyView = getActivity().getLayoutInflater().inflate(
R.layout.empty_print_state, contentRoot, false);
TextView textView = (TextView) emptyView.findViewById(R.id.message);
textView.setText(R.string.print_no_services_installed);
final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
if (addNewServiceIntent != null) {
mAddNewServiceButton = (Button) emptyView.findViewById(R.id.add_new_service);
mAddNewServiceButton.setOnClickListener(this);
// The empty is used elsewhere too so it's hidden by default.
mAddNewServiceButton.setVisibility(View.VISIBLE);
}
contentRoot.addView(emptyView);
setEmptyView(emptyView);
}
@Override
public void onStart() {
super.onStart();
startSettings();
}
@VisibleForTesting
void startSettings() {
if (mIsUiRestricted) {
getPreferenceScreen().removeAll();
return;
}
setHasOptionsMenu(true);
startSubSettingsIfNeeded();
}
@Override
protected String getIntentActionString() {
return Settings.ACTION_PRINT_SETTINGS;
}
/**
* Adds preferences for all print services to the {@value PRINT_SERVICES_CATEGORY} cathegory.
*/
private final class PrintServicesController implements LoaderCallbacks<List<PrintServiceInfo>> {
@Override
public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
PrintManager printManager =
(PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
if (printManager != null) {
return new SettingsPrintServicesLoader(printManager, getContext(),
PrintManager.ALL_SERVICES);
} else {
return null;
}
}
@Override
public void onLoadFinished(Loader<List<PrintServiceInfo>> loader,
List<PrintServiceInfo> services) {
if (services.isEmpty()) {
getPreferenceScreen().removePreference(mPrintServicesCategory);
return;
} else if (getPreferenceScreen().findPreference(PRINT_SERVICES_CATEGORY) == null) {
getPreferenceScreen().addPreference(mPrintServicesCategory);
}
mPrintServicesCategory.removeAll();
PackageManager pm = getActivity().getPackageManager();
final Context context = getPrefContext();
if (context == null) {
Log.w(TAG, "No preference context, skip adding print services");
return;
}
for (PrintServiceInfo service : services) {
AppPreference preference = new AppPreference(context);
String title = service.getResolveInfo().loadLabel(pm).toString();
preference.setTitle(title);
ComponentName componentName = service.getComponentName();
preference.setKey(componentName.flattenToString());
preference.setFragment(PrintServiceSettingsFragment.class.getName());
preference.setPersistent(false);
if (service.isEnabled()) {
preference.setSummary(getString(R.string.print_feature_state_on));
} else {
preference.setSummary(getString(R.string.print_feature_state_off));
}
Drawable drawable = service.getResolveInfo().loadIcon(pm);
if (drawable != null) {
preference.setIcon(drawable);
}
Bundle extras = preference.getExtras();
extras.putBoolean(EXTRA_CHECKED, service.isEnabled());
extras.putString(EXTRA_TITLE, title);
extras.putString(EXTRA_SERVICE_COMPONENT_NAME, componentName.flattenToString());
mPrintServicesCategory.addPreference(preference);
}
Preference addNewServicePreference = newAddServicePreferenceOrNull();
if (addNewServicePreference != null) {
mPrintServicesCategory.addPreference(addNewServicePreference);
}
}
@Override
public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) {
getPreferenceScreen().removePreference(mPrintServicesCategory);
}
}
private Preference newAddServicePreferenceOrNull() {
final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
if (addNewServiceIntent == null) {
return null;
}
Preference preference = new Preference(getPrefContext());
preference.setTitle(R.string.print_menu_item_add_service);
preference.setIcon(R.drawable.ic_add_24dp);
preference.setOrder(ORDER_LAST);
preference.setIntent(addNewServiceIntent);
preference.setPersistent(false);
return preference;
}
private Intent createAddNewServiceIntentOrNull() {
final String searchUri = Settings.Secure.getString(getContentResolver(),
Settings.Secure.PRINT_SERVICE_SEARCH_URI);
if (TextUtils.isEmpty(searchUri)) {
return null;
}
return new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri));
}
private void startSubSettingsIfNeeded() {
if (getArguments() == null) {
return;
}
String componentName = getArguments().getString(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
if (componentName != null) {
getArguments().remove(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
Preference prereference = findPreference(componentName);
if (prereference != null) {
prereference.performClick();
}
}
}
@Override
public void onClick(View v) {
if (mAddNewServiceButton == v) {
final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
if (addNewServiceIntent != null) { // check again just in case.
try {
startActivity(addNewServiceIntent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Unable to start activity", e);
}
}
}
}
private final class PrintJobsController implements LoaderCallbacks<List<PrintJobInfo>> {
@Override
public Loader<List<PrintJobInfo>> onCreateLoader(int id, Bundle args) {
if (id == LOADER_ID_PRINT_JOBS_LOADER) {
return new PrintJobsLoader(getContext());
}
return null;
}
@Override
public void onLoadFinished(Loader<List<PrintJobInfo>> loader,
List<PrintJobInfo> printJobs) {
if (printJobs == null || printJobs.isEmpty()) {
getPreferenceScreen().removePreference(mActivePrintJobsCategory);
} else {
if (getPreferenceScreen().findPreference(PRINT_JOBS_CATEGORY) == null) {
getPreferenceScreen().addPreference(mActivePrintJobsCategory);
}
mActivePrintJobsCategory.removeAll();
final Context context = getPrefContext();
if (context == null) {
Log.w(TAG, "No preference context, skip adding print jobs");
return;
}
for (PrintJobInfo printJob : printJobs) {
Preference preference = new Preference(context);
preference.setPersistent(false);
preference.setFragment(PrintJobSettingsFragment.class.getName());
preference.setKey(printJob.getId().flattenToString());
switch (printJob.getState()) {
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED:
if (!printJob.isCancelling()) {
preference.setTitle(getString(
R.string.print_printing_state_title_template,
printJob.getLabel()));
} else {
preference.setTitle(getString(
R.string.print_cancelling_state_title_template,
printJob.getLabel()));
}
break;
case PrintJobInfo.STATE_FAILED:
preference.setTitle(getString(
R.string.print_failed_state_title_template,
printJob.getLabel()));
break;
case PrintJobInfo.STATE_BLOCKED:
if (!printJob.isCancelling()) {
preference.setTitle(getString(
R.string.print_blocked_state_title_template,
printJob.getLabel()));
} else {
preference.setTitle(getString(
R.string.print_cancelling_state_title_template,
printJob.getLabel()));
}
break;
}
preference.setSummary(getString(R.string.print_job_summary,
printJob.getPrinterName(), DateUtils.formatSameDayTime(
printJob.getCreationTime(), printJob.getCreationTime(),
DateFormat.SHORT, DateFormat.SHORT)));
TypedArray a = getActivity().obtainStyledAttributes(new int[]{
android.R.attr.colorControlNormal});
int tintColor = a.getColor(0, 0);
a.recycle();
switch (printJob.getState()) {
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED: {
Drawable icon = getActivity().getDrawable(
com.android.internal.R.drawable.ic_print);
icon.setTint(tintColor);
preference.setIcon(icon);
break;
}
case PrintJobInfo.STATE_FAILED:
case PrintJobInfo.STATE_BLOCKED: {
Drawable icon = getActivity().getDrawable(
com.android.internal.R.drawable.ic_print_error);
icon.setTint(tintColor);
preference.setIcon(icon);
break;
}
}
Bundle extras = preference.getExtras();
extras.putString(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString());
mActivePrintJobsCategory.addPreference(preference);
}
}
}
@Override
public void onLoaderReset(Loader<List<PrintJobInfo>> loader) {
getPreferenceScreen().removePreference(mActivePrintJobsCategory);
}
}
private static final class PrintJobsLoader extends AsyncTaskLoader<List<PrintJobInfo>> {
private static final String LOG_TAG = "PrintJobsLoader";
private static final boolean DEBUG = false;
private List<PrintJobInfo> mPrintJobs = new ArrayList<PrintJobInfo>();
private final PrintManager mPrintManager;
private PrintJobStateChangeListener mPrintJobStateChangeListener;
public PrintJobsLoader(Context context) {
super(context);
mPrintManager = ((PrintManager) context.getSystemService(
Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(
context.getUserId());
}
@Override
public void deliverResult(List<PrintJobInfo> printJobs) {
if (isStarted()) {
super.deliverResult(printJobs);
}
}
@Override
protected void onStartLoading() {
if (DEBUG) {
Log.i(LOG_TAG, "onStartLoading()");
}
// If we already have a result, deliver it immediately.
if (!mPrintJobs.isEmpty()) {
deliverResult(new ArrayList<PrintJobInfo>(mPrintJobs));
}
// Start watching for changes.
if (mPrintJobStateChangeListener == null) {
mPrintJobStateChangeListener = new PrintJobStateChangeListener() {
@Override
public void onPrintJobStateChanged(PrintJobId printJobId) {
onForceLoad();
}
};
mPrintManager.addPrintJobStateChangeListener(
mPrintJobStateChangeListener);
}
// If the data changed or we have no data - load it now.
if (mPrintJobs.isEmpty()) {
onForceLoad();
}
}
@Override
protected void onStopLoading() {
if (DEBUG) {
Log.i(LOG_TAG, "onStopLoading()");
}
// Cancel the load in progress if possible.
onCancelLoad();
}
@Override
protected void onReset() {
if (DEBUG) {
Log.i(LOG_TAG, "onReset()");
}
// Stop loading.
onStopLoading();
// Clear the cached result.
mPrintJobs.clear();
// Stop watching for changes.
if (mPrintJobStateChangeListener != null) {
mPrintManager.removePrintJobStateChangeListener(
mPrintJobStateChangeListener);
mPrintJobStateChangeListener = null;
}
}
@Override
public List<PrintJobInfo> loadInBackground() {
List<PrintJobInfo> printJobInfos = null;
List<PrintJob> printJobs = mPrintManager.getPrintJobs();
final int printJobCount = printJobs.size();
for (int i = 0; i < printJobCount; i++) {
PrintJobInfo printJob = printJobs.get(i).getInfo();
if (shouldShowToUser(printJob)) {
if (printJobInfos == null) {
printJobInfos = new ArrayList<>();
}
printJobInfos.add(printJob);
}
}
return printJobInfos;
}
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.print_settings);
}

View File

@@ -0,0 +1,81 @@
/*
* 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.print;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import com.android.settings.R;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.dashboard.profileselector.UserAdapter;
/**
* Base fragment class for per profile settings.
*/
public abstract class ProfileSettingsPreferenceFragment extends RestrictedDashboardFragment {
public ProfileSettingsPreferenceFragment(String restrictionKey) {
super(restrictionKey);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
final UserAdapter profileSpinnerAdapter =
UserAdapter.createUserSpinnerAdapter(um, getActivity());
if (profileSpinnerAdapter != null) {
final Spinner spinner = (Spinner) setPinnedHeaderView(R.layout.spinner_view);
spinner.setAdapter(profileSpinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
final UserHandle selectedUser = profileSpinnerAdapter.getUserHandle(position);
if (selectedUser.getIdentifier() != UserHandle.myUserId()) {
final Activity activity = getActivity();
Intent intent = new Intent(getIntentActionString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivityAsUser(intent, selectedUser);
// Go back to default selection, which is the first one
spinner.setSelection(0);
activity.finish();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Nothing to do
}
});
}
}
/**
* @return intent action string that will bring user to this fragment.
*/
protected abstract String getIntentActionString();
}

View File

@@ -0,0 +1,82 @@
/*
* 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.print.PrintManager;
import android.print.PrintServicesLoader;
import android.printservice.PrintServiceInfo;
import androidx.annotation.NonNull;
import androidx.loader.content.Loader;
import com.android.internal.util.Preconditions;
import java.util.List;
/**
* Loader for the list of print services. Can be parametrized to select a subset.
*/
public class SettingsPrintServicesLoader extends Loader<List<PrintServiceInfo>> {
private PrintServicesLoader mLoader;
public SettingsPrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
int selectionFlags) {
super(Preconditions.checkNotNull(context));
mLoader = new PrintServicesLoader(printManager, context, selectionFlags) {
@Override
public void deliverResult(List<PrintServiceInfo> data) {
super.deliverResult(data);
// deliver the result to outer Loader class
SettingsPrintServicesLoader.this.deliverResult(data);
}
};
}
@Override
protected void onForceLoad() {
mLoader.forceLoad();
}
@Override
protected void onStartLoading() {
mLoader.startLoading();
}
@Override
protected void onStopLoading() {
mLoader.stopLoading();
}
@Override
protected boolean onCancelLoad() {
return mLoader.cancelLoad();
}
@Override
protected void onAbandon() {
mLoader.abandon();
}
@Override
protected void onReset() {
mLoader.reset();
}
}