1/*
2 * Copyright (C) 2013 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
17package com.android.settings.print;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.preference.Preference;
24import android.print.PrintJob;
25import android.print.PrintJobId;
26import android.print.PrintJobInfo;
27import android.print.PrintManager;
28import android.print.PrintManager.PrintJobStateChangeListener;
29import android.text.TextUtils;
30import android.text.format.DateUtils;
31import android.view.Menu;
32import android.view.MenuInflater;
33import android.view.MenuItem;
34import android.view.View;
35
36import com.android.settings.R;
37import com.android.settings.SettingsPreferenceFragment;
38
39import java.text.DateFormat;
40
41/**
42 * Fragment for management of a print job.
43 */
44public class PrintJobSettingsFragment extends SettingsPreferenceFragment {
45    private static final int MENU_ITEM_ID_CANCEL = 1;
46    private static final int MENU_ITEM_ID_RESTART = 2;
47
48    private static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
49
50    private static final String PRINT_JOB_PREFERENCE = "print_job_preference";
51    private static final String PRINT_JOB_MESSAGE_PREFERENCE = "print_job_message_preference";
52
53    private Drawable mListDivider;
54
55    private final PrintJobStateChangeListener mPrintJobStateChangeListener =
56            new PrintJobStateChangeListener() {
57        @Override
58        public void onPrintJobStateChanged(PrintJobId printJobId) {
59            updateUi();
60        }
61    };
62
63    private PrintManager mPrintManager;
64
65    private Preference mPrintJobPreference;
66    private Preference mMessagePreference;
67
68    private PrintJobId mPrintJobId;
69    private PrintJob mPrintJob;
70
71    @Override
72    public void onCreate(Bundle icicle) {
73        super.onCreate(icicle);
74
75        addPreferencesFromResource(R.xml.print_job_settings);
76        mPrintJobPreference = findPreference(PRINT_JOB_PREFERENCE);
77        mMessagePreference = findPreference(PRINT_JOB_MESSAGE_PREFERENCE);
78
79        mPrintManager = ((PrintManager) getActivity().getSystemService(
80                Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(
81                        getActivity().getUserId());
82
83        getActivity().getActionBar().setTitle(R.string.print_print_job);
84
85        processArguments();
86
87        setHasOptionsMenu(true);
88    }
89
90    @Override
91    public void onViewCreated(View view, Bundle savedInstanceState) {
92        super.onViewCreated(view, savedInstanceState);
93        getListView().setEnabled(false);
94    }
95
96    @Override
97    public void onResume() {
98        super.onResume();
99        mPrintManager.addPrintJobStateChangeListener(
100                mPrintJobStateChangeListener);
101        updateUi();
102    }
103
104    @Override
105    public void onPause() {
106        super.onPause();
107        mPrintManager.removePrintJobStateChangeListener(
108                mPrintJobStateChangeListener);
109    }
110
111    @Override
112    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
113        super.onCreateOptionsMenu(menu, inflater);
114
115        PrintJob printJob = getPrintJob();
116        if (printJob == null) {
117            return;
118        }
119
120        if (!printJob.getInfo().isCancelling()) {
121            MenuItem cancel = menu.add(0, MENU_ITEM_ID_CANCEL, Menu.NONE,
122                    getString(R.string.print_cancel));
123            cancel.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
124        }
125
126        if (printJob.isFailed()) {
127            MenuItem restart = menu.add(0, MENU_ITEM_ID_RESTART, Menu.NONE,
128                    getString(R.string.print_restart));
129            restart.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
130        }
131    }
132
133    @Override
134    public boolean onOptionsItemSelected(MenuItem item) {
135        switch (item.getItemId()) {
136            case MENU_ITEM_ID_CANCEL: {
137                getPrintJob().cancel();
138                finish();
139                return true;
140            }
141
142            case MENU_ITEM_ID_RESTART: {
143                getPrintJob().restart();
144                finish();
145                return true;
146            }
147        }
148
149        return super.onOptionsItemSelected(item);
150    }
151
152    private void processArguments() {
153        String printJobId = getArguments().getString(EXTRA_PRINT_JOB_ID);
154        mPrintJobId = PrintJobId.unflattenFromString(printJobId);
155        if (mPrintJobId == null) {
156            finish();
157        }
158    }
159
160    private PrintJob getPrintJob() {
161        if (mPrintJob == null) {
162            mPrintJob = mPrintManager.getPrintJob(mPrintJobId);
163        }
164        return mPrintJob;
165    }
166
167    private void updateUi() {
168        PrintJob printJob = getPrintJob();
169
170        if (printJob == null) {
171            finish();
172            return;
173        }
174
175        if (printJob.isCancelled() || printJob.isCompleted()) {
176            finish();
177            return;
178        }
179
180        PrintJobInfo info = printJob.getInfo();
181
182        switch (info.getState()) {
183            case PrintJobInfo.STATE_QUEUED:
184            case PrintJobInfo.STATE_STARTED: {
185                if (!printJob.getInfo().isCancelling()) {
186                    mPrintJobPreference.setTitle(getString(
187                            R.string.print_printing_state_title_template, info.getLabel()));
188                } else {
189                    mPrintJobPreference.setTitle(getString(
190                            R.string.print_cancelling_state_title_template, info.getLabel()));
191                }
192            } break;
193
194            case PrintJobInfo.STATE_FAILED: {
195                mPrintJobPreference.setTitle(getString(
196                        R.string.print_failed_state_title_template, info.getLabel()));
197            } break;
198
199            case PrintJobInfo.STATE_BLOCKED: {
200                if (!printJob.getInfo().isCancelling()) {
201                    mPrintJobPreference.setTitle(getString(
202                            R.string.print_blocked_state_title_template, info.getLabel()));
203                } else {
204                    mPrintJobPreference.setTitle(getString(
205                            R.string.print_cancelling_state_title_template, info.getLabel()));
206                }
207            } break;
208        }
209
210        mPrintJobPreference.setSummary(getString(R.string.print_job_summary,
211                info.getPrinterName(), DateUtils.formatSameDayTime(
212                        info.getCreationTime(), info.getCreationTime(), DateFormat.SHORT,
213                        DateFormat.SHORT)));
214
215        switch (info.getState()) {
216            case PrintJobInfo.STATE_QUEUED:
217            case PrintJobInfo.STATE_STARTED: {
218                mPrintJobPreference.setIcon(R.drawable.ic_print);
219            } break;
220
221            case PrintJobInfo.STATE_FAILED:
222            case PrintJobInfo.STATE_BLOCKED: {
223                mPrintJobPreference.setIcon(R.drawable.ic_print_error);
224            } break;
225        }
226
227        String stateReason = info.getStateReason();
228        if (!TextUtils.isEmpty(stateReason)) {
229            if (getPreferenceScreen().findPreference(PRINT_JOB_MESSAGE_PREFERENCE) == null) {
230                getPreferenceScreen().addPreference(mMessagePreference);
231            }
232            mMessagePreference.setSummary(stateReason);
233            getListView().setDivider(null);
234        } else {
235            getPreferenceScreen().removePreference(mMessagePreference);
236            getListView().setDivider(mListDivider);
237        }
238
239        getActivity().invalidateOptionsMenu();
240    }
241}
242