use of android.print.PrintJobId in project android_frameworks_base by crdroidandroid.
the class PrintSpoolerService method handleReadPrintJobsLocked.
private void handleReadPrintJobsLocked() {
// Make a map with the files for a print job since we may have
// to delete some. One example of getting orphan files if the
// spooler crashes while constructing a print job. We do not
// persist partially populated print jobs under construction to
// avoid special handling for various attributes missing.
ArrayMap<PrintJobId, File> fileForJobMap = null;
File[] files = getFilesDir().listFiles();
if (files != null) {
final int fileCount = files.length;
for (int i = 0; i < fileCount; i++) {
File file = files[i];
if (file.isFile() && file.getName().startsWith(PRINT_JOB_FILE_PREFIX)) {
if (fileForJobMap == null) {
fileForJobMap = new ArrayMap<PrintJobId, File>();
}
String printJobIdString = file.getName().substring(PRINT_JOB_FILE_PREFIX.length(), file.getName().indexOf('.'));
PrintJobId printJobId = PrintJobId.unflattenFromString(printJobIdString);
fileForJobMap.put(printJobId, file);
}
}
}
final int printJobCount = mPrintJobs.size();
for (int i = 0; i < printJobCount; i++) {
PrintJobInfo printJob = mPrintJobs.get(i);
// We want to have only the orphan files at the end.
if (fileForJobMap != null) {
fileForJobMap.remove(printJob.getId());
}
switch(printJob.getState()) {
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED:
case PrintJobInfo.STATE_BLOCKED:
{
// We have a print job that was queued or started or blocked in
// the past but the device battery died or a crash occurred. In
// this case we assume the print job failed and let the user
// decide whether to restart the job or just cancel it.
setPrintJobState(printJob.getId(), PrintJobInfo.STATE_FAILED, getString(R.string.no_connection_to_printer));
}
break;
}
}
if (!mPrintJobs.isEmpty()) {
// Update the notification.
mNotificationController.onUpdateNotifications(mPrintJobs);
}
// Delete the orphan files.
if (fileForJobMap != null) {
final int orphanFileCount = fileForJobMap.size();
for (int i = 0; i < orphanFileCount; i++) {
File file = fileForJobMap.valueAt(i);
file.delete();
}
}
}
use of android.print.PrintJobId in project android_frameworks_base by crdroidandroid.
the class UserState method getPrintJobInfos.
public List<PrintJobInfo> getPrintJobInfos(int appId) {
List<PrintJobInfo> cachedPrintJobs = mPrintJobForAppCache.getPrintJobs(appId);
// Note that the print spooler is not storing print jobs that
// are in a terminal state as it is non-trivial to properly update
// the spooler state for when to forget print jobs in terminal state.
// Therefore, we fuse the cached print jobs for running apps (some
// jobs are in a terminal state) with the ones that the print
// spooler knows about (some jobs are being processed).
ArrayMap<PrintJobId, PrintJobInfo> result = new ArrayMap<PrintJobId, PrintJobInfo>();
// Add the cached print jobs for running apps.
final int cachedPrintJobCount = cachedPrintJobs.size();
for (int i = 0; i < cachedPrintJobCount; i++) {
PrintJobInfo cachedPrintJob = cachedPrintJobs.get(i);
result.put(cachedPrintJob.getId(), cachedPrintJob);
// Strip out the tag and the advanced print options.
// They are visible only to print services.
cachedPrintJob.setTag(null);
cachedPrintJob.setAdvancedOptions(null);
}
// Add everything else the spooler knows about.
List<PrintJobInfo> printJobs = mSpooler.getPrintJobInfos(null, PrintJobInfo.STATE_ANY, appId);
if (printJobs != null) {
final int printJobCount = printJobs.size();
for (int i = 0; i < printJobCount; i++) {
PrintJobInfo printJob = printJobs.get(i);
result.put(printJob.getId(), printJob);
// Strip out the tag and the advanced print options.
// They are visible only to print services.
printJob.setTag(null);
printJob.setAdvancedOptions(null);
}
}
return new ArrayList<PrintJobInfo>(result.values());
}
use of android.print.PrintJobId in project android_frameworks_base by crdroidandroid.
the class UserState method print.
@SuppressWarnings("deprecation")
public Bundle print(@NonNull String printJobName, @NonNull IPrintDocumentAdapter adapter, @Nullable PrintAttributes attributes, @NonNull String packageName, int appId) {
// Create print job place holder.
final PrintJobInfo printJob = new PrintJobInfo();
printJob.setId(new PrintJobId());
printJob.setAppId(appId);
printJob.setLabel(printJobName);
printJob.setAttributes(attributes);
printJob.setState(PrintJobInfo.STATE_CREATED);
printJob.setCopies(1);
printJob.setCreationTime(System.currentTimeMillis());
// Track this job so we can forget it when the creator dies.
if (!mPrintJobForAppCache.onPrintJobCreated(adapter.asBinder(), appId, printJob)) {
// Not adding a print job means the client is dead - done.
return null;
}
// Spin the spooler to add the job and show the config UI.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
mSpooler.createPrintJob(printJob);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
final long identity = Binder.clearCallingIdentity();
try {
Intent intent = new Intent(PrintManager.ACTION_PRINT_DIALOG);
intent.setData(Uri.fromParts("printjob", printJob.getId().flattenToString(), null));
intent.putExtra(PrintManager.EXTRA_PRINT_DOCUMENT_ADAPTER, adapter.asBinder());
intent.putExtra(PrintManager.EXTRA_PRINT_JOB, printJob);
intent.putExtra(DocumentsContract.EXTRA_PACKAGE_NAME, packageName);
IntentSender intentSender = PendingIntent.getActivityAsUser(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, null, new UserHandle(mUserId)).getIntentSender();
Bundle result = new Bundle();
result.putParcelable(PrintManager.EXTRA_PRINT_JOB, printJob);
result.putParcelable(PrintManager.EXTRA_PRINT_DIALOG_INTENT, intentSender);
return result;
} finally {
Binder.restoreCallingIdentity(identity);
}
}
Aggregations