Merge pull request #2060 from domingos86/gpodnet-notification-option

Make gpodnet sync error notifications optional
This commit is contained in:
Martin Fietz 2016-07-17 20:24:18 +02:00 committed by GitHub
commit 9d5d3508cf
10 changed files with 160 additions and 21 deletions

View File

@ -118,5 +118,14 @@ public class PreferenceActivity extends AppCompatActivity {
activity.preferenceController.onResume();
}
}
@Override
public void onPause() {
PreferenceActivity activity = instance.get();
if(activity != null && activity.preferenceController != null) {
activity.preferenceController.onPause();
}
super.onPause();
}
}
}

View File

@ -54,6 +54,12 @@ public class PreferenceActivityGingerbread extends android.preference.Preference
preferenceController.onResume();
}
@Override
protected void onPause() {
preferenceController.onPause();
super.onPause();
}
@Override
protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
theme.applyStyle(UserPreferences.getTheme(), true);

View File

@ -6,11 +6,13 @@ import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
@ -40,7 +42,7 @@ import de.danoeh.antennapod.core.service.GpodnetSyncService;
* Step 2: Choose device from a list of available devices or create a new one
* Step 3: Choose from a list of actions
*/
public class GpodnetAuthenticationActivity extends ActionBarActivity {
public class GpodnetAuthenticationActivity extends AppCompatActivity {
private static final String TAG = "GpodnetAuthActivity";
private static final String CURRENT_STEP = "current_step";
@ -113,6 +115,9 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progBarLogin);
password.setOnEditorActionListener((v, actionID, event) ->
actionID == EditorInfo.IME_ACTION_GO && login.performClick());
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -131,6 +136,11 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
login.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
txtvError.setVisibility(View.GONE);
// hide the keyboard
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(login.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}

View File

@ -28,10 +28,11 @@ import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ListView;
import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
@ -89,6 +90,7 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
public static final String PREF_GPODNET_SYNC = "pref_gpodnet_sync";
public static final String PREF_GPODNET_LOGOUT = "pref_gpodnet_logout";
public static final String PREF_GPODNET_HOSTNAME = "pref_gpodnet_hostname";
public static final String PREF_GPODNET_NOTIFICATIONS = "pref_gpodnet_notifications";
public static final String PREF_EXPANDED_NOTIFICATION = "prefExpandNotify";
public static final String PREF_PROXY = "prefProxy";
public static final String PREF_KNOWN_ISSUES = "prefKnownIssues";
@ -120,6 +122,14 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
}
}
private final SharedPreferences.OnSharedPreferenceChangeListener gpoddernetListener =
(sharedPreferences, key) -> {
if (GpodnetPreferences.PREF_LAST_SYNC_ATTEMPT_TIMESTAMP.equals(key)) {
updateLastGpodnetSyncReport(GpodnetPreferences.getLastSyncAttemptResult(),
GpodnetPreferences.getLastSyncAttemptTimestamp());
}
};
/**
* Returns the preference activity that should be used on this device.
*
@ -433,9 +443,14 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
setParallelDownloadsText(UserPreferences.getParallelDownloads());
setEpisodeCacheSizeText(UserPreferences.getEpisodeCacheSize());
setDataFolderText();
GpodnetPreferences.registerOnSharedPreferenceChangeListener(gpoddernetListener);
updateGpodnetPreferenceScreen();
}
public void onPause() {
GpodnetPreferences.unregisterOnSharedPreferenceChangeListener(gpoddernetListener);
}
@SuppressLint("NewApi")
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK &&
@ -478,17 +493,39 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc
ui.findPreference(PreferenceController.PREF_GPODNET_SETLOGIN_INFORMATION).setEnabled(loggedIn);
ui.findPreference(PreferenceController.PREF_GPODNET_SYNC).setEnabled(loggedIn);
ui.findPreference(PreferenceController.PREF_GPODNET_LOGOUT).setEnabled(loggedIn);
ui.findPreference(PREF_GPODNET_NOTIFICATIONS).setEnabled(loggedIn);
if(loggedIn) {
String format = ui.getActivity().getString(R.string.pref_gpodnet_login_status);
String summary = String.format(format, GpodnetPreferences.getUsername(),
GpodnetPreferences.getDeviceID());
ui.findPreference(PreferenceController.PREF_GPODNET_LOGOUT).setSummary(Html.fromHtml(summary));
updateLastGpodnetSyncReport(GpodnetPreferences.getLastSyncAttemptResult(),
GpodnetPreferences.getLastSyncAttemptTimestamp());
} else {
ui.findPreference(PreferenceController.PREF_GPODNET_LOGOUT).setSummary(null);
updateLastGpodnetSyncReport(false, 0);
}
ui.findPreference(PreferenceController.PREF_GPODNET_HOSTNAME).setSummary(GpodnetPreferences.getHostname());
}
private void updateLastGpodnetSyncReport(boolean successful, long lastTime) {
Preference sync = ui.findPreference(PREF_GPODNET_SYNC);
if (lastTime != 0) {
sync.setSummary(ui.getActivity().getString(R.string.pref_gpodnet_sync_sum) + "\n" +
ui.getActivity().getString(R.string.pref_gpodnet_sync_sum_last_sync_line,
ui.getActivity().getString(successful ?
R.string.gpodnetsync_pref_report_successful :
R.string.gpodnetsync_pref_report_failed),
DateUtils.getRelativeDateTimeString(ui.getActivity(),
lastTime,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
DateUtils.FORMAT_SHOW_TIME)));
} else {
sync.setSummary(ui.getActivity().getString(R.string.pref_gpodnet_sync_sum));
}
}
private String[] getUpdateIntervalEntries(final String[] values) {
final Resources res = ui.getActivity().getResources();
String[] entries = new String[values.length];

View File

@ -33,7 +33,11 @@
android:layout_margin="8dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
android:cursorVisible="true"
android:maxLines="1"
android:inputType="text"
android:imeOptions="actionNext"
android:nextFocusForward="@id/etxtPassword"/>
<EditText
android:id="@+id/etxtPassword"
@ -45,7 +49,9 @@
android:layout_margin="8dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:cursorVisible="true"/>
android:cursorVisible="true"
android:imeOptions="actionGo"
android:imeActionLabel="@string/gpodnetauth_login_butLabel"/>
<Button
android:id="@+id/butLogin"

View File

@ -74,7 +74,6 @@
android:key="prefQueueAddToFront"
android:summary="@string/pref_queueAddToFront_sum"
android:title="@string/pref_queueAddToFront_title"/>
/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/playback_pref">
@ -233,7 +232,7 @@
android:title="@string/pref_revokeAccess_title"/>
</PreferenceScreen>
<PreferenceScreen
android:key="prefFlattrSettings"
android:key="prefGpodderSettings"
android:title="@string/gpodnet_main_label">
<PreferenceScreen
@ -256,6 +255,11 @@
<Preference
android:key="pref_gpodnet_hostname"
android:title="@string/pref_gpodnet_sethostname_title"/>
<de.danoeh.antennapod.preferences.SwitchCompatPreference
android:key="pref_gpodnet_notifications"
android:title="@string/pref_gpodnet_notifications_title"
android:summary="@string/pref_gpodnet_notifications_sum"
android:defaultValue="true"/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/storage_pref">

View File

@ -37,6 +37,8 @@ public class GpodnetPreferences {
public static final String PREF_SYNC_ADDED = "de.danoeh.antennapod.preferences.gpoddernet.sync_added";
public static final String PREF_SYNC_REMOVED = "de.danoeh.antennapod.preferences.gpoddernet.sync_removed";
public static final String PREF_SYNC_EPISODE_ACTIONS = "de.danoeh.antennapod.preferences.gpoddernet.sync_queued_episode_actions";
public static final String PREF_LAST_SYNC_ATTEMPT_TIMESTAMP = "de.danoeh.antennapod.preferences.gpoddernet.last_sync_attempt_timestamp";
public static final String PREF_LAST_SYNC_ATTEMPT_RESULT = "de.danoeh.antennapod.preferences.gpoddernet.last_sync_attempt_result";
private static String username;
private static String password;
@ -56,12 +58,26 @@ public class GpodnetPreferences {
private static long lastEpisodeActionsSyncTimeStamp;
private static long lastSyncAttemptTimestamp;
private static boolean lastSyncAttemptResult;
private static boolean preferencesLoaded = false;
private static SharedPreferences getPreferences() {
return ClientConfig.applicationCallbacks.getApplicationInstance().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static void registerOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().registerOnSharedPreferenceChangeListener(listener);
}
public static void unregisterOnSharedPreferenceChangeListener(
SharedPreferences.OnSharedPreferenceChangeListener listener) {
getPreferences().unregisterOnSharedPreferenceChangeListener(listener);
}
private static synchronized void ensurePreferencesLoaded() {
if (!preferencesLoaded) {
SharedPreferences prefs = getPreferences();
@ -70,6 +86,8 @@ public class GpodnetPreferences {
deviceID = prefs.getString(PREF_GPODNET_DEVICEID, null);
lastSubscriptionSyncTimestamp = prefs.getLong(PREF_LAST_SUBSCRIPTION_SYNC_TIMESTAMP, 0);
lastEpisodeActionsSyncTimeStamp = prefs.getLong(PREF_LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, 0);
lastSyncAttemptTimestamp = prefs.getLong(PREF_LAST_SYNC_ATTEMPT_TIMESTAMP, 0);
lastSyncAttemptResult = prefs.getBoolean(PREF_LAST_SYNC_ATTEMPT_RESULT, false);
addedFeeds = readListFromString(prefs.getString(PREF_SYNC_ADDED, ""));
removedFeeds = readListFromString(prefs.getString(PREF_SYNC_REMOVED, ""));
queuedEpisodeActions = readEpisodeActionsFromString(prefs.getString(PREF_SYNC_EPISODE_ACTIONS, ""));
@ -82,19 +100,25 @@ public class GpodnetPreferences {
private static void writePreference(String key, String value) {
SharedPreferences.Editor editor = getPreferences().edit();
editor.putString(key, value);
editor.commit();
editor.apply();
}
private static void writePreference(String key, long value) {
SharedPreferences.Editor editor = getPreferences().edit();
editor.putLong(key, value);
editor.commit();
editor.apply();
}
private static void writePreference(String key, Collection<String> value) {
SharedPreferences.Editor editor = getPreferences().edit();
editor.putString(key, writeListToString(value));
editor.commit();
editor.apply();
}
private static void writePreference(String key, boolean value) {
SharedPreferences.Editor editor = getPreferences().edit();
editor.putBoolean(key, value);
editor.apply();
}
public static String getUsername() {
@ -147,6 +171,23 @@ public class GpodnetPreferences {
writePreference(PREF_LAST_EPISODE_ACTIONS_SYNC_TIMESTAMP, timestamp);
}
public static long getLastSyncAttemptTimestamp() {
ensurePreferencesLoaded();
return lastSyncAttemptTimestamp;
}
public static boolean getLastSyncAttemptResult() {
ensurePreferencesLoaded();
return lastSyncAttemptResult;
}
public static void setLastSyncAttempt(boolean result, long timestamp) {
GpodnetPreferences.lastSyncAttemptResult = result;
GpodnetPreferences.lastSyncAttemptTimestamp = timestamp;
writePreference(PREF_LAST_SYNC_ATTEMPT_RESULT, result);
writePreference(PREF_LAST_SYNC_ATTEMPT_TIMESTAMP, timestamp);
}
public static String getHostname() {
ensurePreferencesLoaded();
return hostname;
@ -269,6 +310,8 @@ public class GpodnetPreferences {
writePreference(PREF_SYNC_EPISODE_ACTIONS, writeEpisodeActionsToString(queuedEpisodeActions));
feedListLock.unlock();
setLastSubscriptionSyncTimestamp(0);
setLastSyncAttempt(false, 0);
UserPreferences.setGpodnetNotificationsEnabled();
}
private static Set<String> readListFromString(String s) {

View File

@ -92,6 +92,7 @@ public class UserPreferences {
// Services
public static final String PREF_AUTO_FLATTR = "pref_auto_flattr";
public static final String PREF_AUTO_FLATTR_PLAYED_DURATION_THRESHOLD = "prefAutoFlattrPlayedDurationThreshold";
public static final String PREF_GPODNET_NOTIFICATIONS = "pref_gpodnet_notifications";
// Other
public static final String PREF_DATA_FOLDER = "prefDataFolder";
@ -546,6 +547,16 @@ public class UserPreferences {
.apply();
}
public static boolean gpodnetNotificationsEnabled() {
return prefs.getBoolean(PREF_GPODNET_NOTIFICATIONS, true);
}
public static void setGpodnetNotificationsEnabled() {
prefs.edit()
.putBoolean(PREF_GPODNET_NOTIFICATIONS, true)
.apply();
}
public static void setHiddenDrawerItems(List<String> items) {
String str = TextUtils.join(",", items);
prefs.edit()

View File

@ -30,6 +30,7 @@ import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionPostRespon
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetSubscriptionChange;
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
@ -107,7 +108,7 @@ public class GpodnetSyncService extends Service {
private synchronized void sync() {
if (GpodnetPreferences.loggedIn() == false || NetworkUtils.networkAvailable() == false) {
if (!GpodnetPreferences.loggedIn() || !NetworkUtils.networkAvailable()) {
stopSelf();
return;
}
@ -161,6 +162,7 @@ public class GpodnetSyncService extends Service {
GpodnetPreferences.removeRemovedFeeds(localRemoved);
}
GpodnetPreferences.setLastSubscriptionSyncTimestamp(newTimeStamp);
GpodnetPreferences.setLastSyncAttempt(true, System.currentTimeMillis());
clearErrorNotifications();
} catch (GpodnetServiceException e) {
e.printStackTrace();
@ -177,15 +179,15 @@ public class GpodnetSyncService extends Service {
// local changes are always superior to remote changes!
// add subscription if (1) not already subscribed and (2) not just unsubscribed
for (String downloadUrl : changes.getAdded()) {
if (false == localSubscriptions.contains(downloadUrl) &&
false == localRemoved.contains(downloadUrl)) {
if (!localSubscriptions.contains(downloadUrl) &&
!localRemoved.contains(downloadUrl)) {
Feed feed = new Feed(downloadUrl, null);
DownloadRequester.getInstance().downloadFeed(this, feed);
}
}
// remove subscription if not just subscribed (again)
for (String downloadUrl : changes.getRemoved()) {
if(false == localAdded.contains(downloadUrl)) {
if(!localAdded.contains(downloadUrl)) {
DBTasks.removeFeedWithDownloadUrl(GpodnetSyncService.this, downloadUrl);
}
}
@ -215,6 +217,7 @@ public class GpodnetSyncService extends Service {
GpodnetPreferences.removeQueuedEpisodeActions(localActions);
}
GpodnetPreferences.setLastEpisodeActionsSyncTimestamp(lastUpdate);
GpodnetPreferences.setLastSyncAttempt(true, System.currentTimeMillis());
clearErrorNotifications();
} catch (GpodnetServiceException e) {
e.printStackTrace();
@ -298,8 +301,8 @@ public class GpodnetSyncService extends Service {
private void updateErrorNotification(GpodnetServiceException exception) {
Log.d(TAG, "Posting error notification");
GpodnetPreferences.setLastSyncAttempt(false, System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
final String title;
final String description;
final int id;
@ -308,18 +311,23 @@ public class GpodnetSyncService extends Service {
description = getString(R.string.gpodnetsync_auth_error_descr);
id = R.id.notification_gpodnet_sync_autherror;
} else {
title = getString(R.string.gpodnetsync_error_title);
description = getString(R.string.gpodnetsync_error_descr) + exception.getMessage();
id = R.id.notification_gpodnet_sync_error;
if (UserPreferences.gpodnetNotificationsEnabled()) {
title = getString(R.string.gpodnetsync_error_title);
description = getString(R.string.gpodnetsync_error_descr) + exception.getMessage();
id = R.id.notification_gpodnet_sync_error;
} else {
return;
}
}
PendingIntent activityIntent = ClientConfig.gpodnetCallbacks.getGpodnetSyncServiceErrorNotificationPendingIntent(this);
Notification notification = builder.setContentTitle(title)
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(activityIntent)
.setSmallIcon(R.drawable.stat_notify_sync_error)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, notification);

View File

@ -365,9 +365,12 @@
<string name="pref_gpodnet_setlogin_information_title">Change login information</string>
<string name="pref_gpodnet_setlogin_information_sum">Change the login information for your gpodder.net account.</string>
<string name="pref_gpodnet_sync_title">Sync now</string>
<string name="pref_gpodnet_sync_sum">Sync subscriptions and episode states with gpodder.net</string>
<string name="pref_gpodnet_sync_sum">Sync subscriptions and episode states with gpodder.net.</string>
<string name="pref_gpodnet_sync_sum_last_sync_line">Last sync attempt: %1$s (%2$s)</string>
<string name="pref_gpodnet_sync_started">Sync started</string>
<string name="pref_gpodnet_login_status"><![CDATA[Logged in as <i>%1$s</i> with device <i>%2$s</i>]]></string>
<string name="pref_gpodnet_notifications_title">Show sync error notifications</string>
<string name="pref_gpodnet_notifications_sum">This setting does not apply to authentication errors.</string>
<string name="pref_playback_speed_title">Playback Speeds</string>
<string name="pref_playback_speed_sum">Customize the speeds available for variable speed audio playback</string>
<string name="pref_fast_forward">Fast forward time</string>
@ -502,6 +505,8 @@
<string name="gpodnetsync_auth_error_descr">Wrong username or password</string>
<string name="gpodnetsync_error_title">gpodder.net sync error</string>
<string name="gpodnetsync_error_descr">An error occurred during syncing:\u0020</string>
<string name="gpodnetsync_pref_report_successful">Successful</string>
<string name="gpodnetsync_pref_report_failed">Failed</string>
<!-- Directory chooser -->
<string name="selected_folder_label">Selected folder:</string>