use of android.print.PrinterId in project android_frameworks_base by DirtyUnicorns.
the class PrintSpoolerService method setPrintJobState.
public boolean setPrintJobState(PrintJobId printJobId, int state, String error) {
boolean success = false;
synchronized (mLock) {
PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY);
if (printJob != null) {
final int oldState = printJob.getState();
if (oldState == state) {
return false;
}
success = true;
printJob.setState(state);
printJob.setStatus(error);
printJob.setCancelling(false);
if (DEBUG_PRINT_JOB_LIFECYCLE) {
Slog.i(LOG_TAG, "[STATE CHANGED] " + printJob);
}
MetricsLogger.histogram(this, "print_job_state", state);
switch(state) {
case PrintJobInfo.STATE_COMPLETED:
case PrintJobInfo.STATE_CANCELED:
mPrintJobs.remove(printJob);
removePrintJobFileLocked(printJob.getId());
case PrintJobInfo.STATE_FAILED:
{
PrinterId printerId = printJob.getPrinterId();
if (printerId != null) {
ComponentName service = printerId.getServiceName();
if (!hasActivePrintJobsForServiceLocked(service)) {
sendOnAllPrintJobsForServiceHandled(service);
}
}
}
break;
case PrintJobInfo.STATE_QUEUED:
{
sendOnPrintJobQueued(new PrintJobInfo(printJob));
}
break;
}
if (shouldPersistPrintJob(printJob)) {
mPersistanceManager.writeStateLocked();
}
if (!hasActivePrintJobsLocked()) {
notifyOnAllPrintJobsHandled();
}
notifyPrintJobUpdated(printJob);
}
}
return success;
}
use of android.print.PrinterId in project android_frameworks_base by crdroidandroid.
the class FusedPrintersProvider method loadInternal.
private void loadInternal() {
if (mDiscoverySession == null) {
PrintManager printManager = (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
mDiscoverySession = printManager.createPrinterDiscoverySession();
mPersistenceManager.readPrinterHistory();
} else if (mPersistenceManager.isHistoryChanged()) {
mPersistenceManager.readPrinterHistory();
}
if (mPersistenceManager.isReadHistoryCompleted() && !mDiscoverySession.isPrinterDiscoveryStarted()) {
mDiscoverySession.setOnPrintersChangeListener(new OnPrintersChangeListener() {
@Override
public void onPrintersChanged() {
if (DEBUG) {
Log.i(LOG_TAG, "onPrintersChanged() count:" + mDiscoverySession.getPrinters().size() + " " + FusedPrintersProvider.this.hashCode());
}
updatePrinters(mDiscoverySession.getPrinters(), mFavoritePrinters, getCurrentLocation());
}
});
final int favoriteCount = mFavoritePrinters.size();
List<PrinterId> printerIds = new ArrayList<>(favoriteCount);
for (int i = 0; i < favoriteCount; i++) {
printerIds.add(mFavoritePrinters.get(i).first.getId());
}
mDiscoverySession.startPrinterDiscovery(printerIds);
List<PrinterInfo> printers = mDiscoverySession.getPrinters();
updatePrinters(printers, mFavoritePrinters, getCurrentLocation());
}
}
use of android.print.PrinterId in project android_frameworks_base by crdroidandroid.
the class FusedPrintersProvider method computeAndDeliverResult.
/**
* Compute the printers, order them appropriately and deliver the printers to the clients. We
* prefer printers that have been previously used (favorites) and printers that have been used
* previously close to the current location (near printers).
*
* @param discoveredPrinters All printers currently discovered by the print discovery session.
* @param favoritePrinters The ordered list of printers. The earlier in the list, the more
* preferred.
*/
private void computeAndDeliverResult(Map<PrinterId, PrinterInfo> discoveredPrinters, List<Pair<PrinterInfo, Location>> favoritePrinters) {
List<PrinterInfo> printers = new ArrayList<>();
// Store the printerIds that have already been added. We cannot compare the printerInfos in
// "printers" as they might have been taken from discoveredPrinters and the printerInfo does
// not equals() anymore
HashSet<PrinterId> alreadyAddedPrinter = new HashSet<>(MAX_FAVORITE_PRINTER_COUNT);
Location location = getCurrentLocation();
// Add the favorite printers that have last been used close to the current location
final int favoritePrinterCount = favoritePrinters.size();
if (location != null) {
for (int i = 0; i < favoritePrinterCount; i++) {
// Only add a certain amount of favorite printers
if (printers.size() == MAX_FAVORITE_PRINTER_COUNT) {
break;
}
PrinterInfo favoritePrinter = favoritePrinters.get(i).first;
Location printerLocation = favoritePrinters.get(i).second;
if (printerLocation != null && !alreadyAddedPrinter.contains(favoritePrinter.getId())) {
if (printerLocation.distanceTo(location) <= MAX_PRINTER_DISTANCE) {
updateAndAddPrinter(printers, favoritePrinter, discoveredPrinters);
alreadyAddedPrinter.add(favoritePrinter.getId());
}
}
}
}
// Add the other favorite printers
for (int i = 0; i < favoritePrinterCount; i++) {
// Only add a certain amount of favorite printers
if (printers.size() == MAX_FAVORITE_PRINTER_COUNT) {
break;
}
PrinterInfo favoritePrinter = favoritePrinters.get(i).first;
if (!alreadyAddedPrinter.contains(favoritePrinter.getId())) {
updateAndAddPrinter(printers, favoritePrinter, discoveredPrinters);
alreadyAddedPrinter.add(favoritePrinter.getId());
}
}
// Add other updated printers. Printers that have already been added have been removed from
// discoveredPrinters in the calls to updateAndAddPrinter
final int printerCount = mPrinters.size();
for (int i = 0; i < printerCount; i++) {
PrinterInfo printer = mPrinters.get(i);
PrinterInfo updatedPrinter = discoveredPrinters.remove(printer.getId());
if (updatedPrinter != null) {
printers.add(updatedPrinter);
}
}
// Add the new printers, i.e. what is left.
printers.addAll(discoveredPrinters.values());
// Update the list of printers.
mPrinters.clear();
mPrinters.addAll(printers);
if (isStarted()) {
// If stated deliver the new printers.
deliverResult(printers);
} else {
// Otherwise, take a note for the change.
onContentChanged();
}
}
use of android.print.PrinterId in project android_frameworks_base by crdroidandroid.
the class FusedPrintersProvider method updatePrinters.
private void updatePrinters(List<PrinterInfo> printers, List<Pair<PrinterInfo, Location>> favoritePrinters, Location location) {
if (mPrintersUpdatedBefore && mPrinters.equals(printers) && mFavoritePrinters.equals(favoritePrinters) && Objects.equals(mLocationOfLastPrinterUpdate, location)) {
return;
}
mLocationOfLastPrinterUpdate = location;
mPrintersUpdatedBefore = true;
// Some of the found printers may have be a printer that is in the
// history but with its properties changed. Hence, we try to update the
// printer to use its current properties instead of the historical one.
mPersistenceManager.updateHistoricalPrintersIfNeeded(printers);
Map<PrinterId, PrinterInfo> printersMap = new LinkedHashMap<>();
final int printerCount = printers.size();
for (int i = 0; i < printerCount; i++) {
PrinterInfo printer = printers.get(i);
printersMap.put(printer.getId(), printer);
}
computeAndDeliverResult(printersMap, favoritePrinters);
}
use of android.print.PrinterId in project android_frameworks_base by crdroidandroid.
the class PrintSpoolerService method setPrintJobState.
public boolean setPrintJobState(PrintJobId printJobId, int state, String error) {
boolean success = false;
synchronized (mLock) {
PrintJobInfo printJob = getPrintJobInfo(printJobId, PrintManager.APP_ID_ANY);
if (printJob != null) {
final int oldState = printJob.getState();
if (oldState == state) {
return false;
}
success = true;
printJob.setState(state);
printJob.setStatus(error);
printJob.setCancelling(false);
if (DEBUG_PRINT_JOB_LIFECYCLE) {
Slog.i(LOG_TAG, "[STATE CHANGED] " + printJob);
}
MetricsLogger.histogram(this, "print_job_state", state);
switch(state) {
case PrintJobInfo.STATE_COMPLETED:
case PrintJobInfo.STATE_CANCELED:
mPrintJobs.remove(printJob);
removePrintJobFileLocked(printJob.getId());
case PrintJobInfo.STATE_FAILED:
{
PrinterId printerId = printJob.getPrinterId();
if (printerId != null) {
ComponentName service = printerId.getServiceName();
if (!hasActivePrintJobsForServiceLocked(service)) {
sendOnAllPrintJobsForServiceHandled(service);
}
}
}
break;
case PrintJobInfo.STATE_QUEUED:
{
sendOnPrintJobQueued(new PrintJobInfo(printJob));
}
break;
}
if (shouldPersistPrintJob(printJob)) {
mPersistanceManager.writeStateLocked();
}
if (!hasActivePrintJobsLocked()) {
notifyOnAllPrintJobsHandled();
}
notifyPrintJobUpdated(printJob);
}
}
return success;
}
Aggregations