quick&dirty preference screen. Allows to enable/disable SMS/K9/generic notifications

live-sensor-data
Andreas Shimokawa 2015-03-06 14:00:56 +01:00
parent bc40f41eab
commit e52e26168a
10 changed files with 74 additions and 185 deletions

View File

@ -90,8 +90,8 @@ public class ControlCenter extends ActionBarActivity {
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//Intent intent = new Intent(this, SettingsActivity.class);
//startActivity(intent);
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.action_debug) {

View File

@ -3,9 +3,11 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.PowerManager;
import android.preference.PreferenceManager;
public class K9Receiver extends BroadcastReceiver {
@ -15,10 +17,16 @@ public class K9Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
if (powermanager.isScreenOn()) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("notifications_k9mail", true)) {
return;
}
if (!sharedPrefs.getBoolean("notifications_k9mail_whenscreenon", false)) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
if (powermanager.isScreenOn()) {
return;
}
}
// get sender and subject from the Intent
String sender = intent.getStringExtra("com.fsck.k9.intent.extra.FROM");

View File

@ -2,8 +2,10 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.app.Notification;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
@ -28,10 +30,16 @@ public class NotificationListener extends NotificationListenerService {
String source = sbn.getPackageName();
Log.i(TAG, source);
PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
if (powermanager.isScreenOn() && !source.equals(getPackageName())) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!sharedPrefs.getBoolean("notifications_generic", true)) {
return;
}
if (!sharedPrefs.getBoolean("notifications_generic_whenscreenon", false)) {
PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
if (powermanager.isScreenOn()) {
return;
}
}
Notification notification = sbn.getNotification();

View File

@ -3,8 +3,10 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
@ -12,10 +14,16 @@ public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
if (powermanager.isScreenOn()) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!sharedPrefs.getBoolean("notifications_sms", true)) {
return;
}
if (!sharedPrefs.getBoolean("notifications_sms_whenscreenon", false)) {
PowerManager powermanager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
if (powermanager.isScreenOn()) {
return;
}
}
Bundle bundle = intent.getExtras();
if (bundle != null) {

View File

@ -3,9 +3,6 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
@ -14,8 +11,6 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;
import java.util.List;
@ -51,6 +46,7 @@ public class SettingsActivity extends PreferenceActivity {
* Shows the simplified settings UI if the device configuration if the
* device configuration dictates that a simplified, single-pane UI should be
* shown.
* shown.
*/
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
@ -69,19 +65,12 @@ public class SettingsActivity extends PreferenceActivity {
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_notification);
// Add 'data and sync' preferences, and a corresponding header.
fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_data_sync);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_data_sync);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
bindPreferenceSummaryToValue(findPreference("example_text"));
bindPreferenceSummaryToValue(findPreference("example_list"));
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
//bindPreferenceSummaryToValue(findPreference("notifications_sms"));
//bindPreferenceSummaryToValue(findPreference("notifications_sms_whenscreenon"));
}
/**
@ -146,27 +135,6 @@ public class SettingsActivity extends PreferenceActivity {
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
@ -236,23 +204,4 @@ public class SettingsActivity extends PreferenceActivity {
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class DataSyncPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_data_sync);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
}
}

View File

@ -1,60 +1,12 @@
<resources>
<!-- Strings related to Settings -->
<string name="pref_header_general" >General Settings</string>
<!-- Example General settings -->
<string name="pref_header_general">General</string>
<string name="pref_title_social_recommendations">Enable social recommendations</string>
<string name="pref_description_social_recommendations">Recommendations for people to contact
based on your message history
</string>
<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>
<string name="pref_title_add_friends_to_messages">Add friends to messages</string>
<string-array name="pref_example_list_titles">
<item>Always</item>
<item>When possible</item>
<item>Never</item>
</string-array>
<string-array name="pref_example_list_values">
<item>1</item>
<item>0</item>
<item>-1</item>
</string-array>
<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>
<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>
<string name="pref_title_system_sync_settings">System sync settings</string>
<!-- Example settings for Notifications -->
<!-- Strings related to Notifications -->
<string name="pref_header_notifications">Notifications</string>
<string name="pref_title_new_message_notifications">New message notifications</string>
<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>
<string name="pref_title_vibrate">Vibrate</string>
<string name="pref_title_notifications_sms">Notification for SMS</string>
<string name="pref_title_notifications_k9mail">Notification for K9-Mail</string>
<string name="pref_title_notifications_generic">Generic notification support</string>
<string name="pref_title_whenscreenon">… also when screen is on</string>
</resources>

View File

@ -1,21 +0,0 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:key="sync_frequency"
android:title="@string/pref_title_sync_frequency"
android:entries="@array/pref_sync_frequency_titles"
android:entryValues="@array/pref_sync_frequency_values"
android:defaultValue="180"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
<!-- This preference simply launches an intent when selected. Use this UI sparingly, per
design guidelines. -->
<Preference android:title="@string/pref_title_system_sync_settings">
<intent android:action="android.settings.SYNC_SETTINGS" />
</Preference>
</PreferenceScreen>

View File

@ -1,33 +1,3 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="example_checkbox"
android:title="@string/pref_title_social_recommendations"
android:summary="@string/pref_description_social_recommendations"
android:defaultValue="true" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:key="example_text"
android:title="@string/pref_title_display_name"
android:defaultValue="@string/pref_default_display_name"
android:selectAllOnFocus="true"
android:inputType="textCapWords"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:key="example_list"
android:title="@string/pref_title_add_friends_to_messages"
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null" />
</PreferenceScreen>

View File

@ -9,7 +9,4 @@
android:fragment="nodomain.freeyourgadget.gadgetbridge.SettingsActivity$NotificationPreferenceFragment"
android:title="@string/pref_header_notifications" />
<header android:fragment="nodomain.freeyourgadget.gadgetbridge.SettingsActivity$DataSyncPreferenceFragment"
android:title="@string/pref_header_data_sync" />
</preference-headers>

View File

@ -3,25 +3,43 @@
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<CheckBoxPreference
android:key="notifications_new_message"
android:title="@string/pref_title_new_message_notifications"
android:key="notifications_sms"
android:title="@string/pref_title_notifications_sms"
android:defaultValue="true" />
<!-- Allows the user to choose a ringtone in the 'notification' category. -->
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<!-- NOTE: RingtonePreference's summary should be set to its value by the activity code. -->
<RingtonePreference
android:dependency="notifications_new_message"
android:key="notifications_new_message_ringtone"
android:title="@string/pref_title_ringtone"
android:ringtoneType="notification"
android:defaultValue="content://settings/system/notification_sound" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<CheckBoxPreference
android:dependency="notifications_new_message"
android:key="notifications_new_message_vibrate"
android:title="@string/pref_title_vibrate"
android:dependency="notifications_sms"
android:key="notifications_sms_whenscreenon"
android:title="@string/pref_title_whenscreenon"
android:defaultValue="false" />
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<CheckBoxPreference
android:key="notifications_k9mail"
android:title="@string/pref_title_notifications_k9mail"
android:defaultValue="true" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<CheckBoxPreference
android:dependency="notifications_k9mail"
android:key="notifications_k9mail_whenscreenon"
android:title="@string/pref_title_whenscreenon"
android:defaultValue="false" />
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<CheckBoxPreference
android:key="notifications_generic"
android:title="@string/pref_title_notifications_generic"
android:defaultValue="true" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<CheckBoxPreference
android:dependency="notifications_generic"
android:key="notifications_generic_whenscreenon"
android:title="@string/pref_title_whenscreenon"
android:defaultValue="false" />
</PreferenceScreen>