diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 55b695aaa..4a59e9ea7 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -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); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsOptimizationFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsOptimizationFragment.java index 804206adc..efefea20c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsOptimizationFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsOptimizationFragment.java @@ -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 . */ + 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 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); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index c018de1a8..e002bac15 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -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; + + } + } } diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index b895d615c..57dfcc192 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -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"> - - - + + + diff --git a/app/src/main/res/layout/tablayout_settings.xml b/app/src/main/res/layout/tablayout_settings.xml index 21bed1f79..781e08e10 100644 --- a/app/src/main/res/layout/tablayout_settings.xml +++ b/app/src/main/res/layout/tablayout_settings.xml @@ -15,11 +15,12 @@ You should have received a copy of the GNU General Public License along with Mastalab; if not, see . --> - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f6fb5fd04..622f65e74 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -342,6 +342,15 @@ Automatically expand cw Allow third-party cookies Layout for timelines: + + + + + Normal battery drain + Medium battery drain + Low battery drain + + Tabs Menu @@ -664,6 +673,7 @@ There are no email clients installed. Send a bug report The account id has been copied in the clipboard! + Optimization of the battery Never