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 && mPrintManager != null 64 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 mPreference = screen.findPreference(getPreferenceKey()); 71 } 72 73 @Override onStart()74 public void onStart() { 75 if (mPrintManager != null) { 76 mPrintManager.addPrintJobStateChangeListener(this); 77 } 78 } 79 80 @Override onStop()81 public void onStop() { 82 if (mPrintManager != null) { 83 mPrintManager.removePrintJobStateChangeListener(this); 84 } 85 } 86 87 @Override onPrintJobStateChanged(PrintJobId printJobId)88 public void onPrintJobStateChanged(PrintJobId printJobId) { 89 updateState(mPreference); 90 } 91 92 @Override updateState(Preference preference)93 public void updateState(Preference preference) { 94 super.updateState(preference); 95 ((RestrictedPreference) preference).checkRestrictionAndSetDisabled( 96 UserManager.DISALLOW_PRINTING); 97 } 98 99 @Override getSummary()100 public CharSequence getSummary() { 101 final List<PrintJob> printJobs = mPrintManager.getPrintJobs(); 102 103 int numActivePrintJobs = 0; 104 if (printJobs != null) { 105 for (PrintJob job : printJobs) { 106 if (shouldShowToUser(job.getInfo())) { 107 numActivePrintJobs++; 108 } 109 } 110 } 111 112 if (numActivePrintJobs > 0) { 113 return mContext.getResources().getQuantityString( 114 R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs); 115 } else { 116 final List<PrintServiceInfo> services = 117 mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES); 118 if (services == null || services.isEmpty()) { 119 return mContext.getText(R.string.print_settings_summary_no_service); 120 } else { 121 final int count = services.size(); 122 return mContext.getResources().getQuantityString( 123 R.plurals.print_settings_summary, count, count); 124 } 125 } 126 } 127 128 /** 129 * Should the print job the shown to the user in the settings app. 130 * 131 * @param printJob The print job in question. 132 * @return true iff the print job should be shown. 133 */ shouldShowToUser(PrintJobInfo printJob)134 static boolean shouldShowToUser(PrintJobInfo printJob) { 135 switch (printJob.getState()) { 136 case PrintJobInfo.STATE_QUEUED: 137 case PrintJobInfo.STATE_STARTED: 138 case PrintJobInfo.STATE_BLOCKED: 139 case PrintJobInfo.STATE_FAILED: { 140 return true; 141 } 142 } 143 return false; 144 } 145 } 146