Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74b16200 authored by Philip P. Moltmann's avatar Philip P. Moltmann Committed by android-build-merger
Browse files

Merge "Add "app printer activity" and always keep the print service state...

Merge "Add "app printer activity" and always keep the print service state updated. Also fiddle with the UI to use more standard values." into nyc-dev
am: 02a465ac

* commit '02a465ac':
  Add "app printer activity" and always keep the print service state updated. Also fiddle with the UI to use more standard values.
parents b5069e05 02a465ac
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -246,6 +246,7 @@ LOCAL_SRC_FILES += \
	core/java/android/print/IPrintDocumentAdapter.aidl \
	core/java/android/print/IPrintDocumentAdapterObserver.aidl \
	core/java/android/print/IPrintJobStateChangeListener.aidl \
	core/java/android/print/IPrintServicesChangeListener.aidl \
	core/java/android/print/IPrintManager.aidl \
	core/java/android/print/IPrintSpooler.aidl \
	core/java/android/print/IPrintSpoolerCallbacks.aidl \
+43 −2
Original line number Diff line number Diff line
@@ -16,12 +16,14 @@

package android.print;

import android.content.ComponentName;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.print.IPrinterDiscoveryObserver;
import android.print.IPrintDocumentAdapter;
import android.print.PrintJobId;
import android.print.IPrintJobStateChangeListener;
import android.print.IPrintServicesChangeListener;
import android.print.PrinterId;
import android.print.PrintJobInfo;
import android.print.PrintAttributes;
@@ -45,8 +47,47 @@ interface IPrintManager {
    void removePrintJobStateChangeListener(in IPrintJobStateChangeListener listener,
            int userId);

    List<PrintServiceInfo> getInstalledPrintServices(int userId);
    List<PrintServiceInfo> getEnabledPrintServices(int userId);
    /**
     * Listen for changes to the installed and enabled print services.
     *
     * @param listener the listener to add
     * @param userId the id of the user listening
     *
     * @see android.print.PrintManager#getPrintServices(int, String)
     */
    void addPrintServicesChangeListener(in IPrintServicesChangeListener listener,
            int userId);

    /**
     * Stop listening for changes to the installed and enabled print services.
     *
     * @param listener the listener to remove
     * @param userId the id of the user requesting the removal
     *
     * @see android.print.PrintManager#getPrintServices(int, String)
     */
    void removePrintServicesChangeListener(in IPrintServicesChangeListener listener,
            int userId);

    /**
     * Get the print services.
     *
     * @param selectionFlags flags selecting which services to get
     * @param selectedService if not null, the id of the print service to get
     * @param userId the id of the user requesting the services
     *
     * @return the list of selected print services.
     */
    List<PrintServiceInfo> getPrintServices(int selectionFlags, int userId);

    /**
     * Enable or disable a print service.
     *
     * @param service The service to enabled or disable
     * @param isEnabled whether the service should be enabled or disabled
     * @param userId the id of the user requesting the services
     */
    void setPrintServiceEnabled(in ComponentName service, boolean isEnabled, int userId);

    void createPrinterDiscoverySession(in IPrinterDiscoveryObserver observer, int userId);
    void startPrinterDiscovery(in IPrinterDiscoveryObserver observer,
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.print;

/**
 * Interface for observing print services changes.
 *
 * @hide
 */
oneway interface IPrintServicesChangeListener {
    void onPrintServicesChanged();
}
+165 −19
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Activity;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
@@ -111,6 +112,38 @@ public final class PrintManager {
    private static final boolean DEBUG = false;

    private static final int MSG_NOTIFY_PRINT_JOB_STATE_CHANGED = 1;
    private static final int MSG_NOTIFY_PRINT_SERVICES_CHANGED = 2;

    /**
     * Package name of print spooler.
     *
     * @hide
     */
    public static final String PRINT_SPOOLER_PACKAGE_NAME = "com.android.printspooler";

    /**
     * Select enabled services.
     * </p>
     * @see #getPrintServices
     * @hide
     */
    public static final int ENABLED_SERVICES = 1 << 0;

    /**
     * Select disabled services.
     * </p>
     * @see #getPrintServices
     * @hide
     */
    public static final int DISABLED_SERVICES = 1 << 1;

    /**
     * Select all services.
     * </p>
     * @see #getPrintServices
     * @hide
     */
    public static final int ALL_SERVICES = ENABLED_SERVICES | DISABLED_SERVICES;

    /**
     * The action for launching the print dialog activity.
@@ -165,7 +198,10 @@ public final class PrintManager {

    private final Handler mHandler;

    private Map<PrintJobStateChangeListener, PrintJobStateChangeListenerWrapper> mPrintJobStateChangeListeners;
    private Map<PrintJobStateChangeListener, PrintJobStateChangeListenerWrapper>
            mPrintJobStateChangeListeners;
    private Map<PrintServicesChangeListener, PrintServicesChangeListenerWrapper>
            mPrintServicesChangeListeners;

    /** @hide */
    public interface PrintJobStateChangeListener {
@@ -178,6 +214,15 @@ public final class PrintManager {
        public void onPrintJobStateChanged(PrintJobId printJobId);
    }

    /** @hide */
    public interface PrintServicesChangeListener {

        /**
         * Callback notifying that the print services changed.
         */
        public void onPrintServicesChanged();
    }

    /**
     * Creates a new instance.
     *
@@ -207,6 +252,15 @@ public final class PrintManager {
                        }
                        args.recycle();
                    } break;
                    case MSG_NOTIFY_PRINT_SERVICES_CHANGED: {
                        PrintServicesChangeListenerWrapper wrapper =
                                (PrintServicesChangeListenerWrapper) message.obj;
                        PrintServicesChangeListener listener = wrapper.getListener();
                        if (listener != null) {
                            listener.onPrintServicesChanged();
                        }
                    } break;

                }
            }
        };
@@ -478,42 +532,82 @@ public final class PrintManager {
    }

    /**
     * Gets the list of enabled print services.
     * Listen for changes to the installed and enabled print services.
     *
     * @return The enabled service list or an empty list.
     * @hide
     * @param listener the listener to add
     *
     * @see android.print.PrintManager#getPrintServices
     */
    public List<PrintServiceInfo> getEnabledPrintServices() {
    void addPrintServicesChangeListener(@NonNull PrintServicesChangeListener listener) {
        if (mService == null) {
            Log.w(LOG_TAG, "Feature android.software.print not available");
            return Collections.emptyList();
            return;
        }
        try {
            List<PrintServiceInfo> enabledServices = mService.getEnabledPrintServices(mUserId);
            if (enabledServices != null) {
                return enabledServices;
        if (mPrintServicesChangeListeners == null) {
            mPrintServicesChangeListeners = new ArrayMap<PrintServicesChangeListener,
                    PrintServicesChangeListenerWrapper>();
        }
        PrintServicesChangeListenerWrapper wrappedListener =
                new PrintServicesChangeListenerWrapper(listener, mHandler);
        try {
            mService.addPrintServicesChangeListener(wrappedListener, mUserId);
            mPrintServicesChangeListeners.put(listener, wrappedListener);
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
        }
        return Collections.emptyList();
    }

    /**
     * Gets the list of installed print services.
     * Stop listening for changes to the installed and enabled print services.
     *
     * @return The installed service list or an empty list.
     * @hide
     * @param listener the listener to remove
     *
     * @see android.print.PrintManager#getPrintServices
     */
    public List<PrintServiceInfo> getInstalledPrintServices() {
    void removePrintServicesChangeListener(@NonNull PrintServicesChangeListener listener) {
        if (mService == null) {
            Log.w(LOG_TAG, "Feature android.software.print not available");
            return Collections.emptyList();
            return;
        }
        if (mPrintServicesChangeListeners == null) {
            return;
        }
        PrintServicesChangeListenerWrapper wrappedListener =
                mPrintServicesChangeListeners.remove(listener);
        if (wrappedListener == null) {
            return;
        }
        if (mPrintServicesChangeListeners.isEmpty()) {
            mPrintServicesChangeListeners = null;
        }
        wrappedListener.destroy();
        try {
            List<PrintServiceInfo> installedServices = mService.getInstalledPrintServices(mUserId);
            if (installedServices != null) {
                return installedServices;
            mService.removePrintServicesChangeListener(wrappedListener, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error removing print services change listener", re);
        }
    }


    /**
     * Gets the list of print services, but does not register for updates. The user has to register
     * for updates by itself, or use {@link PrintServicesLoader}.
     *
     * @param selectionFlags flags selecting which services to get. Either
     *                       {@link #ENABLED_SERVICES},{@link #DISABLED_SERVICES}, or both.
     *
     * @return The enabled service list or an empty list.
     *
     * @see #addPrintServicesChangeListener(PrintServicesChangeListener)
     * @see #removePrintServicesChangeListener(PrintServicesChangeListener)
     *
     * @hide
     */
    public @NonNull List<PrintServiceInfo> getPrintServices(int selectionFlags) {
        try {
            List<PrintServiceInfo> services = mService.getPrintServices(selectionFlags, mUserId);
            if (services != null) {
                return services;
            }
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
@@ -532,6 +626,26 @@ public final class PrintManager {
        return new PrinterDiscoverySession(mService, mContext, mUserId);
    }

    /**
     * Enable or disable a print service.
     *
     * @param service The service to enabled or disable
     * @param isEnabled whether the service should be enabled or disabled
     *
     * @hide
     */
    public void setPrintServiceEnabled(@NonNull ComponentName service, boolean isEnabled) {
        if (mService == null) {
            Log.w(LOG_TAG, "Feature android.software.print not available");
            return;
        }
        try {
            mService.setPrintServiceEnabled(service, isEnabled, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error enabling or disabling " + service, re);
        }
    }

    /**
     * @hide
     */
@@ -1096,4 +1210,36 @@ public final class PrintManager {
            return mWeakListener.get();
        }
    }

    /**
     * @hide
     */
    public static final class PrintServicesChangeListenerWrapper extends
            IPrintServicesChangeListener.Stub {
        private final WeakReference<PrintServicesChangeListener> mWeakListener;
        private final WeakReference<Handler> mWeakHandler;

        public PrintServicesChangeListenerWrapper(PrintServicesChangeListener listener,
                Handler handler) {
            mWeakListener = new WeakReference<>(listener);
            mWeakHandler = new WeakReference<>(handler);
        }

        @Override
        public void onPrintServicesChanged() {
            Handler handler = mWeakHandler.get();
            PrintServicesChangeListener listener = mWeakListener.get();
            if (handler != null && listener != null) {
                handler.obtainMessage(MSG_NOTIFY_PRINT_SERVICES_CHANGED, this).sendToTarget();
            }
        }

        public void destroy() {
            mWeakListener.clear();
        }

        public PrintServicesChangeListener getListener() {
            return mWeakListener.get();
        }
    }
}
+125 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.print;

import android.annotation.NonNull;
import android.content.Context;
import android.content.Loader;
import android.os.Handler;
import android.os.Message;
import android.printservice.PrintServiceInfo;

import java.util.List;

/**
 * Loader for the list of print services. Can be parametrized to select a subset.
 *
 * @hide
 */
public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> {
    /** What type of services to load. */
    private final int mSelectionFlags;

    /** The print manager to be used by this object */
    private final @NonNull PrintManager mPrintManager;

    /** Handler to sequentialize the delivery of the results to the main thread */
    private Handler mHandler;

    /** Listens for updates to the data from the platform */
    private PrintManager.PrintServicesChangeListener mListener;

    /**
     * Create a new PrintServicesLoader.
     *
     * @param selectionFlags What type of services to load.
     */
    public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
            int selectionFlags) {
        super(context);
        mPrintManager = printManager;
        mSelectionFlags = selectionFlags;
    }

    @Override
    protected void onForceLoad() {
        queueNewResult();
    }

    /**
     * Read the print services and queue it to be delivered on the main thread.
     */
    private void queueNewResult() {
        Message m = mHandler.obtainMessage(0);
        m.obj = mPrintManager.getPrintServices(mSelectionFlags);
        mHandler.sendMessage(m);
    }

    @Override
    protected void onStartLoading() {
        mHandler = new MyHandler();
        mListener = new PrintManager.PrintServicesChangeListener() {
            @Override public void onPrintServicesChanged() {
                queueNewResult();
            }
        };

        mPrintManager.addPrintServicesChangeListener(mListener);

        // Immediately deliver a result
        deliverResult(mPrintManager.getPrintServices(mSelectionFlags));
    }

    @Override
    protected void onStopLoading() {
        if (mListener != null) {
            mPrintManager.removePrintServicesChangeListener(mListener);
            mListener = null;
        }

        if (mHandler != null) {
            mHandler.removeMessages(0);
            mHandler = null;
        }
    }

    @Override
    protected void onReset() {
        onStopLoading();
    }

    /**
     * Handler to sequentialize all the updates to the main thread.
     */
    private class MyHandler extends Handler {
        /**
         * Create a new handler on the main thread.
         */
        public MyHandler() {
            super(getContext().getMainLooper());
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (isStarted()) {
                deliverResult((List<PrintServiceInfo>) msg.obj);
            }
        }
    }
}
Loading