2010-06-05 13:54:40 -04:00
|
|
|
package org.thialfihar.android.apg;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
2010-06-05 20:35:00 -04:00
|
|
|
import java.io.IOException;
|
2010-06-05 13:54:40 -04:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
import org.thialfihar.android.apg.utils.Choice;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
|
|
|
|
|
|
public class GeneralActivity extends BaseActivity {
|
|
|
|
private Intent mIntent;
|
|
|
|
private ArrayAdapter<Choice> mAdapter;
|
|
|
|
private ListView mList;
|
|
|
|
private Button mCancelButton;
|
2010-06-05 20:35:00 -04:00
|
|
|
private String mDataString;
|
|
|
|
private Uri mDataUri;
|
2010-06-05 13:54:40 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.general);
|
|
|
|
|
|
|
|
mIntent = getIntent();
|
|
|
|
|
|
|
|
InputStream inStream = null;
|
|
|
|
{
|
2010-06-05 20:35:00 -04:00
|
|
|
String data = mIntent.getStringExtra(Intent.EXTRA_TEXT);
|
2010-06-05 13:54:40 -04:00
|
|
|
if (data != null) {
|
2010-06-05 20:35:00 -04:00
|
|
|
mDataString = data;
|
|
|
|
inStream = new ByteArrayInputStream(data.getBytes());
|
2010-06-05 13:54:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inStream == null) {
|
|
|
|
Uri data = mIntent.getData();
|
|
|
|
if (data != null) {
|
2010-06-05 20:35:00 -04:00
|
|
|
mDataUri = data;
|
2010-06-05 13:54:40 -04:00
|
|
|
try {
|
|
|
|
inStream = getContentResolver().openInputStream(data);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// didn't work
|
2010-06-05 20:35:00 -04:00
|
|
|
Toast.makeText(this, "failed to open stream", Toast.LENGTH_SHORT).show();
|
2010-06-05 13:54:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inStream == null) {
|
2010-06-05 20:35:00 -04:00
|
|
|
Toast.makeText(this, "no data found", Toast.LENGTH_SHORT).show();
|
2010-06-05 13:54:40 -04:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-05 20:35:00 -04:00
|
|
|
int contentType = Id.content.unknown;
|
|
|
|
try {
|
|
|
|
contentType = Apg.getStreamContent(this, inStream);
|
|
|
|
inStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// just means that there's no PGP data in there
|
|
|
|
}
|
|
|
|
|
2010-06-05 13:54:40 -04:00
|
|
|
mList = (ListView) findViewById(R.id.options);
|
|
|
|
Vector<Choice> choices = new Vector<Choice>();
|
|
|
|
|
2010-06-05 20:35:00 -04:00
|
|
|
if (contentType == Id.content.keys) {
|
|
|
|
choices.add(new Choice(Id.choice.action.import_public,
|
|
|
|
getString(R.string.action_importPublic)));
|
|
|
|
choices.add(new Choice(Id.choice.action.import_secret,
|
|
|
|
getString(R.string.action_importSecret)));
|
2010-06-05 13:54:40 -04:00
|
|
|
}
|
|
|
|
|
2010-06-05 20:35:00 -04:00
|
|
|
if (contentType == Id.content.encrypted_data) {
|
2010-06-05 13:54:40 -04:00
|
|
|
choices.add(new Choice(Id.choice.action.decrypt, getString(R.string.action_decrypt)));
|
|
|
|
}
|
|
|
|
|
2010-06-05 20:35:00 -04:00
|
|
|
if (contentType == Id.content.unknown) {
|
2010-06-05 13:54:40 -04:00
|
|
|
choices.add(new Choice(Id.choice.action.encrypt, getString(R.string.action_encrypt)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mAdapter = new ArrayAdapter<Choice>(this, android.R.layout.simple_list_item_1, choices);
|
|
|
|
mList.setAdapter(mAdapter);
|
|
|
|
|
|
|
|
mList.setOnItemClickListener(new OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
|
|
|
|
clicked(mAdapter.getItem(arg2).getId());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mCancelButton = (Button) findViewById(R.id.btn_cancel);
|
|
|
|
mCancelButton.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
GeneralActivity.this.finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (choices.size() == 1) {
|
|
|
|
clicked(choices.get(0).getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clicked(int id) {
|
|
|
|
Intent intent = new Intent();
|
|
|
|
switch (id) {
|
|
|
|
case Id.choice.action.encrypt: {
|
|
|
|
intent.setClass(this, EncryptActivity.class);
|
2010-06-05 20:35:00 -04:00
|
|
|
if (mDataString != null) {
|
|
|
|
intent.setAction(Apg.Intent.ENCRYPT);
|
|
|
|
intent.putExtra(Apg.EXTRA_TEXT, mDataString);
|
|
|
|
} else if (mDataUri != null) {
|
|
|
|
intent.setAction(Apg.Intent.ENCRYPT_FILE);
|
|
|
|
intent.setDataAndType(mDataUri, mIntent.getType());
|
2010-06-05 13:54:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.choice.action.decrypt: {
|
2010-06-05 20:35:00 -04:00
|
|
|
intent.setClass(this, DecryptActivity.class);
|
|
|
|
if (mDataString != null) {
|
|
|
|
intent.setAction(Apg.Intent.DECRYPT);
|
|
|
|
intent.putExtra(Apg.EXTRA_TEXT, mDataString);
|
|
|
|
} else if (mDataUri != null) {
|
|
|
|
intent.setAction(Apg.Intent.DECRYPT_FILE);
|
|
|
|
intent.setDataAndType(mDataUri, mIntent.getType());
|
|
|
|
}
|
|
|
|
|
2010-06-05 13:54:40 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.choice.action.import_public: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.choice.action.import_secret: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
// shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|