Skip to content

Commit

Permalink
Added declaration, display and request for new permission POST_NOTIFI…
Browse files Browse the repository at this point in the history
…CATIONS. Refactored permission display code, fixed some warnings in Notification display code.
  • Loading branch information
wolpi committed Dec 17, 2023
1 parent d9adbec commit 930fb5b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 71 deletions.
1 change: 1 addition & 0 deletions primitiveFTPd/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.software.leanback" android:required="false" />
Expand Down
7 changes: 7 additions & 0 deletions primitiveFTPd/res/layout/block_connect_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>

<TextView
android:id="@+id/hasNotificationPermissionTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
</LinearLayout>

</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions primitiveFTPd/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<string name="hasFullAccessToStorage">Hat vollen Speicherzugriff: %1$b</string>
<string name="hasNormalAccessToStorage">Hat (normalen) Speicherzugriff: %1$b</string>
<string name="hasAccessToMediaLocation">Hat Zugriff auf Media-Location (Geo-Daten): %1$b</string>
<string name="hasNotificationPermission">Hat Berechtigung um Benachrichtigungen anzuzeigen: %1$b</string>
<string name="Request">Anfragen</string>
<string name="quickShareFiles">QuickShare Dateien:</string>
<string name="logFiles">Log Dateien:</string>
Expand Down
1 change: 1 addition & 0 deletions primitiveFTPd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<string name="hasFullAccessToStorage">Has full access to storage: %1$b</string>
<string name="hasNormalAccessToStorage">Has (normal) access to storage: %1$b</string>
<string name="hasAccessToMediaLocation">Has access to media location (geo data): %1$b</string>
<string name="hasNotificationPermission">Has permission to show notifications: %1$b</string>
<string name="Request">Request</string>
<string name="quickShareFiles">QuickShare files:</string>
<string name="logFiles">Log files:</string>
Expand Down
8 changes: 3 additions & 5 deletions primitiveFTPd/src/org/primftpd/services/DownloadsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public void onDestroy() {
NotificationUtil.createDownloadNotification(
this,
filename,
localPath,
true,
false,
0,
Expand All @@ -172,6 +171,7 @@ public void onDestroy() {
private void download(String urlStr) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
FileOutputStream fos = null;
try {
httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlStr);
Expand Down Expand Up @@ -213,7 +213,7 @@ private void download(String urlStr) {

// open streams
InputStream inputStream = new BufferedInputStream(response.getEntity().getContent());
FileOutputStream fos = new FileOutputStream(localPath);
fos = new FileOutputStream(localPath);

logger.info("downloading {} to file {}", urlStr, localPath);

Expand All @@ -232,7 +232,6 @@ private void download(String urlStr) {
NotificationUtil.createDownloadNotification(
this,
this.filename,
localPath,
false,
false,
bytes,
Expand All @@ -246,7 +245,6 @@ private void download(String urlStr) {
NotificationUtil.createDownloadNotification(
this,
this.filename,
localPath,
false,
true,
0,
Expand All @@ -260,14 +258,14 @@ private void download(String urlStr) {
NotificationUtil.createDownloadNotification(
this,
filename,
localPath,
true,
false,
0,
0);
} finally {
close(response);
close(httpclient);
close(fos);
}
}

Expand Down
85 changes: 38 additions & 47 deletions primitiveFTPd/src/org/primftpd/ui/PftpdFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

Expand Down Expand Up @@ -521,39 +521,19 @@ protected void showLogindata() {
displayNormalStorageAccess();
displayFullStorageAccess();
displayMediaLocationAccess();
displayNotificationPermission();
}

private final ActivityResultLauncher<String[]> permissionRequestLauncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(),
isGranted -> showLogindata());

private void displayNormalStorageAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View view = getView();
if (view == null) {
return;
}
TextView hasNormalStorageAccessTextView = view.findViewById(R.id.hasNormalStorageAccessTextView);
final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
boolean hasNormalStorageAccess = hasPermission(permission);
String hasNormalStorageAccessStr = getString(R.string.hasNormalAccessToStorage, hasNormalStorageAccess);

if (!hasNormalStorageAccess && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
buildPermissionRequestLink(
hasNormalStorageAccessTextView,
hasNormalStorageAccessStr,
v -> {
logger.debug("PermissionRequestLink onClick");

permissionRequestLauncher.launch(new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
});
}
);

} else {
hasNormalStorageAccessTextView.setText(hasNormalStorageAccessStr);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
displayPermission(
R.id.hasNormalStorageAccessTextView,
R.string.hasNormalAccessToStorage,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}

Expand Down Expand Up @@ -581,28 +561,39 @@ private void displayFullStorageAccess() {
}
private void displayMediaLocationAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
View view = getView();
if (view == null) {
return;
}
TextView hasMediaLocationAccessTextView = view.findViewById(R.id.hasMediaLocationAccessTextView);
final String permission = Manifest.permission.ACCESS_MEDIA_LOCATION;
boolean hasMediaLocationAccess = hasPermission(permission);
String hasMediaLocationStr = getString(R.string.hasAccessToMediaLocation, hasMediaLocationAccess);

if (!hasMediaLocationAccess && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
buildPermissionRequestLink(
hasMediaLocationAccessTextView,
hasMediaLocationStr,
v -> permissionRequestLauncher.launch(new String[] {
Manifest.permission.ACCESS_MEDIA_LOCATION,
}));
} else {
hasMediaLocationAccessTextView.setText(hasMediaLocationStr);
}
displayPermission(
R.id.hasMediaLocationAccessTextView,
R.string.hasAccessToMediaLocation,
Manifest.permission.ACCESS_MEDIA_LOCATION);
}
}

private void displayNotificationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
displayPermission(
R.id.hasNotificationPermissionTextView,
R.string.hasNotificationPermission,
Manifest.permission.POST_NOTIFICATIONS);
}
}

private void displayPermission(int textViewId, int textId, String permission) {
View view = getView();
if (view == null) {
return;
}
boolean hasPermission = hasPermission(permission);
TextView textView = view.findViewById(textViewId);
String hasPermissionStr = getString(textId, hasPermission);
if (!hasPermission) {
buildPermissionRequestLink(
textView,
hasPermissionStr,
v -> permissionRequestLauncher.launch(new String[] {permission}));
} else {
textView.setText(hasPermissionStr);
}
}
private void buildPermissionRequestLink(
TextView textView,
String baseText,
Expand All @@ -619,7 +610,7 @@ protected boolean hasPermission(String permission) {
Context context = getContext();
if (context != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
logger.trace("hasPermission({})", permission);
return ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED;
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
return true;
}
Expand Down
32 changes: 13 additions & 19 deletions primitiveFTPd/src/org/primftpd/util/NotificationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static void createStartStopNotification(Context ctxt) {
START_STOP_CHANNEL_ID,
null);

Notification notification = null;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else {
Expand All @@ -195,7 +195,7 @@ public static Notification createStatusbarNotification(
NOTIFICATION_CHANNEL_ID,
quickShareBean);

Notification notification = null;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (prefsBean.showConnectionInfoInNotification()) {
String longText = buildLongText(ctxt, prefsBean, keyFingerprintProvider);
Expand Down Expand Up @@ -290,10 +290,9 @@ public static void buildUrl(StringBuilder str, boolean ipv6, String scheme, Stri
str.append(port);
}

public static Notification createDownloadNotification(
public static void createDownloadNotification(
Context ctxt,
String filename,
String path,
boolean canceled,
boolean finished,
long currentBytes,
Expand All @@ -312,19 +311,15 @@ public static Notification createDownloadNotification(
tickerText = "finished";
} else {
String sizeStr = size != 0 ? String.valueOf(size) : "unknown";
StringBuilder builder = new StringBuilder();
builder.append("downloading ... (");
builder.append(currentBytes);
builder.append(" / ");
builder.append(sizeStr);
builder.append(")");
builder.append("\n");
builder.append("to ");
builder.append("path");
tickerText = builder.toString();
tickerText = "downloading ... (" +
currentBytes +
" / " +
sizeStr +
")" +
"\n" +
"to " +
filename;
}
CharSequence contentTitle = filename;
CharSequence contentText = tickerText;

// use main icon as large one
Bitmap largeIcon = BitmapFactory.decodeResource(
Expand All @@ -335,8 +330,8 @@ public static Notification createDownloadNotification(

Notification.Builder builder = new Notification.Builder(ctxt)
.setTicker(tickerText)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentTitle(filename)
.setContentText(tickerText)
.setSmallIcon(iconId)
.setLargeIcon(largeIcon)
.setWhen(when)
Expand Down Expand Up @@ -378,6 +373,5 @@ public static Notification createDownloadNotification(
NotificationManager notiMgr = (NotificationManager) ctxt.getSystemService(
Context.NOTIFICATION_SERVICE);
notiMgr.notify(DOWNLOAD_ID, notification);
return notification;
}
}

0 comments on commit 930fb5b

Please sign in to comment.