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

Commit dc1e59a3 authored by Robin Lee's avatar Robin Lee Committed by Android (Google) Code Review
Browse files

Merge "Change VPN failure notification to dialog" into nyc-dev

parents 7e61f71b 28210677
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -5211,11 +5211,10 @@
    <string name="vpn_replace_always_on_vpn_title">Replace existing VPN?</string>
    <!-- Dialog message body to set another VPN app to be always-on [CHAR LIMIT=NONE] -->
    <string name="vpn_replace_always_on_vpn_message">You\'re already connected to a VPN. If you connect to a different one, your existing VPN will be replaced.</string>
    <!-- Notification title when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
    <string name="vpn_cant_connect_notification_title"><xliff:g id="vpn_name" example="OpenVPN">%1$s</xliff:g> can\'t connect</string>
    <!-- Notification subtitle when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
    <string name="vpn_tap_for_vpn_settings">Tap for VPN settings</string>
    <!-- Dialog mesage title when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
    <string name="vpn_cant_connect_title"><xliff:g id="vpn_name" example="OpenVPN">%1$s</xliff:g> can\'t connect</string>
    <!-- Dialog message subtitle when the user can't connect an always-on vpn [CHAR LIMIT=NONE] -->
    <string name="vpn_cant_connect_message">This app doesn\'t support always-on VPN.</string>
    <!-- Preference title for VPN settings. [CHAR LIMIT=40] -->
    <string name="vpn_title">VPN</string>
    <!-- Preference title to create a new VPN profile. [CHAR LIMIT=40] -->
+30 −38
Original line number Diff line number Diff line
@@ -177,8 +177,10 @@ public class AppManagementFragment extends SettingsPreferenceFragment
            VpnUtils.clearLockdownVpn(getContext());
        }
        mConnectivityManager.setAlwaysOnVpnPackageForUser(mUserId, isEnabled ? mPackageName : null);
        if (isEnabled && !isVpnAlwaysOn()) {
            CannotConnectFragment.show(this, mVpnLabel);
        }
        updateUI();
        showCantConnectNotificationIfNeeded(isEnabled);
    }

    private void updateUI() {
@@ -246,52 +248,42 @@ public class AppManagementFragment extends SettingsPreferenceFragment
        return getAlwaysOnVpnPackage() != null && !isVpnAlwaysOn();
    }

    private void showCantConnectNotificationIfNeeded(boolean isEnabledExpected) {
        // Display notification only when user tries to turn on but system fails to turn it on.
        if (isEnabledExpected && !isVpnAlwaysOn()) {
            String appDisplayName = mPackageName;
            try {
                appDisplayName = VpnConfig.getVpnLabel(getContext(), mPackageName).toString();
            } catch (NameNotFoundException e) {
                // Use default package name as app name. Quietly fail.
            }
            postCantConnectNotification(getContext(), appDisplayName,
                    mPackageUid /* notificationId */);
    public static class CannotConnectFragment extends DialogFragment {
        private static final String TAG = "CannotConnect";
        private static final String ARG_VPN_LABEL = "label";

        public static void show(AppManagementFragment parent, String vpnLabel) {
            if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
                final Bundle args = new Bundle();
                args.putString(ARG_VPN_LABEL, vpnLabel);

                final DialogFragment frag = new CannotConnectFragment();
                frag.setArguments(args);
                frag.show(parent.getFragmentManager(), TAG);
            }
        }

    /**
     * @param notificationId should be unique to the vpn app, e.g. uid, to keep one notification per
     *                       vpn app per user
     */
    private static void postCantConnectNotification(Context context, @NonNull String vpnName,
            int notificationId) {
        final Resources res = context.getResources();
        // Only action is specified to match cross-profile intent filter set by ManagedProfileSetup
        final Intent intent = new Intent(Settings.ACTION_VPN_SETTINGS);
        final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                Intent.FLAG_ACTIVITY_NEW_TASK);

        final Notification notification = new Notification.Builder(context)
                .setContentTitle(res.getString(R.string.vpn_cant_connect_notification_title,
                        vpnName))
                .setContentText(res.getString(R.string.vpn_tap_for_vpn_settings))
                .setSmallIcon(R.drawable.ic_settings_wireless)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

        NotificationManager nm = context.getSystemService(NotificationManager.class);
        nm.notify(notificationId, notification);
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final String vpnLabel = getArguments().getString(ARG_VPN_LABEL);
            return new AlertDialog.Builder(getActivity())
                    .setTitle(getActivity().getString(R.string.vpn_cant_connect_title, vpnLabel))
                    .setMessage(getActivity().getString(R.string.vpn_cant_connect_message))
                    .setPositiveButton(R.string.okay, null)
                    .create();
        }
    }

    public static class ReplaceExistingVpnFragment extends DialogFragment
            implements DialogInterface.OnClickListener {
        private static final String TAG = "ReplaceExistingVpn";

        public static void show(AppManagementFragment parent) {
            if (parent.getFragmentManager().findFragmentByTag(TAG) == null) {
                final ReplaceExistingVpnFragment frag = new ReplaceExistingVpnFragment();
                frag.setTargetFragment(parent, 0);
            frag.show(parent.getFragmentManager(), null);
                frag.show(parent.getFragmentManager(), TAG);
            }
        }

        @Override