Merge "Make tether settings intent explicit"

This commit is contained in:
Paul Hu
2020-06-08 10:31:21 +00:00
committed by Gerrit Code Review
2 changed files with 44 additions and 5 deletions

View File

@@ -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,

View File

@@ -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))
}
}