Android Open Source - dCache-Cloud Upload Activity






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.Activities;
/*from w  ww .ja  v a  2  s . c  om*/
import java.util.ArrayList;

import javax.crypto.SecretKey;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;
import de.desy.dCacheCloud.CryptoHelper;
import de.desy.dCacheCloud.KeyStoreHelper;
import de.desy.dCacheCloud.R;
import de.desy.dCacheCloud.UploadService;

public class UploadActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_share);
    finish();

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (type == null)
      return;

    if (!intent.hasExtra(Intent.EXTRA_STREAM))
      return;

    if (Intent.ACTION_SEND.equals(action)) {
      Uri fileUri = getFilePath((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
      encryptFile(fileUri);
      uploadFile(fileUri);
      deleteLocalFile(fileUri);
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
      ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
      for (Parcelable p : list) {
        Uri fileUri = getFilePath((Uri) p);
        encryptFile(fileUri);
        uploadFile(fileUri);
        deleteLocalFile(fileUri);
      }
    }
  }

  private void encryptFile(Uri file)
  {
    SecretKey key = CryptoHelper.generateBlockCipherKey(256);
    String encryptedFileName = file.getLastPathSegment();
    
    if (CryptoHelper.encryptBlockCipherWithIV(file, key))
    {
        KeyStoreHelper.storeKey(encryptedFileName, key);
    }
  }
  
  private void uploadFile(Uri uri) {
    Log.d("davsync", "Sharing " + uri.toString());

    Intent ulIntent = new Intent(this, UploadService.class);
    ulIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startService(ulIntent);
  }
  
  private void deleteLocalFile(Uri uri)
  {
    //TODO: check if upload is completed!
  }
  
  private Uri getFilePath(Uri uriToFile) {
    
    ContentResolver cr = getContentResolver();
    String filename = null;
    String scheme = uriToFile.getScheme();
    Uri uri = uriToFile;
    
    if (scheme.equals("content")) {

        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = cr.query(uriToFile, proj, null, null, null);
        if (cursor != null && cursor.getCount() != 0) {
          int columnIndex = -1;
          try {
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          } catch (IllegalArgumentException e) {
            Log.d("dCache", e.toString());
          }
          
            cursor.moveToFirst();
            filename = cursor.getString(columnIndex);
        }
        
        uri = Uri.parse("file://"  + filename);
    }
    
    return uri;
  }
}




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