Merge changes I0c45635b,I2201d0e4

* changes:
  Remove SystemMessageProto usage
  Replace PendingIntent/NotificationManager @hide APIs
This commit is contained in:
Lorenzo Colitti
2020-01-13 12:04:44 +00:00
committed by Gerrit Code Review
2 changed files with 22 additions and 31 deletions

View File

@@ -107,7 +107,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import com.android.internal.util.MessageUtils; import com.android.internal.util.MessageUtils;
import com.android.internal.util.State; import com.android.internal.util.State;
@@ -663,19 +662,19 @@ public class Tethering {
if (usbTethered) { if (usbTethered) {
if (wifiTethered || bluetoothTethered) { if (wifiTethered || bluetoothTethered) {
showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); showTetheredNotification(R.drawable.stat_sys_tether_general);
} else { } else {
showTetheredNotification(SystemMessage.NOTE_TETHER_USB); showTetheredNotification(R.drawable.stat_sys_tether_usb);
} }
} else if (wifiTethered) { } else if (wifiTethered) {
if (bluetoothTethered) { if (bluetoothTethered) {
showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); showTetheredNotification(R.drawable.stat_sys_tether_general);
} else { } else {
/* We now have a status bar icon for WifiTethering, so drop the notification */ /* We now have a status bar icon for WifiTethering, so drop the notification */
clearTetheredNotification(); clearTetheredNotification();
} }
} else if (bluetoothTethered) { } else if (bluetoothTethered) {
showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH); showTetheredNotification(R.drawable.stat_sys_tether_bluetooth);
} else { } else {
clearTetheredNotification(); clearTetheredNotification();
} }
@@ -688,7 +687,8 @@ public class Tethering {
@VisibleForTesting @VisibleForTesting
protected void showTetheredNotification(int id, boolean tetheringOn) { protected void showTetheredNotification(int id, boolean tetheringOn) {
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) mContext.createContextAsUser(UserHandle.ALL, 0)
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) { if (notificationManager == null) {
return; return;
} }
@@ -698,26 +698,11 @@ public class Tethering {
NotificationManager.IMPORTANCE_LOW); NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
int icon = 0;
switch(id) {
case SystemMessage.NOTE_TETHER_USB:
icon = R.drawable.stat_sys_tether_usb;
break;
case SystemMessage.NOTE_TETHER_BLUETOOTH:
icon = R.drawable.stat_sys_tether_bluetooth;
break;
case SystemMessage.NOTE_TETHER_GENERAL:
default:
icon = R.drawable.stat_sys_tether_general;
break;
}
if (mLastNotificationId != 0) { if (mLastNotificationId != 0) {
if (mLastNotificationId == icon) { if (mLastNotificationId == id) {
return; return;
} }
notificationManager.cancelAsUser(null, mLastNotificationId, notificationManager.cancel(null, mLastNotificationId);
UserHandle.ALL);
mLastNotificationId = 0; mLastNotificationId = 0;
} }
@@ -725,8 +710,8 @@ public class Tethering {
intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0, PendingIntent pi = PendingIntent.getActivity(
null, UserHandle.CURRENT); mContext.createContextAsUser(UserHandle.CURRENT, 0), 0, intent, 0, null);
Resources r = mContext.getResources(); Resources r = mContext.getResources();
final CharSequence title; final CharSequence title;
@@ -749,23 +734,23 @@ public class Tethering {
.setVisibility(Notification.VISIBILITY_PUBLIC) .setVisibility(Notification.VISIBILITY_PUBLIC)
.setCategory(Notification.CATEGORY_STATUS); .setCategory(Notification.CATEGORY_STATUS);
} }
mTetheredNotificationBuilder.setSmallIcon(icon) mTetheredNotificationBuilder.setSmallIcon(id)
.setContentTitle(title) .setContentTitle(title)
.setContentText(message) .setContentText(message)
.setContentIntent(pi); .setContentIntent(pi);
mLastNotificationId = id; mLastNotificationId = id;
notificationManager.notifyAsUser(null, mLastNotificationId, notificationManager.notify(null, mLastNotificationId,
mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL); mTetheredNotificationBuilder.buildInto(new Notification()));
} }
@VisibleForTesting @VisibleForTesting
protected void clearTetheredNotification() { protected void clearTetheredNotification() {
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) mContext.createContextAsUser(UserHandle.ALL, 0)
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null && mLastNotificationId != 0) { if (notificationManager != null && mLastNotificationId != 0) {
notificationManager.cancelAsUser(null, mLastNotificationId, notificationManager.cancel(null, mLastNotificationId);
UserHandle.ALL);
mLastNotificationId = 0; mLastNotificationId = 0;
} }
} }

View File

@@ -224,6 +224,11 @@ public class TetheringTest {
if (TelephonyManager.class.equals(serviceClass)) return Context.TELEPHONY_SERVICE; if (TelephonyManager.class.equals(serviceClass)) return Context.TELEPHONY_SERVICE;
return super.getSystemServiceName(serviceClass); return super.getSystemServiceName(serviceClass);
} }
@Override
public Context createContextAsUser(UserHandle user, int flags) {
return mContext;
}
} }
public class MockIpServerDependencies extends IpServer.Dependencies { public class MockIpServerDependencies extends IpServer.Dependencies {
@@ -432,6 +437,7 @@ public class TetheringTest {
.thenReturn(true); .thenReturn(true);
mServiceContext = new TestContext(mContext); mServiceContext = new TestContext(mContext);
when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(null);
mContentResolver = new MockContentResolver(mServiceContext); mContentResolver = new MockContentResolver(mServiceContext);
mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0); Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0);