1
0
mirror of https://github.com/etesync/android synced 2024-11-16 04:49:06 +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:
Tom Hacohen 2017-04-06 11:16:36 +01:00
parent 2570c9671d
commit f650d38435
3 changed files with 57 additions and 14 deletions

View File

@ -24,6 +24,7 @@ import com.etesync.syncadapter.App;
import com.etesync.syncadapter.R;
import com.etesync.syncadapter.model.ServiceDB;
import com.etesync.syncadapter.model.Settings;
import com.etesync.syncadapter.utils.HintManager;
public class AppSettingsActivity extends AppCompatActivity {
@ -150,7 +151,7 @@ public class AppSettingsActivity extends AppCompatActivity {
}
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();
}

View File

@ -25,18 +25,15 @@ import android.support.v7.app.AlertDialog;
import com.etesync.syncadapter.BuildConfig;
import com.etesync.syncadapter.Constants;
import com.etesync.syncadapter.R;
import com.etesync.syncadapter.model.ServiceDB;
import com.etesync.syncadapter.model.Settings;
import com.etesync.syncadapter.utils.HintManager;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import lombok.Cleanup;
public class StartupDialogFragment extends DialogFragment {
public static final String
HINT_BATTERY_OPTIMIZATIONS = "hint_BatteryOptimizations";
private static final HintManager.Hint
HINT_BATTERY_OPTIMIZATIONS = HintManager.registerHint("BatteryOptimizations");
private static final String ARGS_MODE = "mode";
@ -49,14 +46,11 @@ public class StartupDialogFragment extends DialogFragment {
public static StartupDialogFragment[] getStartupDialogs(Context context) {
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"))
dialogs.add(StartupDialogFragment.instantiate(Mode.DEVELOPMENT_VERSION));
// 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);
if (!powerManager.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID))
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() {
@Override
public void onClick(DialogInterface dialog, int which) {
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(getContext());
Settings settings = new Settings(dbHelper.getWritableDatabase());
settings.putBoolean(HINT_BATTERY_OPTIMIZATIONS, false);
HintManager.setHintSeen(getContext(), HINT_BATTERY_OPTIMIZATIONS, true);
}
})
.create();

View File

@ -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;
}
}
}