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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
| package com.example.driver_final;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.io.CopyStreamAdapter;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.driver_final.R.id;
import com.example.driver_final.document.connexion;
public class document extends Activity {
private ActionBar actionBar;
private Button bt2;
private Button bt1;
private Button bt3;
//private ArrayList<String> cahine_save;
private ArrayList<String> tab_chaine;
private HashSet<String> tab_ville;
private int requestCode = 1;
private Iterator<String> it;
private String password="pass";
private String host="host";
private String username="user";
FTPFile[] listeFile = null;//la liste des fichiers
Connexion con=null; //une tache connexion pour telecharger les fichiers
ListeFile Conlistfile;//une tache ListeFile pour chercher la liste des fichiers
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type_document);
actionBar = getActionBar();
actionBar.setTitle("driver");
actionBar.setSubtitle(R.string.title2);
bt1 = (Button) findViewById(id.buon1);
bt2 = (Button) findViewById(id.buon2);
bt3 = (Button) findViewById(id.buon3);
final Intent i = getIntent();
//on execute cette tache pour retourner la liste des fichiers qui se trouve dans le ftpserver
try {
Conlistfile= new ListeFile();
listeFile= Conlistfile.execute(host, username, password).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//pas encore fait
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// pas envore fait
}
});
//le bouton 3 pour les fichiers de type VBR
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText( getBaseContext(),"voile la chaine generé "+ i.getStringExtra("num_date"),Toast.LENGTH_LONG).show();
//liste por sauvegarder les noms de fichiers à telecharger
tab_chaine = new ArrayList<String>();
tab_ville = new HashSet<String>();
//liste pour sauvegarder les villes trouveés pour un num_date_ville_type, par exemple on
//peut trouvé ces deux noms de fichiers 1000_02041987_bruxelle_vbr_1 et 1000_02041987_liege_vbr_1
//alors la on doit extraire les villes et demander à l'utilisateur de choisir une ville
//puis on retourne la ville à l'activity , puis on selectione le bon path pour le telechargement
for (FTPFile file : listeFile) {
if (ressemble(file.getName(), i.getStringExtra("num_date"),
"VBR") == true) {
tab_chaine.add(file.getName());
String ville = extraire_ville(file.getName(), "VBR");
tab_ville.add(ville);
//Toast.makeText(getBaseContext(), ville, 1).show();
}
}
it = tab_ville.iterator();
Toast.makeText(getBaseContext(),"voila le nombre de ville"+tab_ville.size(), 1).show();
Log.i("voila", "voila on est la 3");
if (tab_ville.size() == 0) {
Toast.makeText(getBaseContext(), "pas de fichiers", 1)
.show();
return;
}
if (tab_ville.size() > 1) {
Intent intent_name = new Intent();
intent_name.setClass(getApplicationContext(),
choix_gare.class);
Bundle b = new Bundle();
b.putSerializable("lesvilles", tab_ville);
intent_name.putExtras(b);
startActivityForResult(intent_name, 1);
Log.i("voila", "voila on est la**");
}
else {
Toast.makeText(getBaseContext(), "la chaine "+tab_chaine.get(1), 1).show() ;
//new connexion().execute(tab_chaine.get(1));
}
}
});
}
public String extraire_ville(String nom_fichier, String type) {
String[] tabl;
String ville;
if (type.equals("LYST") || (type.equals("REBU"))) {
tabl = new String[4];
tabl = nom_fichier.split("_");
ville = tabl[2];
return ville;
} else {
tabl = new String[5];
tabl = nom_fichier.split("_");
ville = tabl[2];
return ville;
}
}
public boolean ressemble(String nom_fichier, String chaine, String type) {
if ((nom_fichier.contains(chaine)) && (nom_fichier.contains(type))) {
return true;
}
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// on a utilisé '1' comme requestCode dans le onClick...
if (requestCode == 1) {
ArrayList<String> listepath = new ArrayList<String>();
if (resultCode == RESULT_OK) {
for (int i = 0; i < tab_chaine.size(); i++) {
if (tab_chaine.get(i).contains(data.getExtras().getString("ville"))) {
//bon ici je cree une liste de path pour telecharger les fichiers
listepath.add(tab_chaine.get(i).toString());
Log.i("voila le path du fichier a telecharger", tab_chaine
.get(i).toString());
}
}
//la je teste si le thread est en cours ou il a été tué
if (con!=null){
Log.i("msg", "voila le'asyntask et encore en cours");
con.cancel(true);
con=null; // pas utile vu la ligne dessous !
con = new Connexion();
con.execute(listepath);
} else {
Log.i("msg", "voila le'asyntask est mort");
con = new Connexion();
con.execute(listepath);
}
} else
super.onActivityResult(requestCode, resultCode, data);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_back:
finish();
}
return true;
}
class ListeFile extends AsyncTask<String, String, FTPFile[]> {
FTPClient mFTPClient;
private FTPFile[] files;
@Override
protected FTPFile[] doInBackground(String... params) {
try {
mFTPClient = new FTPClient();
mFTPClient.connect(params[0], 21);
Log.i("voila", "voila on est la 4");
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
boolean status = mFTPClient.login(params[1], params[2]);
mFTPClient.enterLocalPassiveMode();
Log.i("voila", "voila on est la 5");
files = mFTPClient.listFiles("/fromCIS");
return files;
}
} catch (Exception e) {
}
return files;
}
@Override
protected void onPostExecute(FTPFile[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i("voila", "voila on est la 6");
listeFile = result;
Conlistfile.cancel(true);
}
}
class Connexion extends AsyncTask<ArrayList<String>, String, String> {
FTPClient mFTPClient;
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
con=null;
}@Override
protected void onCancelled() {
// TODO Auto-generated method stub
Log.i("sortir ", "on va sortir");
super.onCancelled();
con=null;
}
@Override
protected String doInBackground(ArrayList<String>... params) {
String retour;
Iterator<String> ite;
String chaine = null;
mFTPClient = new FTPClient();
try {
mFTPClient.connect(host, 21);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// Intent intent = new Intent(Intent.ACTION_VIEW);
boolean status = mFTPClient.login(username, password);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
mFTPClient.enterLocalPassiveMode();
ite =params[0].iterator();
while(it.hasNext()){
chaine=ite.next();
boolean sta = ftpDownload("/fromCIS/" +chaine ,
Environment.getExternalStorageDirectory()
+ "/Fromcis/" + chaine);
}
mFTPClient.logout();
mFTPClient.disconnect();
/*File file = new File("/sdcard" + "/Fromcis/" + chaine);
Uri path = Uri.fromFile(file);
Intent intent = null;
intent.setDataAndType(path, "application/pdf");
// optionnel a toi de voir quel flag tu souhaites
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// new
// ToastMessageTask().execute("on a demandé une nouvelle fenetre adobe reader");
try {
startActivity(intent);
Log.i("adobe ", "on a demandé d'aaficher le document ");
} catch (ActivityNotFoundException e) {
Log.e("intentdobe ", e.getMessage());
}*/
}
} catch (Exception e) {
return "zaki";
}
return "zaki";
}
/**
* mFTPClient: FTP client connection object (see FTP connection example)
* srcFilePath: path to the source file in FTP server desFilePath: path
* to the destination file to be saved in sdcard
*/
public boolean ftpDownload(String srcFilePath, String desFilePath) {
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(
desFilePath);
;
status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(e.getCause() + "", "download failed");
}
return status;
}
private class ToastMessageTask extends
AsyncTask<String, String, String> {
String toastMessage;
@Override
protected String doInBackground(String... params) {
toastMessage = params[0];
return toastMessage;
}
protected void OnProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
// This is executed in the context of the main GUI thread
protected void onPostExecute(String result) {
if (result.equals("NO")) {
AlertDialog.Builder adb = new AlertDialog.Builder(
document.this);
adb.setTitle("info");
adb.setMessage("Aucun document trouvé pour cette date et ce numero");
adb.setPositiveButton("Ok", null);
adb.setIcon(R.drawable.info);
adb.show();
}
Toast toast = Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG);
toast.show();
}
}
}
} |