Skip to content

Commit

Permalink
Merge branch 'release/0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownmoon committed Jun 7, 2016
2 parents 37f46eb + b105c62 commit 4d52de6
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file removed app/build/outputs/apk/app-debug-unaligned.apk
Binary file not shown.
Binary file removed app/build/outputs/apk/app-debug.apk
Binary file not shown.
10 changes: 10 additions & 0 deletions app/src/main/java/unknownmoon/cryforlight/LightService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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);
Expand Down
73 changes: 62 additions & 11 deletions app/src/main/java/unknownmoon/cryforlight/SliderPreference.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -65,7 +93,7 @@ protected void onBindView(View view) {

if (seekBarView != null) {

seekBarView.setMax(mSliderMax);
updateSliderMax(seekBarView, mSliderMax);
seekBarView.setProgress(mSliderValue);

seekBarView.setOnSeekBarChangeListener(this);
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
<string name="pref_sound_file_title">Sound</string>
<string name="current_brightness_header">Current Info</string>
<string name="current_brightness_value_format">%1$.2f lx</string>
<string name="pref_light_max_title">Max Brightness</string>
<string name="pref_light_max_summary">The max value of the brightness setting.</string>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/xml/preferences_light.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
android:key="light_threshold"
android:title="@string/pref_cat_light">

<EditTextPreference
android:defaultValue="@integer/pref_light_def_max_val"
android:key="pref_light_max"
android:title="@string/pref_light_max_title"
android:summary="@string/pref_light_max_summary"
android:inputType="number"
/>

<unknownmoon.cryforlight.SliderPreference
android:defaultValue="@integer/pref_light_def_val"
android:key="pref_light"
Expand Down

0 comments on commit 4d52de6

Please sign in to comment.