use of android.print.PrinterInfo in project android_frameworks_base by AOSPA.
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.PrinterInfo in project android_frameworks_base by AOSPA.
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.PrinterInfo in project android_frameworks_base by AOSPA.
the class PrinterDiscoverySession method removePrinters.
/**
* Removes added printers. Removing an already removed or never added
* printer has no effect. Removed printers can be added again. You can
* call this method multiple times during the lifetime of this session.
* <p>
* <strong>Note: </strong> Calls to this method after the session is
* destroyed, that is after the {@link #onDestroy()} callback, will be ignored.
* </p>
*
* @param printerIds The ids of the removed printers.
*
* @see #addPrinters(List)
* @see #getPrinters()
* @see #isDestroyed()
*/
public final void removePrinters(@NonNull List<PrinterId> printerIds) {
PrintService.throwIfNotCalledOnMainThread();
// If the session is destroyed - nothing do to.
if (mIsDestroyed) {
Log.w(LOG_TAG, "Not removing printers - session destroyed.");
return;
}
if (mIsDiscoveryStarted) {
// If during discovery, remove existing printers and send them.
List<PrinterId> removedPrinterIds = new ArrayList<PrinterId>();
final int removedPrinterIdCount = printerIds.size();
for (int i = 0; i < removedPrinterIdCount; i++) {
PrinterId removedPrinterId = printerIds.get(i);
if (mPrinters.remove(removedPrinterId) != null) {
removedPrinterIds.add(removedPrinterId);
}
}
// Send the removed printers, if such.
if (!removedPrinterIds.isEmpty()) {
try {
mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
} catch (RemoteException re) {
Log.e(LOG_TAG, "Error sending removed printers", re);
}
}
} else {
// Remember the last sent printers if needed.
if (mLastSentPrinters == null) {
mLastSentPrinters = new ArrayMap<PrinterId, PrinterInfo>(mPrinters);
}
// Update the printers.
final int removedPrinterIdCount = printerIds.size();
for (int i = 0; i < removedPrinterIdCount; i++) {
PrinterId removedPrinterId = printerIds.get(i);
mPrinters.remove(removedPrinterId);
}
}
}
use of android.print.PrinterInfo in project android_frameworks_base by AOSPA.
the class PrinterDiscoverySession method sendOutOfDiscoveryPeriodPrinterChanges.
private void sendOutOfDiscoveryPeriodPrinterChanges() {
// Noting changed since the last discovery period - nothing to do.
if (mLastSentPrinters == null || mLastSentPrinters.isEmpty()) {
mLastSentPrinters = null;
return;
}
// Determine the added printers.
List<PrinterInfo> addedPrinters = null;
for (PrinterInfo printer : mPrinters.values()) {
PrinterInfo sentPrinter = mLastSentPrinters.get(printer.getId());
if (sentPrinter == null || !sentPrinter.equals(printer)) {
if (addedPrinters == null) {
addedPrinters = new ArrayList<PrinterInfo>();
}
addedPrinters.add(printer);
}
}
// Send the added printers, if such.
if (addedPrinters != null) {
try {
mObserver.onPrintersAdded(new ParceledListSlice<PrinterInfo>(addedPrinters));
} catch (RemoteException re) {
Log.e(LOG_TAG, "Error sending added printers", re);
}
}
// Determine the removed printers.
List<PrinterId> removedPrinterIds = null;
for (PrinterInfo sentPrinter : mLastSentPrinters.values()) {
if (!mPrinters.containsKey(sentPrinter.getId())) {
if (removedPrinterIds == null) {
removedPrinterIds = new ArrayList<PrinterId>();
}
removedPrinterIds.add(sentPrinter.getId());
}
}
// Send the removed printers, if such.
if (removedPrinterIds != null) {
try {
mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
} catch (RemoteException re) {
Log.e(LOG_TAG, "Error sending removed printers", re);
}
}
mLastSentPrinters = null;
}
use of android.print.PrinterInfo in project android_frameworks_base by ResurrectionRemix.
the class SelectPrinterActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setIcon(R.drawable.ic_print);
setContentView(R.layout.select_printer_activity);
mEnabledPrintServices = new ArrayMap<>();
mPrinterRegistry = new PrinterRegistry(this, null, LOADER_ID_PRINT_REGISTRY, LOADER_ID_PRINT_REGISTRY_INT);
// Hook up the list view.
mListView = (ListView) findViewById(android.R.id.list);
final DestinationAdapter adapter = new DestinationAdapter();
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
if (!isFinishing() && adapter.getCount() <= 0) {
updateEmptyView(adapter);
}
}
@Override
public void onInvalidated() {
if (!isFinishing()) {
updateEmptyView(adapter);
}
}
});
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!((DestinationAdapter) mListView.getAdapter()).isActionable(position)) {
return;
}
PrinterInfo printer = (PrinterInfo) mListView.getAdapter().getItem(position);
if (printer == null) {
startAddPrinterActivity();
} else {
onPrinterSelected(printer);
}
}
});
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startAddPrinterActivity();
}
});
registerForContextMenu(mListView);
getLoaderManager().initLoader(LOADER_ID_ENABLED_PRINT_SERVICES, null, this);
// If some are disabled and some are enabled show a toast to notify the user
if (savedInstanceState == null || !savedInstanceState.getBoolean(KEY_NOT_FIRST_CREATE)) {
List<PrintServiceInfo> allServices = ((PrintManager) getSystemService(Context.PRINT_SERVICE)).getPrintServices(PrintManager.ALL_SERVICES);
boolean hasEnabledServices = false;
boolean hasDisabledServices = false;
if (allServices != null) {
final int numServices = allServices.size();
for (int i = 0; i < numServices; i++) {
if (allServices.get(i).isEnabled()) {
hasEnabledServices = true;
} else {
hasDisabledServices = true;
}
}
}
if (!hasEnabledServices) {
startAddPrinterActivity();
} else if (hasDisabledServices) {
String disabledServicesSetting = Settings.Secure.getString(getContentResolver(), Settings.Secure.DISABLED_PRINT_SERVICES);
if (!TextUtils.isEmpty(disabledServicesSetting)) {
Toast.makeText(this, getString(R.string.print_services_disabled_toast), Toast.LENGTH_LONG).show();
}
}
}
}
Aggregations