Android Open Source - dCache-Cloud Download Service






From Project

Back to project page dCache-Cloud.

License

The source code is released under:

Copyright ? 2013, Michael Stapelberg and contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project dCache-Cloud listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.desy.dCacheCloud;
/*from   w w  w  . j a  v a  2s.co m*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectHandler;
import org.apache.http.protocol.HttpContext;

import android.app.Dialog;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
//import android.app.Notification.Builder;

public class DownloadService extends IntentService {
    
  ProgressBar pb;
  Dialog dialog;
  int downloadSize = 0;
  int totalSize = 0;
  TextView cur_val;
  String downloadFilePath;
  String fileName = null;
  private NotificationManager mNotificationManager = null;
  private HttpGet httpGet = null;
  private DefaultHttpClient httpClient = null;
  private boolean isRedirected = false;
  
  private List<String> target = new ArrayList<String>();
  
  private Builder NotificationFailurePrepare(String text) {
    
    Builder mBuilder = new Builder(this);
    
    mBuilder.setContentText(fileName + ": " + text);
    mBuilder.setContentTitle("Error uploading to dCache server");
    mBuilder.setProgress(0, 0, false);
    mBuilder.setOngoing(false);
    
    return mBuilder;
  }
  
  private NotificationCompat.Builder NotificationUploadPrepare(String text){
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    
    /* Setup Notification Manager Begin */
    mBuilder.setContentTitle("Download from dCache server");
    mBuilder.setContentText(text);
    mBuilder.setSmallIcon(android.R.drawable.ic_menu_upload);
    mBuilder.setOngoing(true);
    mBuilder.setProgress(100, 0, false);
    /* Setup Notification Manager End */
    
    return mBuilder;
  }
  
  @SuppressWarnings("deprecation")
  private void NotificationNotify(String tag, Builder mBuilder) {
    if (android.os.Build.VERSION.SDK_INT < 11) {
      // Not supported
    }
    if (android.os.Build.VERSION.SDK_INT < 16)
      mNotificationManager.notify(tag, 0,mBuilder.getNotification());
    else
      mNotificationManager.notify(tag, 0, mBuilder.build());
  }
    
  private void NotificationCancel(String tag) {
    mNotificationManager.cancel(tag, 0);
    DatabaseHelper helper = new DatabaseHelper(this);
  }
    
  public DownloadService() {
    super("DownloadService");
  }
  
  @Override
  public int onStartCommand (Intent intent, int flags, int startId) {
      Log.i("2.", "onstartcommand");
      downloadFilePath = (String) intent.getExtras().get("url"); 
      fileName = (String) intent.getExtras().get("filename");
    InitializeComponents();
      return START_REDELIVER_INTENT;
  }
  
  protected void onHandleIntent(Intent intent) {
      Log.i("2.", "onhandleintent");
  }
  
  private boolean downloadFile() throws IllegalStateException, IOException {
    
    isRedirected = false;
    
    // the return Value from the doors seems to be like "[Location: http:....?uid=...]"
    String finalTarget = target.get(target.size()-1);
    if (finalTarget != null && !finalTarget.startsWith("http")) {
      finalTarget = finalTarget.substring(finalTarget.indexOf("http"), finalTarget.length()-1);
      target.set(target.size()-1, finalTarget);
    }

    httpGet.setURI(URI.create(finalTarget));
    
    
    if (isNetworkConnected()) {
          HttpResponse response = null;
      try {
        response = httpClient.execute(httpGet);
      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }    
          
      if (isRedirected) {
        downloadFile();
        return true;
      }
      
          final Builder mBuilder = NotificationUploadPrepare(fileName);
      
          InputStream in = response.getEntity().getContent();
          Long fileSize = response.getEntity().getContentLength();
          FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/dCacheCloud/" + fileName));
          
          byte[] buffer = new byte[4096];
          int length;
          long bytesWritten = 0;
          int lastPercent = 0;
          
          while((length = in.read(buffer)) > 0) {
              fos.write(buffer, 0, length);
              bytesWritten += length;
              
        int percent = (int) ((bytesWritten * 100) / fileSize);
        if (lastPercent != percent) {
          mBuilder.setProgress(100, percent, false);
          NotificationNotify(fileName, mBuilder);
          lastPercent = percent;
        }            
          }
          
          mBuilder.setOngoing(false);
          NotificationNotify(fileName, mBuilder);
          /* end Download */
          
          CryptoHelper.decryptBlockCipherWithIV(Uri.parse(fileName), KeyStoreHelper.getKey(fileName));
          
    } else {
      Toast.makeText(getApplicationContext(), "You are not connected to the internet!", Toast.LENGTH_LONG).show();
    }
        return false;
  }
  
  
    public boolean isNetworkConnected() {
        final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
   }
  
  
  
    private void InitializeComponents(){      
        try {
        SharedPreferences preferences = getSharedPreferences("de.desy.dCacheCloud_preferences", Context.MODE_PRIVATE);
        String user = preferences.getString("webdav_user", null);
        String password = preferences.getString("webdav_password", null);
            
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
            File folder = new File(Environment.getExternalStorageDirectory() + "/dCacheCloud/");
            if (!folder.exists()) {
                folder.mkdir();
            }
            File file = new File(Environment.getExternalStorageDirectory() + "/dCacheCloud/" + fileName);             
            
            httpGet = new HttpGet(downloadFilePath);
            
            try {
        httpClient = ServerHelper.getClient();
      } catch (KeyManagementException e) {
        e.printStackTrace();
      } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
      } catch (KeyStoreException e) {
        e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
      } catch (CertificateException e) {
        e.printStackTrace();
      }
            
        httpClient.setRedirectHandler(new DefaultRedirectHandler() {  
          @Override
          public URI getLocationURI(HttpResponse response, HttpContext contet) throws org.apache.http.ProtocolException {
            
            Log.d("Rederection!!: ", Arrays.toString(response.getHeaders("Location")));
            System.out.println(Arrays.toString(response.getHeaders("Location")));
            
            target.add(Arrays.toString(response.getHeaders("Location")));
            isRedirected = true;
            return super.getLocationURI(response, contet);
          }
          
        });
            
        ServerHelper.setCredentials(httpClient, httpGet, user, password);
        target.add(downloadFilePath);
        downloadFile();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
    }
}




Java Source Code List

External.Contents.java
External.IntentIntegrator.java
External.IntentResult.java
External.QRCodeEncoder.java
de.desy.dCacheCloud.CountingInputStreamEntity.java
de.desy.dCacheCloud.CryptoHelper.java
de.desy.dCacheCloud.DatabaseHelper.java
de.desy.dCacheCloud.DownloadService.java
de.desy.dCacheCloud.KeyStoreHelper.java
de.desy.dCacheCloud.MySSLSocketFactory.java
de.desy.dCacheCloud.ServerHelper.java
de.desy.dCacheCloud.UploadService.java
de.desy.dCacheCloud.Activities.FriendFoundActivity.java
de.desy.dCacheCloud.Activities.ImportDataActivity.java
de.desy.dCacheCloud.Activities.MainActivity.java
de.desy.dCacheCloud.Activities.ProfileActivity.java
de.desy.dCacheCloud.Activities.ServerViewActivity.java
de.desy.dCacheCloud.Activities.SettingsActivity.java
de.desy.dCacheCloud.Activities.ShareDataActivity.java
de.desy.dCacheCloud.Activities.ShareWithFriendActivity.java
de.desy.dCacheCloud.Activities.UploadActivity.java
de.desy.dCacheCloud.Activities.UserPasswordActivity.java
de.desy.dCacheCloud.BCReceiver.NetworkReceiver.java
de.desy.dCacheCloud.BCReceiver.NewMediaReceiver.java