Add Notification Permission for T

From T, apps need POST_NOTIFICATION permission
to send the notification.

-. Add the permission into Manifest.
-. Request runtime permission when it's needed.

Bug: 209479751
Test: Manual test as following steps:
      1. Open EditTextVariations at T device.
      2. Tap direct reply to send notification.
      3. Verify the permission dialog show up.
      4. Send the notification if permission granted.
Change-Id: Iadae00f635ee556d1d1de9abbb9da0498b4070f1
This commit is contained in:
Wilson Wu 2022-01-13 17:32:41 +08:00
parent 60f0c0d99c
commit 75f77d6666
2 changed files with 41 additions and 1 deletions

View File

@ -18,6 +18,7 @@
package="com.android.inputmethod.tools.edittextvariations"
android:versionName="0.67"
android:versionCode="67">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<supports-screens android:resizeable="true"/>
<uses-sdk android:targetSdkVersion="27"
android:minSdkVersion="11"/>

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.tools.edittextvariations;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
@ -24,6 +25,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
@ -45,7 +47,9 @@ import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@ -64,6 +68,7 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi
private static final String PREF_THEME = "theme";
private static final String PREF_NAVIGATE = "navigate";
private static final String PREF_SOFTINPUT = "softinput";
private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 0;
private SharedPreferences prefs;
private View[] fields;
@ -199,11 +204,35 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi
saveSoftInputMode(itemId == MENU_SOFTINPUT_VISIBLE);
restartActivity();
} else if (itemId == MENU_DIRECT_REPLY) {
NotificationUtils.sendDirectReplyNotification(this);
final boolean needPermissionCheck = isNeedNotificationPermission()
&& checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED;
if (needPermissionCheck) {
requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS },
NOTIFICATION_PERMISSION_REQUEST_CODE);
} else {
NotificationUtils.sendDirectReplyNotification(this);
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == NOTIFICATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted. Continue to send the notification.
NotificationUtils.sendDirectReplyNotification(this);
} else {
Log.d(TAG, "POST_NOTIFICATIONS Permissions denied.");
Toast.makeText(this, "Required permission has denied",
Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
saveTheme(ThemeItem.THEME_LIST.get(which));
@ -476,4 +505,14 @@ public final class EditTextVariations extends Activity implements TextView.OnEdi
}
return text;
}
private static boolean isNeedNotificationPermission() {
for(Field field : Manifest.permission.class.getFields()) {
if (field.getName().equals("POST_NOTIFICATIONS")) {
Log.d(TAG, "Need notification permission.");
return true;
}
}
return false;
}
}