-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: No prompt to enable notifications #603
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e1bed8
Update censorship measurement prompt to show in new activity
aanorbel a76e563
added notification prompt for test progress
aanorbel b5df094
Update preference fragment to request for notification permission
aanorbel db3db7b
Updated class based on review comments
aanorbel a2033a0
Merge branch 'master' of github.com:ooni/probe-android into issues/25…
aanorbel 2497e56
Removed unrequired todo
aanorbel f3a9129
Merge branch 'master' of github.com:ooni/probe-android into issues/25…
aanorbel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletions
209
app/src/main/java/org/openobservatory/ooniprobe/activity/PromptActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package org.openobservatory.ooniprobe.activity; | ||
|
||
|
||
import android.Manifest; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
|
||
import androidx.activity.result.ActivityResultLauncher; | ||
import androidx.activity.result.contract.ActivityResultContracts; | ||
import androidx.annotation.StringRes; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import com.google.android.material.snackbar.Snackbar; | ||
|
||
import org.openobservatory.ooniprobe.R; | ||
import org.openobservatory.ooniprobe.databinding.ActivityPromptBinding; | ||
import org.openobservatory.ooniprobe.domain.UpdatesNotificationManager; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class PromptActivity extends AbstractActivity { | ||
private static final String PROMPT_ITEM = "promptItem"; | ||
@Inject | ||
UpdatesNotificationManager notificationManager; | ||
private ActivityPromptBinding binding; | ||
private Prompt prompt; | ||
|
||
private ActivityResultLauncher<String> requestPermissionLauncher; | ||
|
||
public static Intent newIntent(Context context, Prompt prompt) { | ||
return new Intent(context, PromptActivity.class).putExtra(PROMPT_ITEM, prompt); | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
getActivityComponent().inject(this); | ||
binding = ActivityPromptBinding.inflate(getLayoutInflater()); | ||
setContentView(binding.getRoot()); | ||
|
||
prompt = (Prompt) getIntent().getExtras().get(PROMPT_ITEM); | ||
binding.title.setText(prompt.title); | ||
binding.description.setText(prompt.paragraph); | ||
|
||
registerPermissionRequest(); | ||
setUpClickListeners(); | ||
} | ||
|
||
private void registerPermissionRequest() { | ||
requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), (result) -> { | ||
if (!result) { | ||
Snackbar.make(binding.getRoot(), "Please grant Notification permission from App Settings", Snackbar.LENGTH_LONG).setAction(R.string.Settings_Title, view -> { | ||
Intent intent = new Intent(); | ||
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); | ||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
|
||
//for Android 5-7 | ||
intent.putExtra("app_package", getPackageName()); | ||
intent.putExtra("app_uid", getApplicationInfo().uid); | ||
|
||
// for Android 8 and above | ||
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName()); | ||
|
||
startActivity(intent); | ||
}).show(); | ||
} | ||
setResult(result ? Activity.RESULT_OK : Activity.RESULT_CANCELED); | ||
finish(); | ||
}); | ||
} | ||
|
||
private void setUpClickListeners() { | ||
OnPromptAction actions; | ||
switch (prompt) { | ||
case CENSORSHIP_CONSENT: | ||
actions = new InternetCensorshipConsentActions(); | ||
break; | ||
case TEST_PROGRESS_CONSENT: | ||
actions = new TestProgressNotificationConsentActions(); | ||
break; | ||
default: | ||
actions = new OnPromptAction() { | ||
@Override | ||
public void onClickPositive(View view) { | ||
} | ||
|
||
@Override | ||
public void onClickNeutral(View view) { | ||
} | ||
|
||
@Override | ||
public void onClickNegative(View view) { | ||
} | ||
aanorbel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
binding.soundsGreat.setOnClickListener(actions::onClickPositive); | ||
binding.notNow.setOnClickListener(actions::onClickNeutral); | ||
binding.dontAskAgain.setOnClickListener(actions::onClickNegative); | ||
} | ||
|
||
public void requestNotificationPermission() { | ||
if (ContextCompat.checkSelfPermission( | ||
this, Manifest.permission.POST_NOTIFICATIONS) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); | ||
} | ||
} | ||
} | ||
|
||
enum Prompt { | ||
CENSORSHIP_CONSENT(R.string.Modal_EnableNotifications_Title, R.string.Modal_EnableNotifications_Paragraph), | ||
TEST_PROGRESS_CONSENT(R.string.Prompt_EnableTestProgressNotifications_Title, R.string.Prompt_EnableTestProgressNotifications_Paragraph); | ||
|
||
private final int title; | ||
private final int paragraph; | ||
|
||
Prompt(@StringRes int title, @StringRes int paragraph) { | ||
this.title = title; | ||
this.paragraph = paragraph; | ||
} | ||
} | ||
|
||
interface OnPromptAction { | ||
/** | ||
* Callback for View#onClick of `soundsGreat` button. | ||
* | ||
* @param view | ||
*/ | ||
void onClickPositive(View view); | ||
|
||
/** | ||
* Callback for View#onClick of `notNow` button. | ||
* | ||
* @param view | ||
*/ | ||
void onClickNeutral(View view); | ||
|
||
/** | ||
* Callback for View#onClick of `dontAskAgain` button. | ||
* | ||
* @param view | ||
*/ | ||
void onClickNegative(View view); | ||
} | ||
|
||
public class InternetCensorshipConsentActions implements OnPromptAction { | ||
aanorbel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public void onClickPositive(View view) { | ||
notificationManager.getUpdates(true); | ||
if (ContextCompat.checkSelfPermission( | ||
PromptActivity.this, | ||
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){ | ||
PromptActivity.this.requestNotificationPermission(); | ||
} else { | ||
setResult(Activity.RESULT_OK); | ||
finish(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClickNeutral(View view) { | ||
PromptActivity.this.setResult(Activity.RESULT_CANCELED); | ||
PromptActivity.this.finish(); | ||
} | ||
|
||
@Override | ||
public void onClickNegative(View view) { | ||
notificationManager.disableAskNotificationDialog(); | ||
onClickNeutral(view); | ||
} | ||
} | ||
|
||
public class TestProgressNotificationConsentActions implements OnPromptAction { | ||
aanorbel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public void onClickPositive(View view) { | ||
notificationManager.setTestProgressNotificationConsent(true); | ||
if (ContextCompat.checkSelfPermission( | ||
PromptActivity.this, | ||
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){ | ||
PromptActivity.this.requestNotificationPermission(); | ||
} else { | ||
setResult(Activity.RESULT_OK); | ||
finish(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClickNeutral(View view) { | ||
PromptActivity.this.setResult(Activity.RESULT_CANCELED); | ||
PromptActivity.this.finish(); | ||
} | ||
|
||
@Override | ||
public void onClickNegative(View view) { | ||
notificationManager.disableAskTestProgressNotificationConsent(); | ||
onClickNeutral(view); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we need to do this before merging