-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: No prompt to enable notifications (#603)
Fixes ooni/probe#2507 ## Proposed Changes - Create views to match designs ( https://www.figma.com/file/G0MMzbEyAUEYpAgV9ZEzTC/OONI-Probe-Mobile-Android?type=design&node-id=48-900&mode=design&t=NPmXdEojkX3gqQRg-0 ) - Add action to request for system notification when `Sounds great` is clicked. - Update preference fragment to request for system notification if option is toggled. | .| . | . | . | . | |-|-| -| -|-| | ![Screenshot_20230822_202406](https://github.com/ooni/probe-android/assets/17911892/cbbfa6cf-e288-40c2-92aa-f41937e20a9c) | ![Screenshot_20230822_202514](https://github.com/ooni/probe-android/assets/17911892/1f1d2d54-8923-4de8-97dd-8cb7267b865f) | ![Screenshot_20230823_140954](https://github.com/ooni/probe-android/assets/17911892/e1a1e55c-14d1-4a68-8fb4-d2b22178b60d) | ![Screenshot_20230822_202527](https://github.com/ooni/probe-android/assets/17911892/c242648f-0723-4b39-90fa-04e6d3254228) | ![Screenshot_20230823_140044](https://github.com/ooni/probe-android/assets/17911892/da76f553-d37d-4225-bb55-a9fd6fde640a) |
- Loading branch information
Showing
13 changed files
with
516 additions
and
68 deletions.
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
206 changes: 206 additions & 0 deletions
206
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,206 @@ | ||
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) { /*No Implementation*/ } | ||
|
||
@Override | ||
public void onClickNeutral(View view) { /*No Implementation*/ } | ||
|
||
@Override | ||
public void onClickNegative(View view) { /*No Implementation*/ } | ||
}; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
public 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 The view that was clicked. | ||
*/ | ||
void onClickPositive(View view); | ||
|
||
/** | ||
* Callback for View#onClick of `notNow` button. | ||
* | ||
* @param view The view that was clicked. | ||
*/ | ||
void onClickNeutral(View view); | ||
|
||
/** | ||
* Callback for View#onClick of `dontAskAgain` button. | ||
* | ||
* @param view The view that was clicked. | ||
*/ | ||
void onClickNegative(View view); | ||
} | ||
|
||
private class InternetCensorshipConsentActions implements OnPromptAction { | ||
|
||
@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); | ||
} | ||
} | ||
|
||
private class TestProgressNotificationConsentActions implements OnPromptAction { | ||
|
||
@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
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.