Android Open Source - dCache-Cloud Share With Friend 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.j  a  v  a  2 s  . c  om*/
import java.security.PublicKey;
import java.util.List;

import javax.crypto.SecretKey;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.desy.dCacheCloud.CryptoHelper;
import de.desy.dCacheCloud.DatabaseHelper;
import de.desy.dCacheCloud.KeyStoreHelper;
import de.desy.dCacheCloud.R;

public class ShareWithFriendActivity extends Activity implements OnItemClickListener {

  private DatabaseHelper dbh = null;
  private ListView lvFriends = null;
  private boolean friendsFound = true;
  private String fileName = "";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.file_list);
    dbh = new DatabaseHelper(getApplicationContext());
    lvFriends = (ListView) findViewById(R.id.listView1);
    lvFriends.setOnItemClickListener(this);
    fileName = getIntent().getStringExtra("fileName");
    
    super.onCreate(savedInstanceState);
  }

  @Override
  protected void onResume() {
    List<String> friends  = dbh.getAllFriends();
    
    if (friends.size() == 0)
    {
      friendsFound = false;
      friends.add("no friends found");
    }
    
    lvFriends.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends));
    super.onResume();
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // if no friends were found, then the message cant be clicked!
    if (!friendsFound)
      return;
    
    PublicKey usersPublic = dbh.getPersonPublicKey(lvFriends.getItemAtPosition(position).toString());
    share(usersPublic);
  }
  
  private void share(PublicKey key)
  {
    SecretKey fileAESKey = KeyStoreHelper.getKey(fileName);
    
    JSONObject json = new JSONObject();
    try
    {
      json.put("name", fileName);
      json.put("key", Base64.encode(fileAESKey.getEncoded(), Base64.DEFAULT));
    }
    catch (JSONException e)
    {
      e.printStackTrace();
    }
    
    String message = json.toString();
    String messageHash = CryptoHelper.hash(message);
    
    // TODO: signature lenght = 1024?
    byte[] signature = CryptoHelper.encryptAsymmetric(messageHash, true, KeyStoreHelper.getOwnPriv());
    // TODO: String + byte[] ?!? --> what happened here?
    byte[] encrypted = CryptoHelper.encryptAsymmetric(message + signature, false, key);
    String ownPubHash = CryptoHelper.hash(CryptoHelper.PublicKeyToString(KeyStoreHelper.getOwnPub()));
    
    String output = Base64.encodeToString(encrypted, Base64.DEFAULT);
    output += ownPubHash;
//    Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG).show();
    
    Intent intent = new Intent(getApplicationContext(), ShareDataActivity.class);
    intent.putExtra("DATA", output);
    startActivity(intent);
    finish();
    
    return;
  }
}




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