Added preferences for pull notifications! Also bits of cleanup.

This commit is contained in:
Vavassor 2017-02-05 02:34:55 -05:00
parent 0a45b72391
commit e0ab25334b
17 changed files with 185 additions and 27 deletions

View File

@ -36,6 +36,7 @@
<activity android:name=".ViewThreadActivity" />
<activity android:name=".ViewTagActivity" />
<activity android:name=".AccountActivity" />
<activity android:name=".PreferencesActivity" />
<service
android:name=".PullNotificationService"
android:description="@string/notification_service_description"

View File

@ -27,7 +27,6 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;

View File

@ -20,12 +20,10 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Contacts;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

View File

@ -49,12 +49,10 @@ import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

View File

@ -43,7 +43,7 @@ public class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> {
protected Boolean doInBackground(Bitmap... bitmaps) {
final int count = bitmaps.length;
resultList = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
for (Bitmap bitmap : bitmaps) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
/* Unfortunately, there isn't a determined worst case compression ratio for image
* formats. So, the only way to tell if they're too big is to compress them and
@ -54,16 +54,16 @@ public class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> {
int scaledImageSize = 4096;
do {
stream.reset();
Bitmap bitmap = scaleDown(bitmaps[i], scaledImageSize, true);
Bitmap scaledBitmap = scaleDown(bitmap, scaledImageSize, true);
Bitmap.CompressFormat format;
/* It's not likely the user will give transparent images over the upload limit, but
* if they do, make sure the transparency is retained. */
if (!bitmap.hasAlpha()) {
if (!scaledBitmap.hasAlpha()) {
format = Bitmap.CompressFormat.JPEG;
} else {
format = Bitmap.CompressFormat.PNG;
}
bitmap.compress(format, 75, stream);
scaledBitmap.compress(format, 75, stream);
scaledImageSize /= 2;
iterations++;
} while (stream.size() > sizeLimit);

View File

@ -1,3 +1,18 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
* <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky;
import android.content.Context;

View File

@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
@ -81,11 +82,12 @@ public class MainActivity extends AppCompatActivity {
tabLayout.setupWithViewPager(viewPager);
// Retrieve notification update preference.
SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
notificationServiceEnabled = preferences.getBoolean("notificationService", false);
long notificationCheckInterval =
preferences.getLong("notificationCheckInterval", 5 * 60 * 1000);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
notificationServiceEnabled = preferences.getBoolean("pullNotifications", true);
String minutesString = preferences.getString("pullNotificationCheckInterval", "15");
long notificationCheckInterval = 60 * 1000 * Integer.valueOf(minutesString);
Log.d(TAG, String.format("pull notifications: %b %dm", notificationServiceEnabled,
Integer.valueOf(minutesString)));
// Start up the PullNotificationsService.
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, PullNotificationService.class);
@ -252,6 +254,11 @@ public class MainActivity extends AppCompatActivity {
startActivity(intent);
}
private void viewPreferences() {
Intent intent = new Intent(this, PreferencesActivity.class);
startActivity(intent);
}
private void logOut() {
if (notificationServiceEnabled) {
alarmManager.cancel(serviceAlarmIntent);
@ -284,6 +291,10 @@ public class MainActivity extends AppCompatActivity {
viewProfile();
return true;
}
case R.id.action_preferences: {
viewPreferences();
return true;
}
case R.id.action_logout: {
logOut();
return true;

View File

@ -18,7 +18,6 @@ package com.keylesspalace.tusky;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -83,7 +82,6 @@ public class NotificationsAdapter extends RecyclerView.Adapter implements Adapte
case MENTION: {
StatusViewHolder holder = (StatusViewHolder) viewHolder;
Status status = notification.getStatus();
assert(status != null);
holder.setupWithStatus(status, statusListener, position);
break;
}

View File

@ -35,9 +35,7 @@ import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

View File

@ -0,0 +1,30 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
* <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class PreferencesActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PreferencesFragment())
.commit();
}
}

View File

@ -0,0 +1,27 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky. If not, see
* <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class PreferencesFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

View File

@ -21,6 +21,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
@ -46,7 +47,7 @@ public class PullNotificationService extends IntentService {
private final int NOTIFY_ID = 6; // This is an arbitrary number.
public PullNotificationService() {
super("Tusky Notification Service");
super("Tusky Pull Notification Service");
}
@Override
@ -60,8 +61,6 @@ public class PullNotificationService extends IntentService {
if (date != 0) {
lastUpdate = new Date(date);
}
assert(domain != null);
assert(accessToken != null);
checkNotifications(domain, accessToken, lastUpdate);
}
@ -171,8 +170,7 @@ public class PullNotificationService extends IntentService {
private void updateNotification(List<MentionResult> mentions, @Nullable Bitmap icon) {
final int NOTIFICATION_CONTENT_LIMIT = 40;
SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String title;
if (mentions.size() > 1) {
title = String.format(
@ -189,13 +187,13 @@ public class PullNotificationService extends IntentService {
if (icon != null) {
builder.setLargeIcon(icon);
}
if (preferences.getBoolean("notificationAlertSound", false)) {
if (preferences.getBoolean("notificationAlertSound", true)) {
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
}
if (preferences.getBoolean("notificationStyleVibrate", false)) {
builder.setVibrate(new long[] { 500, 500 });
}
if (preferences.getBoolean("notificationStyleLight", false)) {
if (preferences.getBoolean("notificationStyleLight", true)) {
builder.setLights(0xFF00FF8F, 300, 1000);
}
for (int i = 0; i < mentions.size(); i++) {

View File

@ -18,7 +18,6 @@ package com.keylesspalace.tusky;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.ImageReader;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -37,7 +36,6 @@ import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

View File

@ -14,6 +14,11 @@
android:title="@string/action_profile"
app:showAsAction="never" />
<item
android:id="@+id/action_preferences"
android:title="@string/action_preferences"
app:showAsAction="never" />
<item
android:id="@+id/action_logout"
android:title="@string/action_logout"

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pull_notification_check_interval_names">
<item>5 minutes</item>
<item>10 minutes</item>
<item>15 minutes</item>
<item>20 minutes</item>
<item>25 minutes</item>
<item>30 minutes</item>
<item>45 minutes</item>
<item>1 hour</item>
<item>2 hours</item>
</string-array>
<string-array name="pull_notification_check_intervals" inputType="integer">
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>25</item>
<item>30</item>
<item>45</item>
<item>60</item>
<item>120</item>
</string-array>
</resources>

View File

@ -96,6 +96,8 @@
<string name="action_back">Back</string>
<string name="action_profile">Profile</string>
<string name="action_open_in_web">Open In Web</string>
<string name="action_preferences">Preferences</string>
<string name="action_set_time">Set</string>
<string name="confirmation_send">Toot!</string>
@ -119,4 +121,13 @@
<string name="notification_service_several_mentions">%d new mentions</string>
<string name="notification_service_one_mention">Mention from %s</string>
<string name="pref_title_notification_settings">Notifications</string>
<string name="pref_title_pull_notifications">Enable Pull Notifcations</string>
<string name="pref_summary_pull_notifications">check for notifications periodically</string>
<string name="pref_title_pull_notification_check_interval">Check Interval</string>
<string name="pref_summary_pull_notification_check_interval">how often to pull</string>
<string name="pref_title_notification_alert_sound">Notify with a sound</string>
<string name="pref_title_notification_style_vibrate">Notify with vibration</string>
<string name="pref_title_notification_style_light">Notify with light</string>
</resources>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="@string/preferences_file_key">
<PreferenceCategory
android:title="@string/pref_title_notification_settings">
<CheckBoxPreference
android:key="pullNotifications"
android:title="@string/pref_title_pull_notifications"
android:summary="@string/pref_summary_pull_notifications"
android:defaultValue="true" />
<ListPreference
android:dependency="pullNotifications"
android:key="pullNotificationCheckInterval"
android:title="@string/pref_title_pull_notification_check_interval"
android:summary="@string/pref_summary_pull_notification_check_interval"
android:entries="@array/pull_notification_check_interval_names"
android:entryValues="@array/pull_notification_check_intervals"
android:defaultValue="15" />
<CheckBoxPreference
android:dependency="pullNotifications"
android:key="notificationAlertSound"
android:title="@string/pref_title_notification_alert_sound"
android:defaultValue="true" />
<CheckBoxPreference
android:dependency="pullNotifications"
android:key="notificationStyleVibrate"
android:title="@string/pref_title_notification_style_vibrate"
android:defaultValue="false" />
<CheckBoxPreference
android:dependency="pullNotifications"
android:key="notificationStyleLight"
android:title="@string/pref_title_notification_style_light"
android:defaultValue="false" />
</PreferenceCategory>
</PreferenceScreen>