Search in sources :

Example 6 with PrintJobId

use of android.print.PrintJobId in project android_frameworks_base by DirtyUnicorns.

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();
        }
    }
}
Also used : PrintJobId(android.print.PrintJobId) PrintJobInfo(android.print.PrintJobInfo) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 7 with PrintJobId

use of android.print.PrintJobId in project android_frameworks_base by crdroidandroid.

the class NotificationController method updateNotifications.

/**
     * Update notifications for the given print jobs, remove all other notifications.
     *
     * @param printJobs The print job that we want to create notifications for.
     */
private void updateNotifications(List<PrintJobInfo> printJobs) {
    ArraySet<PrintJobId> removedPrintJobs = new ArraySet<>(mNotifications);
    final int numPrintJobs = printJobs.size();
    // Create summary notification
    if (numPrintJobs > 1) {
        createStackedNotification(printJobs);
    } else {
        mNotificationManager.cancel(PRINT_JOB_NOTIFICATION_SUMMARY, 0);
    }
    // Create per print job notification
    for (int i = 0; i < numPrintJobs; i++) {
        PrintJobInfo printJob = printJobs.get(i);
        PrintJobId printJobId = printJob.getId();
        removedPrintJobs.remove(printJobId);
        mNotifications.add(printJobId);
        createSimpleNotification(printJob);
    }
    // Remove notifications for print jobs that do not exist anymore
    final int numRemovedPrintJobs = removedPrintJobs.size();
    for (int i = 0; i < numRemovedPrintJobs; i++) {
        PrintJobId removedPrintJob = removedPrintJobs.valueAt(i);
        mNotificationManager.cancel(removedPrintJob.flattenToString(), 0);
        mNotifications.remove(removedPrintJob);
    }
}
Also used : ArraySet(android.util.ArraySet) PrintJobId(android.print.PrintJobId) PrintJobInfo(android.print.PrintJobInfo)

Example 8 with PrintJobId

use of android.print.PrintJobId in project android_frameworks_base by AOSPA.

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();
        }
    }
}
Also used : PrintJobId(android.print.PrintJobId) PrintJobInfo(android.print.PrintJobInfo) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 9 with PrintJobId

use of android.print.PrintJobId in project platform_frameworks_base by android.

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();
        }
    }
}
Also used : PrintJobId(android.print.PrintJobId) PrintJobInfo(android.print.PrintJobInfo) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 10 with PrintJobId

use of android.print.PrintJobId in project platform_frameworks_base by android.

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);
    }
}
Also used : PrintJobId(android.print.PrintJobId) Bundle(android.os.Bundle) PrintJobInfo(android.print.PrintJobInfo) UserHandle(android.os.UserHandle) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) IntentSender(android.content.IntentSender)

Aggregations

PrintJobId (android.print.PrintJobId)18 PrintJobInfo (android.print.PrintJobInfo)18 ArraySet (android.util.ArraySet)5 AtomicFile (android.util.AtomicFile)5 File (java.io.File)5 PendingIntent (android.app.PendingIntent)4 Intent (android.content.Intent)4 IntentSender (android.content.IntentSender)4 Bundle (android.os.Bundle)4 UserHandle (android.os.UserHandle)4 ArrayMap (android.util.ArrayMap)4 ArrayList (java.util.ArrayList)4