diff --git a/app/build.gradle b/app/build.gradle index 6c3878d..bee2586 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "unknownmoon.cryforlight" minSdkVersion 23 targetSdkVersion 23 - versionCode 4 - versionName "0.2.2" + versionCode 5 + versionName "0.2.3" } buildTypes { release { diff --git a/app/build/outputs/apk/app-debug-unaligned.apk b/app/build/outputs/apk/app-debug-unaligned.apk deleted file mode 100644 index a3c3d1b..0000000 Binary files a/app/build/outputs/apk/app-debug-unaligned.apk and /dev/null differ diff --git a/app/build/outputs/apk/app-debug.apk b/app/build/outputs/apk/app-debug.apk deleted file mode 100644 index 5fed94e..0000000 Binary files a/app/build/outputs/apk/app-debug.apk and /dev/null differ diff --git a/app/src/main/java/unknownmoon/cryforlight/LightService.java b/app/src/main/java/unknownmoon/cryforlight/LightService.java index e6631bf..e3c5511 100644 --- a/app/src/main/java/unknownmoon/cryforlight/LightService.java +++ b/app/src/main/java/unknownmoon/cryforlight/LightService.java @@ -34,6 +34,7 @@ public class LightService extends Service { private int mPrefSoundLevel; private String mPrefSoundFile; private int mPrefLightThreshold; + private int mPrefLightThresholdMaxValue; private float mLastBrightness; private Boolean mIsCrying = false; private Ringtone mRingtone; @@ -162,6 +163,7 @@ private void broadcastStopped() { private void syncPrefs() { syncPref("pref_light"); + syncPref("pref_light_max"); syncPref("pref_sound_level"); syncPref("pref_sound_file"); } @@ -173,6 +175,9 @@ private void syncPref(String key) { case "pref_light": updateLightThreshold(sharedPrefs.getInt(key, mPrefLightThreshold)); break; + case "pref_light_max": + updateLightThresholdMaxValue(Integer.parseInt(sharedPrefs.getString(key, "" + mPrefLightThresholdMaxValue))); + break; case "pref_sound_level": updateSoundLevel(sharedPrefs.getInt(key, mPrefSoundLevel)); break; @@ -245,6 +250,11 @@ private void updateLightThreshold(int lvl) { Log.d(TAG, "light - " + lvl); } + private void updateLightThresholdMaxValue(int max) { + mPrefLightThresholdMaxValue = max; + updateLightThreshold(Math.min(mPrefLightThreshold, mPrefLightThresholdMaxValue)); + } + private void shouldWeCry() { if (mLastBrightness <= mPrefLightThreshold && !mIsCrying) { diff --git a/app/src/main/java/unknownmoon/cryforlight/SettingsLightFragment.java b/app/src/main/java/unknownmoon/cryforlight/SettingsLightFragment.java index 9af06d5..7ae6410 100644 --- a/app/src/main/java/unknownmoon/cryforlight/SettingsLightFragment.java +++ b/app/src/main/java/unknownmoon/cryforlight/SettingsLightFragment.java @@ -6,6 +6,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceFragment; +import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -14,7 +15,7 @@ * A simple {@link Fragment} subclass. */ public class SettingsLightFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { - + public final String TAG = "SettingsLight"; public SettingsLightFragment() { // Required empty public constructor @@ -27,6 +28,15 @@ public void onCreate(Bundle savedInstanceState) { addPreferencesFromResource(R.xml.preferences_light); } + @Override + public void onStart() { + super.onStart(); + + // initialise the saved max value. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + int max = Integer.parseInt(sharedPreferences.getString("pref_light_max", "-1")); + broadcastConfigMaxChanged(max, "pref_light"); + } @Override public void onResume() { @@ -48,13 +58,37 @@ public void onPause() { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("pref_light")) { // TODO - Log.d("Light", String.format("%s: %d", key, sharedPreferences.getInt(key, getResources().getInteger(R.integer.pref_light_def_val)))); + Log.d(TAG, String.format("%s: %d", key, sharedPreferences.getInt(key, getResources().getInteger(R.integer.pref_light_def_val)))); broadcastConfigChanged("pref_light"); + } else if (key.equals("pref_light_max")) { + int max = Integer.parseInt(sharedPreferences.getString(key, "-1")); + + Log.d(TAG, String.format("%s: %d", key, max)); + broadcastConfigMaxChanged(max, "pref_light"); + broadcastConfigChanged("pref_light_max"); } } + /** + * Send to SliderPreference only + * + * @param max New max value. + * @param key Preference key of target preference. + */ + private void broadcastConfigMaxChanged(int max, String key) { + Intent notifyStartedIntent = new Intent("on-slider-max-changed"); + notifyStartedIntent.putExtra("changedMax", max); + notifyStartedIntent.putExtra("key", key); + LocalBroadcastManager.getInstance(getContext()).sendBroadcast(notifyStartedIntent); + } + + /** + * General notification, for example for sending to LightService + * + * @param key Changed key. + */ private void broadcastConfigChanged(String key) { - // Answer the started + Intent notifyStartedIntent = new Intent("on-configuration-changed"); notifyStartedIntent.putExtra("changedKey", key); LocalBroadcastManager.getInstance(getContext()).sendBroadcast(notifyStartedIntent); diff --git a/app/src/main/java/unknownmoon/cryforlight/SliderPreference.java b/app/src/main/java/unknownmoon/cryforlight/SliderPreference.java index 9446a8f..08f1001 100644 --- a/app/src/main/java/unknownmoon/cryforlight/SliderPreference.java +++ b/app/src/main/java/unknownmoon/cryforlight/SliderPreference.java @@ -1,9 +1,15 @@ package unknownmoon.cryforlight; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.res.TypedArray; import android.preference.Preference; +import android.preference.PreferenceManager; +import android.support.v4.content.LocalBroadcastManager; import android.util.AttributeSet; +import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.SeekBar; @@ -12,19 +18,20 @@ /** * Created by jing on 05/06/2016. */ -public class SliderPreference extends Preference implements SeekBar.OnSeekBarChangeListener { - +public class SliderPreference extends Preference implements SeekBar.OnSeekBarChangeListener, PreferenceManager.OnActivityDestroyListener { public static Boolean DISPLAY_HEADER_LABEL = true; public static Boolean DISPLAY_HEADER_VALUE = true; public static int SLIDER_MAX = 100; - + public final String TAG = "SliderPreference"; private Boolean mDisplayHeaderLabel; private Boolean mDisplayHeaderValue; private String mSummary; private String mHeaderLabelText; private String mHeaderValueFormat; + private int DEFAULT_SLIDER_MAX; private int mSliderMax; private int mSliderValue; + private BroadcastReceiver mMessageReceiver; public SliderPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); @@ -37,7 +44,28 @@ public SliderPreference(Context context, AttributeSet attrs, int defStyleAttr, i mSummary = a.getString(R.styleable.SliderPreference_summary); mHeaderLabelText = a.getString(R.styleable.SliderPreference_headerLabelText); mHeaderValueFormat = a.getString(R.styleable.SliderPreference_headerValueFormat); - mSliderMax = a.getInt(R.styleable.SliderPreference_sliderMaxValue, SLIDER_MAX); + DEFAULT_SLIDER_MAX = a.getInt(R.styleable.SliderPreference_sliderMaxValue, SLIDER_MAX); + mSliderMax = getPersistedInt(DEFAULT_SLIDER_MAX); + + mMessageReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, final Intent intent) { + String key = intent.getStringExtra("key"); + + if (key.equals(getKey())) { + int changedMax = intent.getIntExtra("changedMax", -1); + Log.d(TAG, "changedMax: " + changedMax); + if (changedMax > 0) { + Log.d(TAG, "update slider max: " + mSliderMax); + updateSliderMax(null, changedMax); + persistSlideValue(); + } + } + } + }; + + LocalBroadcastManager.getInstance(getContext()).registerReceiver(mMessageReceiver, + new IntentFilter("on-slider-max-changed")); a.recycle(); } @@ -65,7 +93,7 @@ protected void onBindView(View view) { if (seekBarView != null) { - seekBarView.setMax(mSliderMax); + updateSliderMax(seekBarView, mSliderMax); seekBarView.setProgress(mSliderValue); seekBarView.setOnSeekBarChangeListener(this); @@ -112,9 +140,28 @@ protected void onSetInitialValue(boolean restorePersistedValue, Object defaultVa } - private void updateHeaderValueText(TextView v, int value) { - if (v != null) { - v.setText(String.format(mHeaderValueFormat, value)); + private void updateSliderMax(SeekBar seekBarView, int max) { + if (seekBarView == null) { + seekBarView = (SeekBar) getView(null, null).findViewById(R.id.pref_seek_bar); + } + + mSliderMax = max > 0 ? max : DEFAULT_SLIDER_MAX; + + if (seekBarView != null) { + seekBarView.setMax(max); + mSliderValue = Math.min(mSliderValue, max); + seekBarView.setProgress(mSliderValue); + } + } + + private void updateHeaderValueText(TextView headerValueView, int value) { + + if (headerValueView == null) { + headerValueView = (TextView) getView(null, null).findViewById(R.id.pref_header_value); + } + + if (headerValueView != null) { + headerValueView.setText(String.format(mHeaderValueFormat, value)); } } @@ -124,13 +171,13 @@ private void updateSlideValue(int newValue) { private void persistSlideValue() { persistInt(mSliderValue); + notifyChanged(); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - TextView headerValueView = (TextView) getView(null, (ViewGroup) seekBar.getParent()).findViewById(R.id.pref_header_value); updateSlideValue(progress); - updateHeaderValueText(headerValueView, progress); + updateHeaderValueText((TextView) ((ViewGroup) seekBar.getParent()).findViewById(R.id.pref_header_value), progress); } @Override @@ -140,6 +187,10 @@ public void onStartTrackingTouch(SeekBar seekBar) { @Override public void onStopTrackingTouch(SeekBar seekBar) { persistSlideValue(); - notifyChanged(); + } + + @Override + public void onActivityDestroy() { + LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mMessageReceiver); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4cb7c55..0b38eb5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,4 +20,6 @@ Sound Current Info %1$.2f lx + Max Brightness + The max value of the brightness setting. diff --git a/app/src/main/res/xml/preferences_light.xml b/app/src/main/res/xml/preferences_light.xml index e005fda..c9b112a 100644 --- a/app/src/main/res/xml/preferences_light.xml +++ b/app/src/main/res/xml/preferences_light.xml @@ -6,6 +6,14 @@ android:key="light_threshold" android:title="@string/pref_cat_light"> + +