Added automation support

This commit is contained in:
M66B
2019-02-14 13:28:14 +00:00
parent 572607a76b
commit 582ce275db
6 changed files with 115 additions and 19 deletions

View File

@@ -63,6 +63,7 @@ import static android.app.Activity.RESULT_OK;
public class FragmentOptions extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private SwitchCompat swEnabled;
private SwitchCompat swSchedule;
private TextView tvScheduleStart;
private TextView tvScheduleEnd;
@@ -131,6 +132,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
// Get controls
swEnabled = view.findViewById(R.id.swEnabled);
swSchedule = view.findViewById(R.id.swSchedule);
tvScheduleStart = view.findViewById(R.id.tvScheduleStart);
tvScheduleEnd = view.findViewById(R.id.tvScheduleEnd);
@@ -185,6 +187,19 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
}
});
swSchedule.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("schedule", checked).apply();
if (checked)
ServiceSynchronize.schedule(getContext());
else {
prefs.edit().putBoolean("enabled", true).apply();
ServiceSynchronize.reload(getContext(), "schedule=" + checked);
}
}
});
tvScheduleStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@@ -513,6 +528,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
swEnabled.setChecked(prefs.getBoolean("enabled", true));
swSchedule.setChecked(prefs.getBoolean("schedule", false));
tvScheduleStart.setText(formatHour(prefs.getInt("schedule_start", 0)));
tvScheduleEnd.setText(formatHour(prefs.getInt("schedule_end", 0)));
@@ -601,7 +617,10 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
boolean start = args.getBoolean("start");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute).apply();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("schedule_" + (start ? "start" : "end"), hour * 60 + minute);
editor.putBoolean("schedule", true);
editor.apply();
ServiceSynchronize.schedule(getContext());
}
@@ -624,7 +643,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
}
};
public void showConnectionType() {
private void showConnectionType() {
FragmentActivity activity = getActivity();
if (activity == null)
return;
@@ -665,6 +684,8 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if ("enabled".equals(key))
swEnabled.setChecked(prefs.getBoolean(key, true));
else if ("schedule".equals(key))
swSchedule.setChecked(prefs.getBoolean(key, false));
else if ("schedule_start".equals(key))
tvScheduleStart.setText(formatHour(prefs.getInt(key, 0)));
else if ("schedule_end".equals(key))

View File

@@ -0,0 +1,46 @@
package eu.faircode.email;
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.annotation.Nullable;
public class ServiceExternal extends IntentService {
private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE";
private final static String ACTION_DISABLE = "eu.faircode.email.DISABLE";
// adb shell am startservice -a eu.faircode.email.ENABLE
// adb shell am startservice -a eu.faircode.email.DISABLE
public ServiceExternal() {
super(ServiceExternal.class.getName());
}
public ServiceExternal(String name) {
super(name);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent == null)
return;
Boolean enabled = null;
if (ACTION_ENABLE.equals(intent.getAction()))
enabled = true;
else if (ACTION_DISABLE.equals(intent.getAction()))
enabled = false;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean previous = prefs.getBoolean("enabled", true);
if (enabled != null) {
prefs.edit().putBoolean("schedule", false).apply();
if (!enabled.equals(previous)) {
prefs.edit().putBoolean("enabled", enabled).apply();
ServiceSynchronize.reload(this, "external");
}
}
}
}

View File

@@ -3239,6 +3239,9 @@ public class ServiceSynchronize extends LifecycleService {
am.cancel(piSchedule);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this);
if (!prefs.getBoolean("schedule", false))
return;
int minuteStart = prefs.getInt("schedule_start", 0);
int minuteEnd = prefs.getInt("schedule_end", 0);