ask to be excluded from battery optimization

This commit is contained in:
Daniel Gultsch 2016-01-11 11:17:45 +01:00
parent 95db6db935
commit c296d6f446
7 changed files with 362 additions and 243 deletions

View File

@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission
android:name="android.permission.READ_PHONE_STATE"

View File

@ -16,6 +16,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.util.Log;
@ -36,6 +37,8 @@ import android.widget.Toast;
import net.java.otr4j.session.SessionStatus;
import org.openintents.openpgp.util.OpenPgpApi;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -61,7 +64,6 @@ import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import org.openintents.openpgp.util.OpenPgpApi;
public class ConversationActivity extends XmppActivity
implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
@ -969,7 +971,8 @@ public class ConversationActivity extends XmppActivity
return true;
} else if (modifier && key == downKey) {
if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
showConversationsOverview();;
showConversationsOverview();
;
}
return selectDownConversation();
} else if (modifier && key == upKey) {
@ -1196,7 +1199,9 @@ public class ConversationActivity extends XmppActivity
}
forbidProcessingPendings = false;
ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService)) {
openBatteryOptimizationDialogIfNeeded();
}
setIntent(new Intent());
}
@ -1272,8 +1277,7 @@ public class ConversationActivity extends XmppActivity
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_DECRYPT_PGP) {
@ -1344,6 +1348,39 @@ public class ConversationActivity extends XmppActivity
if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
mConversationFragment.onActivityResult(requestCode, resultCode, data);
}
if (requestCode == REQUEST_BATTERY_OP) {
setNeverAskForBatteryOptimizationsAgain();
}
}
}
private void setNeverAskForBatteryOptimizationsAgain() {
getPreferences().edit().putBoolean("show_battery_optimization", false).commit();
}
private void openBatteryOptimizationDialogIfNeeded() {
if (showBatteryOptimizationWarning() && getPreferences().getBoolean("show_battery_optimization", true)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.battery_optimizations_enabled);
builder.setMessage(R.string.battery_optimizations_enabled_dialog);
builder.setPositiveButton(R.string.next, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
Uri uri = Uri.parse("package:" + getPackageName());
intent.setData(uri);
startActivityForResult(intent, REQUEST_BATTERY_OP);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
setNeverAskForBatteryOptimizationsAgain();
}
});
}
builder.create().show();
}
}

View File

@ -5,9 +5,10 @@ import android.app.AlertDialog.Builder;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.security.KeyChain;
import android.security.KeyChainAliasCallback;
import android.text.Editable;
@ -52,16 +53,17 @@ import eu.siacs.conversations.xmpp.pep.Avatar;
public class EditAccountActivity extends XmppActivity implements OnAccountUpdate,
OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast {
private LinearLayout mMainLayout;
private AutoCompleteTextView mAccountJid;
private EditText mPassword;
private EditText mPasswordConfirm;
private CheckBox mRegisterNew;
private Button mCancelButton;
private Button mSaveButton;
private Button mDisableBatterOptimizations;
private TableLayout mMoreTable;
private LinearLayout mStats;
private RelativeLayout mBatteryOptimizations;
private TextView mServerInfoSm;
private TextView mServerInfoRosterVersion;
private TextView mServerInfoCarbons;
@ -311,6 +313,14 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_BATTERY_OP) {
updateAccountInformation(mAccount == null);
}
}
protected void updateSaveButton() {
if (accountInfoEdited() && !mInitMode) {
this.mSaveButton.setText(R.string.save);
@ -372,7 +382,6 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_account);
this.mMainLayout = (LinearLayout) findViewById(R.id.account_main_layout);
this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
this.mAccountJid.addTextChangedListener(this.mTextWatcher);
this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
@ -387,6 +396,17 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
this.mAvatar.setOnClickListener(this.mAvatarClickListener);
this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
this.mStats = (LinearLayout) findViewById(R.id.stats);
this.mBatteryOptimizations = (RelativeLayout) findViewById(R.id.battery_optimization);
this.mDisableBatterOptimizations = (Button) findViewById(R.id.batt_op_disable);
this.mDisableBatterOptimizations.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);
startActivityForResult(intent,REQUEST_BATTERY_OP);
}
});
this.mSessionEst = (TextView) findViewById(R.id.session_est);
this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
@ -595,6 +615,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
}
if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
this.mStats.setVisibility(View.VISIBLE);
this.mBatteryOptimizations.setVisibility(showBatteryOptimizationWarning() ? View.VISIBLE : View.GONE);
this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
.getLastSessionEstablished()));
Features features = this.mAccount.getXmppConnection().getFeatures();

View File

@ -35,6 +35,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.InputType;
@ -91,6 +92,7 @@ public abstract class XmppActivity extends Activity {
protected static final int REQUEST_ANNOUNCE_PGP = 0x0101;
protected static final int REQUEST_INVITE_TO_CONVERSATION = 0x0102;
protected static final int REQUEST_CHOOSE_PGP_ID = 0x0103;
protected static final int REQUEST_BATTERY_OP = 0x13849ff;
public XmppConnectionService xmppConnectionService;
public boolean xmppConnectionServiceBound = false;
@ -372,6 +374,15 @@ public abstract class XmppActivity extends Activity {
}
}
protected boolean showBatteryOptimizationWarning() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
return !pm.isIgnoringBatteryOptimizations(getPackageName());
} else {
return false;
}
}
protected boolean usingEnterKey() {
return getPreferences().getBoolean("display_enter_key", false);
}

View File

@ -24,6 +24,7 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
@ -35,14 +36,13 @@ public class ExceptionHelper {
}
}
public static void checkForCrash(Context context,
final XmppConnectionService service) {
public static boolean checkForCrash(ConversationActivity activity, final XmppConnectionService service) {
try {
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
.getDefaultSharedPreferences(activity);
boolean neverSend = preferences.getBoolean("never_send", false);
if (neverSend) {
return;
return false;
}
List<Account> accounts = service.getAccounts();
Account account = null;
@ -53,24 +53,25 @@ public class ExceptionHelper {
}
}
if (account == null) {
return;
return false;
}
final Account finalAccount = account;
FileInputStream file = context.openFileInput("stacktrace.txt");
FileInputStream file = activity.openFileInput("stacktrace.txt");
InputStreamReader inputStreamReader = new InputStreamReader(file);
BufferedReader stacktrace = new BufferedReader(inputStreamReader);
final StringBuilder report = new StringBuilder();
PackageManager pm = context.getPackageManager();
PackageManager pm = activity.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
packageInfo = pm.getPackageInfo(activity.getPackageName(), 0);
report.append("Version: " + packageInfo.versionName + '\n');
report.append("Last Update: "
+ DateUtils.formatDateTime(context,
+ DateUtils.formatDateTime(activity,
packageInfo.lastUpdateTime,
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE) + '\n');
} catch (NameNotFoundException e) {
return false;
}
String line;
while ((line = stacktrace.readLine()) != null) {
@ -78,11 +79,11 @@ public class ExceptionHelper {
report.append('\n');
}
file.close();
context.deleteFile("stacktrace.txt");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getString(R.string.crash_report_title));
builder.setMessage(context.getText(R.string.crash_report_message));
builder.setPositiveButton(context.getText(R.string.send_now),
activity.deleteFile("stacktrace.txt");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(activity.getString(R.string.crash_report_title));
builder.setMessage(activity.getText(R.string.crash_report_message));
builder.setPositiveButton(activity.getText(R.string.send_now),
new OnClickListener() {
@Override
@ -102,7 +103,7 @@ public class ExceptionHelper {
service.sendMessage(message);
}
});
builder.setNegativeButton(context.getText(R.string.send_never),
builder.setNegativeButton(activity.getText(R.string.send_never),
new OnClickListener() {
@Override
@ -112,8 +113,9 @@ public class ExceptionHelper {
}
});
builder.create().show();
return true;
} catch (final IOException ignored) {
}
return false;
}
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey200">
@ -22,10 +22,10 @@
android:id="@+id/editor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:background="@drawable/infocard_border"
android:orientation="vertical"
android:padding="@dimen/infocard_padding">
@ -40,8 +40,8 @@
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/avater">
android:layout_toRightOf="@+id/avater"
android:orientation="vertical">
<TextView
android:id="@+id/account_jid_label"
android:layout_width="wrap_content"
@ -80,38 +80,38 @@
<LinearLayout
android:id="@+id/name_port"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8">
android:layout_weight="0.8"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/account_settings_hostname"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"
android:id="@+id/textView"/>
android:textSize="?attr/TextSizeBody"/>
<EditText
android:id="@+id/hostname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hostname_or_onion"
android:inputType="textNoSuggestions"
android:textColor="@color/black87"
android:textColorHint="@color/black54"
android:textSize="?attr/TextSizeBody"
android:id="@+id/hostname"
android:inputType="textNoSuggestions"
android:hint="@string/hostname_or_onion"/>
android:textSize="?attr/TextSizeBody"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
@ -120,14 +120,14 @@
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"/>
<EditText
android:id="@+id/port"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:maxLength="5"
android:textColor="@color/black87"
android:textColorHint="@color/black54"
android:textSize="?attr/TextSizeBody"
android:id="@+id/port"/>
android:textSize="?attr/TextSizeBody"/>
</LinearLayout>
</LinearLayout>
<CheckBox
@ -155,21 +155,64 @@
android:layout_marginTop="8dp"
android:hint="@string/confirm_password"
android:inputType="textPassword"
android:visibility="gone"
android:textColor="@color/black87"
android:textColorHint="@color/black54"
android:textSize="?attr/TextSizeBody" />
android:textSize="?attr/TextSizeBody"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/battery_optimization"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@drawable/infocard_border"
android:orientation="vertical"
android:padding="@dimen/infocard_padding"
android:visibility="gone">
<TextView
android:id="@+id/batt_op_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/battery_optimizations_enabled"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeHeadline"
android:textStyle="bold"/>
<TextView
android:id="@+id/batt_op_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/batt_op_headline"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="@string/battery_optimizations_enabled_explained"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"/>
<Button
android:id="@+id/batt_op_disable"
style="?android:attr/borderlessButtonStyle"
android:layout_marginRight="-8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" android:layout_below="@+id/batt_op_body"
android:text="@string/disable" android:textColor="@color/accent"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/stats"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:background="@drawable/infocard_border"
android:orientation="vertical"
android:padding="@dimen/infocard_padding"
@ -379,9 +422,9 @@
</TableLayout>
<RelativeLayout
android:id="@+id/otr_fingerprint_box"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/otr_fingerprint_box"
android:layout_marginTop="32dp">
<LinearLayout
@ -395,17 +438,17 @@
android:id="@+id/otr_fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"
android:typeface="monospace"
android:fontFamily="monospace"/>
android:typeface="monospace"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/otr_fingerprint"
android:textColor="@color/black54"
android:textSize="?attr/TextSizeInfo"
android:text="@string/otr_fingerprint"/>
android:textSize="?attr/TextSizeInfo"/>
</LinearLayout>
<ImageButton
@ -415,15 +458,15 @@
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?android:selectableItemBackground"
android:contentDescription="@string/copy_otr_clipboard_description"
android:padding="@dimen/image_button_padding"
android:src="?attr/icon_copy"
android:visibility="visible"
android:contentDescription="@string/copy_otr_clipboard_description"/>
android:visibility="visible"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/axolotl_fingerprint_box"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/axolotl_fingerprint_box"
android:layout_marginTop="32dp">
<LinearLayout
@ -437,25 +480,25 @@
android:id="@+id/axolotl_fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeBody"
android:typeface="monospace"
android:fontFamily="monospace"/>
android:typeface="monospace"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/this_device_omemo_fingerprint"
android:textColor="@color/black54"
android:textSize="?attr/TextSizeInfo"
android:text="@string/this_device_omemo_fingerprint"/>
android:textSize="?attr/TextSizeInfo"/>
</LinearLayout>
<LinearLayout
android:id="@+id/axolotl_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/axolotl_actions"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<ImageButton
@ -463,19 +506,19 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:contentDescription="@string/copy_omemo_clipboard_description"
android:padding="@dimen/image_button_padding"
android:src="?attr/icon_copy"
android:visibility="visible"
android:contentDescription="@string/copy_omemo_clipboard_description"/>
android:visibility="visible"/>
<ImageButton
android:id="@+id/action_regenerate_axolotl_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:contentDescription="@string/regenerate_omemo_key"
android:padding="@dimen/image_button_padding"
android:src="?attr/icon_refresh"
android:visibility="gone"
android:contentDescription="@string/regenerate_omemo_key"/>
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
@ -484,10 +527,10 @@
android:id="@+id/other_device_keys_card"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:background="@drawable/infocard_border"
android:orientation="vertical"
android:padding="@dimen/infocard_padding"
@ -497,10 +540,10 @@
android:id="@+id/other_device_keys_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/other_devices"
android:textColor="@color/black87"
android:textSize="?attr/TextSizeHeadline"
android:textStyle="bold"
android:text="@string/other_devices"/>
android:textStyle="bold"/>
<LinearLayout
android:id="@+id/other_device_keys"
@ -519,10 +562,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
android:layout_alignParentRight="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/cancel_button"

View File

@ -575,4 +575,8 @@
<string name="pref_picture_compression_summary">Resize and compressed pictures</string>
<string name="always">Always</string>
<string name="automatically">Automatically</string>
<string name="battery_optimizations_enabled">Battery optimizations enabled</string>
<string name="battery_optimizations_enabled_explained">Your phone is doing some heavy battery optimizations on Conversations that might lead to delayed notifications or even message loss. It is recommended to disable those.</string>
<string name="battery_optimizations_enabled_dialog">Your phone is doing some heavy battery optimizations on Conversations that might lead to delayed notifications or even message loss.\n\nYou will now be asked to disable those.</string>
<string name="disable">Disable</string>
</resources>