mirror of
https://github.com/etesync/android
synced 2024-12-24 23:48:35 +00:00
Hints manager: Add a class to manage hints (show once info)
This is much cleaner than what was there before, and enables to easily add more hints.
This commit is contained in:
parent
2570c9671d
commit
f650d38435
@ -24,6 +24,7 @@ import com.etesync.syncadapter.App;
|
|||||||
import com.etesync.syncadapter.R;
|
import com.etesync.syncadapter.R;
|
||||||
import com.etesync.syncadapter.model.ServiceDB;
|
import com.etesync.syncadapter.model.ServiceDB;
|
||||||
import com.etesync.syncadapter.model.Settings;
|
import com.etesync.syncadapter.model.Settings;
|
||||||
|
import com.etesync.syncadapter.utils.HintManager;
|
||||||
|
|
||||||
public class AppSettingsActivity extends AppCompatActivity {
|
public class AppSettingsActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@ -150,7 +151,7 @@ public class AppSettingsActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void resetHints() {
|
private void resetHints() {
|
||||||
settings.remove(StartupDialogFragment.HINT_BATTERY_OPTIMIZATIONS);
|
HintManager.resetHints(getContext());
|
||||||
Snackbar.make(getView(), R.string.app_settings_reset_hints_success, Snackbar.LENGTH_LONG).show();
|
Snackbar.make(getView(), R.string.app_settings_reset_hints_success, Snackbar.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,18 +25,15 @@ import android.support.v7.app.AlertDialog;
|
|||||||
import com.etesync.syncadapter.BuildConfig;
|
import com.etesync.syncadapter.BuildConfig;
|
||||||
import com.etesync.syncadapter.Constants;
|
import com.etesync.syncadapter.Constants;
|
||||||
import com.etesync.syncadapter.R;
|
import com.etesync.syncadapter.R;
|
||||||
import com.etesync.syncadapter.model.ServiceDB;
|
import com.etesync.syncadapter.utils.HintManager;
|
||||||
import com.etesync.syncadapter.model.Settings;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import lombok.Cleanup;
|
|
||||||
|
|
||||||
public class StartupDialogFragment extends DialogFragment {
|
public class StartupDialogFragment extends DialogFragment {
|
||||||
public static final String
|
private static final HintManager.Hint
|
||||||
HINT_BATTERY_OPTIMIZATIONS = "hint_BatteryOptimizations";
|
HINT_BATTERY_OPTIMIZATIONS = HintManager.registerHint("BatteryOptimizations");
|
||||||
|
|
||||||
private static final String ARGS_MODE = "mode";
|
private static final String ARGS_MODE = "mode";
|
||||||
|
|
||||||
@ -49,14 +46,11 @@ public class StartupDialogFragment extends DialogFragment {
|
|||||||
public static StartupDialogFragment[] getStartupDialogs(Context context) {
|
public static StartupDialogFragment[] getStartupDialogs(Context context) {
|
||||||
List<StartupDialogFragment> dialogs = new LinkedList<>();
|
List<StartupDialogFragment> dialogs = new LinkedList<>();
|
||||||
|
|
||||||
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
|
|
||||||
Settings settings = new Settings(dbHelper.getReadableDatabase());
|
|
||||||
|
|
||||||
if (BuildConfig.VERSION_NAME.contains("-alpha") || BuildConfig.VERSION_NAME.contains("-beta") || BuildConfig.VERSION_NAME.contains("-rc"))
|
if (BuildConfig.VERSION_NAME.contains("-alpha") || BuildConfig.VERSION_NAME.contains("-beta") || BuildConfig.VERSION_NAME.contains("-rc"))
|
||||||
dialogs.add(StartupDialogFragment.instantiate(Mode.DEVELOPMENT_VERSION));
|
dialogs.add(StartupDialogFragment.instantiate(Mode.DEVELOPMENT_VERSION));
|
||||||
|
|
||||||
// battery optimization whitelisting
|
// battery optimization whitelisting
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && settings.getBoolean(HINT_BATTERY_OPTIMIZATIONS, true)) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !HintManager.getHintSeen(context, HINT_BATTERY_OPTIMIZATIONS)) {
|
||||||
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
||||||
if (!powerManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID))
|
if (!powerManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID))
|
||||||
dialogs.add(StartupDialogFragment.instantiate(Mode.BATTERY_OPTIMIZATIONS));
|
dialogs.add(StartupDialogFragment.instantiate(Mode.BATTERY_OPTIMIZATIONS));
|
||||||
@ -104,9 +98,7 @@ public class StartupDialogFragment extends DialogFragment {
|
|||||||
.setNegativeButton(R.string.startup_dont_show_again, new DialogInterface.OnClickListener() {
|
.setNegativeButton(R.string.startup_dont_show_again, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(getContext());
|
HintManager.setHintSeen(getContext(), HINT_BATTERY_OPTIMIZATIONS, true);
|
||||||
Settings settings = new Settings(dbHelper.getWritableDatabase());
|
|
||||||
settings.putBoolean(HINT_BATTERY_OPTIMIZATIONS, false);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.create();
|
.create();
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.etesync.syncadapter.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class HintManager {
|
||||||
|
private final static String PREF_NAME = "hints";
|
||||||
|
private static List<Hint> hints = new LinkedList<>();
|
||||||
|
|
||||||
|
public static Hint registerHint(String hint) {
|
||||||
|
hint = "hint_" + hint;
|
||||||
|
Hint ret = new Hint(hint);
|
||||||
|
hints.add(ret);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SharedPreferences getPrefs(Context context) {
|
||||||
|
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setHintSeen(Context context, Hint hint, boolean seen) {
|
||||||
|
SharedPreferences prefs = getPrefs(context);
|
||||||
|
|
||||||
|
prefs.edit().putBoolean(hint.string, seen).apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean getHintSeen(Context context, Hint hint) {
|
||||||
|
SharedPreferences prefs = getPrefs(context);
|
||||||
|
|
||||||
|
return prefs.getBoolean(hint.string, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void resetHints(Context context) {
|
||||||
|
for (Hint hint : hints) {
|
||||||
|
setHintSeen(context, hint, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Hint {
|
||||||
|
private String string;
|
||||||
|
private Hint(String hint) {
|
||||||
|
this.string = hint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user