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

Commit 720a4dac authored by Paul Hu's avatar Paul Hu Committed by Gerrit Code Review
Browse files

Merge "Make tether settings intent explicit"

parents 90712460 05ebcd34
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -24,8 +24,10 @@ import android.app.Notification.Action;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.NetworkCapabilities;
@@ -252,6 +254,14 @@ public class TetheringNotificationUpdater {
        mNotificationManager.cancel(null /* tag */, id);
    }

    @VisibleForTesting
    static String getSettingsPackageName(@NonNull final PackageManager pm) {
        final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
        final ComponentName settingsComponent = settingsIntent.resolveActivity(pm);
        return settingsComponent != null
                ? settingsComponent.getPackageName() : "com.android.settings";
    }

    @VisibleForTesting
    void notifyTetheringDisabledByRestriction() {
        final Resources res = getResourcesForSubId(mContext, mActiveDataSubId);
@@ -262,8 +272,9 @@ public class TetheringNotificationUpdater {
        final PendingIntent pi = PendingIntent.getActivity(
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                new Intent(Settings.ACTION_TETHER_SETTINGS),
                Intent.FLAG_ACTIVITY_NEW_TASK,
                new Intent(Settings.ACTION_TETHER_SETTINGS)
                        .setPackage(getSettingsPackageName(mContext.getPackageManager())),
                Intent.FLAG_ACTIVITY_NEW_TASK | PendingIntent.FLAG_IMMUTABLE,
                null /* options */);

        showNotification(R.drawable.stat_sys_tether_general, title, message,
@@ -284,7 +295,7 @@ public class TetheringNotificationUpdater {
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                intent,
                0 /* flags */);
                PendingIntent.FLAG_IMMUTABLE);
        final Action action = new Action.Builder(NO_ICON_ID, disableButton, pi).build();

        showNotification(R.drawable.stat_sys_tether_general, title, message,
@@ -305,8 +316,9 @@ public class TetheringNotificationUpdater {
        final PendingIntent pi = PendingIntent.getActivity(
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                new Intent(Settings.ACTION_TETHER_SETTINGS),
                Intent.FLAG_ACTIVITY_NEW_TASK,
                new Intent(Settings.ACTION_TETHER_SETTINGS)
                        .setPackage(getSettingsPackageName(mContext.getPackageManager())),
                Intent.FLAG_ACTIVITY_NEW_TASK | PendingIntent.FLAG_IMMUTABLE,
                null /* options */);

        showNotification(R.drawable.stat_sys_tether_general, title, message,
+27 −0
Original line number Diff line number Diff line
@@ -19,6 +19,10 @@ package com.android.networkstack.tethering
import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.res.Resources
import android.net.ConnectivityManager.TETHERING_WIFI
import android.os.Handler
@@ -51,6 +55,7 @@ import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.times
@@ -351,4 +356,26 @@ class TetheringNotificationUpdaterTest {
        notificationUpdater.onUpstreamCapabilitiesChanged(ROAMING_CAPABILITIES)
        verifyNotificationCancelled(listOf(NO_UPSTREAM_NOTIFICATION_ID, ROAMING_NOTIFICATION_ID))
    }

    @Test
    fun testGetSettingsPackageName() {
        val defaultSettingsPackageName = "com.android.settings"
        val testSettingsPackageName = "com.android.test.settings"
        val pm = mock(PackageManager::class.java)
        doReturn(null).`when`(pm).resolveActivity(any(), anyInt())
        assertEquals(defaultSettingsPackageName,
                TetheringNotificationUpdater.getSettingsPackageName(pm))

        val resolveInfo = ResolveInfo().apply {
            activityInfo = ActivityInfo().apply {
                name = "test"
                applicationInfo = ApplicationInfo().apply {
                    packageName = testSettingsPackageName
                }
            }
        }
        doReturn(resolveInfo).`when`(pm).resolveActivity(any(), anyInt())
        assertEquals(testSettingsPackageName,
                TetheringNotificationUpdater.getSettingsPackageName(pm))
    }
}