219 lines
8.3 KiB
Java
219 lines
8.3 KiB
Java
/*
|
|
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package org.thialfihar.android.apg;
|
|
|
|
import java.util.Vector;
|
|
import java.util.regex.Matcher;
|
|
|
|
import android.app.ListActivity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.database.Cursor;
|
|
import android.net.Uri;
|
|
import android.os.Bundle;
|
|
import android.text.Html;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AdapterView;
|
|
import android.widget.BaseAdapter;
|
|
import android.widget.ImageView;
|
|
import android.widget.ListAdapter;
|
|
import android.widget.TextView;
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
|
|
public class MailListActivity extends ListActivity {
|
|
LayoutInflater mInflater = null;
|
|
|
|
private static class Conversation {
|
|
public long id;
|
|
public String subject;
|
|
public Vector<Message> messages;
|
|
|
|
public Conversation(long id, String subject) {
|
|
this.id = id;
|
|
this.subject = subject;
|
|
}
|
|
}
|
|
|
|
private static class Message {
|
|
public Conversation parent;
|
|
public long id;
|
|
public String subject;
|
|
public String fromAddress;
|
|
public String data;
|
|
public String replyTo;
|
|
public boolean signedOnly;
|
|
|
|
public Message(Conversation parent, long id, String subject,
|
|
String fromAddress, String replyTo,
|
|
String data, boolean signedOnly) {
|
|
this.parent = parent;
|
|
this.id = id;
|
|
this.subject = subject;
|
|
this.fromAddress = fromAddress;
|
|
this.replyTo = replyTo;
|
|
this.data = data;
|
|
if (this.replyTo == null || this.replyTo.equals("")) {
|
|
this.replyTo = this.fromAddress;
|
|
}
|
|
this.signedOnly = signedOnly;
|
|
}
|
|
}
|
|
|
|
private Vector<Conversation> mconversations;
|
|
private Vector<Message> mmessages;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
mconversations = new Vector<Conversation>();
|
|
mmessages = new Vector<Message>();
|
|
|
|
String account = getIntent().getExtras().getString("account");
|
|
// TODO: what if account is null?
|
|
Uri uri = Uri.parse("content://gmail-ls/conversations/" + account);
|
|
Cursor cursor =
|
|
managedQuery(uri, new String[] { "conversation_id", "subject" }, null, null, null);
|
|
for (int i = 0; i < cursor.getCount(); ++i) {
|
|
cursor.moveToPosition(i);
|
|
|
|
int idIndex = cursor.getColumnIndex("conversation_id");
|
|
int subjectIndex = cursor.getColumnIndex("subject");
|
|
long conversationId = cursor.getLong(idIndex);
|
|
Conversation conversation =
|
|
new Conversation(conversationId, cursor.getString(subjectIndex));
|
|
Uri messageUri = Uri.withAppendedPath(uri, "" + conversationId + "/messages");
|
|
Cursor messageCursor =
|
|
managedQuery(messageUri, new String[] {
|
|
"messageId",
|
|
"subject",
|
|
"fromAddress",
|
|
"replyToAddresses",
|
|
"body" }, null, null, null);
|
|
Vector<Message> messages = new Vector<Message>();
|
|
for (int j = 0; j < messageCursor.getCount(); ++j) {
|
|
messageCursor.moveToPosition(j);
|
|
idIndex = messageCursor.getColumnIndex("messageId");
|
|
subjectIndex = messageCursor.getColumnIndex("subject");
|
|
int fromAddressIndex = messageCursor.getColumnIndex("fromAddress");
|
|
int replyToIndex = messageCursor.getColumnIndex("replyToAddresses");
|
|
int bodyIndex = messageCursor.getColumnIndex("body");
|
|
String data = messageCursor.getString(bodyIndex);
|
|
data = Html.fromHtml(data).toString();
|
|
boolean signedOnly = false;
|
|
Matcher matcher = Apg.PGP_MESSAGE.matcher(data);
|
|
if (matcher.matches()) {
|
|
data = matcher.group(1);
|
|
} else {
|
|
matcher = Apg.PGP_SIGNED_MESSAGE.matcher(data);
|
|
if (matcher.matches()) {
|
|
data = matcher.group(1);
|
|
signedOnly = true;
|
|
} else {
|
|
data = null;
|
|
}
|
|
}
|
|
Message message =
|
|
new Message(conversation,
|
|
messageCursor.getLong(idIndex),
|
|
messageCursor.getString(subjectIndex),
|
|
messageCursor.getString(fromAddressIndex),
|
|
messageCursor.getString(replyToIndex),
|
|
data, signedOnly);
|
|
|
|
messages.add(message);
|
|
mmessages.add(message);
|
|
}
|
|
conversation.messages = messages;
|
|
mconversations.add(conversation);
|
|
}
|
|
|
|
setListAdapter(new MailboxAdapter());
|
|
getListView().setOnItemClickListener(new OnItemClickListener() {
|
|
@Override
|
|
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
|
|
Intent intent = new Intent(MailListActivity.this, DecryptMessageActivity.class);
|
|
intent.setAction(Apg.Intent.DECRYPT);
|
|
Message message = (Message) ((MailboxAdapter) getListAdapter()).getItem(position);
|
|
intent.putExtra("data", message.data);
|
|
intent.putExtra("subject", message.subject);
|
|
intent.putExtra("replyTo", message.replyTo);
|
|
startActivity(intent);
|
|
}
|
|
});
|
|
}
|
|
|
|
private class MailboxAdapter extends BaseAdapter implements ListAdapter {
|
|
|
|
@Override
|
|
public boolean isEnabled(int position) {
|
|
Message message = (Message) getItem(position);
|
|
return message.data != null;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasStableIds() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
return mmessages.size();
|
|
}
|
|
|
|
@Override
|
|
public Object getItem(int position) {
|
|
return mmessages.get(position);
|
|
}
|
|
|
|
@Override
|
|
public long getItemId(int position) {
|
|
return mmessages.get(position).id;
|
|
}
|
|
|
|
@Override
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
View view = mInflater.inflate(R.layout.mailbox_message_item, null);
|
|
|
|
Message message = (Message) getItem(position);
|
|
|
|
TextView subject = (TextView) view.findViewById(R.id.subject);
|
|
TextView email = (TextView) view.findViewById(R.id.email_address);
|
|
ImageView status = (ImageView) view.findViewById(R.id.ic_status);
|
|
|
|
subject.setText(message.subject);
|
|
email.setText(message.fromAddress);
|
|
if (message.data != null) {
|
|
if (message.signedOnly) {
|
|
status.setImageResource(R.drawable.signed);
|
|
} else {
|
|
status.setImageResource(R.drawable.encrypted);
|
|
}
|
|
status.setVisibility(View.VISIBLE);
|
|
} else {
|
|
status.setVisibility(View.INVISIBLE);
|
|
}
|
|
|
|
return view;
|
|
}
|
|
}
|
|
}
|