/
UpdaterFragment.java
244 lines (183 loc) · 9.52 KB
/
UpdaterFragment.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.aero.control.fragments;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.aero.control.AeroActivity;
import com.aero.control.R;
import com.aero.control.helpers.updateHelper;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
/**
* Created by Alexander Christ on 16.09.13.
* Default Updater Fragment
*/
public class UpdaterFragment extends PreferenceFragment {
private static final String SDPATH = Environment.getExternalStorageDirectory().getPath();
private static final String timeStamp = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()).format(Calendar.getInstance().getTime());
private static final File BACKUP_PATH = new File(SDPATH + "/com.aero.control/backup/" + timeStamp + "/zImage");
private static final File IMAGE = new File (AeroActivity.files.zImage);
private static final String AERO_PATH = "/sdcard/com.aero.control/backup";
private static final updateHelper update = new updateHelper();
private Preference mBackupKernel;
private ListPreference mRestoreKernel;
private String mBackup = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We have to load the xml layout first;
addPreferencesFromResource(R.layout.updater_fragment);
PreferenceScreen root = this.getPreferenceScreen();
mBackupKernel = root.findPreference("backup_kernel");
mRestoreKernel = (ListPreference) root.findPreference("restore_kernel");
for (String s : AeroActivity.files.BACKUPPATH) {
if (AeroActivity.genHelper.doesExist(s)) {
mBackup = s;
}
}
// If device doesn't have this kernel path;
if (AeroActivity.shell.getInfo(AeroActivity.files.zImage).equals("Unavailable"))
mBackupKernel.setEnabled(false);
if (mBackup != null)
mBackupKernel.setEnabled(true);
if (AeroActivity.shell.getInfo(AeroActivity.files.zImage).equals("Unavailable"))
mRestoreKernel.setEnabled(false);
// Fresh Start, no backup found;
try {
mBackupKernel.setSummary(getText(R.string.last_backup_from)+ " " + AeroActivity.shell.getDirInfo(SDPATH + "/com.aero.control/backup", false)[0]);
mRestoreKernel.setEnabled(true);
} catch (NullPointerException e) {
mBackupKernel.setSummary(getText(R.string.last_backup_from)+ " " + getText(R.string.unavailable));
mRestoreKernel.setEnabled(false);
}
mBackupKernel.setIcon(R.drawable.ic_action_copy);
mRestoreKernel.setIcon(R.drawable.ic_action_time);
mRestoreKernel.setEntries(AeroActivity.shell.getDirInfo(SDPATH + File.separator + "/com.aero.control/backup/", false));
mRestoreKernel.setEntryValues(AeroActivity.shell.getDirInfo(SDPATH + "/com.aero.control/backup/", false));
mRestoreKernel.setDialogIcon(R.drawable.restore);
mRestoreKernel.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
final String s = (String) o;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.about_screen, null);
TextView aboutText = (TextView) layout.findViewById(R.id.aboutScreen);
builder.setTitle(getText(R.string.backup_from) + " " + s);
aboutText.setText(getText(R.string.restore_from_backup) + " " + s + " ?");
AeroActivity.shell.remountSystem();
// Remove it;
preference.getEditor().remove(preference.getKey()).commit();
builder.setView(layout)
.setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (mBackup != null)
restoreBoot(s);
else
restorezImage(s);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do nothing
}
});
builder.show();
return true;
};
});
mBackupKernel.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.about_screen, null);
TextView aboutText = (TextView) layout.findViewById(R.id.aboutScreen);
builder.setTitle("Backup");
builder.setIcon(R.drawable.backup);
aboutText.setText(R.string.proceed_backup);
builder.setView(layout)
.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (mBackup != null)
backupBoot();
else
backupzImage();
mBackupKernel.setSummary(getText(R.string.last_backup_from) + " " + timeStamp);
// Prepare the UI, otherwise it would throw a Exception;
mRestoreKernel.setEntries(AeroActivity.shell.getDirInfo(SDPATH + "/com.aero.control/backup/", false));
mRestoreKernel.setEntryValues(AeroActivity.shell.getDirInfo(SDPATH + "/com.aero.control/backup/", false));
mRestoreKernel.setEnabled(true);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do nothing
}
});
builder.show();
return true;
}
;
});
}
private void backupzImage() {
try {
update.copyFile(IMAGE, BACKUP_PATH, false);
Toast.makeText(getActivity(), "Backup was successful!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("Aero", "A problem occured while saving a backup.", e);
}
}
private void backupBoot() {
String backuppath = AERO_PATH + "/" + timeStamp;
// Create file-structure if necessary;
if (!(AeroActivity.genHelper.doesExist(AERO_PATH))) {
if (!(new File(AERO_PATH).mkdir()))
if (!(new File(AERO_PATH).mkdirs()))
Log.e("Aero", "Couldn't create file: " + AERO_PATH);
}
if (!(AeroActivity.genHelper.doesExist(backuppath))) {
if (!(new File(backuppath).mkdir()))
if (!(new File(backuppath).mkdirs()))
Log.e("Aero", "Couldn't create file: " + backuppath);
}
String[] commands = new String[] {
"dd if=" + mBackup + " " + "of=" + backuppath + "/boot.img",
"chmod 777 " + backuppath + "/boot.img"
};
AeroActivity.shell.setRootInfo(commands);
}
private void restorezImage(String s) {
// Delete old zImage first, then copy backup;
String[] commands = new String[] {
"rm -f " + AeroActivity.files.zImage,
"cp " + "/sdcard/com.aero.control/backup/" + s + "/zImage" + " " + AeroActivity.files.zImage,
};
AeroActivity.shell.setRootInfo(commands);
Toast.makeText(getActivity(), R.string.need_reboot, Toast.LENGTH_LONG).show();
}
private void restoreBoot(String s) {
String filepath = new File("/sdcard/com.aero.control/backup/" + s + "/boot.img").getPath();
String[] commands = new String[] {
"chmod 0777 " + filepath,
"dd if=" + filepath + " of=" + mBackup
};
AeroActivity.shell.setRootInfo(commands);
Toast.makeText(getActivity(), R.string.need_reboot, Toast.LENGTH_LONG).show();
}
}