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

Commit b307d623 authored by Shuo Qian's avatar Shuo Qian
Browse files

Mainline API - Make a new method for Intent.resolveSystemService

Test: manual; treehugger
Bug: 147303724
Bug: 138745534
Change-Id: I0c418220c61154300daf0c9e0487c9b6cb031f27
parent 21a593e3
Loading
Loading
Loading
Loading
+33 −1
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_DELIVERY_IND;
import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND;
import static com.google.android.mms.pdu.PduHeaders.MESSAGE_TYPE_READ_ORIG_IND;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Activity;
import android.app.AppOpsManager;
import android.app.BroadcastOptions;
@@ -31,6 +33,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteException;
@@ -61,6 +65,7 @@ import com.google.android.mms.pdu.PduPersister;
import com.google.android.mms.pdu.ReadOrigInd;

import java.util.HashMap;
import java.util.List;

/**
 * WAP push handler class.
@@ -109,7 +114,7 @@ public class WapPushOverSms implements ServiceConnection {

    private void bindWapPushManagerService(Context context) {
        Intent intent = new Intent(IWapPushManager.class.getName());
        ComponentName comp = intent.resolveSystemService(context.getPackageManager(), 0);
        ComponentName comp = resolveSystemService(context.getPackageManager(), intent);
        intent.setComponent(comp);
        if (comp == null || !context.bindService(intent, this, Context.BIND_AUTO_CREATE)) {
            Rlog.e(TAG, "bindService() for wappush manager failed");
@@ -121,6 +126,33 @@ public class WapPushOverSms implements ServiceConnection {
        }
    }

    /**
     * Special function for use by the system to resolve service
     * intents to system apps.  Throws an exception if there are
     * multiple potential matches to the Intent.  Returns null if
     * there are no matches.
     */
    private static @Nullable ComponentName resolveSystemService(@NonNull PackageManager pm,
            @NonNull Intent intent) {
        List<ResolveInfo> results = pm.queryIntentServices(
                intent, PackageManager.MATCH_SYSTEM_ONLY);
        if (results == null) {
            return null;
        }
        ComponentName comp = null;
        for (int i = 0; i < results.size(); i++) {
            ResolveInfo ri = results.get(i);
            ComponentName foundComp = new ComponentName(ri.serviceInfo.applicationInfo.packageName,
                    ri.serviceInfo.name);
            if (comp != null) {
                throw new IllegalStateException("Multiple system services handle " + intent
                    + ": " + comp + ", " + foundComp);
            }
            comp = foundComp;
        }
        return comp;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mWapPushManager = IWapPushManager.Stub.asInterface(service);