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

Commit 03755a1f authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Syncronosuly notify SelectPrinterActivity about changes to enabled print

services

If the SelectPrinterActivity is visible it shows different emptyState
depending on if there are print services available or not. Hence the
activity has to listen to changes to the list of enabled services.

This also fixes a small syncronsation problem if two observers are
registered for the enabled print services.

Bug: 25666802
Change-Id: I79af66f25f10e66347b48ce9bb99c1657b30a8dd
parent 387aac6a
Loading
Loading
Loading
Loading
+51 −6
Original line number Diff line number Diff line
@@ -33,10 +33,12 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.database.ContentObserver;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.print.PrintManager;
import android.print.PrinterId;
import android.print.PrinterInfo;
@@ -84,9 +86,7 @@ public final class SelectPrinterActivity extends Activity {

    private static final String EXTRA_PRINTER_ID = "EXTRA_PRINTER_ID";

    /**
     * If there are any enabled print services
     */
    /** If there are any enabled print services */
    private boolean mHasEnabledPrintServices;

    private final ArrayList<PrintServiceInfo> mAddPrinterServices =
@@ -98,6 +98,9 @@ public final class SelectPrinterActivity extends Activity {

    private AnnounceFilterResult mAnnounceFilterResult;

    /** Monitor if new print services get enabled or disabled */
    private ContentObserver mPrintServicesObserver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -227,14 +230,50 @@ public final class SelectPrinterActivity extends Activity {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
    /**
     * Adjust the UI if the enabled print services changed.
     */
    private synchronized void onPrintServicesUpdate() {
        updateServicesWithAddPrinterActivity();
        updateEmptyView((DestinationAdapter)mListView.getAdapter());
        invalidateOptionsMenu();
    }

    /**
     * Register listener for changes to the enabled print services.
     */
    private void registerServiceMonitor() {
        mPrintServicesObserver = new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                onPrintServicesUpdate();
            }
        };

        getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(Settings.Secure.ENABLED_PRINT_SERVICES), false,
                mPrintServicesObserver);
    }

    /**
     * Unregister {@link #mPrintServicesObserver listener for changes to the enabled print services}
     * or nothing if the listener is not registered.
     */
    private void unregisterServiceMonitorIfNeeded() {
        if (mPrintServicesObserver != null) {
            getContentResolver().unregisterContentObserver(mPrintServicesObserver);

            mPrintServicesObserver = null;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        registerServiceMonitor();
        onPrintServicesUpdate();
    }

    @Override
    public void onPause() {
        if (mAnnounceFilterResult != null) {
@@ -243,6 +282,12 @@ public final class SelectPrinterActivity extends Activity {
        super.onPause();
    }

    @Override
    public void onStop() {
        unregisterServiceMonitorIfNeeded();
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_add_printer) {
+5 −0
Original line number Diff line number Diff line
@@ -228,6 +228,11 @@ public final class PrintManagerService extends SystemService {
                    return null;
                }
                userState = getOrCreateUserStateLocked(resolvedUserId);

                // The user state might be updated via the same observer-set as the caller of this
                // interface. If the caller is called back first the user state is not yet updated
                // and the user gets and inconsistent view. Hence force an update.
                userState.updateIfNeededLocked();
            }
            final long identity = Binder.clearCallingIdentity();
            try {