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 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.core.BasePreferenceController; 32 import com.android.settingslib.RestrictedPreference; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnStart; 35 import com.android.settingslib.core.lifecycle.events.OnStop; 36 37 import java.util.List; 38 39 /** 40 * {@link BasePreferenceController} for Print settings. 41 */ 42 public class PrintSettingPreferenceController extends BasePreferenceController implements 43 LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener { 44 45 private static final String KEY_PRINTING_SETTINGS = "connected_device_printing"; 46 47 private final PackageManager mPackageManager; 48 private final PrintManager mPrintManager; 49 50 private Preference mPreference; 51 52 public PrintSettingPreferenceController(Context context) { 53 super(context, KEY_PRINTING_SETTINGS); 54 mPackageManager = context.getPackageManager(); 55 mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE)) 56 .getGlobalPrintManagerForUser(context.getUserId()); 57 } 58 59 @Override 60 public int getAvailabilityStatus() { 61 return mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING) 62 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 63 } 64 65 @Override 66 public void displayPreference(PreferenceScreen screen) { 67 super.displayPreference(screen); 68 mPreference = screen.findPreference(getPreferenceKey()); 69 } 70 71 @Override 72 public void onStart() { 73 mPrintManager.addPrintJobStateChangeListener(this); 74 } 75 76 @Override 77 public void onStop() { 78 mPrintManager.removePrintJobStateChangeListener(this); 79 } 80 81 @Override 82 public void onPrintJobStateChanged(PrintJobId printJobId) { 83 updateState(mPreference); 84 } 85 86 @Override 87 public void updateState(Preference preference) { 88 super.updateState(preference); 89 ((RestrictedPreference) preference).checkRestrictionAndSetDisabled( 90 UserManager.DISALLOW_PRINTING); 91 } 92 93 @Override 94 public CharSequence getSummary() { 95 final List<PrintJob> printJobs = mPrintManager.getPrintJobs(); 96 97 int numActivePrintJobs = 0; 98 if (printJobs != null) { 99 for (PrintJob job : printJobs) { 100 if (shouldShowToUser(job.getInfo())) { 101 numActivePrintJobs++; 102 } 103 } 104 } 105 106 if (numActivePrintJobs > 0) { 107 return mContext.getResources().getQuantityString( 108 R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs); 109 } else { 110 final List<PrintServiceInfo> services = 111 mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES); 112 if (services == null || services.isEmpty()) { 113 return mContext.getText(R.string.print_settings_summary_no_service); 114 } else { 115 final int count = services.size(); 116 return mContext.getResources().getQuantityString( 117 R.plurals.print_settings_summary, count, count); 118 } 119 } 120 } 121 122 /** 123 * Should the print job the shown to the user in the settings app. 124 * 125 * @param printJob The print job in question. 126 * @return true iff the print job should be shown. 127 */ 128 static boolean shouldShowToUser(PrintJobInfo printJob) { 129 switch (printJob.getState()) { 130 case PrintJobInfo.STATE_QUEUED: 131 case PrintJobInfo.STATE_STARTED: 132 case PrintJobInfo.STATE_BLOCKED: 133 case PrintJobInfo.STATE_FAILED: { 134 return true; 135 } 136 } 137 return false; 138 } 139 } 140