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 import com.android.settingslib.utils.StringUtil;
38 
39 import java.util.List;
40 
41 /**
42  * {@link BasePreferenceController} for Print settings.
43  */
44 public class PrintSettingPreferenceController extends BasePreferenceController implements
45         LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
46 
47     private static final String KEY_PRINTING_SETTINGS = "connected_device_printing";
48 
49     private final PackageManager mPackageManager;
50     private final PrintManager mPrintManager;
51 
52     private Preference mPreference;
53 
PrintSettingPreferenceController(Context context)54     public PrintSettingPreferenceController(Context context) {
55         super(context, KEY_PRINTING_SETTINGS);
56         mPackageManager = context.getPackageManager();
57         mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
58                 .getGlobalPrintManagerForUser(context.getUserId());
59     }
60 
61     @Override
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         return mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)
64                 && mPrintManager != null
65                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
66     }
67 
68     @Override
displayPreference(PreferenceScreen screen)69     public void displayPreference(PreferenceScreen screen) {
70         super.displayPreference(screen);
71         mPreference = screen.findPreference(getPreferenceKey());
72     }
73 
74     @Override
onStart()75     public void onStart() {
76         if (mPrintManager != null) {
77             mPrintManager.addPrintJobStateChangeListener(this);
78         }
79     }
80 
81     @Override
onStop()82     public void onStop() {
83         if (mPrintManager != null) {
84             mPrintManager.removePrintJobStateChangeListener(this);
85         }
86     }
87 
88     @Override
onPrintJobStateChanged(PrintJobId printJobId)89     public void onPrintJobStateChanged(PrintJobId printJobId) {
90         updateState(mPreference);
91     }
92 
93     @Override
updateState(Preference preference)94     public void updateState(Preference preference) {
95         super.updateState(preference);
96         ((RestrictedPreference) preference).checkRestrictionAndSetDisabled(
97                 UserManager.DISALLOW_PRINTING);
98     }
99 
100     @Override
getSummary()101     public CharSequence getSummary() {
102         final List<PrintJob> printJobs = mPrintManager.getPrintJobs();
103 
104         int numActivePrintJobs = 0;
105         if (printJobs != null) {
106             for (PrintJob job : printJobs) {
107                 if (shouldShowToUser(job.getInfo())) {
108                     numActivePrintJobs++;
109                 }
110             }
111         }
112 
113         if (numActivePrintJobs > 0) {
114             return StringUtil.getIcuPluralsString(mContext, numActivePrintJobs,
115                     R.string.print_jobs_summary);
116         } else {
117             final List<PrintServiceInfo> services =
118                     mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
119             if (services == null || services.isEmpty()) {
120                 return mContext.getText(R.string.print_settings_summary_no_service);
121             } else {
122                 return StringUtil.getIcuPluralsString(mContext, services.size(),
123                         R.string.print_settings_summary);
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