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

Commit 63541b48 authored by Soonil Nagarkar's avatar Soonil Nagarkar Committed by Android (Google) Code Review
Browse files

Merge "Create significant place interfaces" into main

parents dc2a1e18 7e8fea2b
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.app.trust;

import android.Manifest;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SystemService;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
@@ -40,6 +41,15 @@ import java.util.List;
@SystemService(Context.TRUST_SERVICE)
public class TrustManager {

    /**
     * Intent action used to identify services that can serve as significant providers.
     *
     * @hide
     */
    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
    public static final String ACTION_BIND_SIGNIFICANT_PLACE_PROVIDER =
            "com.android.trust.provider.SignificantPlaceProvider.BIND";

    private static final int MSG_TRUST_CHANGED = 1;
    private static final int MSG_TRUST_MANAGED_CHANGED = 2;
    private static final int MSG_TRUST_ERROR = 3;
+10 −0
Original line number Diff line number Diff line
package android.hardware.location;

import android.hardware.location.ISignificantPlaceProviderManager;

/**
 * @hide
 */
oneway interface ISignificantPlaceProvider {
    void setSignificantPlaceProviderManager(in ISignificantPlaceProviderManager manager);
}
+8 −0
Original line number Diff line number Diff line
package android.hardware.location;

/**
 * @hide
 */
interface ISignificantPlaceProviderManager {
    void setInSignificantPlace(in boolean inSignificantPlace);
}
+95 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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 com.android.location.provider;

import android.annotation.Nullable;
import android.app.trust.TrustManager;
import android.hardware.location.ISignificantPlaceProvider;
import android.hardware.location.ISignificantPlaceProviderManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;

import com.android.internal.annotations.GuardedBy;

/** @hide */
public class SignificantPlaceProvider {

    public static final String ACTION = TrustManager.ACTION_BIND_SIGNIFICANT_PLACE_PROVIDER;

    private final IBinder mBinder;

    // write locked on mBinder, read lock is optional depending on atomicity requirements
    @Nullable private volatile ISignificantPlaceProviderManager mManager;

    @GuardedBy("mBinder")
    private boolean mInSignificantPlace = false;

    public SignificantPlaceProvider() {
        mBinder = new Service();
        mManager = null;
    }

    public IBinder getBinder() {
        return mBinder;
    }

    /** Set whether the device is currently in a trusted location. */
    public void setInSignificantPlace(boolean inSignificantPlace) {
        synchronized (mBinder) {
            if (inSignificantPlace == mInSignificantPlace) {
                return;
            }

            mInSignificantPlace = inSignificantPlace;
        }

        ISignificantPlaceProviderManager manager = mManager;
        if (manager != null) {
            try {
                manager.setInSignificantPlace(inSignificantPlace);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    }

    private final class Service extends ISignificantPlaceProvider.Stub {

        Service() {}

        @Override
        public void setSignificantPlaceProviderManager(ISignificantPlaceProviderManager manager) {
            if (Binder.getCallingUid() != Process.SYSTEM_UID) {
                return;
            }

            synchronized (mBinder) {
                if (mInSignificantPlace) {
                    try {
                        manager.setInSignificantPlace(true);
                    } catch (RemoteException e) {
                        throw e.rethrowFromSystemServer();
                    }
                }

                mManager = manager;
            }
        }
    }
}