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