Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Commit

Permalink
#178 changing custom summary dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
niccs committed Mar 21, 2016
1 parent e6e61a0 commit 5908d40
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/org/iilab/pb/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@ protected void onStart() {

@Override
public void onBackPressed() {
Log.d(TAG, " outttt fragment back pressed in " + currentPage.getId());
Log.d(TAG, " fragment back pressed in " + currentPage.getId());
if (pageId.equals(PAGE_HOME_READY)) {
// don't go back
// finish();
// startActivity(AppUtil.behaveAsHomeButton());
} else if (currentPage.getComponent() != null && currentPage.getComponent().equals(PAGE_COMPONENT_ADVANCED_SETTINGS)) {
Log.d(TAG, " handling fragment back pressed in " + pageId);
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
Expand All @@ -28,14 +29,15 @@
import static org.iilab.pb.common.AppConstants.PAGE_SETUP_ALARM_RETRAINING;
import static org.iilab.pb.common.AppConstants.PARENT_ACTIVITY;
import static org.iilab.pb.common.ApplicationSettings.getCustomSettings;
import static org.iilab.pb.common.ApplicationSettings.getInitialClicksForAlertTrigger;
import static org.iilab.pb.common.ApplicationSettings.isAlarmConfirmationRequired;
import static org.iilab.pb.common.ApplicationSettings.setAlarmConfirmationRequired;
import static org.iilab.pb.common.ApplicationSettings.setConfirmationFeedbackVibrationPattern;
import static org.iilab.pb.common.ApplicationSettings.setInitialClicksForAlertTrigger;

public class AdvancedSettingsFragment extends PreferenceFragmentCompat {
public class AdvancedSettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = AdvancedSettingsFragment.class.getName();

// CheckBoxPreference customPreference = (CheckBoxPreference) findPreference(getString(R.string.customKey));
public static AdvancedSettingsFragment newInstance(String pageId, int parentActivity) {
AdvancedSettingsFragment f = new AdvancedSettingsFragment();
Bundle args = new Bundle();
Expand Down Expand Up @@ -67,6 +69,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
} else {
enableConfirmationPatterns(false);
}
customPreference.setSummary(getCustomSummary());

Preference redoTrainingButton = (Preference) findPreference(getString(R.string.redoTrainingKey));
redoTrainingButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
Expand Down Expand Up @@ -100,31 +103,6 @@ public boolean onPreferenceChange(Preference preference, Object selectedValue) {
}
});

Preference alertConfirmationSettings = (Preference) findPreference(getString(R.string.confirmationSequenceKey));

alertConfirmationSettings.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object selectedValue) {
Log.d(TAG, "Inside of Alarm confirmation settings");

if (selectedValue.equals(getString(R.string.confirmationSequenceDefault))) {
// disable Confirmation Wait Time/ Confirmation Wait Vibration
enableConfirmationPatterns(false);
setAlarmConfirmationRequired(getActivity(), false);
setConfirmationFeedbackVibrationPattern(getActivity(), ALARM_SENDING_CONFIRMATION_PATTERN_NONE);
Log.d(TAG, "default confirmation press deactivated");
} else {
// enable Confirmation Wait Time/ Confirmation Wait Vibration
enableConfirmationPatterns(true);
setAlarmConfirmationRequired(getActivity(), true);
setConfirmationFeedbackVibrationPattern(getActivity(), ALARM_SENDING_CONFIRMATION_PATTERN_LONG);
Log.d(TAG, "Confirmation press enabled");
}
return true;
}
});


default7RepeatedPress.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object selectedValue) {
Expand All @@ -138,6 +116,7 @@ public boolean onPreferenceChange(Preference preference, Object selectedValue) {
setConfirmationFeedbackVibrationPattern(getActivity(), ALARM_SENDING_CONFIRMATION_PATTERN_NONE);
Log.d(TAG, "Default 7 presses to trigger alarm without confirmation click");
}
customPreference.setSummary(getCustomSummary());
return true;
}
});
Expand All @@ -156,6 +135,7 @@ public boolean onPreferenceChange(Preference preference, Object selectedValue) {
setConfirmationFeedbackVibrationPattern(getActivity(), ALARM_SENDING_CONFIRMATION_PATTERN_LONG);
Log.d(TAG, "Default 5 presses to trigger alarm with confirmation click");
}
customPreference.setSummary(getCustomSummary());
return true;
}
});
Expand All @@ -171,6 +151,8 @@ public boolean onPreferenceChange(Preference preference, Object selectedValue) {
customSettings.setEnabled(true);
setConfirmationFeedbackVibrationPattern(getActivity(), ALARM_SENDING_CONFIRMATION_PATTERN_NONE);
Log.d(TAG, "Custom setting are enabled and confirmation click defaults to false");
}else{
customSettings.setEnabled(false);
}
return true;
}
Expand Down Expand Up @@ -216,5 +198,43 @@ private void displayNotification() {
// pass the Notification object to the system
myNotificationManager.notify(notifyID, mBuilder.build());
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "called for advanced settings");
if (key.equals(R.string.initialPressesKey)) {
Preference initialClicks = findPreference(key);
// Set summary to be the user-description for the selected value
//// x repeated press with confirmation
CheckBoxPreference customPreference = (CheckBoxPreference) findPreference(getString(R.string.customKey));
customPreference.setSummary(sharedPreferences.getString(key, "")+"repeated press");
}
}

@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
Log.d(TAG, "on Content changed called for main settings onResume");
CheckBoxPreference customPreference = (CheckBoxPreference) findPreference(getString(R.string.customKey));
Log.d(TAG, "inside initial clicks " +customPreference);
customPreference.setSummary(getCustomSummary());
}

@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
Log.d(TAG, "on Content changed called for main settings onPause");
}

private String getCustomSummary(){
String initialClicks=getInitialClicksForAlertTrigger(getActivity());
String confirmationString=(isAlarmConfirmationRequired(getActivity())? "with ":"with no ");

return (initialClicks + " Repeated press "+confirmationString+"confirmation");
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.iilab.pb.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.util.Log;
Expand All @@ -15,7 +17,20 @@
import static org.iilab.pb.common.ApplicationSettings.setAlarmConfirmationRequired;
import static org.iilab.pb.common.ApplicationSettings.setConfirmationFeedbackVibrationPattern;

public class AdvancedSettingsSubScreenFragment extends PreferenceFragmentCompat {
public class AdvancedSettingsSubScreenFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG,"called for advanced settings sub screen "+ key+" "+R.string.initialPressesKey);
if (key.equals(getString(R.string.initialPressesKey))) {
Preference initialClicks = findPreference(key);
// Set summary to be the user-description for the selected value
//// x repeated press with confirmation
CheckBoxPreference customPreference = (CheckBoxPreference) findPreference(getString(R.string.customKey));
Log.d(TAG, "inside initial clicks " +customPreference);
// customPreference.setSummary(sharedPreferences.getString(key, "")+"repeated press");
}
}

private static final String TAG = AdvancedSettingsSubScreenFragment.class.getName();

public static AdvancedSettingsSubScreenFragment newInstance(String pageId, int parentActivity) {
Expand Down Expand Up @@ -68,5 +83,19 @@ private void enableConfirmationPatterns(boolean flag) {
Preference confirmationWaitVibration = (Preference) findPreference(getString(R.string.hapticFeedbackVibrationPatternKey));
confirmationWaitVibration.setEnabled(flag);
}

@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
}

0 comments on commit 5908d40

Please sign in to comment.