Skip to content

Commit

Permalink
Fix incorrect toast notification when notifications permission is denied
Browse files Browse the repository at this point in the history
  • Loading branch information
c99koder committed Aug 22, 2023
1 parent 38fcf96 commit ee84b4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ android {
namespace "com.irccloud.android"

defaultConfig {
versionCode 354
versionCode 355
versionName "4.31"
minSdkVersion 22
targetSdkVersion 33
Expand Down
45 changes: 29 additions & 16 deletions src/com/irccloud/android/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
import com.irccloud.android.data.model.Buffer;
import com.irccloud.android.data.model.Channel;
import com.irccloud.android.data.model.Event;
import com.irccloud.android.data.model.Notification;
import com.irccloud.android.data.model.Server;
import com.irccloud.android.data.model.User;
import com.irccloud.android.fragment.AcceptListFragment;
Expand Down Expand Up @@ -2277,7 +2278,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}

try {
if (NetworkConnection.getInstance().session != null && NetworkConnection.getInstance().session.length() > 0) {
if (notificationPermissionsGranted() && NetworkConnection.getInstance().session != null && NetworkConnection.getInstance().session.length() > 0) {
FirebaseMessaging.getInstance().getToken().addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(@NonNull String s) {
Expand All @@ -2293,9 +2294,6 @@ public void onSuccess(@NonNull String s) {
sendBtn.setEnabled(conn.getState() == NetworkConnection.STATE_CONNECTED && messageTxt.getText().length() > 0);
photoBtn.setVisibility(bubble ? View.GONE : View.VISIBLE);
IRCCloudLinkMovementMethod.bubble = bubble;

if(!notificationPermissionsGranted())
requestNotificationPermissions(REQUEST_NOTIFICATIONS);
}

CustomTabsServiceConnection mCustomTabsConnection = new CustomTabsServiceConnection() {
Expand Down Expand Up @@ -3591,6 +3589,8 @@ public void run() {
@Override
public void run() {
recreate();
if(!notificationPermissionsGranted())
requestNotificationPermissions(REQUEST_NOTIFICATIONS);
}
});
} else {
Expand All @@ -3607,6 +3607,8 @@ public void run() {
});
if (launchBid == -1 && server == null && conn != null && conn.getUserInfo() != null)
launchBid = conn.getUserInfo().last_selected_bid;
if(!notificationPermissionsGranted())
requestNotificationPermissions(REQUEST_NOTIFICATIONS);
}
break;
case NetworkConnection.EVENT_STATUSCHANGED:
Expand Down Expand Up @@ -4361,22 +4363,29 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
for(int j = 0; j < grantResults.length; j++) {
if(grantResults[j] != PackageManager.PERMISSION_GRANTED) {
IRCCloudLog.Log(Log.ERROR, "IRCCloud", "Permission denied: " + permissions[j]);
if(fileUploadTask != null) {
if(fileUploadTask.metadataDialog != null) {
try {
fileUploadTask.metadataDialog.cancel();
} catch (Exception e) {
if(requestCode == REQUEST_NOTIFICATIONS) {
SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()).edit();
prefs.putString("notify_type", "0");
prefs.commit();
Toast.makeText(this, "Highlight and mention notifications have been disabled", Toast.LENGTH_SHORT).show();
} else {
if (fileUploadTask != null) {
if (fileUploadTask.metadataDialog != null) {
try {
fileUploadTask.metadataDialog.cancel();
} catch (Exception e) {

}
}
fileUploadTask.cancel(true);
fileUploadTask = null;
}
fileUploadTask.cancel(true);
fileUploadTask = null;
}
if(imgurTask != null) {
imgurTask.cancel(true);
imgurTask = null;
if (imgurTask != null) {
imgurTask.cancel(true);
imgurTask = null;
}
Toast.makeText(this, "Upload cancelled: permission denied", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Upload cancelled: permission denied", Toast.LENGTH_SHORT).show();
return;
}
}
Expand Down Expand Up @@ -4466,6 +4475,10 @@ private boolean notificationPermissionsGranted() {
}

private void requestNotificationPermissions(int requestCode) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext());
if(Integer.parseInt(prefs.getString("notify_type", "1")) == 0)
return;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.POST_NOTIFICATIONS},
Expand Down

0 comments on commit ee84b4f

Please sign in to comment.