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

Commit ab5d96c5 authored by Bjorn Bringert's avatar Bjorn Bringert
Browse files

Use PackageMonitor in SearchManagerService

This should reduce the number of updates to the searchables
list if multiple packages change at the same time.

Fixes http://b/issue?id=2270711

Change-Id: Ieb930022866aa2872f8df1af677e3ea1645349bf
parent e575d3df
Loading
Loading
Loading
Loading
+16 −54
Original line number Diff line number Diff line
@@ -16,20 +16,14 @@

package android.server.search;

import android.app.ActivityManagerNative;
import android.app.IActivityWatcher;
import com.android.internal.content.PackageMonitor;

import android.app.ISearchManager;
import android.app.ISearchManagerCallback;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;

import java.util.List;
@@ -42,13 +36,11 @@ public class SearchManagerService extends ISearchManager.Stub {

    // general debugging support
    private static final String TAG = "SearchManagerService";
    private static final boolean DBG = false;

    // Context that the service is running in.
    private final Context mContext;

    // This field is initialized in ensureSearchablesCreated(), and then never modified.
    // Only accessed by ensureSearchablesCreated() and getSearchables()
    // This field is initialized lazily in getSearchables(), and then never modified.
    private Searchables mSearchables;

    /**
@@ -61,59 +53,29 @@ public class SearchManagerService extends ISearchManager.Stub {
        mContext = context;
    }

    private synchronized void ensureSearchablesCreated() {
        if (mSearchables != null) return;  // already created

    private synchronized Searchables getSearchables() {
        if (mSearchables == null) {
            mSearchables = new Searchables(mContext);
            mSearchables.buildSearchableList();

        IntentFilter packageFilter = new IntentFilter();
        packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        packageFilter.addDataScheme("package");
        mContext.registerReceiver(mPackageChangedReceiver, packageFilter);
        // Register for events related to sdcard installation.
        IntentFilter sdFilter = new IntentFilter();
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
        sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        mContext.registerReceiver(mPackageChangedReceiver, sdFilter);
            new MyPackageMonitor().register(mContext, true);
        }

    private synchronized Searchables getSearchables() {
        ensureSearchablesCreated();
        return mSearchables;
    }

    /**
     * Refreshes the "searchables" list when packages are added/removed.
     */
    private BroadcastReceiver mPackageChangedReceiver = new BroadcastReceiver() {
    class MyPackageMonitor extends PackageMonitor {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (Intent.ACTION_PACKAGE_ADDED.equals(action) ||
                    Intent.ACTION_PACKAGE_REMOVED.equals(action) ||
                    Intent.ACTION_PACKAGE_CHANGED.equals(action) ||
                    Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action) ||
                    Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
                if (DBG) Log.d(TAG, "Got " + action);
        public void onSomePackagesChanged() {
            // Update list of searchable activities
            getSearchables().buildSearchableList();
                broadcastSearchablesChanged();
            }
        }
    };

    /**
     * Informs all listeners that the list of searchables has been updated.
     */
    void broadcastSearchablesChanged() {
            // Inform all listeners that the list of searchables has been updated.
            Intent intent = new Intent(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
            mContext.sendBroadcast(intent);
        }
    }

    //
    // Searchable activities API