show warning in account details when data saver is enabled

This commit is contained in:
Daniel Gultsch 2016-11-07 10:49:43 +01:00
parent d369ec767f
commit 9edbddd7e1
4 changed files with 75 additions and 29 deletions

View File

@ -35,8 +35,6 @@ import android.widget.TableRow;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.util.Log;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -64,17 +62,20 @@ import eu.siacs.conversations.xmpp.pep.Avatar;
public class EditAccountActivity extends XmppActivity implements OnAccountUpdate, public class EditAccountActivity extends XmppActivity implements OnAccountUpdate,
OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched { OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched {
private static final int REQUEST_DATA_SAVER = 0x37af244;
private AutoCompleteTextView mAccountJid; private AutoCompleteTextView mAccountJid;
private EditText mPassword; private EditText mPassword;
private EditText mPasswordConfirm; private EditText mPasswordConfirm;
private CheckBox mRegisterNew; private CheckBox mRegisterNew;
private Button mCancelButton; private Button mCancelButton;
private Button mSaveButton; private Button mSaveButton;
private Button mDisableBatterOptimizations; private Button mDisableOsOptimizationsButton;
private TextView mDisableOsOptimizationsHeadline;
private TextView getmDisableOsOptimizationsBody;
private TableLayout mMoreTable; private TableLayout mMoreTable;
private LinearLayout mStats; private LinearLayout mStats;
private RelativeLayout mBatteryOptimizations; private RelativeLayout mOsOptimizations;
private TextView mServerInfoSm; private TextView mServerInfoSm;
private TextView mServerInfoRosterVersion; private TextView mServerInfoRosterVersion;
private TextView mServerInfoCarbons; private TextView mServerInfoCarbons;
@ -373,7 +374,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_BATTERY_OP) { if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) {
updateAccountInformation(mAccount == null); updateAccountInformation(mAccount == null);
} }
} }
@ -472,21 +473,10 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
this.mAvatar.setOnClickListener(this.mAvatarClickListener); this.mAvatar.setOnClickListener(this.mAvatarClickListener);
this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new); this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
this.mStats = (LinearLayout) findViewById(R.id.stats); this.mStats = (LinearLayout) findViewById(R.id.stats);
this.mBatteryOptimizations = (RelativeLayout) findViewById(R.id.battery_optimization); this.mOsOptimizations = (RelativeLayout) findViewById(R.id.os_optimization);
this.mDisableBatterOptimizations = (Button) findViewById(R.id.batt_op_disable); this.mDisableOsOptimizationsButton = (Button) findViewById(R.id.os_optimization_disable);
this.mDisableBatterOptimizations.setOnClickListener(new OnClickListener() { this.mDisableOsOptimizationsHeadline = (TextView) findViewById(R.id.os_optimization_headline);
@Override this.getmDisableOsOptimizationsBody = (TextView) findViewById(R.id.os_optimization_body);
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
Uri uri = Uri.parse("package:"+getPackageName());
intent.setData(uri);
try {
startActivityForResult(intent, REQUEST_BATTERY_OP);
} catch (ActivityNotFoundException e) {
Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
}
}
});
this.mSessionEst = (TextView) findViewById(R.id.session_est); this.mSessionEst = (TextView) findViewById(R.id.session_est);
this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version); this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons); this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
@ -796,8 +786,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) { if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
Features features = this.mAccount.getXmppConnection().getFeatures(); Features features = this.mAccount.getXmppConnection().getFeatures();
this.mStats.setVisibility(View.VISIBLE); this.mStats.setVisibility(View.VISIBLE);
boolean showOptimizingWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery(); boolean showBatteryWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
this.mBatteryOptimizations.setVisibility(showOptimizingWarning ? View.VISIBLE : View.GONE); boolean showDataSaverWarning = isAffectedByDataSaver();
showOsOptimizationWarning(showBatteryWarning,showDataSaverWarning);
this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection() this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
.getLastSessionEstablished())); .getLastSessionEstablished()));
if (features.rosterVersioning()) { if (features.rosterVersioning()) {
@ -956,6 +947,45 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
} }
} }
private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) {
this.mOsOptimizations.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE);
if (showDataSaverWarning) {
this.mDisableOsOptimizationsHeadline.setText(R.string.data_saver_enabled);
this.getmDisableOsOptimizationsBody.setText(R.string.data_saver_enabled_explained);
this.mDisableOsOptimizationsButton.setText(R.string.allow);
this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS);
Uri uri = Uri.parse("package:"+getPackageName());
intent.setData(uri);
try {
startActivityForResult(intent, REQUEST_DATA_SAVER);
} catch (ActivityNotFoundException e) {
Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_data_saver, Toast.LENGTH_SHORT).show();
}
}
});
} else if (showBatteryWarning) {
this.mDisableOsOptimizationsButton.setText(R.string.disable);
this.mDisableOsOptimizationsHeadline.setText(R.string.battery_optimizations_enabled);
this.getmDisableOsOptimizationsBody.setText(R.string.battery_optimizations_enabled_explained);
this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
Uri uri = Uri.parse("package:"+getPackageName());
intent.setData(uri);
try {
startActivityForResult(intent, REQUEST_BATTERY_OP);
} catch (ActivityNotFoundException e) {
Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
}
}
});
}
}
public void showRegenerateAxolotlKeyDialog() { public void showRegenerateAxolotlKeyDialog() {
Builder builder = new Builder(this); Builder builder = new Builder(this);
builder.setTitle("Regenerate Key"); builder.setTitle("Regenerate Key");

View File

@ -28,6 +28,7 @@ import android.graphics.Color;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.nfc.NdefMessage; import android.nfc.NdefMessage;
import android.nfc.NdefRecord; import android.nfc.NdefRecord;
@ -446,6 +447,16 @@ public abstract class XmppActivity extends Activity {
} }
} }
protected boolean isAffectedByDataSaver() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.isActiveNetworkMetered()
&& cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
} else {
return false;
}
}
protected boolean usingEnterKey() { protected boolean usingEnterKey() {
return getPreferences().getBoolean("display_enter_key", false); return getPreferences().getBoolean("display_enter_key", false);
} }

View File

@ -163,7 +163,7 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/battery_optimization" android:id="@+id/os_optimization"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginBottom="@dimen/activity_vertical_margin"
@ -175,7 +175,7 @@
android:padding="@dimen/infocard_padding" android:padding="@dimen/infocard_padding"
android:visibility="gone"> android:visibility="gone">
<TextView <TextView
android:id="@+id/batt_op_headline" android:id="@+id/os_optimization_headline"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/battery_optimizations_enabled" android:text="@string/battery_optimizations_enabled"
@ -183,25 +183,27 @@
android:textSize="?attr/TextSizeHeadline" android:textSize="?attr/TextSizeHeadline"
android:textStyle="bold"/> android:textStyle="bold"/>
<TextView <TextView
android:id="@+id/batt_op_body" android:id="@+id/os_optimization_body"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/batt_op_headline" android:layout_below="@+id/os_optimization_headline"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="@string/battery_optimizations_enabled_explained" android:text="@string/battery_optimizations_enabled_explained"
android:textColor="?attr/color_text_primary" android:textColor="?attr/color_text_primary"
android:textSize="?attr/TextSizeBody"/> android:textSize="?attr/TextSizeBody"/>
<Button <Button
android:id="@+id/batt_op_disable" android:id="@+id/os_optimization_disable"
style="?android:attr/borderlessButtonStyle" style="?android:attr/borderlessButtonStyle"
android:layout_marginRight="-8dp" android:layout_marginRight="-8dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_below="@+id/batt_op_body" android:layout_alignParentRight="true"
android:text="@string/disable" android:textColor="@color/accent"/> android:layout_below="@+id/os_optimization_body"
android:text="@string/disable"
android:textColor="@color/accent"/>
</RelativeLayout> </RelativeLayout>

View File

@ -696,4 +696,7 @@
<string name="error_publish_avatar_offline">You need to be connected to publish your avatar.</string> <string name="error_publish_avatar_offline">You need to be connected to publish your avatar.</string>
<string name="show_error_message">Show error message</string> <string name="show_error_message">Show error message</string>
<string name="error_message">Error Message</string> <string name="error_message">Error Message</string>
<string name="data_saver_enabled">Data saver enabled</string>
<string name="data_saver_enabled_explained">Your operating system is restricting Conversations from accessing the Internet when in background. To receive notifications of new messages you should allow Conversations unrestricted access when Data saver is on.\nConversations will still make an effort to save data when possible.</string>
<string name="device_does_not_support_data_saver">Your device does not supporting disabling Data saver for Conversations.</string>
</resources> </resources>