Switch to Contextual Action Bar goodness.

This commit is contained in:
Moxie Marlinspike 2012-07-26 16:21:45 -07:00
parent a66cc636c6
commit 7cab26750b
13 changed files with 136 additions and 284 deletions

View File

@ -79,7 +79,6 @@
<activity android:name=".ApplicationPreferencesActivity" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout"></activity>
<activity android:name=".PassphraseCreateActivity" android:theme="@android:style/Theme.Dialog" android:label="Create Passphrase" android:launchMode="singleInstance" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout"></activity>
<activity android:name=".SendKeyActivity" android:theme="@android:style/Theme.Dialog" android:label="Initiate Key Exchange" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout"></activity>
<activity android:name=".ReceiveKeyActivity" android:theme="@android:style/Theme.Dialog" android:label="Complete Key Exchange" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout"></activity>
<activity android:name=".VerifyImportedIdentityActivity" android:theme="@android:style/Theme.Dialog" android:label="Verify Imported Identity" android:configChanges="touchscreen|keyboard|keyboardHidden|orientation|screenLayout"></activity>

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 244 B

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:text="Batch Selection Mode"
android:maxLines="1"
android:singleLine="true"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/selected_conv_count"
android:maxLines="1"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ccffffff"
android:layout_gravity="center_vertical"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.ConversationHeaderView xmlns:android="http://schemas.android.com/apk/res/android"
<org.thoughtcrime.securesms.ConversationListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingRight="10dip"
@ -81,4 +81,4 @@
android:layout_toLeftOf="@id/date"
android:ellipsize="end" />
</org.thoughtcrime.securesms.ConversationHeaderView>
</org.thoughtcrime.securesms.ConversationListItem>

View File

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Normal Mode"
android:id="@+id/menu_normal_mode"
android:icon="@android:drawable/ic_menu_set_as" />
<item android:title="Delete Selected"
android:id="@+id/menu_delete_selected"
@ -11,10 +8,10 @@
<item android:title="Select All"
android:id="@+id/menu_select_all"
android:icon="@android:drawable/ic_menu_add" />
android:icon="@drawable/ic_menu_selectall_holo_dark" />
<item android:title="Unselect All"
android:id="@+id/menu_unselect_all"
android:icon="@android:drawable/ic_menu_revert" />
<!-- <item android:title="Unselect All" -->
<!-- android:id="@+id/menu_unselect_all" -->
<!-- android:icon="@android:drawable/ic_menu_revert" /> -->
</menu>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="View Thread"
android:id="@+id/menu_context_view" />
<item android:title="Delete Thread"
android:id="@+id/menu_context_delete" />
</menu>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Batch Mode"
android:id="@+id/menu_batch_mode"
android:icon="@android:drawable/ic_menu_share" />
</menu>

View File

@ -1,6 +1,6 @@
/**
/**
* Copyright (C) 2011 Whisper Systems
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@ -10,15 +10,17 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessageRecord;
@ -27,32 +29,30 @@ import org.thoughtcrime.securesms.protocol.Prefix;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* A CursorAdapter for building a list of conversation threads.
*
*
* @author Moxie Marlinspike
*/
public class ConversationListAdapter extends CursorAdapter {
private final Context context;
private final Set<Long> batchSet = Collections.synchronizedSet(new HashSet<Long>());
private boolean batchMode = false;
public ConversationListAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.context = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ConversationHeaderView view = new ConversationHeaderView(context, batchSet);
ConversationListItem view = new ConversationListItem(context, batchSet);
bindView(view, context, cursor);
return view;
@ -63,20 +63,20 @@ public class ConversationListAdapter extends CursorAdapter {
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID));
String recipientId = cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.RECIPIENT_IDS));
Recipients recipients = RecipientFactory.getRecipientsForIds(context, recipientId);
long date = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.DATE));
long count = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.MESSAGE_COUNT));
long read = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.READ));
MessageRecord message = new MessageRecord(-1, recipients, date, count, read == 1, threadId);
setBody(cursor, message);
((ConversationHeaderView)view).set(message, batchMode);
((ConversationListItem)view).set(message, batchMode);
}
protected void filterBody(MessageRecord message, String body) {
if (body == null) body = "(No subject)";
if (body.startsWith(Prefix.SYMMETRIC_ENCRYPT) || body.startsWith(Prefix.ASYMMETRIC_ENCRYPT) || body.startsWith(Prefix.ASYMMETRIC_LOCAL_ENCRYPT)) {
message.setBody("Encrypted message, enter passphrase... ");
message.setEmphasis(true);
@ -90,12 +90,16 @@ public class ConversationListAdapter extends CursorAdapter {
protected void setBody(Cursor cursor, MessageRecord message) {
filterBody(message, cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET)));
}
}
public void addToBatchSet(long threadId) {
batchSet.add(threadId);
}
public Set<Long> getBatchSelections() {
return batchSet;
}
public void initializeBatchMode(boolean toggle) {
this.batchMode = toggle;
unselectAllThreads();
@ -103,12 +107,12 @@ public class ConversationListAdapter extends CursorAdapter {
public void unselectAllThreads() {
this.batchSet.clear();
this.notifyDataSetInvalidated();
this.notifyDataSetChanged();
}
public void selectAllThreads() {
Cursor cursor = DatabaseFactory.getThreadDatabase(context).getConversationList();
try {
while (cursor != null && cursor.moveToNext()) {
this.batchSet.add(cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID)));
@ -117,7 +121,7 @@ public class ConversationListAdapter extends CursorAdapter {
if (cursor != null)
cursor.close();
}
this.notifyDataSetInvalidated();
this.notifyDataSetChanged();
}
}

View File

@ -23,35 +23,33 @@ import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.loaders.ConversationListLoader;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import java.util.Set;
public class ConversationListFragment extends SherlockListFragment
implements LoaderManager.LoaderCallbacks<Cursor>
implements LoaderManager.LoaderCallbacks<Cursor>, ActionMode.Callback
{
private ConversationSelectedListener listener;
private MasterSecret masterSecret;
private boolean isBatchMode = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
@ -64,7 +62,7 @@ public class ConversationListFragment extends SherlockListFragment
setHasOptionsMenu(true);
initializeListAdapter();
registerForContextMenu(getListView());
initializeBatchListener();
getLoaderManager().initLoader(0, null, this);
}
@ -77,57 +75,18 @@ public class ConversationListFragment extends SherlockListFragment
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSherlockActivity().getSupportMenuInflater();
if (this.isBatchMode) inflater.inflate(R.menu.conversation_list_batch, menu);
else if (this.masterSecret == null) inflater.inflate(R.menu.conversation_list_locked, menu);
else inflater.inflate(R.menu.conversation_list, menu);
if (this.masterSecret != null) {
MenuInflater inflater = this.getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.conversation_list, menu);
}
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_batch_mode: handleSwitchBatchMode(true); return true;
case R.id.menu_delete_selected: handleDeleteAllSelected(); return true;
case R.id.menu_select_all: handleSelectAllThreads(); return true;
case R.id.menu_unselect_all: handleUnselectAllThreads(); return true;
case R.id.menu_normal_mode: handleSwitchBatchMode(false); return true;
}
return false;
}
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
android.view.MenuInflater inflater = this.getSherlockActivity().getMenuInflater();
menu.clear();
inflater.inflate(R.menu.conversation_list_context, menu);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
Cursor cursor = ((CursorAdapter)this.getListAdapter()).getCursor();
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID));
String recipientId = cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.RECIPIENT_IDS));
Recipients recipients = RecipientFactory.getRecipientsForIds(getActivity(), recipientId);
switch(item.getItemId()) {
case R.id.menu_context_view: handleCreateConversation(threadId, recipients); return true;
case R.id.menu_context_delete: handleDeleteThread(threadId); return true;
}
return false;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if (v instanceof ConversationHeaderView) {
ConversationHeaderView headerView = (ConversationHeaderView) v;
if (v instanceof ConversationListItem) {
ConversationListItem headerView = (ConversationListItem) v;
handleCreateConversation(headerView.getThreadId(), headerView.getRecipients());
}
}
@ -137,6 +96,22 @@ public class ConversationListFragment extends SherlockListFragment
initializeListAdapter();
}
private void initializeBatchListener() {
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) {
ConversationListAdapter adapter = (ConversationListAdapter)getListAdapter();
getSherlockActivity().startActionMode(ConversationListFragment.this);
adapter.initializeBatchMode(true);
adapter.addToBatchSet(((ConversationListItem)v).getThreadId());
adapter.notifyDataSetChanged();
return true;
}
});
}
private void initializeListAdapter() {
if (this.masterSecret == null) {
this.setListAdapter(new ConversationListAdapter(getActivity(), null));
@ -147,12 +122,6 @@ public class ConversationListFragment extends SherlockListFragment
getLoaderManager().restartLoader(0, null, this);
}
private void handleSwitchBatchMode(boolean batchMode) {
this.isBatchMode = batchMode;
((ConversationListAdapter)this.getListAdapter()).initializeBatchMode(batchMode);
this.getSherlockActivity().invalidateOptionsMenu();
}
private void handleDeleteAllSelected() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setIcon(android.R.drawable.ic_dialog_alert);
@ -176,24 +145,6 @@ public class ConversationListFragment extends SherlockListFragment
alert.show();
}
private void handleDeleteThread(final long threadId) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Delete Thread Confirmation");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setCancelable(true);
builder.setMessage("Are you sure that you want to permanently delete this conversation?");
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (threadId > 0) {
DatabaseFactory.getThreadDatabase(getActivity()).deleteConversation(threadId);
}
}
});
builder.setNegativeButton(R.string.no, null);
builder.show();
}
private void handleSelectAllThreads() {
((ConversationListAdapter)this.getListAdapter()).selectAllThreads();
}
@ -225,6 +176,39 @@ public class ConversationListFragment extends SherlockListFragment
public void onCreateConversation(long threadId, Recipients recipients);
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.conversation_list_batch, menu);
LayoutInflater layoutInflater = getSherlockActivity().getLayoutInflater();
View actionModeView = layoutInflater.inflate(R.layout.conversation_fragment_cab, null);
mode.setCustomView(actionModeView);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_select_all: handleSelectAllThreads(); return true;
case R.id.menu_delete_selected: handleDeleteAllSelected(); return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
((ConversationListAdapter)getListAdapter()).initializeBatchMode(false);
}
}

View File

@ -45,7 +45,7 @@ import java.util.Set;
* @author Moxie Marlinspike
*/
public class ConversationHeaderView extends RelativeLayout {
public class ConversationListItem extends RelativeLayout {
private Set<Long> selectedThreads;
private Recipients recipients;
@ -58,14 +58,14 @@ public class ConversationHeaderView extends RelativeLayout {
private CheckBox checkbox;
private QuickContactBadge contactPhoto;
public ConversationHeaderView(Context context, boolean first) {
public ConversationListItem(Context context, boolean first) {
this(context, (Set<Long>)null);
this.first = true;
contactPhoto.setVisibility(View.GONE);
}
public ConversationHeaderView(Context context, Set<Long> selectedThreads) {
public ConversationListItem(Context context, Set<Long> selectedThreads) {
super(context);
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@ -82,7 +82,7 @@ public class ConversationHeaderView extends RelativeLayout {
intializeListeners();
}
public ConversationHeaderView(Context context, AttributeSet attrs) {
public ConversationListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}

View File

@ -1,148 +0,0 @@
/**
* Copyright (C) 2011 Whisper Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms;
import org.thoughtcrime.securesms.components.RecipientsPanel;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.NameAndNumber;
import org.thoughtcrime.securesms.crypto.KeyExchangeInitiator;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.MemoryCleaner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
/**
* Distinct activity for selecting a contact to initiate a key exchange with.
* Can ordinarily be done through ComposeMessageActivity, this allows for
* initiating key exchanges outside of that context.
*
* @author Moxie Marlinspike
*
*/
public class SendKeyActivity extends Activity {
private static final int PICK_CONTACT = 1;
private MasterSecret masterSecret;
private RecipientsPanel recipientsPanel;
private ImageButton addContactButton;
private Button sendButton;
private Button cancelButton;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.send_key_activity);
initializeResources();
initializeDefaults();
}
@Override
protected void onDestroy() {
MemoryCleaner.clean(masterSecret);
super.onDestroy();
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
if (data.getData() == null)
return;
NameAndNumber nameAndNumber = ContactAccessor.getInstance().getNameAndNumberFromContact(this, data.getData());
if (nameAndNumber != null)
recipientsPanel.addRecipient(nameAndNumber.name, nameAndNumber.number);
}
break;
}
}
private void initializeDefaults() {
String name = getIntent().getStringExtra("name");
String number = getIntent().getStringExtra("number");
if (number != null)
recipientsPanel.addRecipient(name, number);
}
private void initializeResources() {
recipientsPanel = (RecipientsPanel)findViewById(R.id.key_recipients);
addContactButton = (ImageButton)findViewById(R.id.contacts_button);
sendButton = (Button)findViewById(R.id.send_key_button);
cancelButton = (Button)findViewById(R.id.cancel_key_button);
masterSecret = (MasterSecret)getIntent().getParcelableExtra("master_secret");
Recipient defaultRecipient = (Recipient)getIntent().getParcelableExtra("recipient");
if (defaultRecipient != null) {
recipientsPanel.addRecipient(defaultRecipient.getName(), defaultRecipient.getNumber());
}
sendButton.setOnClickListener(new SendButtonListener());
cancelButton.setOnClickListener(new CancelButtonListener());
addContactButton.setOnClickListener(new AddRecipientButtonListener());
}
// Listeners
private class AddRecipientButtonListener implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
}
private class CancelButtonListener implements OnClickListener {
public void onClick(View v) {
SendKeyActivity.this.finish();
}
}
private class SendButtonListener implements OnClickListener {
public void onClick(View v) {
try {
Recipients recipients = recipientsPanel.getRecipients();
if (recipients.isEmpty())
return;
KeyExchangeInitiator.initiate(SendKeyActivity.this, masterSecret, recipients.getPrimaryRecipient(), true);
SendKeyActivity.this.finish();
} catch (RecipientFormattingException ex) {
Log.w("compose", ex);
//alert
}
}
}
}