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

Commit f7eecb23 authored by Oli Thompson's avatar Oli Thompson
Browse files

Create emergency button in work profile unlaunchable app

dialog

Test: patch ag/20882588 and btest a.d.g.WorkTelephonyTest -n

Bug: b/258629564

Change-Id: I34aee0a82369c93db2dd3380fabe60f6d804ac9c
parent 03882bc8
Loading
Loading
Loading
Loading
+52 −19
Original line number Diff line number Diff line
@@ -28,11 +28,13 @@ import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.os.UserManager;
import android.telecom.TelecomManager;
import android.util.Log;
import android.view.Window;

@@ -52,6 +54,7 @@ public class UnlaunchableAppActivity extends Activity
    private int mUserId;
    private int mReason;
    private IntentSender mTarget;
    private TelecomManager mTelecomManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -60,9 +63,11 @@ public class UnlaunchableAppActivity extends Activity
        // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Intent intent = getIntent();
        mTelecomManager = getSystemService(TelecomManager.class);
        mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
        mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
        mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT, android.content.IntentSender.class);
        mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT,
                android.content.IntentSender.class);

        if (mUserId == UserHandle.USER_NULL) {
            Log.wtf(TAG, "Invalid user id: " + mUserId + ". Stopping.");
@@ -70,29 +75,40 @@ public class UnlaunchableAppActivity extends Activity
            return;
        }

        String dialogTitle;
        String dialogMessage = null;
        if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
            dialogTitle = getDialogTitle();
            dialogMessage = getDialogMessage();
        } else {
        if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) {
            Log.wtf(TAG, "Invalid unlaunchable type: " + mReason);
            finish();
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(dialogTitle)
                .setMessage(dialogMessage)
                .setOnDismissListener(this);
        if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE) {
            builder.setPositiveButton(R.string.work_mode_turn_on, this)
                    .setNegativeButton(R.string.cancel, null);
        String targetPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        boolean showEmergencyCallButton =
                (targetPackageName != null && targetPackageName.equals(
                        mTelecomManager.getDefaultDialerPackage(UserHandle.of(mUserId))));

        final AlertDialog.Builder builder;
        final String dialogMessage;
        if (showEmergencyCallButton) {
            builder = new AlertDialog.Builder(this, R.style.AlertDialogWithEmergencyButton);
            dialogMessage = getDialogMessage(R.string.work_mode_dialer_off_message);
            builder.setNeutralButton(R.string.work_mode_emergency_call_button, this);
        } else {
            builder.setPositiveButton(R.string.ok, null);
            builder = new AlertDialog.Builder(this);
            dialogMessage = getDialogMessage(R.string.work_mode_off_message);
        }
        builder.setTitle(getDialogTitle())
                .setMessage(dialogMessage)
                .setOnDismissListener(this)
                .setPositiveButton(R.string.work_mode_turn_on, this)
                .setNegativeButton(R.string.cancel, null);

        final AlertDialog dialog = builder.create();
        dialog.create();
        if (showEmergencyCallButton) {
            dialog.getWindow().findViewById(R.id.parentPanel).setPadding(0, 0, 0, 30);
            dialog.getWindow().findViewById(R.id.button3).setOutlineProvider(null);
        }

        // Prevents screen overlay attack.
        getWindow().setHideOverlayWindows(true);
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
@@ -104,10 +120,10 @@ public class UnlaunchableAppActivity extends Activity
                UNLAUNCHABLE_APP_WORK_PAUSED_TITLE, () -> getString(R.string.work_mode_off_title));
    }

    private String getDialogMessage() {
    private String getDialogMessage(int dialogMessageString) {
        return getSystemService(DevicePolicyManager.class).getResources().getString(
                UNLAUNCHABLE_APP_WORK_PAUSED_MESSAGE,
                () -> getString(R.string.work_mode_off_message));
                () -> getString(dialogMessageString));
    }

    @Override
@@ -117,14 +133,27 @@ public class UnlaunchableAppActivity extends Activity

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (mReason == UNLAUNCHABLE_REASON_QUIET_MODE && which == DialogInterface.BUTTON_POSITIVE) {
        if (mReason != UNLAUNCHABLE_REASON_QUIET_MODE) {
            return;
        }
        if (which == DialogInterface.BUTTON_POSITIVE) {
            UserManager userManager = UserManager.get(this);
            new Handler(Looper.getMainLooper()).post(
                    () -> userManager.requestQuietModeEnabled(
                            /* enableQuietMode= */ false, UserHandle.of(mUserId), mTarget));
        } else if (which == DialogInterface.BUTTON_NEUTRAL) {
            launchEmergencyDialer();
        }
    }

    private void launchEmergencyDialer() {
        startActivity(mTelecomManager.createLaunchEmergencyDialerIntent(
                        null /* number*/)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP));
    }

    private static final Intent createBaseIntent() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("android", UnlaunchableAppActivity.class.getName()));
@@ -139,9 +168,13 @@ public class UnlaunchableAppActivity extends Activity
        return intent;
    }

    public static Intent createInQuietModeDialogIntent(int userId, IntentSender target) {
    public static Intent createInQuietModeDialogIntent(int userId, IntentSender target,
            ResolveInfo resolveInfo) {
        Intent intent = createInQuietModeDialogIntent(userId);
        intent.putExtra(Intent.EXTRA_INTENT, target);
        if (resolveInfo != null) {
            intent.putExtra(Intent.EXTRA_PACKAGE_NAME, resolveInfo.getComponentInfo().packageName);
        }
        return intent;
    }
}
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2022 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.
  -->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetTop="6dp"
       android:insetBottom="6dp">
    <shape android:shape="rectangle">
        <corners android:radius="18dp"/>
        <solid android:color="@android:color/system_accent3_100" />
    </shape>
</inset>
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
@@ -5326,6 +5326,10 @@
    <string name="work_mode_off_message">Get access to your work apps and notifications</string>
    <!-- Title for button to turn on work profile. [CHAR LIMIT=NONE] -->
    <string name="work_mode_turn_on">Turn on</string>
    <!-- Title for button to launch the personal safety app to make an emergency call    -->
    <string name="work_mode_emergency_call_button">Emergency</string>
    <!-- Text shown in a dialog when the user tries to launch a disabled work profile app when work apps are paused-->
    <string name="work_mode_dialer_off_message">Get access to your work apps and calls</string>

    <!-- Title of the dialog that is shown when the user tries to launch a blocked application [CHAR LIMIT=50] -->
    <string name="app_blocked_title">App is not available</string>
+49 −32
Original line number Diff line number Diff line
@@ -69,6 +69,19 @@ please see styles_device_defaults.xml.
        <item name="needsDefaultBackgrounds">false</item>
    </style>

    <!-- Base style for the alert dialog with emergency call button   -->
    <style name="AlertDialogWithEmergencyButton" parent="AlertDialog">
        <item name="buttonBarNeutralButtonStyle">@style/AlertDialogEmergencyButtonStyle</item>
    </style>

    <style name="AlertDialogEmergencyButtonStyle" parent="AlertDialogWithEmergencyButton">
        <item name="background">@drawable/work_mode_emergency_button_background</item>
        <item name="textColor">@color/text_color_on_accent_device_default</item>
        <item name="paddingLeft">15dip</item>
        <item name="paddingRight">15dip</item>
        <item name="layout_marginStart">10dip</item>
    </style>

    <style name="Widget.PreferenceFrameLayout">
        <item name="borderTop">0dip</item>
        <item name="borderBottom">0dip</item>
@@ -503,7 +516,8 @@ please see styles_device_defaults.xml.
        <item name="textEditSidePasteWindowLayout">?attr/textEditSidePasteWindowLayout</item>
        <item name="textEditSideNoPasteWindowLayout">?attr/textEditSideNoPasteWindowLayout</item>
        <item name="textEditSuggestionItemLayout">?attr/textEditSuggestionItemLayout</item>
        <item name="textEditSuggestionContainerLayout">?attr/textEditSuggestionContainerLayout</item>
        <item name="textEditSuggestionContainerLayout">?attr/textEditSuggestionContainerLayout
        </item>
        <item name="textEditSuggestionHighlightStyle">?attr/textEditSuggestionHighlightStyle</item>
        <item name="textCursorDrawable">?attr/textCursorDrawable</item>
        <item name="breakStrategy">high_quality</item>
@@ -593,7 +607,8 @@ please see styles_device_defaults.xml.
        <item name="weekNumberColor">#33FFFFFF</item>
        <item name="weekSeparatorLineColor">#19FFFFFF</item>
        <item name="selectedDateVerticalBar">@drawable/day_picker_week_view_dayline_holo</item>
        <item name="weekDayTextAppearance">@style/TextAppearance.Small.CalendarViewWeekDayView</item>
        <item name="weekDayTextAppearance">@style/TextAppearance.Small.CalendarViewWeekDayView
        </item>
        <item name="dateTextAppearance">?attr/textAppearanceSmall</item>
        <item name="calendarViewMode">holo</item>
    </style>
@@ -1298,10 +1313,12 @@ please see styles_device_defaults.xml.
        <item name="textColor">?attr/textColorSecondary</item>
    </style>

    <style name="TextAppearance.Widget.Toolbar.Title" parent="TextAppearance.Widget.ActionBar.Title">
    <style name="TextAppearance.Widget.Toolbar.Title"
           parent="TextAppearance.Widget.ActionBar.Title">
    </style>

    <style name="TextAppearance.Widget.Toolbar.Subtitle" parent="TextAppearance.Widget.ActionBar.Subtitle">
    <style name="TextAppearance.Widget.Toolbar.Subtitle"
           parent="TextAppearance.Widget.ActionBar.Subtitle">
    </style>

    <style name="Widget.ActionButton">
+4 −0
Original line number Diff line number Diff line
@@ -3101,6 +3101,10 @@
  <java-symbol type="string" name="language_selection_title" />
  <java-symbol type="string" name="search_language_hint" />

  <!--  Work profile unlaunchable app alert dialog-->
  <java-symbol type="style" name="AlertDialogWithEmergencyButton"/>
  <java-symbol type="string" name="work_mode_dialer_off_message" />
  <java-symbol type="string" name="work_mode_emergency_call_button" />
  <java-symbol type="string" name="work_mode_off_title" />
  <java-symbol type="string" name="work_mode_off_message" />
  <java-symbol type="string" name="work_mode_turn_on" />
Loading