My favorites | Sign in
Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package divestoclimb.gasmixer;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

import divestoclimb.lib.scuba.Mix;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* A DialogPreference that displays a single TrimixSelector
* @author Ben Roberts (divestoclimb@gmail.com)
*/
public class TrimixPreference extends DialogPreference {
private TrimixSelector mTrimixSelector;

private Mix mMix;
private String mMixString;
private static final NumberFormat nf = new DecimalFormat(".###");

public TrimixPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

mTrimixSelector = new TrimixSelector(context, attrs);
}

public TrimixPreference(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.dialogPreferenceStyle);
}

public TrimixPreference(Context context) {
this(context, null);
}

private static String mixToString(Mix mix) {
return nf.format(mix.getfO2())+" "+nf.format(mix.getfHe());
}

public static Mix stringToMix(String s) {
String ss[] = s.split("\\s");
try {
return new Mix(nf.parse(ss[0]).floatValue(), nf.parse(ss[1]).floatValue());
} catch(ParseException e) { return null; }
}

public void setMix(String mixString) {

mMix = stringToMix(mixString);
mMixString = mixString;

persistString(mixString);

}

public Mix getMix() {
return mMix;
}

@Override
protected View onCreateDialogView() {
return mTrimixSelector;
}

@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);

mTrimixSelector.setMix(mMix);

}

@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);

if(positiveResult) {
String mixString = mixToString(mTrimixSelector.getMix());
if(callChangeListener(mixString)) {
setMix(mixString);
}
}
((ViewGroup)mTrimixSelector.getParent()).removeView(mTrimixSelector);
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setMix(restoreValue? getPersistedString(mMixString): (String)defaultValue);
}

public TrimixSelector getTrimixSelector() {
return mTrimixSelector;
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
return superState;
}

final SavedState myState = new SavedState(superState);
myState.mixString = mixToString(mMix);
return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}

SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
setMix(myState.mixString);
}

private static class SavedState extends BaseSavedState {
String mixString;

public SavedState(Parcel source) {
super(source);
mixString = source.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(mixString);
}

public SavedState(Parcelable superState) {
super(superState);
}

public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

}
Show details Hide details

Change log

r49 by divestoclimb on Nov 27, 2009   Diff
Version 2.7
Go to: 
Project members, sign in to write a code review

Older revisions

r41 by divestoclimb on Oct 11, 2009   Diff
Documentation updates for Gas Mixer
and the CylinderSizeClient that I
forgot to commit earlier
r20 by divestoclimb on Oct 04, 2009   Diff
reorg
r16 by divestoclimb on Oct 01, 2009   Diff
Version 2.0 source, major rewrite
All revisions of this file

File info

Size: 3966 bytes, 163 lines
Hosted by Google Code