Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2159 from vector-im/use_notification_privacy_setting
Browse files Browse the repository at this point in the history
Use the notification privacy setting screen
  • Loading branch information
manuroe authored Apr 17, 2018
2 parents 2001f7f + 1320ba0 commit f191fac
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package im.vector.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.MenuItem;
Expand All @@ -31,7 +34,9 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import im.vector.Matrix;
import im.vector.R;
import im.vector.gcm.GcmRegistrationManager;

/*
* This activity allows the user to choose a notifications privacy policy.
Expand Down Expand Up @@ -105,49 +110,39 @@ protected void onCreate(Bundle savedInstanceState) {
tvNoPermission.setVisibility(View.GONE);
}

refreshNotificationPrivacy();

rlyNormalPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rbPrivacyNormal.setChecked(true);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(false);

rbPrivacyNormal.isChecked();
Log.d(LOG_TAG, "RadioButton NotificationPrivacyNormal is selected");

//TODO
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.NORMAL);
refreshNotificationPrivacy();
}
});

rlyLowDetailNotifications.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(true);
rbPrivacyReduced.setChecked(false);

rbPrivacyLowDetail.isChecked();
Log.d(LOG_TAG, "RadioButton NotificationPrivacyLowDetail is selected");

// TODO
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL);
refreshNotificationPrivacy();
}
});

rlyReducedPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(true);

rbPrivacyReduced.isChecked();
Log.d(LOG_TAG, "RadioButton NotificationPrivacyReduced is selected");

//TODO
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.REDUCED);
refreshNotificationPrivacy();
}
});
}

@Override
protected void onResume() {
super.onResume();
refreshNotificationPrivacy();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand All @@ -158,4 +153,77 @@ public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

private void refreshNotificationPrivacy() {
GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(this).getSharedGCMRegistrationManager();

switch (gcmRegistrationManager.getNotificationPrivacy()) {
case REDUCED:
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(true);
break;
case LOW_DETAIL:
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(true);
rbPrivacyReduced.setChecked(false);
break;
case NORMAL:
rbPrivacyNormal.setChecked(true);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(false);
break;
}
}

/**
* Set the new notification privacy setting.
*
* @param activity the activity from which to display the IgnoreBatteryOptimizations permission request dialog, if required
* @param notificationPrivacy the new setting
*/
static public void setNotificationPrivacy(Activity activity, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(activity).getSharedGCMRegistrationManager();

// first, set the new privacy setting
gcmRegistrationManager.setNotificationPrivacy(notificationPrivacy);

// for the "NORMAL" privacy, the app needs to be able to run in background
// this requires the IgnoreBatteryOptimizations permission from android M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.NORMAL) {
// display the system dialog for granting this permission. If previously granted, the
// system will not show it.
// Note: If the user finally does not grant the permission, gcmRegistrationManager.isBackgroundSyncAllowed()
// will return false and the notification privacy will fallback to "LOW_DETAIL".
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
activity.startActivity(intent);
}
}

/**
* Get the displayed i18ned string for a notification privacy setting.
*
* @param context
* @param notificationPrivacy the setting to stringify
* @return a string
*/
static public String getNotificationPrivacyString(Context context, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
String notificationPrivacyString = null;

switch (notificationPrivacy) {
case REDUCED:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_reduced);
break;
case LOW_DETAIL:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_low_detail);
break;
case NORMAL:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_normal);
break;}

return notificationPrivacyString;
}
}
25 changes: 7 additions & 18 deletions vector/src/main/java/im/vector/activity/VectorHomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,7 @@ private void checkNotificationPrivacySetting() {
PreferencesManager.setDidAskUserToIgnoreBatteryOptimizations(this, true);

// by default, use GCM and low detail notifications
gcmMgr.setContentSendingAllowed(false);
gcmMgr.setBackgroundSyncAllowed(false);
gcmMgr.forceSessionsRegistration(null);
gcmMgr.setNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.startup_notification_privacy_title);
Expand All @@ -624,20 +622,10 @@ public void onClick(DialogInterface dialog, int which) {

Log.d(LOG_TAG, "checkNotificationPrivacySetting: user wants to grant the IgnoreBatteryOptimizations permission");

// this is the normal policy on our "Notification Privacy" setting page:
// use GCM, share only meta data with it. Then, background sync to fetch message content
gcmMgr.setContentSendingAllowed(false);
gcmMgr.setBackgroundSyncAllowed(true);
gcmMgr.forceSessionsRegistration(null);

// display the system dialog for granting the IgnoreBatteryOptimizations permission.
// If already granted, the system will not show it.
// Note: If the user finally does not grant the permission, gcmMgr.isBackgroundSyncAllowed()
// will return false and low detail notifications will be displayed.
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
// use NotificationPrivacyActivity in case we need to display the IgnoreBatteryOptimizations
// grant permission dialog
NotificationPrivacyActivity.setNotificationPrivacy(VectorHomeActivity.this,
GcmRegistrationManager.NotificationPrivacy.NORMAL);
}
});

Expand All @@ -647,7 +635,8 @@ public void onClick(DialogInterface dialog, int which) {

Log.d(LOG_TAG, "checkNotificationPrivacySetting: user opens notification policy setting screen");

// @TODO: Audrey: Open the notification policy setting screen
// open the notification policy setting screen
startActivity(NotificationPrivacyActivity.getIntent(VectorHomeActivity.this));
}
});

Expand Down
Loading

0 comments on commit f191fac

Please sign in to comment.