Search in sources :

Example 26 with PrinterInfo

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

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) RemoteException(android.os.RemoteException) PrinterInfo(android.print.PrinterInfo)

Example 27 with PrinterInfo

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

the class PrinterDiscoverySession method addPrinters.

/**
     * Adds discovered printers. Adding an already added printer updates it.
     * Removed printers can be added again. You can call this method multiple
     * times during the life of this session. Duplicates will be ignored.
     * <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 printers The printers to add.
     *
     * @see #removePrinters(List)
     * @see #getPrinters()
     * @see #isDestroyed()
     */
public final void addPrinters(@NonNull List<PrinterInfo> printers) {
    PrintService.throwIfNotCalledOnMainThread();
    // If the session is destroyed - nothing do to.
    if (mIsDestroyed) {
        Log.w(LOG_TAG, "Not adding printers - session destroyed.");
        return;
    }
    if (mIsDiscoveryStarted) {
        // If during discovery, add the new printers and send them.
        List<PrinterInfo> addedPrinters = null;
        final int addedPrinterCount = printers.size();
        for (int i = 0; i < addedPrinterCount; i++) {
            PrinterInfo addedPrinter = printers.get(i);
            PrinterInfo oldPrinter = mPrinters.put(addedPrinter.getId(), addedPrinter);
            if (oldPrinter == null || !oldPrinter.equals(addedPrinter)) {
                if (addedPrinters == null) {
                    addedPrinters = new ArrayList<PrinterInfo>();
                }
                addedPrinters.add(addedPrinter);
            }
        }
        // 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);
            }
        }
    } else {
        // Remember the last sent printers if needed.
        if (mLastSentPrinters == null) {
            mLastSentPrinters = new ArrayMap<PrinterId, PrinterInfo>(mPrinters);
        }
        // Update the printers.
        final int addedPrinterCount = printers.size();
        for (int i = 0; i < addedPrinterCount; i++) {
            PrinterInfo addedPrinter = printers.get(i);
            if (mPrinters.get(addedPrinter.getId()) == null) {
                mPrinters.put(addedPrinter.getId(), addedPrinter);
            }
        }
    }
}
Also used : PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) RemoteException(android.os.RemoteException)

Example 28 with PrinterInfo

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

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();
            }
        }
    }
}
Also used : PrintManager(android.print.PrintManager) DataSetObserver(android.database.DataSetObserver) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) SearchView(android.widget.SearchView) TextView(android.widget.TextView) ListView(android.widget.ListView) PrintServiceInfo(android.printservice.PrintServiceInfo) OnClickListener(android.view.View.OnClickListener) AdapterView(android.widget.AdapterView) PrinterInfo(android.print.PrinterInfo)

Example 29 with PrinterInfo

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

the class PrintActivity method onSelectPrinterActivityResult.

private void onSelectPrinterActivityResult(int resultCode, Intent data) {
    if (resultCode == RESULT_OK && data != null) {
        PrinterInfo printerInfo = data.getParcelableExtra(SelectPrinterActivity.INTENT_EXTRA_PRINTER);
        if (printerInfo != null) {
            mCurrentPrinter = printerInfo;
            mPrintJob.setPrinterId(printerInfo.getId());
            mPrintJob.setPrinterName(printerInfo.getName());
            mDestinationSpinnerAdapter.ensurePrinterInVisibleAdapterPosition(printerInfo);
        }
    }
    if (mCurrentPrinter != null) {
        // Trigger PrintersObserver.onChanged() to adjust selection back to current printer
        mDestinationSpinnerAdapter.notifyDataSetChanged();
    }
}
Also used : PrinterInfo(android.print.PrinterInfo)

Example 30 with PrinterInfo

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

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();
    }
}
Also used : ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) HashSet(java.util.HashSet) Location(android.location.Location)

Aggregations

PrinterInfo (android.print.PrinterInfo)46 PrinterId (android.print.PrinterId)30 RemoteException (android.os.RemoteException)15 ArrayList (java.util.ArrayList)15 PrintManager (android.print.PrintManager)10 View (android.view.View)6 AdapterView (android.widget.AdapterView)6 ImageView (android.widget.ImageView)6 ListView (android.widget.ListView)6 SearchView (android.widget.SearchView)6 TextView (android.widget.TextView)6 Intent (android.content.Intent)5 DataSetObserver (android.database.DataSetObserver)5 Location (android.location.Location)5 OnPrintersChangeListener (android.print.PrinterDiscoverySession.OnPrintersChangeListener)5 PrintServiceInfo (android.printservice.PrintServiceInfo)5 MenuItem (android.view.MenuItem)5 OnClickListener (android.view.View.OnClickListener)5 AdapterContextMenuInfo (android.widget.AdapterView.AdapterContextMenuInfo)5 HashSet (java.util.HashSet)5