1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.print; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.UserManager; 22 import android.print.PrintJob; 23 import android.print.PrintJobId; 24 import android.print.PrintJobInfo; 25 import android.print.PrintManager; 26 import android.printservice.PrintServiceInfo; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settingslib.RestrictedPreference; 34 import com.android.settingslib.core.lifecycle.LifecycleObserver; 35 import com.android.settingslib.core.lifecycle.events.OnStart; 36 import com.android.settingslib.core.lifecycle.events.OnStop; 37 38 import java.util.List; 39 40 /** 41 * {@link BasePreferenceController} for Print settings. 42 */ 43 public class PrintSettingPreferenceController extends BasePreferenceController implements 44 LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener { 45 46 private static final String KEY_PRINTING_SETTINGS = "connected_device_printing"; 47 48 private final PackageManager mPackageManager; 49 private final PrintManager mPrintManager; 50 51 private Preference mPreference; 52 PrintSettingPreferenceController(Context context)53 public PrintSettingPreferenceController(Context context) { 54 super(context, KEY_PRINTING_SETTINGS); 55 mPackageManager = context.getPackageManager(); 56 mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE)) 57 .getGlobalPrintManagerForUser(context.getUserId()); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 return mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING) 63 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 64 } 65 66 @Override displayPreference(PreferenceScreen screen)67 public void displayPreference(PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mPreference = screen.findPreference(getPreferenceKey()); 70 } 71 72 @Override onStart()73 public void onStart() { 74 mPrintManager.addPrintJobStateChangeListener(this); 75 } 76 77 @Override onStop()78 public void onStop() { 79 mPrintManager.removePrintJobStateChangeListener(this); 80 } 81 82 @Override onPrintJobStateChanged(PrintJobId printJobId)83 public void onPrintJobStateChanged(PrintJobId printJobId) { 84 updateState(mPreference); 85 } 86 87 @Override updateState(Preference preference)88 public void updateState(Preference preference) { 89 super.updateState(preference); 90 ((RestrictedPreference) preference).checkRestrictionAndSetDisabled( 91 UserManager.DISALLOW_PRINTING); 92 } 93 94 @Override getSummary()95 public CharSequence getSummary() { 96 final List<PrintJob> printJobs = mPrintManager.getPrintJobs(); 97 98 int numActivePrintJobs = 0; 99 if (printJobs != null) { 100 for (PrintJob job : printJobs) { 101 if (shouldShowToUser(job.getInfo())) { 102 numActivePrintJobs++; 103 } 104 } 105 } 106 107 if (numActivePrintJobs > 0) { 108 return mContext.getResources().getQuantityString( 109 R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs); 110 } else { 111 final List<PrintServiceInfo> services = 112 mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES); 113 if (services == null || services.isEmpty()) { 114 return mContext.getText(R.string.print_settings_summary_no_service); 115 } else { 116 final int count = services.size(); 117 return mContext.getResources().getQuantityString( 118 R.plurals.print_settings_summary, count, count); 119 } 120 } 121 } 122 123 /** 124 * Should the print job the shown to the user in the settings app. 125 * 126 * @param printJob The print job in question. 127 * @return true iff the print job should be shown. 128 */ shouldShowToUser(PrintJobInfo printJob)129 static boolean shouldShowToUser(PrintJobInfo printJob) { 130 switch (printJob.getState()) { 131 case PrintJobInfo.STATE_QUEUED: 132 case PrintJobInfo.STATE_STARTED: 133 case PrintJobInfo.STATE_BLOCKED: 134 case PrintJobInfo.STATE_FAILED: { 135 return true; 136 } 137 } 138 return false; 139 } 140 } 141