• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.print;
16 
17 import android.content.Context;
18 import android.print.PrintJob;
19 import android.print.PrintJobId;
20 import android.print.PrintManager;
21 import android.util.Log;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnStart;
29 import com.android.settingslib.core.lifecycle.events.OnStop;
30 
31 public abstract class PrintJobPreferenceControllerBase extends BasePreferenceController implements
32         LifecycleObserver, OnStart, OnStop, PrintManager.PrintJobStateChangeListener {
33     private static final String TAG = "PrintJobPrefCtrlBase";
34 
35     private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
36 
37     private final PrintManager mPrintManager;
38     protected Preference mPreference;
39     protected PrintJobSettingsFragment mFragment;
40     protected PrintJobId mPrintJobId;
41 
PrintJobPreferenceControllerBase(Context context, String key)42     public PrintJobPreferenceControllerBase(Context context, String key) {
43         super(context, key);
44         mPrintManager = ((PrintManager) mContext.getSystemService(
45                 Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(mContext.getUserId());
46     }
47 
48     @Override
onStart()49     public void onStart() {
50         mPrintManager.addPrintJobStateChangeListener(this);
51         updateUi();
52     }
53 
54     @Override
onStop()55     public void onStop() {
56         mPrintManager.removePrintJobStateChangeListener(this);
57     }
58 
59     @Override
getAvailabilityStatus()60     public int getAvailabilityStatus() {
61         return AVAILABLE;
62     }
63 
64     @Override
onPrintJobStateChanged(PrintJobId printJobId)65     public void onPrintJobStateChanged(PrintJobId printJobId) {
66         updateUi();
67     }
68 
69     @Override
displayPreference(PreferenceScreen screen)70     public void displayPreference(PreferenceScreen screen) {
71         super.displayPreference(screen);
72         mPreference = screen.findPreference(getPreferenceKey());
73     }
74 
init(PrintJobSettingsFragment fragment)75     public void init(PrintJobSettingsFragment fragment) {
76         mFragment = fragment;
77         processArguments();
78     }
79 
getPrintJob()80     protected PrintJob getPrintJob() {
81         return mPrintManager.getPrintJob(mPrintJobId);
82     }
83 
updateUi()84     protected abstract void updateUi();
85 
processArguments()86     private void processArguments() {
87         String printJobId = mFragment.getArguments().getString(EXTRA_PRINT_JOB_ID);
88         if (printJobId == null) {
89             printJobId = mFragment.getActivity().getIntent().getStringExtra(EXTRA_PRINT_JOB_ID);
90 
91             if (printJobId == null) {
92                 Log.w(TAG, EXTRA_PRINT_JOB_ID + " not set");
93                 mFragment.finish();
94                 return;
95             }
96         }
97         mPrintJobId = PrintJobId.unflattenFromString(printJobId);
98     }
99 }
100