Battery profiles

This commit is contained in:
stom79 2018-11-23 19:18:38 +01:00
parent 2a4db27b06
commit 0b7491c40a
7 changed files with 132 additions and 23 deletions

View File

@ -546,13 +546,14 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
public void onResume(){
super.onResume();
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
int batteryProfile = sharedpreferences.getInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_NORMAL);
if( type == RetrieveFeedsAsyncTask.Type.PUBLIC){
if( getUserVisibleHint() ){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, true);
editor.apply();
if(liveNotifications) {
if(liveNotifications && batteryProfile == Helper.BATTERY_PROFILE_NORMAL) {
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
try {
context.startService(streamingFederatedIntent);
@ -567,7 +568,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, true);
editor.apply();
if( liveNotifications) {
if( liveNotifications && batteryProfile == Helper.BATTERY_PROFILE_NORMAL) {
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
try {
context.startService(streamingLocalIntent);
@ -620,6 +621,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( context == null)
return;
boolean liveNotifications = sharedpreferences.getBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
int batteryProfile = sharedpreferences.getInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_NORMAL);
//Store last toot id for home timeline to avoid to notify for those that have been already seen
if (type == RetrieveFeedsAsyncTask.Type.HOME && visible && statuses != null && statuses.size() > 0) {
updateStatusLastId(statuses.get(0).getId());
@ -628,7 +630,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_FEDERATED + userId + instance, true);
editor.apply();
if(liveNotifications) {
if(liveNotifications && batteryProfile == Helper.BATTERY_PROFILE_NORMAL) {
streamingFederatedIntent = new Intent(context, StreamingFederatedTimelineService.class);
try {
context.startService(streamingFederatedIntent);
@ -649,7 +651,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOULD_CONTINUE_STREAMING_LOCAL + userId + instance, true);
editor.apply();
if( liveNotifications ) {
if( liveNotifications && batteryProfile == Helper.BATTERY_PROFILE_NORMAL) {
streamingLocalIntent = new Intent(context, StreamingLocalTimelineService.class);
try {
context.startService(streamingLocalIntent);

View File

@ -13,7 +13,9 @@ package fr.gouv.etalab.mastodon.fragments;
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
@ -21,12 +23,16 @@ import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
@ -37,6 +43,7 @@ public class SettingsOptimizationFragment extends Fragment {
private Context context;
int count = 0;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -48,6 +55,51 @@ public class SettingsOptimizationFragment extends Fragment {
//Translators
final Spinner battery_layout_spinner = rootView.findViewById(R.id.battery_layout_spinner);
ArrayAdapter<CharSequence> adapterTrans = ArrayAdapter.createFromResource(getContext(),
R.array.battery_profiles, android.R.layout.simple_spinner_item);
battery_layout_spinner.setAdapter(adapterTrans);
int positionSpinner = sharedpreferences.getInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_NORMAL) -1;
battery_layout_spinner.setSelection(positionSpinner);
battery_layout_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if( count > 0){
SharedPreferences.Editor editor = sharedpreferences.edit();
switch (position){
case 0:
editor.putInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_NORMAL);
editor.apply();
break;
case 1:
editor.putInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_MEDIUM);
editor.apply();
break;
case 2:
editor.putInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_LOW);
editor.apply();
break;
}
if( position < 2 ){
try {
((MainActivity) context).startSreaming();
}catch (Exception ignored){ignored.printStackTrace();}
}else{
context.sendBroadcast(new Intent("StopLiveNotificationService"));
}
}else {
count++;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Status per page
SeekBar statusSeekBar = rootView.findViewById(R.id.set_toots_per_page);
final TextView set_toots_page_value = rootView.findViewById(R.id.set_toots_page_value);

View File

@ -280,6 +280,7 @@ public class Helper {
public static final String SET_DISPLAY_CARD = "set_display_card";
public static final String SET_DISPLAY_VIDEO_PREVIEWS= "set_display_video_previews";
public static final String SET_OLD_DIRECT_TIMELINE = "sset_old_direct_timeline";
public static final String SET_BATTERY_PROFILE = "set_battery_profile";
public static final int S_512KO = 1;
public static final int S_1MO = 2;
public static final int S_2MO = 3;
@ -287,6 +288,10 @@ public class Helper {
public static final int ATTACHMENT_WIFI = 2;
public static final int ATTACHMENT_ASK = 3;
public static final int BATTERY_PROFILE_NORMAL = 1;
public static final int BATTERY_PROFILE_MEDIUM = 2;
public static final int BATTERY_PROFILE_LOW = 3;
public static final int THEME_LIGHT = 1;
public static final int THEME_DARK = 2;
public static final int THEME_BLACK = 3;
@ -2815,4 +2820,28 @@ public class Helper {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
public static void changeBatteryProfile(Context context){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int batteryProfile = sharedpreferences.getInt(Helper.SET_BATTERY_PROFILE, Helper.BATTERY_PROFILE_NORMAL);
SharedPreferences.Editor editor = sharedpreferences.edit();
switch (batteryProfile){
case BATTERY_PROFILE_NORMAL:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
editor.putBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, true);
editor.apply();
break;
case BATTERY_PROFILE_MEDIUM:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, true);
editor.putBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, false);
editor.apply();
break;
case BATTERY_PROFILE_LOW:
editor.putBoolean(Helper.SET_LIVE_NOTIFICATIONS, false);
editor.putBoolean(Helper.SET_KEEP_BACKGROUND_PROCESS, false);
editor.apply();
break;
}
}
}

View File

@ -29,11 +29,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="@dimen/fab_margin"
android:paddingLeft="@dimen/fab_margin"
android:paddingRight="@dimen/fab_margin"
android:orientation="vertical"
tools:ignore="UselessParent">
<CheckBox
android:id="@+id/set_share_validation"
android:layout_width="wrap_content"

View File

@ -24,27 +24,37 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="@dimen/fab_margin"
android:paddingLeft="@dimen/fab_margin"
android:paddingRight="@dimen/fab_margin"
android:orientation="vertical"
tools:ignore="UselessParent">
<!-- OPTIMIZATION SETTINGS -->
<TextView
android:text="@string/settings_title_battery"
style="?attr/shapeBorder"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:layout_marginBottom="20dp"
android:id="@+id/battery_layout_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- OPTIMIZATION SETTINGS -->
<TextView
android:text="@string/settings_title_optimisation"
style="?attr/shapeBorder"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Toots per page -->
<TextView
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:typeface="serif"
android:gravity="center_vertical"
android:layout_height="wrap_content"
@ -58,8 +68,10 @@
android:layout_height="wrap_content">
<TextView
android:id="@+id/set_toots_page_value"
android:layout_gravity="center"
android:layout_width="50dp"
android:gravity="end"
android:layout_width="30dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" />
<SeekBar
android:layout_gravity="center_vertical"
@ -85,8 +97,10 @@
android:layout_height="wrap_content">
<TextView
android:id="@+id/set_accounts_page_value"
android:layout_gravity="center"
android:layout_width="50dp"
android:gravity="end"
android:layout_width="30dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" />
<SeekBar
android:layout_gravity="center_vertical"
@ -112,8 +126,10 @@
android:layout_height="wrap_content">
<TextView
android:id="@+id/set_notifications_page_value"
android:layout_gravity="center"
android:layout_width="50dp"
android:gravity="end"
android:layout_width="30dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content" />
<SeekBar
android:layout_gravity="center_vertical"
@ -124,8 +140,8 @@
</LinearLayout>
<!-- Attachment behavior -->
<TextView
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:text="@string/set_attachment_action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

View File

@ -15,11 +15,12 @@
You should have received a copy of the GNU General Public License along with Mastalab; if not,
see <http://www.gnu.org/licenses>.
-->
<android.support.design.widget.AppBarLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@null"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
@ -37,4 +38,4 @@
android:layout_marginBottom="70dp"
/>
</android.support.design.widget.AppBarLayout>
</LinearLayout>

View File

@ -342,6 +342,15 @@
<string name="expand_cw">Automatically expand cw</string>
<string name="use_cookies">Allow third-party cookies</string>
<string name="settings_ui_layout">Layout for timelines: </string>
<string-array name="battery_profiles">
<item>Normal battery drain</item>
<item>Medium battery drain</item>
<item>Low battery drain</item>
</string-array>
<string-array name="settings_menu_tabs">
<item>Tabs</item>
<item>Menu</item>
@ -664,6 +673,7 @@
<string name="no_mail_client">There are no email clients installed.</string>
<string name="bug_report_mail">Send a bug report</string>
<string name="account_id_clipbloard">The account id has been copied in the clipboard!</string>
<string name="settings_title_battery">Optimization of the battery</string>
<string-array name="filter_expire">
<item>Never</item>