-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefreshDialogFragment.java
More file actions
130 lines (119 loc) · 5.11 KB
/
Copy pathRefreshDialogFragment.java
File metadata and controls
130 lines (119 loc) · 5.11 KB
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
package com.your.application;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by anthony on 2/21/14.
*/
public class RefreshDialogFragment extends DialogFragment {
private SharedPreferences mSharedPrefs;
private long duration = 3 * 60 * 60 * 1000; // default 3 hour duration for refresh
private boolean min = false, hour = false;
public RefreshDialogFragment(){
super();
}
public static RefreshDialogFragment newInstance(){
return new RefreshDialogFragment();
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
final View content = inflater.inflate(R.layout.refresh_dialog, null);
//
// Get shared preferences for application package
//
mSharedPrefs = getActivity().getSharedPreferences(
"com.your.appllication", Context.MODE_PRIVATE);
dialog.setView(content);
setFonts(content);
setmSharedPrefs(content);
dialog.setPositiveButton(getString(R.string.set), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
onSaveDuration(content);// Save the duration to Shared Preferences
Toast.makeText(getActivity(), getString(R.string.time_set), Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getActivity(), "Error saving refresh time", Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do nothing on cancel
}
});
return dialog.create();
}
//
// Set's the fonts of the view's to the same as muzei
// Comment out if it's not desirable
// You must have the "alegreya-regular.ttf" in your /assets directory
public void setFonts(View content){
TextView refreshTitle = (TextView)content.findViewById(R.id.refreshTitle);
EditText refreshText = (EditText)content.findViewById(R.id.refreshTime);
RadioButton minRad = (RadioButton)content.findViewById(R.id.radMin);
RadioButton hourRad = (RadioButton)content.findViewById(R.id.radHour);
Typeface light = Typeface.createFromAsset(getActivity().getAssets(), "alegreya-regular.ttf");
refreshText.setTypeface(light);
refreshTitle.setTypeface(light);
minRad.setTypeface(light);
hourRad.setTypeface(light);
}
//
// Writes the duration (in milliseconds) to shared preferences
//
public void onSaveDuration(View content){
EditText refreshTime = (EditText)content.findViewById(R.id.refreshTime);
String refresh = refreshTime.getText().toString();
try{
duration = Long.parseLong(refresh);
duration = getTime(content, duration);
mSharedPrefs.edit().putLong("duration", duration).apply();
mSharedPrefs.edit().putBoolean("min", min).apply();
mSharedPrefs.edit().putBoolean("hour", hour).apply();
}catch(Exception BadIntException){
Toast.makeText(getActivity(), "Error setting refresh", Toast.LENGTH_LONG).show();
}
}
// Determine the correct refresh time, whether it's minutes or seconds
// Then convert that to milliseconds for muzei
// Return it
public long getTime(View content, Long refreshTime){
RadioButton radMin = (RadioButton)content.findViewById(R.id.radMin);
if(radMin.isChecked()){
min = true;
return refreshTime * 60000;
}
else{
hour = true;
return refreshTime * 60 * 60 * 1000;
}
}
//
// Set text field, and radio buttons to it's saved state for startup
//
public void setmSharedPrefs(View content){
EditText refreshText = (EditText)content.findViewById(R.id.refreshTime);
RadioButton minRad = (RadioButton)content.findViewById(R.id.radMin);
RadioButton hourRad = (RadioButton)content.findViewById(R.id.radHour);
if(mSharedPrefs.getBoolean("min", min))
minRad.setChecked(true);
else
hourRad.setChecked(true);
if(minRad.isChecked())
refreshText.setText("" + mSharedPrefs.getLong("duration", duration) / 60000);
else
refreshText.setText("" + mSharedPrefs.getLong("duration", duration)/3600000);
}
}