-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertUrlCanvas.java
More file actions
208 lines (182 loc) · 8.89 KB
/
Copy pathConvertUrlCanvas.java
File metadata and controls
208 lines (182 loc) · 8.89 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
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
import javax.microedition.lcdui.*;
import java.util.Vector;
public class ConvertUrlCanvas extends Form implements CommandListener, Runnable {
private VidmateME midlet;
private TextField urlField;
private final Command convertCmd = new Command("Convertir", Command.SCREEN, 1);
private final Command backCmd = new Command("Retour", Command.BACK, 2);
private Thread workerThread;
public ConvertUrlCanvas(VidmateME m) {
super("Convertir Lien YouTube");
midlet = m;
urlField = new TextField("Lien YouTube:", "", 256, TextField.URL);
append(urlField);
append("\nFormats supportes:\n- https://youtu.be/ID\n- https://youtube.com/watch?v=ID\n- ID seul (11 caracteres)");
addCommand(convertCmd);
addCommand(backCmd);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if (c == backCmd) {
midlet.backToMenu();
} else if (c == convertCmd) {
String input = urlField.getString().trim();
if (input.length() == 0) {
showAlert("Erreur", "Veuillez entrer un lien YouTube");
return;
}
Form progress = new Form("Conversion en cours...");
progress.append(new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING));
midlet.getDisplay().setCurrent(progress);
if (workerThread != null && workerThread.isAlive()) {
workerThread.interrupt();
}
workerThread = new Thread(this);
workerThread.start();
}
}
public void run() {
try {
String input = urlField.getString().trim();
String videoId = UrlConverter.extractVideoId(input);
if (videoId == null) {
showAlert("Erreur", "Lien YouTube invalide ou ID non trouve");
return;
}
Vector results = Ytfinder.searchById(videoId);
if (results.size() == 0) {
VideoItem item = new VideoItem();
item.videoId = videoId;
item.title = "Video " + videoId;
item.author = "YouTube";
item.duration = "Inconnue";
item.thumbnailUrl = "http://i.ytimg.com/vi/" + videoId + "/mqdefault.jpg";
results.addElement(item);
}
VideoItem item = (VideoItem) results.elementAt(0);
showVideoDetails(item);
} catch (Exception e) {
String errorMsg = e.getMessage();
if (errorMsg == null) errorMsg = "Erreur inconnue";
showAlert("Erreur", "Echec: " + errorMsg + "\n\nConseil: Essayez avec S60Tube direct");
}
}
private void showVideoDetails(final VideoItem item) {
midlet.getDisplay().callSerially(new Runnable() {
public void run() {
Form details = new Form("Details Video");
details.append("Titre: " + item.title + "\n");
details.append("ID: " + item.videoId + "\n\n");
// CORRECTION: Bouton miniature avec retour vers ConvertUrlCanvas
if (item.thumbnailUrl != null && item.thumbnailUrl.length() > 0) {
final Command showThumbCmd = new Command("Voir Miniature", Command.SCREEN, 1);
details.addCommand(showThumbCmd);
}
// Qualité étendue
final ChoiceGroup quality = new ChoiceGroup("Qualite:", Choice.EXCLUSIVE);
quality.append("144p (Economique)", null);
quality.append("240p (Basse)", null);
quality.append("360p (Standard)", null);
quality.append("480p (SD)", null);
quality.append("720p (HD)", null);
quality.append("1080p (Full HD)", null);
quality.setSelectedIndex(2, true);
details.append(quality);
// Format étendu
final ChoiceGroup format = new ChoiceGroup("Format:", Choice.EXCLUSIVE);
format.append("VIDEO - MP4", null);
format.append("VIDEO - 3GP (mobile)", null);
format.append("AUDIO - MP3", null);
format.append("AUDIO - AAC", null);
format.append("AUDIO - WAV", null);
details.append(format);
final Command dlCmd = new Command("Telecharger", Command.OK, 2);
final Command cancelCmd = new Command("Annuler", Command.BACK, 3);
details.addCommand(dlCmd);
details.addCommand(cancelCmd);
details.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c.getLabel().indexOf("Miniature") != -1) {
// CORRECTION: Passer ConvertUrlCanvas comme écran de retour
ThumbnailViewer viewer = new ThumbnailViewer(midlet, item.videoId, item.title, ConvertUrlCanvas.this);
midlet.getDisplay().setCurrent(viewer);
} else if (c == dlCmd) {
int qualityIdx = quality.getSelectedIndex();
int formatIdx = format.getSelectedIndex();
startDownload(item, qualityIdx, formatIdx);
} else if (c == cancelCmd || c.getCommandType() == Command.BACK) {
midlet.getDisplay().setCurrent(ConvertUrlCanvas.this);
}
}
});
midlet.getDisplay().setCurrent(details);
}
});
}
private void startDownload(final VideoItem item, final int qualityIdx, final int formatIdx) {
final Form progress = new Form("PREPARATION...");
progress.append(new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING));
progress.append("\nRecuperation du lien de telechargement...");
midlet.getDisplay().setCurrent(progress);
new Thread(new Runnable() {
public void run() {
try {
String q = "360";
switch (qualityIdx) {
case 0: q = "144"; break;
case 1: q = "240"; break;
case 2: q = "360"; break;
case 3: q = "480"; break;
case 4: q = "720"; break;
case 5: q = "1080"; break;
}
String fileFormat = "mp4";
boolean audioOnly = false;
switch (formatIdx) {
case 0:
fileFormat = "mp4";
audioOnly = false;
break;
case 1:
fileFormat = "3gp";
audioOnly = false;
break;
case 2:
fileFormat = "mp3";
audioOnly = true;
break;
case 3:
fileFormat = "aac";
audioOnly = true;
break;
case 4:
fileFormat = "wav";
audioOnly = true;
break;
}
item.downloadUrl = APIManager.getDownloadUrl(item.videoId, q, audioOnly, fileFormat);
item.fileFormat = fileFormat;
DownloadManager.getInstance().queueDownload(item);
showAlert("Succes",
"Telechargement en file d'attente!\n\n" +
"Qualite: " + q + "p\n" +
"Format: " + fileFormat.toUpperCase());
midlet.backToMenu();
} catch (Exception e) {
showAlert("Erreur 403",
"Echec telechargement:\n" + e.getMessage() +
"\n\nSolution:\n1. Allez dans Parametres\n2. Activez les proxys\n3. Reessayez");
}
}
}).start();
}
private void showAlert(final String title, final String msg) {
final Alert a = new Alert(title, msg, null, AlertType.INFO);
a.setTimeout(4000);
midlet.getDisplay().callSerially(new Runnable() {
public void run() {
midlet.getDisplay().setCurrent(a, ConvertUrlCanvas.this);
}
});
}
}