|
|
@ -18,14 +18,12 @@ package org.transdroid.core.gui.navigation; |
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
|
import android.Manifest; |
|
|
|
import android.annotation.SuppressLint; |
|
|
|
import android.annotation.SuppressLint; |
|
|
|
import android.annotation.TargetApi; |
|
|
|
|
|
|
|
import android.app.Activity; |
|
|
|
import android.app.Activity; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.content.pm.PackageManager; |
|
|
|
import android.content.pm.ResolveInfo; |
|
|
|
import android.content.pm.ResolveInfo; |
|
|
|
import android.net.Uri; |
|
|
|
import android.net.Uri; |
|
|
|
import android.os.Build; |
|
|
|
|
|
|
|
import android.text.Spannable; |
|
|
|
import android.text.Spannable; |
|
|
|
import android.text.SpannableString; |
|
|
|
import android.text.SpannableString; |
|
|
|
import android.text.style.TypefaceSpan; |
|
|
|
import android.text.style.TypefaceSpan; |
|
|
@ -60,9 +58,7 @@ import java.util.List; |
|
|
|
@EBean |
|
|
|
@EBean |
|
|
|
public class NavigationHelper { |
|
|
|
public class NavigationHelper { |
|
|
|
|
|
|
|
|
|
|
|
private static final int REQUEST_TORRENT_READ_PERMISSION = 0; |
|
|
|
private static final int REQUEST_NOTIFICATIONS_PERMISSION = 0; |
|
|
|
private static final int REQUEST_SETTINGS_READ_PERMISSION = 1; |
|
|
|
|
|
|
|
private static final int REQUEST_SETTINGS_WRITE_PERMISSION = 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static ImageLoader imageCache; |
|
|
|
private static ImageLoader imageCache; |
|
|
|
@RootContext |
|
|
|
@RootContext |
|
|
@ -134,7 +130,11 @@ public class NavigationHelper { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean checkPermission(final Activity activity, final String permission, final int requestCode) { |
|
|
|
public boolean checkOrRequestNotificationPermission(final Activity activity) { |
|
|
|
|
|
|
|
return checkPermission(activity, Manifest.permission.POST_NOTIFICATIONS, REQUEST_NOTIFICATIONS_PERMISSION, R.string.permission_notifications); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean checkPermission(final Activity activity, final String permission, final int requestCode, final int explainer) { |
|
|
|
if (hasPermission(permission)) |
|
|
|
if (hasPermission(permission)) |
|
|
|
// Permission already granted
|
|
|
|
// Permission already granted
|
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -142,43 +142,27 @@ public class NavigationHelper { |
|
|
|
// Never asked again: show a dialog with an explanation
|
|
|
|
// Never asked again: show a dialog with an explanation
|
|
|
|
activity.runOnUiThread(() -> |
|
|
|
activity.runOnUiThread(() -> |
|
|
|
new AlertDialog.Builder(context) |
|
|
|
new AlertDialog.Builder(context) |
|
|
|
.setMessage(R.string.permission_readtorrent) |
|
|
|
.setMessage(explainer) |
|
|
|
.setPositiveButton(android.R.string.ok, (dialog, which) -> |
|
|
|
.setPositiveButton(android.R.string.ok, (dialog, which) -> |
|
|
|
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode)) |
|
|
|
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode)) |
|
|
|
.show()); |
|
|
|
.show()); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
// Permission not granted (and we asked for it already before)
|
|
|
|
// Permission not granted (and we asked for it already before)
|
|
|
|
ActivityCompat.requestPermissions(activity, new String[]{permission}, REQUEST_TORRENT_READ_PERMISSION); |
|
|
|
ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean hasPermission(String requiredPermission) { |
|
|
|
public boolean hasPermission(String requiredPermission) { |
|
|
|
return ContextCompat.checkSelfPermission(context, requiredPermission) == PackageManager.PERMISSION_GRANTED; |
|
|
|
return ContextCompat.checkSelfPermission(context, requiredPermission) == PackageManager.PERMISSION_GRANTED; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Boolean handleTorrentReadPermissionResult(int requestCode, int[] grantResults) { |
|
|
|
public Boolean handleNotificationPermissionResult(int requestCode, int[] grantResults) { |
|
|
|
if (requestCode == REQUEST_TORRENT_READ_PERMISSION) { |
|
|
|
if (requestCode == REQUEST_NOTIFICATIONS_PERMISSION) { |
|
|
|
// Return permission granting result
|
|
|
|
|
|
|
|
return grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean handleSettingsReadPermissionResult(int requestCode, int[] grantResults) { |
|
|
|
|
|
|
|
if (requestCode == REQUEST_SETTINGS_READ_PERMISSION) { |
|
|
|
|
|
|
|
// Return permission granting result
|
|
|
|
// Return permission granting result
|
|
|
|
return grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
|
|
|
return grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean handleSettingsWritePermissionResult(int requestCode, int[] grantResults) { |
|
|
|
|
|
|
|
if (requestCode == REQUEST_SETTINGS_WRITE_PERMISSION) { |
|
|
|
|
|
|
|
// Return permission granting result
|
|
|
|
|
|
|
|
return grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|