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

Commit 29ce896d authored by Shreerag Jayakrishnan's avatar Shreerag Jayakrishnan
Browse files

Move getEmergencyActionIntent into common Utils

This allows it to be used by all SystemUI flavors

Bug: 345566482
Test: Emergency actions works on non-phones
Flag: NONE trivial copy from one location to another
Change-Id: Ic38045f77dcf27abb01d0c1a38fd0b7a40d599d0
parent f551a0f7
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -36,5 +36,17 @@ public final class EmergencyGesture {
    public static final String ACTION_LAUNCH_EMERGENCY =
            "com.android.systemui.action.LAUNCH_EMERGENCY";

    /**
     * An alternate intent to launch the emergency flow if the device is in retail mode
     */
    public static final String ACTION_LAUNCH_EMERGENCY_RETAIL =
            "com.android.systemui.action.LAUNCH_EMERGENCY_RETAIL";

    /**
     * Launches the emergency information screen
     */
    public static final String ACTION_LAUNCH_EMERGENCY_INFO =
            "com.android.systemui.action.LAUNCH_EMERGENCY_INFO";

    private EmergencyGesture() {}
}
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright 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.systemui.emergency

import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.res.Resources
import android.text.TextUtils
import android.util.Log
import dagger.Module
import dagger.Provides

import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.res.R

/** Module for providing emergency gesture objects. */
@Module
object EmergencyGestureModule {

    val TAG: String = "EmergencyGestureModule"

    @Provides
    fun emergencyGestureIntentFactory(
        packageManager: PackageManager,
        @Main resources: Resources,
    ): EmergencyGestureIntentFactory {
        return object : EmergencyGestureIntentFactory {
            override fun invoke(action: String): Intent? {
                return getEmergencyActionIntent(packageManager, resources, action)
            }
        }
    }

    /**
     * Return the "best" Emergency action intent for a given action
     */
    private fun getEmergencyActionIntent(
        packageManager: PackageManager,
        @Main resources: Resources,
        action: String,
    ): Intent? {
        val emergencyIntent = Intent(action)
        val emergencyActivities = packageManager.queryIntentActivities(emergencyIntent,
                PackageManager.MATCH_SYSTEM_ONLY)
        val resolveInfo: ResolveInfo? = getTopEmergencySosInfo(emergencyActivities, resources)
        if (resolveInfo == null) {
            Log.wtf(TAG, "Couldn't find an app to process the emergency intent.")
            return null
        }
        emergencyIntent.setComponent(ComponentName(resolveInfo.activityInfo.packageName,
                resolveInfo.activityInfo.name))
        emergencyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        return emergencyIntent
    }

    /**
     * Select and return the "best" ResolveInfo for Emergency SOS Activity.
     */
    private fun getTopEmergencySosInfo(
        emergencyActivities: List<ResolveInfo>,
        @Main resources: Resources,
    ): ResolveInfo? {
        // No matched activity.
        if (emergencyActivities.isEmpty()) {
            return null
        }

        // Of multiple matched Activities, give preference to the pre-set package name.
        val preferredAppPackageName =
                resources.getString(R.string.config_preferredEmergencySosPackage)

        // If there is no preferred app, then return first match.
        if (TextUtils.isEmpty(preferredAppPackageName)) {
            return emergencyActivities.get(0)
        }

        for (emergencyInfo: ResolveInfo in emergencyActivities) {
            // If activity is from the preferred app, use it.
            if (TextUtils.equals(emergencyInfo.activityInfo.packageName, preferredAppPackageName)) {
                return emergencyInfo
            }
        }
        // No matching activity: return first match
        return emergencyActivities.get(0)
    }

    /**
     * Creates an intent to launch the Emergency action. If no handler is present, returns `null`.
     */
    public interface EmergencyGestureIntentFactory {
        operator fun invoke(action: String): Intent?
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.statusbar.dagger;

import com.android.systemui.emergency.EmergencyGestureModule;
import com.android.systemui.statusbar.notification.dagger.NotificationsModule;
import com.android.systemui.statusbar.notification.row.NotificationRowModule;
import com.android.systemui.statusbar.phone.StatusBarNotificationPresenterModule;
@@ -26,6 +27,6 @@ import dagger.Module;
/**  */
@Module(includes = {CentralSurfacesDependenciesModule.class,
        StatusBarNotificationPresenterModule.class, StatusBarPhoneModule.class,
        NotificationsModule.class, NotificationRowModule.class})
        NotificationsModule.class, NotificationRowModule.class, EmergencyGestureModule.class})
public interface CentralSurfacesModule {
}
+0 −4
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.wm.shell.transition.Transitions.ENABLE_SHELL_TRANSITIO
import android.annotation.Nullable;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.UserHandle;
@@ -261,9 +260,6 @@ public interface CentralSurfaces extends Dumpable, LifecycleOwner, CoreStartable

    boolean isScreenFullyOff();

    @Nullable
    Intent getEmergencyActionIntent();

    boolean isCameraAllowedByAdmin();

    boolean isGoingToSleep();
+9 −2
Original line number Diff line number Diff line
@@ -46,6 +46,8 @@ import com.android.systemui.camera.CameraIntents;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.DisplayId;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.emergency.EmergencyGesture;
import com.android.systemui.emergency.EmergencyGestureModule.EmergencyGestureIntentFactory;
import com.android.systemui.keyguard.WakefulnessLifecycle;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.qs.QSHost;
@@ -113,6 +115,8 @@ public class CentralSurfacesCommandQueueCallbacks implements CommandQueue.Callba
    private int mDisabled1;
    private int mDisabled2;

    private final EmergencyGestureIntentFactory mEmergencyGestureIntentFactory;

    @Inject
    CentralSurfacesCommandQueueCallbacks(
            CentralSurfaces centralSurfaces,
@@ -143,7 +147,8 @@ public class CentralSurfacesCommandQueueCallbacks implements CommandQueue.Callba
            Lazy<CameraLauncher> cameraLauncherLazy,
            UserTracker userTracker,
            QSHost qsHost,
            ActivityStarter activityStarter) {
            ActivityStarter activityStarter,
            EmergencyGestureIntentFactory emergencyGestureIntentFactory) {
        mCentralSurfaces = centralSurfaces;
        mQsController = quickSettingsController;
        mContext = context;
@@ -176,6 +181,7 @@ public class CentralSurfacesCommandQueueCallbacks implements CommandQueue.Callba
        mCameraLaunchGestureVibrationEffect = getCameraGestureVibrationEffect(
                mVibratorOptional, resources);
        mActivityStarter = activityStarter;
        mEmergencyGestureIntentFactory = emergencyGestureIntentFactory;
    }

    @Override
@@ -395,7 +401,8 @@ public class CentralSurfacesCommandQueueCallbacks implements CommandQueue.Callba

    @Override
    public void onEmergencyActionLaunchGestureDetected() {
        Intent emergencyIntent = mCentralSurfaces.getEmergencyActionIntent();
        Intent emergencyIntent = mEmergencyGestureIntentFactory.invoke(
                EmergencyGesture.ACTION_LAUNCH_EMERGENCY);

        if (emergencyIntent == null) {
            Log.wtf(CentralSurfaces.TAG, "Couldn't find an app to process the emergency intent.");
Loading